Packet Handler helper

This commit is contained in:
Ellpeck 2016-08-09 20:56:09 +02:00
parent 5a3432d622
commit ee9abd4960
14 changed files with 77 additions and 93 deletions

View file

@ -26,6 +26,7 @@ import de.ellpeck.actuallyadditions.mod.items.ItemBooklet;
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.proxy.ClientProxy;
import de.ellpeck.actuallyadditions.mod.update.UpdateChecker;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -392,11 +393,6 @@ public class GuiBooklet extends GuiScreen implements IBookletGui{
NBTTagCompound extraData = new NBTTagCompound();
extraData.setBoolean("BookAlreadyOpened", true);
NBTTagCompound dataToSend = new NBTTagCompound();
dataToSend.setTag("Data", extraData);
dataToSend.setInteger("WorldID", Minecraft.getMinecraft().theWorld.provider.getDimension());
dataToSend.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(dataToSend, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
}
else{
BookletUtils.openLastBookPage(this, data.theCompound.getCompoundTag("BookletData"));
@ -463,11 +459,7 @@ public class GuiBooklet extends GuiScreen implements IBookletGui{
NBTTagCompound extraData = new NBTTagCompound();
extraData.setTag("BookletData", bookletData);
NBTTagCompound dataToSend = new NBTTagCompound();
dataToSend.setTag("Data", extraData);
dataToSend.setInteger("WorldID", mc.theWorld.provider.getDimension());
dataToSend.setInteger("PlayerID", mc.thePlayer.getEntityId());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(dataToSend, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
PacketHandlerHelper.sendChangePlayerDataPacket(extraData);
}
}
}

View file

@ -17,6 +17,7 @@ import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
import de.ellpeck.actuallyadditions.mod.items.InitItems;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
@ -97,11 +98,7 @@ public class CommonEvents{
EntityPlayerMP player = (EntityPlayerMP)event.player;
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
if(!data.theCompound.hasNoTags()){
NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID());
compound.setTag("Data", data.theCompound);
compound.setBoolean("Log", true);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), player);
PacketHandlerHelper.sendPlayerDataToClientPacket(player, data.theCompound, true);
ModUtil.LOGGER.info("Sending Player Data to player "+player.getName()+" with UUID "+player.getUniqueID()+" with info "+data.theCompound+".");
}
else{

View file

@ -14,6 +14,7 @@ import cofh.api.energy.EnergyStorage;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
@ -110,10 +111,6 @@ public class EnergyDisplay extends Gui{
this.displayTesla = !this.displayTesla;
data.setBoolean("DisplayTesla", this.displayTesla);
NBTTagCompound dataToSend = new NBTTagCompound();
dataToSend.setTag("Data", data);
dataToSend.setInteger("WorldID", Minecraft.getMinecraft().theWorld.provider.getDimension());
dataToSend.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(dataToSend, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
PacketHandlerHelper.sendChangePlayerDataPacket(data);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerCoffeeMachine;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityCoffeeMachine;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -130,13 +131,6 @@ public class GuiCoffeeMachine extends GuiContainer{
@Override
public void actionPerformed(GuiButton button){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.x);
compound.setInteger("Y", this.y);
compound.setInteger("Z", this.z);
compound.setInteger("WorldID", this.world.provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.machine, button.id);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerGiantChest;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityGiantChest;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityGiantChestLarge;
@ -63,14 +64,7 @@ public class GuiGiantChest extends GuiContainer{
@Override
protected void actionPerformed(GuiButton button) throws IOException{
if(button.id >= 0 && button.id < 3){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.chest.getPos().getX());
compound.setInteger("Y", this.chest.getPos().getY());
compound.setInteger("Z", this.chest.getPos().getZ());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("WorldID", this.chest.getWorld().provider.getDimension());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.chest, button.id);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerInputter;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityInputter;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -256,14 +257,7 @@ public class GuiInputter extends GuiContainer{
this.setVariable(this.fieldPullEnd, 3);
}
else{
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.x);
compound.setInteger("Y", this.y);
compound.setInteger("Z", this.z);
compound.setInteger("WorldID", this.world.provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.tileInputter, button.id);
}
}

View file

@ -14,6 +14,7 @@ import de.ellpeck.actuallyadditions.mod.inventory.ContainerLaserRelayItemWhiteli
import de.ellpeck.actuallyadditions.mod.inventory.gui.GuiInputter.SmallerButton;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelayItemWhitelist;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -75,14 +76,7 @@ public class GuiLaserRelayItemWhitelist extends GuiContainer{
@Override
public void actionPerformed(GuiButton button){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.tile.getPos().getX());
compound.setInteger("Y", this.tile.getPos().getY());
compound.setInteger("Z", this.tile.getPos().getZ());
compound.setInteger("WorldID", this.tile.getWorld().provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.tile, button.id);
}
@Override

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerMiner;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityMiner;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -72,13 +73,6 @@ public class GuiMiner extends GuiContainer{
@Override
public void actionPerformed(GuiButton button){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.miner.getPos().getX());
compound.setInteger("Y", this.miner.getPos().getY());
compound.setInteger("Z", this.miner.getPos().getZ());
compound.setInteger("WorldID", this.miner.getWorld().provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.miner, button.id);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerPhantomPlacer;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityPhantomPlacer;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -82,15 +83,7 @@ public class GuiPhantomPlacer extends GuiContainer{
@Override
protected void actionPerformed(GuiButton button) throws IOException{
if(!this.placer.isBreaker){
NBTTagCompound compound = new NBTTagCompound();
BlockPos pos = this.placer.getPos();
compound.setInteger("X", pos.getX());
compound.setInteger("Y", pos.getY());
compound.setInteger("Z", pos.getZ());
compound.setInteger("WorldID", this.placer.getWorld().provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.placer, button.id);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerRangedCollector;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityRangedCollector;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -89,13 +90,6 @@ public class GuiRangedCollector extends GuiContainer{
@Override
public void actionPerformed(GuiButton button){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.x);
compound.setInteger("Y", this.y);
compound.setInteger("Z", this.z);
compound.setInteger("WorldID", this.world.provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.collector, button.id);
}
}

View file

@ -13,6 +13,7 @@ package de.ellpeck.actuallyadditions.mod.inventory.gui;
import de.ellpeck.actuallyadditions.mod.inventory.ContainerXPSolidifier;
import de.ellpeck.actuallyadditions.mod.network.PacketClientToServer;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityXPSolidifier;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
@ -94,14 +95,7 @@ public class GuiXPSolidifier extends GuiContainer{
@Override
public void actionPerformed(GuiButton button){
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("X", this.x);
compound.setInteger("Y", this.y);
compound.setInteger("Z", this.z);
compound.setInteger("WorldID", this.world.provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", button.id);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
PacketHandlerHelper.sendButtonPacket(this.solidifier, button.id);
this.solidifier.onButtonPressed(button.id, Minecraft.getMinecraft().thePlayer);
}

View file

@ -14,6 +14,7 @@ import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.items.base.ItemBase;
import de.ellpeck.actuallyadditions.mod.network.PacketHandler;
import de.ellpeck.actuallyadditions.mod.network.PacketHandlerHelper;
import de.ellpeck.actuallyadditions.mod.network.PacketServerToClient;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityLaserRelay;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
@ -95,10 +96,7 @@ public class ItemLaserWrench extends ItemBase{
save.theCompound.setInteger("LaserWrenchMode", currMode);
if(player instanceof EntityPlayerMP){
NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID());
compound.setTag("Data", save.theCompound);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player);
PacketHandlerHelper.sendPlayerDataToClientPacket(player, save.theCompound, false);
}
player.addChatComponentMessage(new TextComponentString("Mode changed to "+WrenchMode.values()[currMode].name+"!"));

View file

@ -156,10 +156,7 @@ public final class PacketHandler{
PlayerData.PlayerSave playerData = PlayerData.getDataFromPlayer(player);
playerData.theCompound.merge(data);
if(player instanceof EntityPlayerMP){
NBTTagCompound tag = new NBTTagCompound();
tag.setUniqueId("UUID", player.getUniqueID());
tag.setTag("Data", playerData.theCompound);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(tag, PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player);
}
}
}

View file

@ -0,0 +1,52 @@
/*
* This file ("PacketHandlerHelper.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.network;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
public final class PacketHandlerHelper{
public static void sendButtonPacket(TileEntity tile, int buttonId){
NBTTagCompound compound = new NBTTagCompound();
BlockPos pos = tile.getPos();
compound.setInteger("X", pos.getX());
compound.setInteger("Y", pos.getY());
compound.setInteger("Z", pos.getZ());
compound.setInteger("WorldID", tile.getWorld().provider.getDimension());
compound.setInteger("PlayerID", Minecraft.getMinecraft().thePlayer.getEntityId());
compound.setInteger("ButtonID", buttonId);
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
}
public static void sendChangePlayerDataPacket(NBTTagCompound data){
NBTTagCompound compound = new NBTTagCompound();
compound.setTag("Data", data);
Minecraft mc = Minecraft.getMinecraft();
compound.setInteger("WorldID", mc.theWorld.provider.getDimension());
compound.setInteger("PlayerID", mc.thePlayer.getEntityId());
PacketHandler.theNetwork.sendToServer(new PacketClientToServer(compound, PacketHandler.CHANGE_PLAYER_DATA_HANDLER));
}
public static void sendPlayerDataToClientPacket(EntityPlayer player, NBTTagCompound data, boolean log){
if(player instanceof EntityPlayerMP){
NBTTagCompound compound = new NBTTagCompound();
compound.setUniqueId("UUID", player.getUniqueID());
compound.setTag("Data", data);
compound.setBoolean("Log", log);
PacketHandler.theNetwork.sendTo(new PacketServerToClient(compound, PacketHandler.PLAYER_DATA_TO_CLIENT_HANDLER), (EntityPlayerMP)player);
}
}
}