mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-22 23:28:35 +01:00
Some low hanging network fruit.
This commit is contained in:
parent
2a6126a1df
commit
3da868fc29
3 changed files with 44 additions and 47 deletions
|
@ -28,13 +28,16 @@ import net.minecraft.inventory.container.Container;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.nbt.CompoundNBT;
|
import net.minecraft.nbt.CompoundNBT;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
import net.minecraft.tileentity.TileEntity;
|
||||||
|
import net.minecraft.util.RegistryKey;
|
||||||
import net.minecraft.util.ResourceLocation;
|
import net.minecraft.util.ResourceLocation;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.registry.Registry;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.common.util.FakePlayer;
|
import net.minecraftforge.common.util.FakePlayer;
|
||||||
import net.minecraftforge.fml.network.NetworkDirection;
|
import net.minecraftforge.fml.network.NetworkDirection;
|
||||||
|
import net.minecraftforge.fml.network.NetworkEvent;
|
||||||
import net.minecraftforge.fml.network.NetworkRegistry;
|
import net.minecraftforge.fml.network.NetworkRegistry;
|
||||||
import net.minecraftforge.fml.network.PacketDistributor;
|
import net.minecraftforge.fml.network.PacketDistributor;
|
||||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
||||||
|
@ -48,19 +51,19 @@ public final class PacketHandler {
|
||||||
public static final IDataHandler LASER_HANDLER = new IDataHandler() {
|
public static final IDataHandler LASER_HANDLER = new IDataHandler() {
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public void handleData(CompoundNBT compound, MessageContext context) {
|
public void handleData(CompoundNBT compound, NetworkEvent.Context 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.getInt("MaxAge"), compound.getDouble("RotationTime"), compound.getFloat("Size"), compound.getFloat("Alpha"));
|
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"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler() {
|
public static final IDataHandler TILE_ENTITY_HANDLER = new IDataHandler() {
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public void handleData(CompoundNBT compound, MessageContext context) {
|
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
|
||||||
World world = Minecraft.getInstance().level;
|
World world = Minecraft.getInstance().level;
|
||||||
if (world != null) {
|
if (world != null) {
|
||||||
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
||||||
if (tile instanceof TileEntityBase) {
|
if (tile instanceof TileEntityBase) {
|
||||||
((TileEntityBase) tile).readSyncableNBT(compound.getCompoundTag("Data"), TileEntityBase.NBTType.SYNC);
|
((TileEntityBase) tile).readSyncableNBT(compound.getCompound("Data"), TileEntityBase.NBTType.SYNC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,9 +71,9 @@ public final class PacketHandler {
|
||||||
public static final IDataHandler LASER_PARTICLE_HANDLER = new IDataHandler() {
|
public static final IDataHandler LASER_PARTICLE_HANDLER = new IDataHandler() {
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public void handleData(CompoundNBT compound, MessageContext context) {
|
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
|
||||||
Minecraft mc = Minecraft.getInstance();
|
Minecraft mc = Minecraft.getInstance();
|
||||||
ItemStack stack = new ItemStack(compound);
|
ItemStack stack = null; //new ItemStack(compound); //TODO
|
||||||
|
|
||||||
double inX = compound.getDouble("InX") + 0.5;
|
double inX = compound.getDouble("InX") + 0.5;
|
||||||
double inY = compound.getDouble("InY") + 0.78;
|
double inY = compound.getDouble("InY") + 0.78;
|
||||||
|
@ -81,11 +84,11 @@ public final class PacketHandler {
|
||||||
double outZ = compound.getDouble("OutZ") + 0.5;
|
double outZ = compound.getDouble("OutZ") + 0.5;
|
||||||
|
|
||||||
Particle fx = new ParticleLaserItem(mc.level, outX, outY, outZ, stack, 0.025, inX, inY, inZ);
|
Particle fx = new ParticleLaserItem(mc.level, outX, outY, outZ, stack, 0.025, inX, inY, inZ);
|
||||||
mc.effectRenderer.addEffect(fx);
|
//mc.effectRenderer.addEffect(fx); //TODO
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = (compound, context) -> {
|
public static final IDataHandler GUI_BUTTON_TO_TILE_HANDLER = (compound, context) -> {
|
||||||
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
|
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
|
||||||
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
||||||
|
|
||||||
if (tile instanceof IButtonReactor) {
|
if (tile instanceof IButtonReactor) {
|
||||||
|
@ -97,7 +100,7 @@ public final class PacketHandler {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = (compound, context) -> {
|
public static final IDataHandler GUI_BUTTON_TO_CONTAINER_HANDLER = (compound, context) -> {
|
||||||
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
|
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
|
||||||
Entity entity = world.getEntity(compound.getInt("PlayerID"));
|
Entity entity = world.getEntity(compound.getInt("PlayerID"));
|
||||||
if (entity instanceof PlayerEntity) {
|
if (entity instanceof PlayerEntity) {
|
||||||
Container container = ((PlayerEntity) entity).containerMenu;
|
Container container = ((PlayerEntity) entity).containerMenu;
|
||||||
|
@ -107,7 +110,7 @@ public final class PacketHandler {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = (compound, context) -> {
|
public static final IDataHandler GUI_NUMBER_TO_TILE_HANDLER = (compound, context) -> {
|
||||||
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
|
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
|
||||||
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
||||||
|
|
||||||
if (tile instanceof INumberReactor) {
|
if (tile instanceof INumberReactor) {
|
||||||
|
@ -116,7 +119,7 @@ public final class PacketHandler {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = (compound, context) -> {
|
public static final IDataHandler GUI_STRING_TO_TILE_HANDLER = (compound, context) -> {
|
||||||
World world = DimensionManager.getWorld(compound.getInt("WorldID"));
|
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("WorldID"))));
|
||||||
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
TileEntity tile = world.getBlockEntity(new BlockPos(compound.getInt("X"), compound.getInt("Y"), compound.getInt("Z")));
|
||||||
|
|
||||||
if (tile instanceof IStringReactor) {
|
if (tile instanceof IStringReactor) {
|
||||||
|
@ -127,9 +130,9 @@ public final class PacketHandler {
|
||||||
public static final IDataHandler SYNC_PLAYER_DATA = new IDataHandler() {
|
public static final IDataHandler SYNC_PLAYER_DATA = new IDataHandler() {
|
||||||
@Override
|
@Override
|
||||||
@OnlyIn(Dist.CLIENT)
|
@OnlyIn(Dist.CLIENT)
|
||||||
public void handleData(CompoundNBT compound, MessageContext context) {
|
public void handleData(CompoundNBT compound, NetworkEvent.Context context) {
|
||||||
CompoundNBT dataTag = compound.getCompoundTag("Data");
|
CompoundNBT dataTag = compound.getCompound("Data");
|
||||||
PlayerEntity player = ActuallyAdditions.PROXY.getCurrentPlayer();
|
PlayerEntity player = context.getSender(); //ActuallyAdditions.PROXY.getCurrentPlayer();
|
||||||
|
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
|
PlayerData.getDataFromPlayer(player).readFromNBT(dataTag, false);
|
||||||
|
@ -143,8 +146,8 @@ public final class PacketHandler {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
public static final IDataHandler PLAYER_DATA_TO_SERVER = (compound, context) -> {
|
public static final IDataHandler PLAYER_DATA_TO_SERVER = (compound, context) -> {
|
||||||
World world = DimensionManager.getWorld(compound.getInt("World"));
|
World world = context.getSender().getServer().getLevel(RegistryKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("World"))));
|
||||||
PlayerEntity player = world.getPlayerEntityByUUID(compound.getUUID("UUID"));
|
PlayerEntity player = world.getServer().getPlayerList().getPlayer(compound.getUUID("UUID"));
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
PlayerData.PlayerSave data = PlayerData.getDataFromPlayer(player);
|
||||||
|
|
||||||
|
@ -179,8 +182,8 @@ public final class PacketHandler {
|
||||||
);
|
);
|
||||||
|
|
||||||
public static void init() {
|
public static void init() {
|
||||||
THE_NETWORK.registerMessage(PacketServerToClient.Handler.class, PacketServerToClient.class, 0, Side.CLIENT);
|
THE_NETWORK.registerMessage(0, PacketServerToClient.class, PacketServerToClient::toBytes, PacketServerToClient::fromBytes, PacketServerToClient::handle, NetworkDirection.PLAY_TO_CLIENT);
|
||||||
THE_NETWORK.registerMessage(PacketClientToServer.Handler.class, PacketClientToServer.class, 1, Side.SERVER);
|
THE_NETWORK.registerMessage(1, PacketClientToServer.class, PacketClientToServer.class, NetworkDirection.PLAY_TO_SERVER);
|
||||||
|
|
||||||
DATA_HANDLERS.add(LASER_HANDLER);
|
DATA_HANDLERS.add(LASER_HANDLER);
|
||||||
DATA_HANDLERS.add(TILE_ENTITY_HANDLER);
|
DATA_HANDLERS.add(TILE_ENTITY_HANDLER);
|
||||||
|
|
|
@ -23,6 +23,7 @@ import net.minecraft.tileentity.TileEntity;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
|
import net.minecraftforge.fml.network.PacketDistributor;
|
||||||
|
|
||||||
public final class PacketHandlerHelper {
|
public final class PacketHandlerHelper {
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ public final class PacketHandlerHelper {
|
||||||
compound.putInt("X", pos.getX());
|
compound.putInt("X", pos.getX());
|
||||||
compound.putInt("Y", pos.getY());
|
compound.putInt("Y", pos.getY());
|
||||||
compound.putInt("Z", pos.getZ());
|
compound.putInt("Z", pos.getZ());
|
||||||
compound.putInt("WorldID", tile.getLevel().getDimension());
|
compound.putString("WorldID", tile.getLevel().dimension().getRegistryName().toString());
|
||||||
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
|
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
|
||||||
compound.putInt("ButtonID", buttonId);
|
compound.putInt("ButtonID", buttonId);
|
||||||
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
|
PacketHandler.THE_NETWORK.sendToServer(new PacketClientToServer(compound, PacketHandler.GUI_BUTTON_TO_TILE_HANDLER));
|
||||||
|
@ -48,7 +49,7 @@ public final class PacketHandlerHelper {
|
||||||
compound.put("Data", data);
|
compound.put("Data", data);
|
||||||
|
|
||||||
if (player instanceof ServerPlayerEntity) {
|
if (player instanceof ServerPlayerEntity) {
|
||||||
PacketHandler.THE_NETWORK.sendTo(new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA), (ServerPlayerEntity) player);
|
PacketHandler.THE_NETWORK.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), new PacketServerToClient(compound, PacketHandler.SYNC_PLAYER_DATA));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,8 +61,8 @@ public final class PacketHandlerHelper {
|
||||||
|
|
||||||
PlayerEntity player = Minecraft.getInstance().player;
|
PlayerEntity player = Minecraft.getInstance().player;
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
compound.putInt("World", player.level.provider.getDimension());
|
compound.putString("World", player.level.dimension().getRegistryName().toString());
|
||||||
compound.setUniqueId("UUID", player.getUUID());
|
compound.putUUID("UUID", player.getUUID());
|
||||||
|
|
||||||
PlayerSave data = PlayerData.getDataFromPlayer(player);
|
PlayerSave data = PlayerData.getDataFromPlayer(player);
|
||||||
|
|
||||||
|
@ -94,7 +95,7 @@ public final class PacketHandlerHelper {
|
||||||
compound.putInt("X", tile.getBlockPos().getX());
|
compound.putInt("X", tile.getBlockPos().getX());
|
||||||
compound.putInt("Y", tile.getBlockPos().getY());
|
compound.putInt("Y", tile.getBlockPos().getY());
|
||||||
compound.putInt("Z", tile.getBlockPos().getZ());
|
compound.putInt("Z", tile.getBlockPos().getZ());
|
||||||
compound.putInt("WorldID", tile.getLevel().provider.getDimension());
|
compound.putString("WorldID", tile.getLevel().dimension().getRegistryName().toString());
|
||||||
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
|
compound.putInt("PlayerID", Minecraft.getInstance().player.getId());
|
||||||
compound.putInt("NumberID", id);
|
compound.putInt("NumberID", id);
|
||||||
compound.putDouble("Number", number);
|
compound.putDouble("Number", number);
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
|
|
||||||
package de.ellpeck.actuallyadditions.mod.network;
|
package de.ellpeck.actuallyadditions.mod.network;
|
||||||
|
|
||||||
|
import com.mojang.brigadier.Message;
|
||||||
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
|
@ -17,12 +18,12 @@ import net.minecraft.nbt.CompoundNBT;
|
||||||
import net.minecraft.network.PacketBuffer;
|
import net.minecraft.network.PacketBuffer;
|
||||||
import net.minecraftforge.api.distmarker.Dist;
|
import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
import net.minecraftforge.fml.network.NetworkEvent;
|
||||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
|
||||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
|
||||||
public class PacketServerToClient implements Message {
|
public class PacketServerToClient {
|
||||||
|
|
||||||
private CompoundNBT data;
|
private CompoundNBT data;
|
||||||
private IDataHandler handler;
|
private IDataHandler handler;
|
||||||
|
@ -36,40 +37,32 @@ public class PacketServerToClient implements Message {
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static PacketServerToClient fromBytes(final PacketBuffer buffer) {
|
||||||
public void fromBytes(ByteBuf buf) {
|
|
||||||
PacketBuffer buffer = new PacketBuffer(buf);
|
|
||||||
try {
|
try {
|
||||||
this.data = buffer.readNbt();
|
CompoundNBT data = buffer.readNbt();
|
||||||
|
|
||||||
int handlerId = buffer.readInt();
|
int handlerId = buffer.readInt();
|
||||||
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
|
if (handlerId >= 0 && handlerId < PacketHandler.DATA_HANDLERS.size()) {
|
||||||
this.handler = PacketHandler.DATA_HANDLERS.get(handlerId);
|
return new PacketServerToClient(data, PacketHandler.DATA_HANDLERS.get(handlerId));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a client packet!", e);
|
ActuallyAdditions.LOGGER.error("Something went wrong trying to receive a client packet!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public static void toBytes(final PacketServerToClient message, PacketBuffer buffer) {
|
||||||
public void toBytes(ByteBuf buf) {
|
buffer.writeNbt(message.data);
|
||||||
PacketBuffer buffer = new PacketBuffer(buf);
|
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(message.handler));
|
||||||
|
|
||||||
buffer.writeNbt(this.data);
|
|
||||||
buffer.writeInt(PacketHandler.DATA_HANDLERS.indexOf(this.handler));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Handler implements IMessageHandler<PacketServerToClient, IMessage> {
|
public static void handle(final PacketServerToClient message, final Supplier<NetworkEvent.Context> ctx) {
|
||||||
|
ctx.get().enqueueWork(
|
||||||
@Override
|
() -> {
|
||||||
@OnlyIn(Dist.CLIENT)
|
|
||||||
public IMessage onMessage(PacketServerToClient message, MessageContext ctx) {
|
|
||||||
Minecraft.getInstance().addScheduledTask(() -> {
|
|
||||||
if (message.data != null && message.handler != null) {
|
if (message.data != null && message.handler != null) {
|
||||||
message.handler.handleData(message.data, ctx);
|
message.handler.handleData(message.data, ctx.get());
|
||||||
}
|
|
||||||
});
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
ctx.get().setPacketHandled(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue