Skip to content

Commit

Permalink
Make SyncmaticaPayload.java be an inner Class of the SyncmaticaPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jun 16, 2024
1 parent c73e81e commit 0d40f40
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 112 deletions.
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/ch/endte/syncmatica/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/ch/endte/syncmatica/Syncmatica.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
45 changes: 0 additions & 45 deletions src/main/java/ch/endte/syncmatica/network/payload/SyncData.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Payload> ID = new Id<>(Syncmatica.NETWORK_ID);
public static final PacketCodec<PacketByteBuf, Payload> 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<Payload> getId()
{
return ID;
}
}
}

This file was deleted.

Loading

0 comments on commit 0d40f40

Please sign in to comment.