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

210 lines
10 KiB
Java
Raw Normal View History

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
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.network;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.data.PlayerData;
import de.ellpeck.actuallyadditions.mod.data.WorldData;
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;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBase;
import de.ellpeck.actuallyadditions.mod.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;
2021-02-26 22:15:48 +01:00
import net.minecraft.entity.player.PlayerEntity;
2021-02-27 16:33:00 +01:00
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.inventory.container.Container;
2016-12-27 17:40:27 +01:00
import net.minecraft.item.ItemStack;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
2021-02-27 16:33:00 +01:00
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2021-02-27 16:33:00 +01:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.fml.network.NetworkDirection;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
2021-02-26 22:15:48 +01:00
import java.util.ArrayList;
import java.util.List;
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
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
2021-02-27 16:33:00 +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.getInt("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
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
World world = Minecraft.getInstance().world;
2019-05-02 09:10:29 +02:00
if (world != null) {
2021-02-27 16:33:00 +01:00
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("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
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
Minecraft mc = Minecraft.getInstance();
2016-12-27 17:40:27 +01:00
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) -> {
2021-02-27 16:33:00 +01:00
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
2019-02-27 19:53:05 +01:00
2019-05-02 09:10:29 +02:00
if (tile instanceof IButtonReactor) {
IButtonReactor reactor = (IButtonReactor) tile;
2021-02-27 16:33:00 +01:00
Entity entity = world.getEntityByID(compound.getInt("PlayerID"));
2021-02-26 22:15:48 +01:00
if (entity instanceof PlayerEntity) {
2021-02-27 16:33:00 +01:00
reactor.onButtonPressed(compound.getInt("ButtonID"), (PlayerEntity) entity);
}
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = (compound, context) -> {
2021-02-27 16:33:00 +01:00
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
Entity entity = world.getEntityByID(compound.getInt("PlayerID"));
2021-02-26 22:15:48 +01:00
if (entity instanceof PlayerEntity) {
Container container = ((PlayerEntity) entity).openContainer;
2019-05-02 09:10:29 +02:00
if (container instanceof IButtonReactor) {
2021-02-27 16:33:00 +01:00
((IButtonReactor) container).onButtonPressed(compound.getInt("ButtonID"), (PlayerEntity) entity);
}
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = (compound, context) -> {
2021-02-27 16:33:00 +01:00
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof INumberReactor) {
INumberReactor reactor = (INumberReactor) tile;
2021-02-27 16:33:00 +01:00
reactor.onNumberReceived(compound.getDouble("Number"), compound.getInt("NumberID"), (PlayerEntity) world.getEntityByID(compound.getInt("PlayerID")));
}
};
2019-02-27 19:53:05 +01:00
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = (compound, context) -> {
2021-02-27 16:33:00 +01:00
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
TileEntity tile = world.getTileEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
2019-05-02 09:10:29 +02:00
if (tile instanceof IStringReactor) {
IStringReactor reactor = (IStringReactor) tile;
2021-02-27 16:33:00 +01:00
reactor.onTextReceived(compound.getString("Text"), compound.getInt("TextID"), (PlayerEntity) world.getEntityByID(compound.getInt("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
2021-02-26 22:15:48 +01:00
@OnlyIn(Dist.CLIENT)
public void handleData(CompoundNBT compound, MessageContext context) {
CompoundNBT dataTag = compound.getCompoundTag("Data");
PlayerEntity 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) -> {
2021-02-27 16:33:00 +01:00
World world = DimensionManager.getWorld(compound.getInt("World"));
2021-02-26 22:15:48 +01:00
PlayerEntity 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);
2021-02-27 16:33:00 +01:00
int type = compound.getInt("Type");
2019-05-02 09:10:29 +02:00
if (type == 0) {
2021-02-26 22:15:48 +01: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) {
2021-02-26 22:15:48 +01:00
data.loadTrials(compound.getList("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
};
2021-02-27 16:33:00 +01:00
private static final String PROTOCOL_VERSION = Integer.toString(4);
public static final SimpleChannel THE_NETWORK = NetworkRegistry.newSimpleChannel(
new ResourceLocation(ActuallyAdditions.MODID, "main"),
() -> PROTOCOL_VERSION,
PROTOCOL_VERSION::equals,
PROTOCOL_VERSION::equals
);
2019-05-02 09:10:29 +02:00
public static void init() {
2021-02-27 16:33:00 +01:00
THE_NETWORK.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
THE_NETWORK.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);
2016-12-27 17:40:27 +01:00
DATA_HANDLERS.add(LASER_PARTICLE_HANDLER);
DATA_HANDLERS.add(PLAYER_DATA_TO_SERVER);
}
2021-02-27 16:33:00 +01:00
public static void sendTo(Object msg, ServerPlayerEntity player) {
if (!(player instanceof FakePlayer)) {
THE_NETWORK.sendTo(msg, player.connection.getNetworkManager(), NetworkDirection.PLAY_TO_CLIENT);
}
}
public static void sendToServer(Object msg) {
THE_NETWORK.sendToServer(msg);
}
public static void send(Object msg, PacketDistributor.PacketTarget target) {
THE_NETWORK.send(target, msg);
}
}