Flywheel/src/main/java/com/simibubi/create/AllPackets.java

73 lines
2.6 KiB
Java
Raw Normal View History

package com.simibubi.create;
2019-08-08 19:31:46 +02:00
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;
import com.simibubi.create.foundation.packet.NbtPacket;
2019-08-08 19:31:46 +02:00
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;
2019-08-08 19:31:46 +02:00
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
2019-08-08 19:31:46 +02:00
import net.minecraftforge.fml.network.NetworkEvent.Context;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.simple.SimpleChannel;
2019-08-08 19:31:46 +02:00
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;
2019-08-08 19:31:46 +02:00
private LoadedPacket<?> packet;
private <T extends SimplePacketBase> AllPackets(Class<T> type, Function<PacketBuffer, T> factory) {
packet = new LoadedPacket<>(type, factory);
}
2019-08-08 19:31:46 +02:00
public static void registerPackets() {
channel = NetworkRegistry.ChannelBuilder.named(CHANNEL_NAME).serverAcceptedVersions(s -> true)
.clientAcceptedVersions(s -> true).networkProtocolVersion(() -> NETWORK_VERSION).simpleChannel();
2019-08-08 19:31:46 +02:00
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;
2019-08-08 19:31:46 +02:00
private LoadedPacket(Class<T> type, Function<PacketBuffer, T> factory) {
encoder = T::write;
decoder = factory;
handler = T::handle;
this.type = type;
}
2019-08-08 19:31:46 +02:00
private void register() {
channel.messageBuilder(type, index++).encoder(encoder).decoder(decoder).consumer(handler).add();
}
}
}