diff options
Diffstat (limited to 'src/Java/gtPlusPlus/core/util')
5 files changed, 302 insertions, 0 deletions
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 |