From 05c78126859231a68e199dc34613689bd0978e2f Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Mon, 11 Apr 2016 19:44:54 +0300 Subject: Initial commit --- ihl/ServerProxy.java | 222 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 ihl/ServerProxy.java (limited to 'ihl/ServerProxy.java') diff --git a/ihl/ServerProxy.java b/ihl/ServerProxy.java new file mode 100644 index 0000000..6aefab0 --- /dev/null +++ b/ihl/ServerProxy.java @@ -0,0 +1,222 @@ +package ihl; + +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +import javax.xml.parsers.ParserConfigurationException; + +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerChangedDimensionEvent; +import cpw.mods.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; +import cpw.mods.fml.common.network.FMLEventChannel; +import cpw.mods.fml.common.network.FMLNetworkEvent; +import cpw.mods.fml.common.network.NetworkRegistry; +import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; +import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import ihl.flexible_cable.NodeEntity; +import ihl.interfaces.INetworkListener; +import ihl.items_blocks.FlexibleCableItem; +import ihl.items_blocks.MachineBaseBlock.MachineType; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufInputStream; +import io.netty.buffer.ByteBufOutputStream; +import io.netty.buffer.Unpooled; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLivingBase; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.server.MinecraftServer; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.MovingObjectPosition; +import net.minecraft.world.World; +import net.minecraft.world.WorldServer; +import net.minecraftforge.client.event.TextureStitchEvent.Pre; +import net.minecraftforge.event.entity.living.LivingDeathEvent; + +public class ServerProxy { + + protected static FMLEventChannel channel; + public static final int updatePeriod = 1; + private static final int maxPacketDataLength = 128; + protected Map entityList = new HashMap(); + protected Set entityServerList = new HashSet(); + protected Map delayedEntityDataPacket = new HashMap(); + public Map> nodeEntityRegistry = new HashMap(); + + public ServerProxy() {} + + public void load() throws ParserConfigurationException + { + if(channel==null) + { + channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(IHLModInfo.MODID); + channel.register(this); + } + } + public void spawnParticle(int particle, World world, double x, double y, double z, double mx, double my, double mz, float paticleScale){} + + public void spawnParticleFromServer(int particle, World world, double x, double y, double z, double mx, double my, double mz, float paticleScale) + { + ByteBuf bb = Unpooled.buffer(36); + ByteBufOutputStream byteBufOutputStream = new ByteBufOutputStream(bb); + try + { + byteBufOutputStream.write(0); + byteBufOutputStream.write(particle); + byteBufOutputStream.writeFloat((float) x); + byteBufOutputStream.writeFloat((float) y); + byteBufOutputStream.writeFloat((float) z); + byteBufOutputStream.writeFloat((float) mx); + byteBufOutputStream.writeFloat((float) my); + byteBufOutputStream.writeFloat((float) mz); + byteBufOutputStream.writeFloat(paticleScale); + channel.sendToAllAround(new FMLProxyPacket(byteBufOutputStream.buffer(),IHLModInfo.MODID), new TargetPoint(world.provider.dimensionId, x, y, z, 32d)); + byteBufOutputStream.close(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public void registerIcons(Pre event) {} + public void initBlockRenderer(){} + public Object getRenderForEntityClass(Class entityClass) + { + return null; + } + public int getGLDisplayList() {return -1;} + + public File getMinecraftDir() + { + return new File("."); + } + public int shareBlockRendererByMachineType(MachineType type) + { + return 0; + } + + public void addEntityToList(INetworkListener entity) + { + this.entityList.put(entity.getId(), entity); + } + + public void recieveDelayedDataPacket(INetworkListener listener) + { + ByteBuf data = delayedEntityDataPacket.remove(listener.getId()); + if(data!=null) + { + ByteBufInputStream byteBufInputStream = new ByteBufInputStream(data); + try + { + //byteBufInputStream.skipBytes(5); + listener.recieveData(byteBufInputStream); + byteBufInputStream.close(); + IHLMod.log.debug("Delayed data read."); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + else + { + IHLMod.log.debug("Delayed data is null. Entity ID="+listener.getId()); + } + } + + public void sendFromServerToAll(FMLProxyPacket fmlProxyPacket) + { + channel.sendToAll(fmlProxyPacket); + } + + public void sendFromServerToPlayer(FMLProxyPacket fmlProxyPacket, EntityPlayerMP player) + { + channel.sendTo(fmlProxyPacket, player); + } + + @SubscribeEvent + public void onPacketFromClientToServer(FMLNetworkEvent.ServerCustomPacketEvent event) throws IOException + { + ByteBuf data = event.packet.payload(); + ByteBufInputStream byteBufInputStream = new ByteBufInputStream(data); + switch(byteBufInputStream.read()) + { + case 0: + int playerEntityId = byteBufInputStream.readInt(); + int worldDimensionId = byteBufInputStream.readInt(); + int containerSlotNumber = byteBufInputStream.readInt(); + int fieldValue = byteBufInputStream.readInt(); + String fieldName = byteBufInputStream.readUTF(); + EntityPlayerMP player = (EntityPlayerMP) MinecraftServer.getServer().worldServerForDimension(worldDimensionId).getEntityByID(playerEntityId); + ItemStack stack = ((Slot)player.openContainer.inventorySlots.get(containerSlotNumber)).getStack(); + stack.stackTagCompound.setInteger(fieldName, fieldValue); + player.openContainer.detectAndSendChanges(); + System.out.println("Field now "+stack.stackTagCompound.getInteger(fieldName)); + break; + } + + byteBufInputStream.close(); + + } + + @SubscribeEvent + public void onPlayerConnectedToServer(PlayerLoggedInEvent event) + { + IHLMod.log.debug("player connected"); + Iterator inli=this.entityServerList.iterator(); + while(inli.hasNext()) + { + INetworkListener inl = inli.next(); + if(inl.isInvalid()) + { + inli.remove(); + } + else if(event.player instanceof EntityPlayerMP) + { + inl.registerAndSendData((EntityPlayerMP)event.player); + } + } + } + + @SubscribeEvent + public void onPlayerTeleport(PlayerChangedDimensionEvent event) + { + FlexibleCableItem.instance.onPlayerTeleport(event); + Iterator inli=this.entityServerList.iterator(); + while(inli.hasNext()) + { + INetworkListener inl = inli.next(); + if(inl.isInvalid()) + { + inli.remove(); + } + else if(event.player instanceof EntityPlayerMP) + { + inl.registerAndSendData((EntityPlayerMP)event.player); + } + } + } + + public void addEntityToServerList(INetworkListener entity) + { + this.entityServerList.add(entity); + } + + public boolean renderTESpecialSelectionBox(TileEntity te, EntityPlayer player, ItemStack currentItem, MovingObjectPosition target, float partialTicks) { + return false; + } + + public void sendItemStackNBTTagFromClientToServerPlayer(EntityPlayer player, int slotNumber, String fieldName, int fieldValue){} +} -- cgit v1.2.3