diff options
author | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2018-09-01 14:24:32 +1000 |
---|---|---|
committer | Alkalus <3060479+draknyte1@users.noreply.github.com> | 2018-09-01 14:24:32 +1000 |
commit | 64bb2d7847ddb4eb9fba868f2b27ccd2c9582a00 (patch) | |
tree | 095ad534f7a1957902ae68b7521dcc3ea5a1b349 /src/Java/gtPlusPlus/core | |
parent | db20475a5973d588a9ef9e52feb22da0f4b92205 (diff) | |
download | GT5-Unofficial-64bb2d7847ddb4eb9fba868f2b27ccd2c9582a00.tar.gz GT5-Unofficial-64bb2d7847ddb4eb9fba868f2b27ccd2c9582a00.tar.bz2 GT5-Unofficial-64bb2d7847ddb4eb9fba868f2b27ccd2c9582a00.zip |
$ Reworked the Chunk loaders.
> Maybe they work now?
Diffstat (limited to 'src/Java/gtPlusPlus/core')
8 files changed, 740 insertions, 0 deletions
diff --git a/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java b/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java new file mode 100644 index 0000000000..7a3a01b27c --- /dev/null +++ b/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java @@ -0,0 +1,228 @@ +package gtPlusPlus.core.gui.machine; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.lwjgl.Sys; + +import com.google.common.collect.Lists; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiOptionButton; +import net.minecraft.client.gui.GuiResourcePackAvailable; +import net.minecraft.client.gui.GuiResourcePackSelected; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.resources.I18n; +import net.minecraft.client.resources.ResourcePackListEntry; +import net.minecraft.client.resources.ResourcePackListEntryFound; +import net.minecraft.client.resources.ResourcePackRepository; +import net.minecraft.client.resources.ResourcePackRepository.Entry; +import net.minecraft.util.Util; + +@SideOnly(Side.CLIENT) +public class GUI_ScrollTest extends GuiScreen +{ + private static final Logger logger = LogManager.getLogger(); + private GuiScreen aThisGUIScreen; + private List<?> field_146966_g; + private List<?> field_146969_h; + private GuiResourcePackAvailable MapOfFreeResourcePacks; + private GuiResourcePackSelected MapOfActiveResourcePacks; + private static final String __OBFID = "CL_00000820"; + + public GUI_ScrollTest(GuiScreen p_i45050_1_) + { + this.aThisGUIScreen = p_i45050_1_; + } + + /** + * Adds the buttons (and other controls) to the screen in question. + */ + @SuppressWarnings("unchecked") + public void initGui() + { + this.buttonList.add(new GuiOptionButton(2, this.width / 2 - 154, this.height - 48, I18n.format("resourcePack.openFolder", new Object[0]))); + this.buttonList.add(new GuiOptionButton(1, this.width / 2 + 4, this.height - 48, I18n.format("gui.done", new Object[0]))); + this.field_146966_g = new ArrayList<Object>(); + this.field_146969_h = new ArrayList<Entry>(); + ResourcePackRepository resourcepackrepository = this.mc.getResourcePackRepository(); + resourcepackrepository.updateRepositoryEntriesAll(); + ArrayList<?> arraylist = Lists.newArrayList(resourcepackrepository.getRepositoryEntriesAll()); + arraylist.removeAll(resourcepackrepository.getRepositoryEntries()); + Iterator<?> iterator = arraylist.iterator(); + ResourcePackRepository.Entry entry; + + while (iterator.hasNext()) + { + entry = (ResourcePackRepository.Entry)iterator.next(); + //this.field_146966_g.add(new ResourcePackListEntryFound(this, entry)); + } + + iterator = Lists.reverse(resourcepackrepository.getRepositoryEntries()).iterator(); + + while (iterator.hasNext()) + { + entry = (ResourcePackRepository.Entry)iterator.next(); + //this.field_146969_h.add(new ResourcePackListEntryFound(this, entry)); + } + + //this.field_146969_h.add(new ResourcePackListEntryDefault(this)); + this.MapOfFreeResourcePacks = new GuiResourcePackAvailable(this.mc, 200, this.height, this.field_146966_g); + this.MapOfFreeResourcePacks.setSlotXBoundsFromLeft(this.width / 2 - 4 - 200); + this.MapOfFreeResourcePacks.registerScrollButtons(7, 8); + this.MapOfActiveResourcePacks = new GuiResourcePackSelected(this.mc, 200, this.height, this.field_146969_h); + this.MapOfActiveResourcePacks.setSlotXBoundsFromLeft(this.width / 2 + 4); + this.MapOfActiveResourcePacks.registerScrollButtons(7, 8); + } + + public boolean func_146961_a(ResourcePackListEntry p_146961_1_) + { + return this.field_146969_h.contains(p_146961_1_); + } + + public List<?> func_146962_b(ResourcePackListEntry p_146962_1_) + { + return this.func_146961_a(p_146962_1_) ? this.field_146969_h : this.field_146966_g; + } + + public List<?> func_146964_g() + { + return this.field_146966_g; + } + + public List<?> func_146963_h() + { + return this.field_146969_h; + } + + protected void actionPerformed(GuiButton p_146284_1_) + { + if (p_146284_1_.enabled) + { + if (p_146284_1_.id == 2) + { + File file1 = this.mc.getResourcePackRepository().getDirResourcepacks(); + String s = file1.getAbsolutePath(); + + if (Util.getOSType() == Util.EnumOS.OSX) + { + try + { + logger.info(s); + Runtime.getRuntime().exec(new String[] {"/usr/bin/open", s}); + return; + } + catch (IOException ioexception1) + { + logger.error("Couldn\'t open file", ioexception1); + } + } + else if (Util.getOSType() == Util.EnumOS.WINDOWS) + { + String s1 = String.format("cmd.exe /C start \"Open file\" \"%s\"", new Object[] {s}); + + try + { + Runtime.getRuntime().exec(s1); + return; + } + catch (IOException ioexception) + { + logger.error("Couldn\'t open file", ioexception); + } + } + + boolean flag = false; + + try + { + Class<?> oclass = Class.forName("java.awt.Desktop"); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {file1.toURI()}); + } + catch (Throwable throwable) + { + logger.error("Couldn\'t open link", throwable); + flag = true; + } + + if (flag) + { + logger.info("Opening via system class!"); + Sys.openURL("file://" + s); + } + } + else if (p_146284_1_.id == 1) + { + ArrayList<Entry> arraylist = Lists.newArrayList(); + Iterator<?> iterator = this.field_146969_h.iterator(); + + while (iterator.hasNext()) + { + ResourcePackListEntry resourcepacklistentry = (ResourcePackListEntry)iterator.next(); + + if (resourcepacklistentry instanceof ResourcePackListEntryFound) + { + arraylist.add(((ResourcePackListEntryFound)resourcepacklistentry).func_148318_i()); + } + } + + Collections.reverse(arraylist); + this.mc.getResourcePackRepository().func_148527_a(arraylist); + this.mc.gameSettings.resourcePacks.clear(); + iterator = arraylist.iterator(); + + while (iterator.hasNext()) + { + ResourcePackRepository.Entry entry = (ResourcePackRepository.Entry)iterator.next(); + this.mc.gameSettings.resourcePacks.add(entry.getResourcePackName()); + } + + this.mc.gameSettings.saveOptions(); + this.mc.refreshResources(); + this.mc.displayGuiScreen(this.aThisGUIScreen); + } + } + } + + /** + * Called when the mouse is clicked. + */ + protected void mouseClicked(int p_73864_1_, int p_73864_2_, int p_73864_3_) + { + super.mouseClicked(p_73864_1_, p_73864_2_, p_73864_3_); + this.MapOfFreeResourcePacks.func_148179_a(p_73864_1_, p_73864_2_, p_73864_3_); + this.MapOfActiveResourcePacks.func_148179_a(p_73864_1_, p_73864_2_, p_73864_3_); + } + + /** + * Called when the mouse is moved or a mouse button is released. Signature: (mouseX, mouseY, which) which==-1 is + * mouseMove, which==0 or which==1 is mouseUp + */ + protected void mouseMovedOrUp(int p_146286_1_, int p_146286_2_, int p_146286_3_) + { + super.mouseMovedOrUp(p_146286_1_, p_146286_2_, p_146286_3_); + } + + /** + * Draws the screen and all the components in it. + */ + public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_) + { + this.drawBackground(0); + this.MapOfFreeResourcePacks.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + this.MapOfActiveResourcePacks.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + this.drawCenteredString(this.fontRendererObj, I18n.format("resourcePack.title", new Object[0]), this.width / 2, 16, 16777215); + this.drawCenteredString(this.fontRendererObj, I18n.format("resourcePack.folderInfo", new Object[0]), this.width / 2 - 77, this.height - 26, 8421504); + super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java new file mode 100644 index 0000000000..bc690f250c --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java @@ -0,0 +1,57 @@ +package gtPlusPlus.core.handler.chunkloading; + +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import gtPlusPlus.GTplusplus; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.api.objects.minecraft.ChunkManager; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.core.util.minecraft.network.PacketHandler; +import net.minecraftforge.common.ForgeChunkManager; + +public class ChunkLoading { + + private final ChunkLoading instance; + + public ChunkLoading() { + instance = this; + } + + public ChunkLoading getInstance() { + return this.instance; + } + + + public void preInit(final FMLPreInitializationEvent event) { + PacketHandler.init(); + ForgeChunkManager.setForcedChunkLoadingCallback(GTplusplus.instance, ChunkManager.getInstance()); + Utils.registerEvent(ChunkManager.getInstance()); + } + + + public void init(final FMLInitializationEvent event) { + + } + + + public void postInit(final FMLPostInitializationEvent event) { + + } + + + public synchronized void serverStarting(final FMLServerStartingEvent event) { + + } + + public void serverStopping(final FMLServerStoppingEvent event){ + //Chunkload Handler + if (ChunkManager.mChunkLoaderManagerMap.size() > 0) { + Logger.INFO("Clearing Chunk Loaders."); + ChunkManager.clearInternalMaps(); + } + } + +} diff --git a/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java new file mode 100644 index 0000000000..f95c4e7a78 --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java @@ -0,0 +1,153 @@ +package gtPlusPlus.core.handler.chunkloading; + +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.ListMultimap; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic.GregtechMetaTileEntityChunkLoader; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import net.minecraft.entity.Entity; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.ChunkCoordIntPair; +import net.minecraft.world.World; +import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.OrderedLoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.PlayerOrderedLoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.Ticket; +import net.minecraftforge.event.entity.EntityEvent.EnteringChunk; + +public class ChunkManager implements LoadingCallback, OrderedLoadingCallback, PlayerOrderedLoadingCallback { + private static ChunkManager instance; + + public static ChunkManager getInstance() { + if (instance == null) { + instance = new ChunkManager(); + } + + return instance; + } + + @SubscribeEvent + public void entityEnteredChunk(EnteringChunk event) { + + } + + public Set<ChunkCoordIntPair> getChunksBetween(int xChunkA, int zChunkA, int xChunkB, int zChunkB, int max) { + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + if (xChunkA != xChunkB && zChunkA != zChunkB) { + return chunkList; + } else { + int xStart = Math.min(xChunkA, xChunkB); + int xEnd = Math.max(xChunkA, xChunkB); + int zStart = Math.min(zChunkA, zChunkB); + int zEnd = Math.max(zChunkA, zChunkB); + + for (int xx = xStart; xx <= xEnd; ++xx) { + for (int zz = zStart; zz <= zEnd; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + if (chunkList.size() >= max) { + return chunkList; + } + } + } + + return chunkList; + } + } + + public Set<ChunkCoordIntPair> getChunksAround(int xChunk, int zChunk, int radius) { + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + + for (int xx = xChunk - radius; xx <= xChunk + radius; ++xx) { + for (int zz = zChunk - radius; zz <= zChunk + radius; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + } + } + + return chunkList; + } + + public Set<ChunkCoordIntPair> getBufferAround(int xWorld, int zWorld, int radius) { + int minX = xWorld - radius >> 4; + int maxX = xWorld + radius >> 4; + int minZ = zWorld - radius >> 4; + int maxZ = zWorld + radius >> 4; + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + + for (int xx = minX; xx <= maxX; ++xx) { + for (int zz = minZ; zz <= maxZ; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + } + } + + return chunkList; + } + + public void ticketsLoaded(List<Ticket> tickets, World world) { + Iterator<Ticket> var3 = tickets.iterator(); + while (var3.hasNext()) { + Ticket ticket = (Ticket) var3.next(); + if (!ticket.isPlayerTicket()) { + Entity entity = ticket.getEntity(); + if (entity == null) { + int x = ticket.getModData().getInteger("xCoord"); + int y = ticket.getModData().getInteger("yCoord"); + int z = ticket.getModData().getInteger("zCoord"); + if (y >= 0) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof IGregTechTileEntity) { + IGregTechTileEntity g = (IGregTechTileEntity) tile; + if (g instanceof GregtechMetaTileEntityChunkLoader) { + GregtechMetaTileEntityChunkLoader t = (GregtechMetaTileEntityChunkLoader) g; + t.forceChunkLoading(t.getBaseMetaTileEntity(), ticket); + // this.printChunkLoader(t.getName(), x, y, z); + } + } + } + } + } + } + } + + public List<Ticket> ticketsLoaded(List<Ticket> tickets, World world, int maxTicketCount) { + Set<Ticket> adminTickets = new HashSet<Ticket>(); + Set<Ticket> worldTickets = new HashSet<Ticket>(); + Set<Ticket> cartTickets = new HashSet<Ticket>(); + Iterator<Ticket> var7 = tickets.iterator(); + + while (var7.hasNext()) { + Ticket ticket = (Ticket) var7.next(); + Entity entity = ticket.getEntity(); + if (entity == null) { + int x = ticket.getModData().getInteger("xCoord"); + int y = ticket.getModData().getInteger("yCoord"); + int z = ticket.getModData().getInteger("zCoord"); + String type = ticket.getModData().getString("type"); + if (y >= 0) { + if (type.equals("AdminChunkLoader")) { + adminTickets.add(ticket); + } else if (type.equals("StandardChunkLoader")) { + worldTickets.add(ticket); + } else if (type.isEmpty()) { + worldTickets.add(ticket); + } + } + } + } + + List<Ticket> claimedTickets = new LinkedList<Ticket>(); + claimedTickets.addAll(cartTickets); + claimedTickets.addAll(adminTickets); + claimedTickets.addAll(worldTickets); + return claimedTickets; + } + + public ListMultimap<String, Ticket> playerTicketsLoaded(ListMultimap<String, Ticket> tickets, World world) { + return LinkedListMultimap.create(); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java b/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java new file mode 100644 index 0000000000..def836eac9 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java @@ -0,0 +1,39 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import io.netty.buffer.Unpooled; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import mods.railcraft.common.util.misc.Game; + +public abstract class CustomPacket { + public static final String CHANNEL_NAME = "GTPP"; + + public enum PacketType { + TILE_ENTITY, + } + + public FMLProxyPacket getPacket() { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream(bytes); + try { + data.writeByte(this.getID()); + this.writeData(data); + } catch (IOException var4) { + Game.logThrowable("Error constructing packet: {0}", var4, new Object[]{this.getClass()}); + } + return new FMLProxyPacket(Unpooled.wrappedBuffer(bytes.toByteArray()), "GTPP"); + } + + public abstract void writeData(DataOutputStream var1) throws IOException; + + public abstract void readData(DataInputStream var1) throws IOException; + + public abstract int getID(); + + public String toString() { + return this.getClass().getSimpleName(); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java new file mode 100644 index 0000000000..edbc6aaf83 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java @@ -0,0 +1,25 @@ +package gtPlusPlus.core.util.minecraft.network; + +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import net.minecraft.world.WorldServer; + +public class PacketBuilder { + + private static PacketBuilder instance; + + public static PacketBuilder instance() { + if (instance == null) { + instance = new PacketBuilder(); + } + return instance; + } + + public void sendTileEntityPacket(IGregTechTileEntity tile) { + if (tile.getWorld() instanceof WorldServer) { + WorldServer world = (WorldServer) tile.getWorld(); + PacketTileEntity pkt = new PacketTileEntity(tile); + PacketDispatcher.sendToWatchers(pkt, world, tile.getXCoord(), tile.getZCoord()); + } + } + +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java new file mode 100644 index 0000000000..3e3801bff3 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java @@ -0,0 +1,88 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; +import cpw.mods.fml.relauncher.ReflectionHelper; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.preloader.DevHelper; + +import java.lang.reflect.Method; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.Packet; +import net.minecraft.server.management.PlayerManager; +import net.minecraft.world.WorldServer; + +@SuppressWarnings("unchecked") +public class PacketDispatcher { + private static final Class playerInstanceClass; + private static final Method getOrCreateChunkWatcher; + private static final Method sendToAllPlayersWatchingChunk; + + public static void sendToServer(CustomPacket packet) { + PacketHandler.INSTANCE.channel.sendToServer(packet.getPacket()); + } + + public static void sendToPlayer(CustomPacket packet, EntityPlayerMP player) { + PacketHandler.INSTANCE.channel.sendTo(packet.getPacket(), player); + } + + public static void sendToAll(CustomPacket packet) { + PacketHandler.INSTANCE.channel.sendToAll(packet.getPacket()); + } + + public static void sendToAllAround(CustomPacket packet, TargetPoint zone) { + PacketHandler.INSTANCE.channel.sendToAllAround(packet.getPacket(), zone); + } + + public static void sendToDimension(CustomPacket packet, int dimensionId) { + PacketHandler.INSTANCE.channel.sendToDimension(packet.getPacket(), dimensionId); + } + + public static void sendToWatchers(CustomPacket packet, WorldServer world, int worldX, int worldZ) { + try { + Object playerInstance = getOrCreateChunkWatcher.invoke(world.getPlayerManager(), worldX >> 4, worldZ >> 4, + false); + if (playerInstance != null) { + sendToAllPlayersWatchingChunk.invoke(playerInstance, packet.getPacket()); + } + + } catch (Exception var5) { + Logger.ERROR("Reflection Failure in PacketDispatcher.sendToWatchers() {0} {1}" + 20 + var5 + + new Object[]{getOrCreateChunkWatcher.getName() + sendToAllPlayersWatchingChunk.getName()}); + throw new RuntimeException(var5); + } + } + + static { + try { + playerInstanceClass = PlayerManager.class.getDeclaredClasses()[0]; + + Method a, b; + + try { + a = DevHelper.getInstance().getForgeMethod(PlayerManager.class, "getOrCreateChunkWatcher", int.class, int.class, boolean.class); + } + catch (Throwable t) { + a = ReflectionHelper.findMethod(playerInstanceClass, (Object) null, + new String[]{"func_72690_a", "getOrCreateChunkWatcher"}, + new Class[]{Integer.TYPE, Integer.TYPE, Boolean.TYPE}); + } + try { + b = DevHelper.getInstance().getForgeMethod(PlayerManager.class, "sendToAllPlayersWatchingChunk", Packet.class); + } + catch (Throwable t) { + b = ReflectionHelper.findMethod(playerInstanceClass, (Object) null, + new String[]{"func_151251_a", "sendToAllPlayersWatchingChunk"}, + new Class[]{Packet.class}); + } + + + getOrCreateChunkWatcher = a; + sendToAllPlayersWatchingChunk = b; + getOrCreateChunkWatcher.setAccessible(true); + sendToAllPlayersWatchingChunk.setAccessible(true); + } catch (Exception var1) { + Logger.ERROR("Reflection Failure in PacketDispatcher initalization {0} {1}" + var1); + throw new RuntimeException(var1); + } + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java new file mode 100644 index 0000000000..158f9f9483 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java @@ -0,0 +1,71 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.network.FMLEventChannel; +import cpw.mods.fml.common.network.NetworkRegistry; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.core.util.minecraft.network.CustomPacket.PacketType; +import cpw.mods.fml.common.network.FMLNetworkEvent.ClientCustomPacketEvent; +import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.Arrays; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.NetHandlerPlayServer; + +public class PacketHandler { + public static final PacketHandler INSTANCE = new PacketHandler(); + private static final PacketType[] packetTypes = PacketType.values(); + final FMLEventChannel channel; + + private PacketHandler() { + this.channel = NetworkRegistry.INSTANCE.newEventDrivenChannel("GTPP"); + this.channel.register(this); + } + + public static void init() { + } + + @SubscribeEvent + public void onPacket(ServerCustomPacketEvent event) { + byte[] data = new byte[event.packet.payload().readableBytes()]; + event.packet.payload().readBytes(data); + this.onPacketData(data, ((NetHandlerPlayServer) event.handler).playerEntity); + } + + @SubscribeEvent + public void onPacket(ClientCustomPacketEvent event) { + byte[] data = new byte[event.packet.payload().readableBytes()]; + event.packet.payload().readBytes(data); + this.onPacketData(data, (EntityPlayerMP) null); + } + + public void onPacketData(byte[] bData, EntityPlayerMP player) { + DataInputStream data = new DataInputStream(new ByteArrayInputStream(bData)); + + try { + byte packetID = data.readByte(); + if (packetID < 0) { + return; + } + PacketType type = packetTypes[packetID]; + Object pkt; + + switch(type.ordinal()) { + case 0: + pkt = new PacketTileEntity(); + break; + default: + return; + } + + if (pkt != null) { + ((CustomPacket)pkt).readData(data); + } + } catch (IOException var7) { + Logger.ERROR("Exception in PacketHandler.onPacketData: {0}"+ var7 + new Object[]{Arrays.toString(bData)}); + } + + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java new file mode 100644 index 0000000000..d59bee3c27 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java @@ -0,0 +1,79 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.client.FMLClientHandler; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gtPlusPlus.api.interfaces.IGregtechPacketEntity; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import mods.railcraft.common.util.misc.Game; +import net.minecraft.client.Minecraft; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class PacketTileEntity extends CustomPacket { + private IGregTechTileEntity tile; + private IGregtechPacketEntity ptile; + + public PacketTileEntity() { + } + + public PacketTileEntity(IGregTechTileEntity tile) { + this.tile = tile; + if (tile instanceof IGregtechPacketEntity) { + ptile = (IGregtechPacketEntity) tile; + } + } + + public void writeData(DataOutputStream data) throws IOException { + if (ptile != null) { + data.writeInt(this.tile.getXCoord()); + data.writeInt(this.tile.getYCoord()); + data.writeInt(this.tile.getZCoord()); + data.writeShort(this.tile.getMetaTileID()); + this.ptile.writePacketData(data); + } + } + + @SideOnly(Side.CLIENT) + public void readData(DataInputStream data) throws IOException { + Minecraft mc = FMLClientHandler.instance().getClient(); + World world = mc != null ? mc.theWorld : null; + if (world != null) { + int x = data.readInt(); + int y = data.readInt(); + int z = data.readInt(); + short id = data.readShort(); + if (id >= 0 && y >= 0 && world.blockExists(x, y, z)) { + TileEntity te = world.getTileEntity(x, y, z); + if (te instanceof IGregTechTileEntity) { + this.tile = (IGregTechTileEntity) te; + if (this.tile.getMetaTileID() != id) { + this.tile = null; + } + } else { + this.tile = null; + } + if (this.tile != null) { + if (tile instanceof IGregtechPacketEntity) { + ptile = (IGregtechPacketEntity) tile; + try { + this.ptile.readPacketData(data); + } catch (IOException var10) { + throw var10; + } catch (RuntimeException var11) { + Game.logThrowable("Exception in PacketTileEntity.readData:", var11, new Object[0]); + } + } + } + } + } + } + + public int getID() { + return 0; + } +}
\ No newline at end of file |