From 25309e40b40c39d669e7d6dea8061d88e5e83e96 Mon Sep 17 00:00:00 2001 From: simibubi <31564874+simibubi@users.noreply.github.com> Date: Thu, 8 Aug 2019 19:31:46 +0200 Subject: [PATCH] Refactor Packet registering --- .../java/com/simibubi/create/AllPackets.java | 63 ++++++++++++++----- .../create/foundation/packet/NbtPacket.java | 6 +- .../foundation/packet/SimplePacketBase.java | 13 ++++ .../BuilderGunBeamPacket.java | 5 +- .../packet/ConfigureSchematicannonPacket.java | 5 +- .../packet/SchematicPlacePacket.java | 5 +- .../packet/SchematicUploadPacket.java | 5 +- .../symmetry/SymmetryEffectPacket.java | 6 +- 8 files changed, 79 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/packet/SimplePacketBase.java diff --git a/src/main/java/com/simibubi/create/AllPackets.java b/src/main/java/com/simibubi/create/AllPackets.java index 0d8308eb0..e2f7f9e64 100644 --- a/src/main/java/com/simibubi/create/AllPackets.java +++ b/src/main/java/com/simibubi/create/AllPackets.java @@ -1,41 +1,72 @@ package com.simibubi.create; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.function.Supplier; + import com.simibubi.create.foundation.packet.NbtPacket; +import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.modules.curiosities.placementHandgun.BuilderGunBeamPacket; import com.simibubi.create.modules.schematics.packet.ConfigureSchematicannonPacket; import com.simibubi.create.modules.schematics.packet.SchematicPlacePacket; import com.simibubi.create.modules.schematics.packet.SchematicUploadPacket; import com.simibubi.create.modules.symmetry.SymmetryEffectPacket; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; +import net.minecraftforge.fml.network.NetworkEvent.Context; import net.minecraftforge.fml.network.NetworkRegistry; import net.minecraftforge.fml.network.simple.SimpleChannel; -public class AllPackets { +public enum AllPackets { + + // Client to Server + NBT(NbtPacket.class, NbtPacket::new), + CONFIGURE_SCHEMATICANNON(ConfigureSchematicannonPacket.class, ConfigureSchematicannonPacket::new), + PLACE_SCHEMATIC(SchematicPlacePacket.class, SchematicPlacePacket::new), + UPLOAD_SCHEMATIC(SchematicUploadPacket.class, SchematicUploadPacket::new), + + // Server to Client + SYMMETRY_EFFECT(SymmetryEffectPacket.class, SymmetryEffectPacket::new), + BEAM_EFFECT(BuilderGunBeamPacket.class, BuilderGunBeamPacket::new), + + ; public static final ResourceLocation CHANNEL_NAME = new ResourceLocation(Create.ID, "network"); public static final String NETWORK_VERSION = new ResourceLocation(Create.ID, "1").toString(); public static SimpleChannel channel; - public static void registerPackets() { - int i = 0; + private LoadedPacket packet; + private AllPackets(Class type, Function factory) { + packet = new LoadedPacket<>(type, factory); + } + + public static void registerPackets() { channel = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME).serverAcceptedVersions(s -> true) .clientAcceptedVersions(s -> true).networkProtocolVersion(() -> NETWORK_VERSION).simpleChannel(); - - channel.messageBuilder(NbtPacket.class, i++).decoder(NbtPacket::new).encoder(NbtPacket::toBytes) - .consumer(NbtPacket::handle).add(); - channel.messageBuilder(SchematicPlacePacket.class, i++).decoder(SchematicPlacePacket::new) - .encoder(SchematicPlacePacket::toBytes).consumer(SchematicPlacePacket::handle).add(); - channel.messageBuilder(ConfigureSchematicannonPacket.class, i++).decoder(ConfigureSchematicannonPacket::new) - .encoder(ConfigureSchematicannonPacket::toBytes).consumer(ConfigureSchematicannonPacket::handle).add(); - channel.messageBuilder(SchematicUploadPacket.class, i++).decoder(SchematicUploadPacket::new) - .encoder(SchematicUploadPacket::toBytes).consumer(SchematicUploadPacket::handle).add(); - channel.messageBuilder(SymmetryEffectPacket.class, i++).decoder(SymmetryEffectPacket::new) - .encoder(SymmetryEffectPacket::toBytes).consumer(SymmetryEffectPacket::handle).add(); - channel.messageBuilder(BuilderGunBeamPacket.class, i++).decoder(BuilderGunBeamPacket::new) - .encoder(BuilderGunBeamPacket::toBytes).consumer(BuilderGunBeamPacket::handle).add(); + for (AllPackets packet : values()) + packet.packet.register(); } + private static class LoadedPacket { + private static int index = 0; + BiConsumer encoder; + Function decoder; + BiConsumer> handler; + Class type; + + private LoadedPacket(Class type, Function factory) { + encoder = T::write; + decoder = factory; + handler = T::handle; + this.type = type; + } + + private void register() { + channel.messageBuilder(type, index++).encoder(encoder).decoder(decoder).consumer(handler).add(); + } + } + } diff --git a/src/main/java/com/simibubi/create/foundation/packet/NbtPacket.java b/src/main/java/com/simibubi/create/foundation/packet/NbtPacket.java index 3a313975b..26875bc49 100644 --- a/src/main/java/com/simibubi/create/foundation/packet/NbtPacket.java +++ b/src/main/java/com/simibubi/create/foundation/packet/NbtPacket.java @@ -7,7 +7,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class NbtPacket { +public class NbtPacket extends SimplePacketBase { public ItemStack stack; public int slot; @@ -25,8 +25,8 @@ public class NbtPacket { stack = buffer.readItemStack(); slot = buffer.readInt(); } - - public void toBytes(PacketBuffer buffer) { + + public void write(PacketBuffer buffer) { buffer.writeItemStack(stack); buffer.writeInt(slot); } diff --git a/src/main/java/com/simibubi/create/foundation/packet/SimplePacketBase.java b/src/main/java/com/simibubi/create/foundation/packet/SimplePacketBase.java new file mode 100644 index 000000000..03d6cc064 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/packet/SimplePacketBase.java @@ -0,0 +1,13 @@ +package com.simibubi.create.foundation.packet; + +import java.util.function.Supplier; + +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkEvent.Context; + +public abstract class SimplePacketBase { + + public abstract void write(PacketBuffer buffer); + public abstract void handle(Supplier context); + +} diff --git a/src/main/java/com/simibubi/create/modules/curiosities/placementHandgun/BuilderGunBeamPacket.java b/src/main/java/com/simibubi/create/modules/curiosities/placementHandgun/BuilderGunBeamPacket.java index 375c21131..6615e77d4 100644 --- a/src/main/java/com/simibubi/create/modules/curiosities/placementHandgun/BuilderGunBeamPacket.java +++ b/src/main/java/com/simibubi/create/modules/curiosities/placementHandgun/BuilderGunBeamPacket.java @@ -2,6 +2,7 @@ package com.simibubi.create.modules.curiosities.placementHandgun; import java.util.function.Supplier; +import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.modules.curiosities.placementHandgun.BuilderGunHandler.LaserBeam; import net.minecraft.client.Minecraft; @@ -13,7 +14,7 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class BuilderGunBeamPacket { +public class BuilderGunBeamPacket extends SimplePacketBase { public Vec3d start; public Vec3d target; @@ -34,7 +35,7 @@ public class BuilderGunBeamPacket { self = buffer.readBoolean(); } - public void toBytes(PacketBuffer buffer) { + public void write(PacketBuffer buffer) { buffer.writeDouble(start.x); buffer.writeDouble(start.y); buffer.writeDouble(start.z); diff --git a/src/main/java/com/simibubi/create/modules/schematics/packet/ConfigureSchematicannonPacket.java b/src/main/java/com/simibubi/create/modules/schematics/packet/ConfigureSchematicannonPacket.java index 5423905b4..bbe111009 100644 --- a/src/main/java/com/simibubi/create/modules/schematics/packet/ConfigureSchematicannonPacket.java +++ b/src/main/java/com/simibubi/create/modules/schematics/packet/ConfigureSchematicannonPacket.java @@ -2,6 +2,7 @@ package com.simibubi.create.modules.schematics.packet; import java.util.function.Supplier; +import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.modules.schematics.block.SchematicannonTileEntity; import com.simibubi.create.modules.schematics.block.SchematicannonTileEntity.State; @@ -12,7 +13,7 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class ConfigureSchematicannonPacket { +public class ConfigureSchematicannonPacket extends SimplePacketBase { public static enum Option { DONT_REPLACE, REPLACE_SOLID, REPLACE_ANY, REPLACE_EMPTY, SKIP_MISSING, SKIP_TILES, PLAY, PAUSE, STOP; @@ -39,7 +40,7 @@ public class ConfigureSchematicannonPacket { set = buffer.readBoolean(); } - public void toBytes(PacketBuffer buffer) { + public void write(PacketBuffer buffer) { buffer.writeBlockPos(pos); buffer.writeInt(option.ordinal()); buffer.writeBoolean(set); diff --git a/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicPlacePacket.java b/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicPlacePacket.java index ec79bcbc9..420b2e422 100644 --- a/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicPlacePacket.java +++ b/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicPlacePacket.java @@ -2,6 +2,7 @@ package com.simibubi.create.modules.schematics.packet; import java.util.function.Supplier; +import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.modules.schematics.item.BlueprintItem; import net.minecraft.entity.player.ServerPlayerEntity; @@ -11,7 +12,7 @@ import net.minecraft.network.PacketBuffer; import net.minecraft.world.gen.feature.template.Template; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class SchematicPlacePacket { +public class SchematicPlacePacket extends SimplePacketBase { public ItemStack stack; @@ -23,7 +24,7 @@ public class SchematicPlacePacket { stack = buffer.readItemStack(); } - public void toBytes(PacketBuffer buffer) { + public void write(PacketBuffer buffer) { buffer.writeItemStack(stack); } diff --git a/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicUploadPacket.java b/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicUploadPacket.java index 5044484de..4c8f800b1 100644 --- a/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicUploadPacket.java +++ b/src/main/java/com/simibubi/create/modules/schematics/packet/SchematicUploadPacket.java @@ -3,6 +3,7 @@ package com.simibubi.create.modules.schematics.packet; import java.util.function.Supplier; import com.simibubi.create.Create; +import com.simibubi.create.foundation.packet.SimplePacketBase; import com.simibubi.create.foundation.type.DimensionPos; import com.simibubi.create.modules.schematics.block.SchematicTableContainer; @@ -11,7 +12,7 @@ import net.minecraft.network.PacketBuffer; import net.minecraft.util.math.BlockPos; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class SchematicUploadPacket { +public class SchematicUploadPacket extends SimplePacketBase { public static final int BEGIN = 0; public static final int WRITE = 1; @@ -53,7 +54,7 @@ public class SchematicUploadPacket { data = buffer.readByteArray(); } - public void toBytes(PacketBuffer buffer) { + public void write(PacketBuffer buffer) { buffer.writeInt(code); buffer.writeString(schematic); diff --git a/src/main/java/com/simibubi/create/modules/symmetry/SymmetryEffectPacket.java b/src/main/java/com/simibubi/create/modules/symmetry/SymmetryEffectPacket.java index 3dab4f809..a74e89d61 100644 --- a/src/main/java/com/simibubi/create/modules/symmetry/SymmetryEffectPacket.java +++ b/src/main/java/com/simibubi/create/modules/symmetry/SymmetryEffectPacket.java @@ -4,6 +4,8 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; +import com.simibubi.create.foundation.packet.SimplePacketBase; + import net.minecraft.client.Minecraft; import net.minecraft.network.PacketBuffer; import net.minecraft.util.math.BlockPos; @@ -12,7 +14,7 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.network.NetworkEvent.Context; -public class SymmetryEffectPacket { +public class SymmetryEffectPacket extends SimplePacketBase { private BlockPos mirror; private List positions; @@ -31,7 +33,7 @@ public class SymmetryEffectPacket { } } - public void toBytes(PacketBuffer buffer) { + public void write(PacketBuffer buffer) { buffer.writeBlockPos(mirror); buffer.writeInt(positions.size()); for (BlockPos blockPos : positions) {