mirror of
https://github.com/Creators-of-Create/Create.git
synced 2024-12-24 14:06:42 +01:00
Refactor Packet registering
This commit is contained in:
parent
5e9950ba98
commit
25309e40b4
8 changed files with 79 additions and 29 deletions
|
@ -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 <T extends SimplePacketBase> AllPackets(Class<T> type, Function<PacketBuffer, T> 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<T extends SimplePacketBase> {
|
||||
private static int index = 0;
|
||||
BiConsumer<T, PacketBuffer> encoder;
|
||||
Function<PacketBuffer, T> decoder;
|
||||
BiConsumer<T, Supplier<Context>> handler;
|
||||
Class<T> type;
|
||||
|
||||
private LoadedPacket(Class<T> type, Function<PacketBuffer, T> 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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
@ -26,7 +26,7 @@ public class NbtPacket {
|
|||
slot = buffer.readInt();
|
||||
}
|
||||
|
||||
public void toBytes(PacketBuffer buffer) {
|
||||
public void write(PacketBuffer buffer) {
|
||||
buffer.writeItemStack(stack);
|
||||
buffer.writeInt(slot);
|
||||
}
|
||||
|
|
|
@ -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> context);
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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<BlockPos> 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) {
|
||||
|
|
Loading…
Reference in a new issue