ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/network/PacketHandler.java

178 lines
9.1 KiB
Java
Raw Normal View History

package de.ellpeck.actuallyadditions.common.network;
2019-05-02 09:10:29 +02:00
import java.util.ArrayList;
import java.util.List;
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.data.PlayerData;
import de.ellpeck.actuallyadditions.common.data.WorldData;
import de.ellpeck.actuallyadditions.common.network.gui.IButtonReactor;
import de.ellpeck.actuallyadditions.common.network.gui.INumberReactor;
import de.ellpeck.actuallyadditions.common.network.gui.IStringReactor;
import de.ellpeck.actuallyadditions.common.particle.ParticleLaserItem;
import de.ellpeck.actuallyadditions.common.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.common.util.AssetUtil;
import net.minecraft.client.Minecraft;
2016-12-27 17:40:27 +01:00
import net.minecraft.client.particle.Particle;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
2016-12-27 17:40:27 +01:00
import net.minecraft.item.ItemStack;
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;
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;
import net.minecraftforge.fml.relauncher.SideOnly;
2019-05-02 09:10:29 +02:00
public final class PacketHandler {
2019-02-27 19:53:05 +01:00
public static final List<IDataHandler> DATA_HANDLERS = new ArrayList<>();
2019-05-02 09:10:29 +02:00
public static final IDataHandler LASER_HANDLER = new IDataHandler() {
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void handleData(NBTTagCompound compound, MessageContext context) {
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"));
}
};
2019-05-02 09:10:29 +02:00
public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler() {
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void handleData(NBTTagCompound compound, MessageContext context) {
2016-11-26 21:32:27 +01:00
World world = Minecraft.getMinecraft().world;
2019-05-02 09:10:29 +02:00
if (world != null) {
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof TileEntityBase) {
((TileEntityBase) tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
}
}
}
};
2019-05-02 09:10:29 +02:00
public static final IDataHandler LASER_PARTICLE_HANDLER = new IDataHandler() {
2016-12-27 17:40:27 +01:00
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02: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);
2019-05-02 09:10:29 +02:00
double inX = compound.getDouble("InX") + 0.5;
double inY = compound.getDouble("InY") + 0.78;
double inZ = compound.getDouble("InZ") + 0.5;
2016-12-27 17:40:27 +01:00
2019-05-02 09:10:29 +02:00
double outX = compound.getDouble("OutX") + 0.5;
double outY = compound.getDouble("OutY") + 0.525;
double outZ = compound.getDouble("OutZ") + 0.5;
2016-12-27 17:40:27 +01:00
Particle fx = new ParticleLaserItem(mc.world, outX, outY, outZ, stack, 0.025, inX, inY, inZ);
mc.effectRenderer.addEffect(fx);
2016-12-27 17:40:27 +01:00
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof IButtonReactor) {
IButtonReactor reactor = (IButtonReactor) tile;
2019-02-27 19:53:05 +01:00
Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
2019-05-02 09:10:29 +02:00
if (entity instanceof EntityPlayer) {
reactor.onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer) entity);
}
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
Entity entity = world.getEntityByID(compound.getInteger("PlayerID"));
2019-05-02 09:10:29 +02:00
if (entity instanceof EntityPlayer) {
Container container = ((EntityPlayer) entity).openContainer;
if (container instanceof IButtonReactor) {
((IButtonReactor) container).onButtonPressed(compound.getInteger("ButtonID"), (EntityPlayer) entity);
}
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof INumberReactor) {
INumberReactor reactor = (INumberReactor) tile;
reactor.onNumberReceived(compound.getDouble("Number"), compound.getInteger("NumberID"), (EntityPlayer) world.getEntityByID(compound.getInteger("PlayerID")));
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInteger("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInteger("X"), compound.getInteger("Y"), compound.getInteger("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof IStringReactor) {
IStringReactor reactor = (IStringReactor) tile;
reactor.onTextReceived(compound.getString("Text"), compound.getInteger("TextID"), (EntityPlayer) world.getEntityByID(compound.getInteger("PlayerID")));
}
};
2019-05-02 09:10:29 +02:00
public static final IDataHandler SYNC_PLAYER_DATA = new IDataHandler() {
2016-06-17 23:50:38 +02:00
@Override
@SideOnly(Side.CLIENT)
2019-05-02 09:10:29 +02:00
public void handleData(NBTTagCompound compound, MessageContext context) {
NBTTagCompound dataTag = compound.getCompoundTag("Data");
2018-05-10 11:38:58 +02:00
EntityPlayer player = ActuallyAdditions.PROXY.getCurrentPlayer();
2019-05-02 09:10:29 +02:00
if (player != null) {
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
2019-05-02 09:10:29 +02:00
if (compound.getBoolean("Log")) {
ActuallyAdditions.LOGGER.info("Receiving (new or changed) Player Data for player " + player.getName() + ".");
}
2019-05-02 09:10:29 +02:00
} else {
2018-05-10 11:38:58 +02:00
ActuallyAdditions.LOGGER.error("Tried to receive Player Data for the current player, but he doesn't seem to be present!");
}
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler PLAYER_DATA_TO_SERVER = (compound, context) -> {
World world = DimensionManager.getWorld(compound.getInteger("World"));
EntityPlayer player = world.getPlayerEntityByUUID(compound.getUniqueId("UUID"));
2019-05-02 09:10:29 +02:00
if (player != null) {
2019-02-27 19:53:05 +01:00
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
int type = compound.getInteger("Type");
2019-05-02 09:10:29 +02:00
if (type == 0) {
2020-09-21 19:59:08 +02:00
data.loadBookmarks(compound.getList("Bookmarks", 8));
2019-05-02 09:10:29 +02:00
} else if (type == 1) {
2019-02-27 19:53:05 +01:00
data.didBookTutorial = compound.getBoolean("DidBookTutorial");
2019-05-02 09:10:29 +02:00
} else if (type == 2) {
2019-02-27 19:53:05 +01:00
data.loadTrials(compound.getTagList("Trials", 8));
2019-05-02 09:10:29 +02:00
if (compound.getBoolean("Achievement")) {
2019-02-27 19:53:05 +01:00
//TheAchievements.COMPLETE_TRIALS.get(player);
}
}
2019-02-27 19:53:05 +01:00
WorldData.get(world).markDirty();
2019-05-02 09:10:29 +02:00
if (compound.getBoolean("Log")) {
ActuallyAdditions.LOGGER.info("Receiving changed Player Data for player " + player.getName() + ".");
2016-06-17 23:50:38 +02:00
}
2019-05-02 09:10:29 +02:00
} else {
ActuallyAdditions.LOGGER.error("Tried to receive Player Data for UUID " + compound.getUniqueId("UUID") + ", but he doesn't seem to be present!");
2019-02-27 19:53:05 +01:00
}
2016-06-17 23:50:38 +02:00
};
public static SimpleNetworkWrapper theNetwork;
2019-05-02 09:10:29 +02:00
public static void init() {
2018-05-10 11:38:58 +02:00
theNetwork = NetworkRegistry.INSTANCE.newSimpleChannel(ActuallyAdditions.MODID);
theNetwork.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
theNetwork.registerMessage(PacketClientToServer.Handler.class, PacketClientToServer.class, 1, Side.SERVER);
2016-12-27 17:40:27 +01:00
DATA_HANDLERS.add(LASER_HANDLER);
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(SYNC_PLAYER_DATA);
DATA_HANDLERS.add(GUI_BUTTON_TO_CONTAINER_HANDLER);
2020-09-09 16:49:01 +02:00
DATA_HANDLERS.add(LASER_PARTICLE_HANDLER);
DATA_HANDLERS.add(PLAYER_DATA_TO_SERVER);
}
}