From 0d40f407fd57d5ff2dfed8ae8ebd92501db2b5f9 Mon Sep 17 00:00:00 2001 From: Sakura Ryoko Date: Sun, 16 Jun 2024 14:31:45 -0400 Subject: [PATCH] Make SyncmaticaPayload.java be an inner Class of the SyncmaticaPacket --- gradle.properties | 10 +-- .../java/ch/endte/syncmatica/Context.java | 10 +-- .../java/ch/endte/syncmatica/Syncmatica.java | 6 +- .../communication/ExchangeTarget.java | 4 +- .../mixin/MixinClientPlayNetworkHandler.java | 4 +- .../syncmatica/mixin/MixinPlayerManager.java | 4 +- .../mixin/MixinServerPlayNetworkHandler.java | 4 +- .../network/handler/ClientPlayHandler.java | 11 ++- .../network/handler/ServerPlayHandler.java | 19 +++-- .../syncmatica/network/payload/SyncData.java | 45 ------------ .../network/payload/SyncmaticaPacket.java | 70 +++++++++++++++++++ .../network/payload/SyncmaticaPayload.java | 28 -------- src/main/resources/fabric.mod.json | 4 +- 13 files changed, 107 insertions(+), 112 deletions(-) delete mode 100644 src/main/java/ch/endte/syncmatica/network/payload/SyncData.java create mode 100644 src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPacket.java delete mode 100644 src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPayload.java diff --git a/gradle.properties b/gradle.properties index 7efcfa278..f45a035a5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,16 +10,16 @@ mod_name = Syncmatica mod_file_name = syncmatica-fabric # Mod Version -mod_version = 0.3.13-sakura.1 +mod_version = 0.3.13-sakura.2 # Dependencies (malilib, litematica) #malilib_fileid=4946328 #litematica_fileid=4946471 -malilib_version = 0.19.999-sakura.1 -litematica_version = 0.18.999-sakura.2 +malilib_version = 0.19.999-sakura.2 +litematica_version = 0.18.999-sakura.3 -malilib_id = c9b950bef7 -litematica_id = cb50b259df +malilib_id = 3244b550e0 +litematica_id = e561d31b48 # Minecraft, Fabric Loader and API and mappings versions minecraft_version_out = 1.21 diff --git a/src/main/java/ch/endte/syncmatica/Context.java b/src/main/java/ch/endte/syncmatica/Context.java index 4fd3e8af1..af0a6471e 100644 --- a/src/main/java/ch/endte/syncmatica/Context.java +++ b/src/main/java/ch/endte/syncmatica/Context.java @@ -12,8 +12,8 @@ import ch.endte.syncmatica.data.SyncmaticManager; import ch.endte.syncmatica.extended_core.PlayerIdentifierProvider; import ch.endte.syncmatica.network.handler.ClientPlayHandler; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; import ch.endte.syncmatica.network.handler.ServerPlayHandler; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import ch.endte.syncmatica.service.DebugService; import ch.endte.syncmatica.service.IService; import ch.endte.syncmatica.service.JsonConfiguration; @@ -162,7 +162,7 @@ public void registerReceivers() if (Reference.isServer() || Reference.isIntegratedServer() || Reference.isOpenToLan()) { Syncmatica.debug("Context#registerReceivers(): [SERVER] -> registerSyncmaticaHandler"); - ServerPlayNetworking.registerGlobalReceiver(SyncmaticaPayload.TYPE, ServerPlayHandler::receiveSyncPayload); + ServerPlayNetworking.registerGlobalReceiver(SyncmaticaPacket.Payload.ID, ServerPlayHandler::receiveSyncPayload); registerC2S = true; } } @@ -171,7 +171,7 @@ public void registerReceivers() if (Reference.isClient() && !registerS2C) { Syncmatica.debug("Context#registerReceivers(): [CLIENT] -> registerSyncmaticaHandler"); - ClientPlayNetworking.registerGlobalReceiver(SyncmaticaPayload.TYPE, ClientPlayHandler::receiveSyncPayload); + ClientPlayNetworking.registerGlobalReceiver(SyncmaticaPacket.Payload.ID, ClientPlayHandler::receiveSyncPayload); registerS2C = true; } } @@ -184,7 +184,7 @@ public void unregisterReceivers() if (Reference.isServer() || Reference.isIntegratedServer() || Reference.isOpenToLan()) { Syncmatica.debug("Context#unregisterReceivers(): [SERVER] -> unregisterSyncmaticaHandlers"); - ServerPlayNetworking.unregisterGlobalReceiver(SyncmaticaPayload.TYPE.id()); + ServerPlayNetworking.unregisterGlobalReceiver(SyncmaticaPacket.Payload.ID.id()); registerC2S = false; } } @@ -193,7 +193,7 @@ public void unregisterReceivers() if (Reference.isClient()) { Syncmatica.debug("Context#unregisterReceivers(): [CLIENT] -> unregisterSyncmaticaHandlers"); - ClientPlayNetworking.unregisterGlobalReceiver(SyncmaticaPayload.TYPE.id()); + ClientPlayNetworking.unregisterGlobalReceiver(SyncmaticaPacket.Payload.ID.id()); registerS2C = false; } } diff --git a/src/main/java/ch/endte/syncmatica/Syncmatica.java b/src/main/java/ch/endte/syncmatica/Syncmatica.java index 103516af5..cfb3a2645 100644 --- a/src/main/java/ch/endte/syncmatica/Syncmatica.java +++ b/src/main/java/ch/endte/syncmatica/Syncmatica.java @@ -8,7 +8,7 @@ import ch.endte.syncmatica.data.IFileStorage; import ch.endte.syncmatica.data.SyncmaticManager; import ch.endte.syncmatica.network.actor.ActorClientPlayHandler; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -34,8 +34,8 @@ public class Syncmatica public static void preInit() { Syncmatica.debug("Syncmatica#preInit(): registering play channel(s)"); - PayloadTypeRegistry.playC2S().register(SyncmaticaPayload.TYPE, SyncmaticaPayload.CODEC); - PayloadTypeRegistry.playS2C().register(SyncmaticaPayload.TYPE, SyncmaticaPayload.CODEC); + PayloadTypeRegistry.playC2S().register(SyncmaticaPacket.Payload.ID, SyncmaticaPacket.Payload.CODEC); + PayloadTypeRegistry.playS2C().register(SyncmaticaPacket.Payload.ID, SyncmaticaPacket.Payload.CODEC); // These need to be registered ASAP at launch. } diff --git a/src/main/java/ch/endte/syncmatica/communication/ExchangeTarget.java b/src/main/java/ch/endte/syncmatica/communication/ExchangeTarget.java index e0496791b..8df806a34 100644 --- a/src/main/java/ch/endte/syncmatica/communication/ExchangeTarget.java +++ b/src/main/java/ch/endte/syncmatica/communication/ExchangeTarget.java @@ -9,7 +9,7 @@ import ch.endte.syncmatica.network.handler.ClientPlayHandler; import ch.endte.syncmatica.network.handler.ServerPlayHandler; import ch.endte.syncmatica.network.payload.PacketType; -import ch.endte.syncmatica.network.payload.SyncData; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import net.minecraft.client.network.ClientPlayNetworkHandler; import net.minecraft.network.PacketByteBuf; import net.minecraft.server.network.ServerPlayNetworkHandler; @@ -51,7 +51,7 @@ public void sendPacket(final PacketType type, final PacketByteBuf byteBuf, final if (context != null) { context.getDebugService().logSendPacket(type, persistentName); } - final SyncData newPacket = new SyncData(type.getId(), byteBuf); + final SyncmaticaPacket newPacket = new SyncmaticaPacket(type.getId(), byteBuf); if (newPacket.getType() == null) { diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java b/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java index e14fb9bc8..5ce53d646 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinClientPlayNetworkHandler.java @@ -8,7 +8,7 @@ import ch.endte.syncmatica.communication.ExchangeTarget; import ch.endte.syncmatica.network.actor.IClientPlay; import ch.endte.syncmatica.network.handler.ClientPlayHandler; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; @@ -41,7 +41,7 @@ public abstract class MixinClientPlayNetworkHandler implements IClientPlay if (packet.getId().id().getNamespace().equals(Reference.MOD_ID)) { - SyncmaticaPayload payload = (SyncmaticaPayload) packet; + SyncmaticaPacket.Payload payload = (SyncmaticaPacket.Payload) packet; ClientPlayHandler.decodeSyncData(payload.data(), (ClientPlayNetworkHandler) (Object) this); // Cancel unnecessary processing if a PacketType we own is caught diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinPlayerManager.java b/src/main/java/ch/endte/syncmatica/mixin/MixinPlayerManager.java index 3668eb966..4ce3b9f17 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinPlayerManager.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinPlayerManager.java @@ -5,7 +5,7 @@ import ch.endte.syncmatica.Syncmatica; import ch.endte.syncmatica.network.handler.ServerPlayHandler; import ch.endte.syncmatica.network.payload.PacketType; -import ch.endte.syncmatica.network.payload.SyncData; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import io.netty.buffer.Unpooled; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -36,7 +36,7 @@ public class MixinPlayerManager PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); buf.writeString(Reference.MOD_VERSION); - ServerPlayHandler.encodeSyncData(new SyncData(PacketType.REGISTER_VERSION.getId(), buf), player); + ServerPlayHandler.encodeSyncData(new SyncmaticaPacket(PacketType.REGISTER_VERSION.getId(), buf), player); } } } diff --git a/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java b/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java index 83818190a..d10444af1 100644 --- a/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java +++ b/src/main/java/ch/endte/syncmatica/mixin/MixinServerPlayNetworkHandler.java @@ -8,7 +8,7 @@ import ch.endte.syncmatica.communication.ServerCommunicationManager; import ch.endte.syncmatica.network.actor.IServerPlay; import ch.endte.syncmatica.network.handler.ServerPlayHandler; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -53,7 +53,7 @@ public abstract class MixinServerPlayNetworkHandler implements IServerPlay if (thisPayload.getId().id().getNamespace().equals(Reference.MOD_ID)) { - SyncmaticaPayload payload = (SyncmaticaPayload) thisPayload; + SyncmaticaPacket.Payload payload = (SyncmaticaPacket.Payload) thisPayload; ServerPlayHandler.decodeSyncData(payload.data(), this); // Cancel unnecessary processing if a PacketType we own is caught diff --git a/src/main/java/ch/endte/syncmatica/network/handler/ClientPlayHandler.java b/src/main/java/ch/endte/syncmatica/network/handler/ClientPlayHandler.java index 013766577..460fedf31 100644 --- a/src/main/java/ch/endte/syncmatica/network/handler/ClientPlayHandler.java +++ b/src/main/java/ch/endte/syncmatica/network/handler/ClientPlayHandler.java @@ -3,8 +3,7 @@ import javax.annotation.Nonnull; import java.util.Objects; import ch.endte.syncmatica.network.actor.ActorClientPlayHandler; -import ch.endte.syncmatica.network.payload.SyncData; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.client.network.ClientPlayNetworkHandler; @@ -17,15 +16,15 @@ */ public class ClientPlayHandler { - public static void decodeSyncData(@Nonnull SyncData data, @Nonnull ClientPlayNetworkHandler handler) + public static void decodeSyncData(@Nonnull SyncmaticaPacket data, @Nonnull ClientPlayNetworkHandler handler) { CallbackInfo ci = new CallbackInfo("receiveSyncPacket", false); ActorClientPlayHandler.getInstance().packetEvent(data.getType(), data.getPacket(), handler, ci); } - public static void encodeSyncData(@Nonnull SyncData data, ClientPlayNetworkHandler handler) + public static void encodeSyncData(@Nonnull SyncmaticaPacket data, ClientPlayNetworkHandler handler) { - SyncmaticaPayload payload = new SyncmaticaPayload(data); + SyncmaticaPacket.Payload payload = new SyncmaticaPacket.Payload(data); if (handler != null) { sendSyncPacket(payload, handler); @@ -36,7 +35,7 @@ public static void encodeSyncData(@Nonnull SyncData data, ClientPlayNetworkHandl } } - public static void receiveSyncPayload(SyncmaticaPayload payload, ClientPlayNetworking.Context context) + public static void receiveSyncPayload(SyncmaticaPacket.Payload payload, ClientPlayNetworking.Context context) { decodeSyncData(payload.data(), Objects.requireNonNull(context.client().getNetworkHandler())); } diff --git a/src/main/java/ch/endte/syncmatica/network/handler/ServerPlayHandler.java b/src/main/java/ch/endte/syncmatica/network/handler/ServerPlayHandler.java index d07d9c2c1..f72fc2f0f 100644 --- a/src/main/java/ch/endte/syncmatica/network/handler/ServerPlayHandler.java +++ b/src/main/java/ch/endte/syncmatica/network/handler/ServerPlayHandler.java @@ -2,42 +2,41 @@ import javax.annotation.Nonnull; import ch.endte.syncmatica.network.actor.IServerPlay; -import ch.endte.syncmatica.network.payload.SyncData; -import ch.endte.syncmatica.network.payload.SyncmaticaPayload; +import ch.endte.syncmatica.network.payload.SyncmaticaPacket; +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.network.packet.CustomPayload; import net.minecraft.network.packet.Packet; import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket; import net.minecraft.server.network.ServerPlayNetworkHandler; import net.minecraft.server.network.ServerPlayerEntity; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; /** * Network packet senders / receivers (Server Context) */ public abstract class ServerPlayHandler { - public static void decodeSyncData(@Nonnull SyncData data, @Nonnull ServerPlayNetworkHandler handler) + public static void decodeSyncData(@Nonnull SyncmaticaPacket data, @Nonnull ServerPlayNetworkHandler handler) { IServerPlay iDo = ((IServerPlay) handler); iDo.syncmatica$operateComms(sm -> sm.onPacket(iDo.syncmatica$getExchangeTarget(), data.getType(), data.getPacket())); } - public static void decodeSyncData(@Nonnull SyncData data, @Nonnull IServerPlay iDo) + public static void decodeSyncData(@Nonnull SyncmaticaPacket data, @Nonnull IServerPlay iDo) { iDo.syncmatica$operateComms(sm -> sm.onPacket(iDo.syncmatica$getExchangeTarget(), data.getType(), data.getPacket())); } - public static void encodeSyncData(@Nonnull SyncData data, @Nonnull ServerPlayNetworkHandler handler) + public static void encodeSyncData(@Nonnull SyncmaticaPacket data, @Nonnull ServerPlayNetworkHandler handler) { - sendSyncPacket(new SyncmaticaPayload(data), handler); + sendSyncPacket(new SyncmaticaPacket.Payload(data), handler); } - public static void encodeSyncData(@Nonnull SyncData data, @Nonnull ServerPlayerEntity player) + public static void encodeSyncData(@Nonnull SyncmaticaPacket data, @Nonnull ServerPlayerEntity player) { - sendSyncPacket(new SyncmaticaPayload(data), player); + sendSyncPacket(new SyncmaticaPacket.Payload(data), player); } - public static void receiveSyncPayload(SyncmaticaPayload payload, ServerPlayNetworking.Context context) + public static void receiveSyncPayload(SyncmaticaPacket.Payload payload, ServerPlayNetworking.Context context) { decodeSyncData(payload.data(), context.player().networkHandler); } diff --git a/src/main/java/ch/endte/syncmatica/network/payload/SyncData.java b/src/main/java/ch/endte/syncmatica/network/payload/SyncData.java deleted file mode 100644 index 952154078..000000000 --- a/src/main/java/ch/endte/syncmatica/network/payload/SyncData.java +++ /dev/null @@ -1,45 +0,0 @@ -package ch.endte.syncmatica.network.payload; - -import javax.annotation.Nonnull; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.util.Identifier; - -public class SyncData -{ - private final PacketByteBuf packet; - private final PacketType type; - private final Identifier channel; - - public SyncData(@Nonnull Identifier channel, @Nonnull PacketByteBuf packet) - { - this.channel = channel; - this.packet = packet; - this.type = PacketType.getType(channel); - } - - public PacketType getType() - { - return this.type; - } - - public Identifier getChannel() - { - return this.channel; - } - - public PacketByteBuf getPacket() - { - return new PacketByteBuf(this.packet); - } - - protected static SyncData fromPacket(PacketByteBuf input) - { - return new SyncData(input.readIdentifier(), new PacketByteBuf(input.readBytes(input.readableBytes()))); - } - - protected void toPacket(PacketByteBuf output) - { - output.writeIdentifier(this.channel); - output.writeBytes(this.packet.readBytes(this.packet.readableBytes())); - } -} diff --git a/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPacket.java b/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPacket.java new file mode 100644 index 000000000..d51aca659 --- /dev/null +++ b/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPacket.java @@ -0,0 +1,70 @@ +package ch.endte.syncmatica.network.payload; + +import javax.annotation.Nonnull; +import ch.endte.syncmatica.Syncmatica; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.network.packet.CustomPayload; +import net.minecraft.util.Identifier; + +public class SyncmaticaPacket +{ + private final PacketByteBuf packet; + private final PacketType type; + private final Identifier channel; + + public SyncmaticaPacket(@Nonnull Identifier channel, @Nonnull PacketByteBuf packet) + { + this.channel = channel; + this.packet = packet; + this.type = PacketType.getType(channel); + } + + public PacketType getType() + { + return this.type; + } + + public Identifier getChannel() + { + return this.channel; + } + + public PacketByteBuf getPacket() + { + return new PacketByteBuf(this.packet); + } + + protected static SyncmaticaPacket fromPacket(PacketByteBuf input) + { + return new SyncmaticaPacket(input.readIdentifier(), new PacketByteBuf(input.readBytes(input.readableBytes()))); + } + + protected void toPacket(PacketByteBuf output) + { + output.writeIdentifier(this.channel); + output.writeBytes(this.packet.readBytes(this.packet.readableBytes())); + } + + public record Payload(SyncmaticaPacket data) implements CustomPayload + { + public static final Id ID = new Id<>(Syncmatica.NETWORK_ID); + public static final PacketCodec CODEC = CustomPayload.codecOf(Payload::write, Payload::new); + + public Payload(PacketByteBuf input) + { + this(SyncmaticaPacket.fromPacket(input)); + } + + private void write(PacketByteBuf output) + { + data.toPacket(output); + } + + @Override + public Id getId() + { + return ID; + } + } +} diff --git a/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPayload.java b/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPayload.java deleted file mode 100644 index 73af8851f..000000000 --- a/src/main/java/ch/endte/syncmatica/network/payload/SyncmaticaPayload.java +++ /dev/null @@ -1,28 +0,0 @@ -package ch.endte.syncmatica.network.payload; - -import ch.endte.syncmatica.Syncmatica; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.network.codec.PacketCodec; -import net.minecraft.network.packet.CustomPayload; - -public record SyncmaticaPayload(SyncData data) implements CustomPayload -{ - public static final Id TYPE = new Id<>(Syncmatica.NETWORK_ID); - public static final PacketCodec CODEC = CustomPayload.codecOf(SyncmaticaPayload::write, SyncmaticaPayload::new); - - public SyncmaticaPayload(PacketByteBuf input) - { - this(SyncData.fromPacket(input)); - } - - private void write(PacketByteBuf output) - { - data.toPacket(output); - } - - @Override - public Id getId() - { - return TYPE; - } -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index bd8f5fd03..da3f4ad39 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -27,8 +27,8 @@ "fabric-networking-api-v1": ">=4.1.0" }, "suggests": { - "litematica": ">=0.18.999-sakura.2", - "malilib": ">=0.19.999-sakura.1" + "litematica": ">=0.18.999-sakura.3", + "malilib": ">=0.19.999-sakura.2" }, "icon": "assets/syncmatica/icon/logo.png",