2015-08-29 14:33:25 +02:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("PacketHandler.java") is part of the Actually Additions mod for Minecraft.
|
2015-08-29 14:33:25 +02:00
|
|
|
* It is created and owned by Ellpeck and distributed
|
|
|
|
* under the Actually Additions License to be found at
|
2016-05-16 22:52:27 +02:00
|
|
|
* http://ellpeck.de/actaddlicense
|
2015-08-29 14:33:25 +02:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-08-29 14:33:25 +02:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.network;
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
|
2016-12-28 14:55:03 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.data.WorldData;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IButtonReactor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.INumberReactor;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.network.gui.IStringReactor;
|
2016-12-27 17:40:27 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.particle.ParticleLaserItem;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
|
|
|
|
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
|
2016-06-15 16:43:59 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
2016-12-27 17:40:27 +01:00
|
|
|
import net.minecraft.client.particle.Particle;
|
2016-07-24 12:48:35 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2016-06-15 16:43:59 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2016-08-02 16:32:13 +02:00
|
|
|
import net.minecraft.inventory.Container;
|
2016-12-27 17:40:27 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-06-15 16:43:59 +02:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import net.minecraft.tileentity.TileEntity;
|
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
|
|
|
import net.minecraftforge.common.DimensionManager;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
2016-12-28 14:55:03 +01:00
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
2016-01-07 18:20:59 +01:00
|
|
|
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
2016-06-15 16:43:59 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2016-06-15 16:43:59 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2016-07-14 20:25:12 +02:00
|
|
|
import java.util.UUID;
|
2016-06-15 16:43:59 +02:00
|
|
|
|
|
|
|
public final class PacketHandler{
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2016-06-15 16:43:59 +02:00
|
|
|
public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<IDataHandler>();
|
2016-12-27 17:40:27 +01:00
|
|
|
public static final IDataHandler LASER_HANDLER = new IDataHandler(){
|
2016-06-15 16:43:59 +02:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-12-09 21:36:43 +01:00
|
|
|
AssetUtil.spawnLaserWithTimeClient(compound.getDouble("StartX"), compound.getDouble("StartY"), compound.getDouble("StartZ"), compound.getDouble("EndX"), compound.getDouble("EndY"), compound.getDouble("EndZ"), new float[]{compound.getFloat("Color1"), compound.getFloat("Color2"), compound.getFloat("Color3")}, compound.getInteger("MaxAge"), compound.getDouble("RotationTime"), compound.getFloat("Size"), compound.getFloat("Alpha"));
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-11-26 21:32:27 +01:00
|
|
|
World world = Minecraft.getMinecraft().world;
|
2016-06-15 16:43:59 +02:00
|
|
|
if(world != null){
|
|
|
|
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
|
2016-11-16 20:31:16 +01:00
|
|
|
if(tile instanceof TileEntityBase){
|
2016-09-01 17:08:34 +02:00
|
|
|
((TileEntityBase)tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-12-27 17:40:27 +01:00
|
|
|
public static final IDataHandler LASER_PARTICLE_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-12-27 17:40:27 +01:00
|
|
|
Minecraft mc = Minecraft.getMinecraft();
|
|
|
|
ItemStack stack = new ItemStack(compound);
|
|
|
|
|
|
|
|
double inX = compound.getDouble("InX")+0.5;
|
2016-12-27 21:43:00 +01:00
|
|
|
double inY = compound.getDouble("InY")+0.78;
|
2016-12-27 17:40:27 +01:00
|
|
|
double inZ = compound.getDouble("InZ")+0.5;
|
|
|
|
|
|
|
|
double outX = compound.getDouble("OutX")+0.5;
|
|
|
|
double outY = compound.getDouble("OutY")+0.525;
|
|
|
|
double outZ = compound.getDouble("OutZ")+0.5;
|
|
|
|
|
|
|
|
if(mc.player.getDistance(outX, outY, outZ) <= 16){
|
|
|
|
Particle fx = new ParticleLaserItem(mc.world, outX, outY, outZ, stack, 0.025, inX, inY, inZ);
|
|
|
|
mc.effectRenderer.addEffect(fx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-06-15 16:43:59 +02:00
|
|
|
public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-06-15 16:43:59 +02:00
|
|
|
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
|
|
|
|
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
|
|
|
|
|
|
|
|
if(tile instanceof IButtonReactor){
|
|
|
|
IButtonReactor reactor = (IButtonReactor)tile;
|
2016-07-24 12:48:35 +02:00
|
|
|
Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
|
2016-11-16 20:31:16 +01:00
|
|
|
if(entity instanceof EntityPlayer){
|
2016-07-24 12:48:35 +02:00
|
|
|
reactor.onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer)entity);
|
|
|
|
}
|
2016-06-15 16:43:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-08-02 16:32:13 +02:00
|
|
|
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-08-02 16:32:13 +02:00
|
|
|
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
|
|
|
|
Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
|
2016-11-16 20:31:16 +01:00
|
|
|
if(entity instanceof EntityPlayer){
|
2016-08-02 16:32:13 +02:00
|
|
|
Container container = ((EntityPlayer)entity).openContainer;
|
2016-11-16 20:31:16 +01:00
|
|
|
if(container instanceof IButtonReactor){
|
2016-08-02 16:32:13 +02:00
|
|
|
((IButtonReactor)container).onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer)entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-06-15 16:43:59 +02:00
|
|
|
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-06-15 16:43:59 +02:00
|
|
|
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
|
|
|
|
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
|
|
|
|
|
|
|
|
if(tile instanceof INumberReactor){
|
|
|
|
INumberReactor reactor = (INumberReactor)tile;
|
|
|
|
reactor.onNumberReceived(compound.getInteger("Number"), compound.getInteger("NumberID"), (EntityPlayer)world.getEntityByID(compound.getInteger("PlayerID")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = new IDataHandler(){
|
|
|
|
@Override
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
2016-06-15 16:43:59 +02:00
|
|
|
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
|
|
|
|
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
|
|
|
|
|
|
|
|
if(tile instanceof IStringReactor){
|
|
|
|
IStringReactor reactor = (IStringReactor)tile;
|
|
|
|
reactor.onTextReceived(compound.getString("Text"), compound.getInteger("TextID"), (EntityPlayer)world.getEntityByID(compound.getInteger("PlayerID")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-11-11 21:41:07 +01:00
|
|
|
public static final IDataHandler CHANGE_PLAYER_DATA_HANDLER = new IDataHandler(){
|
2016-06-17 23:50:38 +02:00
|
|
|
@Override
|
2016-12-28 14:55:03 +01:00
|
|
|
public void handleData(NBTTagCompound compound, MessageContext context){
|
|
|
|
NBTTagCompound dataTag = compound.getCompoundTag("Data");
|
2016-07-14 20:25:12 +02:00
|
|
|
UUID id = compound.getUniqueId("UUID");
|
2016-12-28 14:55:03 +01:00
|
|
|
EntityPlayer player = null;
|
|
|
|
|
|
|
|
if(context.side == Side.SERVER){
|
|
|
|
int dim = compound.getInteger("Dimension");
|
|
|
|
World world = DimensionManager.getWorld(dim);
|
|
|
|
if(world != null){
|
|
|
|
player = world.getPlayerEntityByUUID(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
player = Minecraft.getMinecraft().player;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(player != null){
|
|
|
|
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
|
|
|
|
WorldData.get(player.getEntityWorld()).markDirty();
|
|
|
|
|
|
|
|
if(compound.getBoolean("Log")){
|
|
|
|
ModUtil.LOGGER.info("Receiving (new or changed) Player Data for player "+player.getName()+" with UUID "+id+".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
ModUtil.LOGGER.error("Tried to receive Player Data for player with UUID "+id+", but he doesn't seem to be present!");
|
2016-06-17 23:50:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-11-16 20:31:16 +01:00
|
|
|
public static SimpleNetworkWrapper theNetwork;
|
2016-06-15 16:43:59 +02:00
|
|
|
|
2015-02-20 22:45:33 +01:00
|
|
|
public static void init(){
|
2016-04-20 21:39:03 +02:00
|
|
|
theNetwork = NetworkRegistry.INSTANCE.newSimpleChannel(ModUtil.MOD_ID);
|
2016-06-15 16:43:59 +02:00
|
|
|
theNetwork.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
|
|
|
|
theNetwork.registerMessage(PacketClientToServer.Handler.class, PacketClientToServer.class, 1, Side.SERVER);
|
2015-02-20 22:45:33 +01:00
|
|
|
|
2016-12-27 17:40:27 +01:00
|
|
|
DATA_HANDLERS.add(LASER_HANDLER);
|
2016-06-15 16:43:59 +02:00
|
|
|
DATA_HANDLERS.add(TILE_ENTITY_HANDLER);
|
|
|
|
DATA_HANDLERS.add(GUI_BUTTON_TO_TILE_HANDLER);
|
|
|
|
DATA_HANDLERS.add(GUI_STRING_TO_TILE_HANDLER);
|
|
|
|
DATA_HANDLERS.add(GUI_NUMBER_TO_TILE_HANDLER);
|
|
|
|
DATA_HANDLERS.add(CHANGE_PLAYER_DATA_HANDLER);
|
2016-08-02 16:32:13 +02:00
|
|
|
DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
|
2016-12-27 17:40:27 +01:00
|
|
|
DATA_HANDLERS.add(LASER_PARTICLE_HANDLER);
|
2015-02-20 22:45:33 +01:00
|
|
|
}
|
|
|
|
}
|