Skip to content

Commit

Permalink
Fix data channel for servux
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jun 27, 2024
1 parent 339f6e7 commit e2ba05e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 60 deletions.
22 changes: 11 additions & 11 deletions src/main/java/fi/dy/masa/litematica/data/EntitiesDataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import fi.dy.masa.litematica.Reference;
import fi.dy.masa.litematica.config.Configs;
import fi.dy.masa.litematica.mixin.IMixinDataQueryHandler;
import fi.dy.masa.litematica.network.ServuxEntitiesHandler;
import fi.dy.masa.litematica.network.ServuxEntitiesPacket;
import fi.dy.masa.litematica.network.ServuxLitematicaHandler;
import fi.dy.masa.litematica.network.ServuxLitematicaPacket;
import fi.dy.masa.litematica.util.EntityUtils;

public class EntitiesDataStorage implements IClientTickHandler
Expand All @@ -36,7 +36,7 @@ public static EntitiesDataStorage getInstance()
return INSTANCE;
}

private final static ServuxEntitiesHandler<ServuxEntitiesPacket.Payload> HANDLER = ServuxEntitiesHandler.getInstance();
private final static ServuxLitematicaHandler<ServuxLitematicaPacket.Payload> HANDLER = ServuxLitematicaHandler.getInstance();
private final static MinecraftClient mc = MinecraftClient.getInstance();
private int uptimeTicks = 0;
private boolean servuxServer = false;
Expand Down Expand Up @@ -106,7 +106,7 @@ public void onClientTick(MinecraftClient mc)

public Identifier getNetworkChannel()
{
return ServuxEntitiesHandler.CHANNEL_ID;
return ServuxLitematicaHandler.CHANNEL_ID;
}

private static ClientPlayNetworkHandler getVanillaHandler()
Expand All @@ -119,7 +119,7 @@ private static ClientPlayNetworkHandler getVanillaHandler()
return null;
}

public IPluginClientPlayHandler<ServuxEntitiesPacket.Payload> getNetworkHandler()
public IPluginClientPlayHandler<ServuxLitematicaPacket.Payload> getNetworkHandler()
{
return HANDLER;
}
Expand Down Expand Up @@ -168,14 +168,14 @@ public void setServuxVersion(String ver)
public void onGameInit()
{
ClientPlayHandler.getInstance().registerClientPlayHandler(HANDLER);
HANDLER.registerPlayPayload(ServuxEntitiesPacket.Payload.ID, ServuxEntitiesPacket.Payload.CODEC, IPluginClientPlayHandler.BOTH_CLIENT);
HANDLER.registerPlayPayload(ServuxLitematicaPacket.Payload.ID, ServuxLitematicaPacket.Payload.CODEC, IPluginClientPlayHandler.BOTH_CLIENT);
}

public void onWorldPre()
{
if (DataManager.getInstance().hasIntegratedServer() == false)
{
HANDLER.registerPlayReceiver(ServuxEntitiesPacket.Payload.ID, HANDLER::receivePlayPayload);
HANDLER.registerPlayReceiver(ServuxLitematicaPacket.Payload.ID, HANDLER::receivePlayPayload);
}
}

Expand All @@ -191,7 +191,7 @@ public void requestMetadata()
NbtCompound nbt = new NbtCompound();
nbt.putString("version", Reference.MOD_STRING);

HANDLER.encodeClientData(ServuxEntitiesPacket.MetadataRequest(nbt));
HANDLER.encodeClientData(ServuxLitematicaPacket.MetadataRequest(nbt));
}
}

Expand All @@ -201,7 +201,7 @@ public boolean receiveServuxMetadata(NbtCompound data)
{
Litematica.debugLog("EntitiesDataStorage#receiveServuxMetadata(): received METADATA from Servux");

if (data.getInt("version") != ServuxEntitiesPacket.PROTOCOL_VERSION)
if (data.getInt("version") != ServuxLitematicaPacket.PROTOCOL_VERSION)
{
Litematica.logger.warn("entityDataChannel: Mis-matched protocol version!");
}
Expand Down Expand Up @@ -263,12 +263,12 @@ private void requestQueryEntityData(int entityId)

private void requestServuxBlockEntityData(BlockPos pos)
{
HANDLER.encodeClientData(ServuxEntitiesPacket.BlockEntityRequest(pos));
HANDLER.encodeClientData(ServuxLitematicaPacket.BlockEntityRequest(pos));
}

private void requestServuxEntityData(int entityId)
{
HANDLER.encodeClientData(ServuxEntitiesPacket.EntityRequest(entityId));
HANDLER.encodeClientData(ServuxLitematicaPacket.EntityRequest(entityId));
}

// BlockEntity.createNbtWithIdentifyingData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@
import fi.dy.masa.litematica.data.EntitiesDataStorage;

@Environment(EnvType.CLIENT)
public abstract class ServuxEntitiesHandler<T extends CustomPayload> implements IPluginClientPlayHandler<T>
public abstract class ServuxLitematicaHandler<T extends CustomPayload> implements IPluginClientPlayHandler<T>
{
private final static ServuxEntitiesHandler<ServuxEntitiesPacket.Payload> INSTANCE = new ServuxEntitiesHandler<>()
private final static ServuxLitematicaHandler<ServuxLitematicaPacket.Payload> INSTANCE = new ServuxLitematicaHandler<>()
{
@Override
public void receive(ServuxEntitiesPacket.Payload payload, ClientPlayNetworking.Context context)
public void receive(ServuxLitematicaPacket.Payload payload, ClientPlayNetworking.Context context)
{
ServuxEntitiesHandler.INSTANCE.receivePlayPayload(payload, context);
ServuxLitematicaHandler.INSTANCE.receivePlayPayload(payload, context);
}
};
public static ServuxEntitiesHandler<ServuxEntitiesPacket.Payload> getInstance() { return INSTANCE; }
public static ServuxLitematicaHandler<ServuxLitematicaPacket.Payload> getInstance() { return INSTANCE; }

public static final Identifier CHANNEL_ID = Identifier.of("servux", "entity_data");
public static final Identifier CHANNEL_ID = Identifier.of("servux", "litematics");

private boolean servuxRegistered;
private boolean payloadRegistered = false;
Expand Down Expand Up @@ -65,7 +65,7 @@ public void setPlayRegistered(Identifier channel)
@Override
public <P extends IClientPayloadData> void decodeClientData(Identifier channel, P data)
{
ServuxEntitiesPacket packet = (ServuxEntitiesPacket) data;
ServuxLitematicaPacket packet = (ServuxLitematicaPacket) data;

if (!channel.equals(CHANNEL_ID))
{
Expand All @@ -89,7 +89,7 @@ public <P extends IClientPayloadData> void decodeClientData(Identifier channel,
this.readingSessionKey = Random.create(Util.getMeasuringTimeMs()).nextLong();
}

Litematica.debugLog("ServuxEntitiesHandler#decodeClientData(): received Entity Data Packet Slice of size {} (in bytes) // reading session key [{}]", packet.getTotalSize(), this.readingSessionKey);
Litematica.debugLog("ServuxLitematicaHandler#decodeClientData(): received Entity Data Packet Slice of size {} (in bytes) // reading session key [{}]", packet.getTotalSize(), this.readingSessionKey);
PacketByteBuf fullPacket = PacketSplitter.receive(this, this.readingSessionKey, packet.getBuffer());

if (fullPacket != null)
Expand All @@ -102,11 +102,11 @@ public <P extends IClientPayloadData> void decodeClientData(Identifier channel,
}
catch (Exception e)
{
Litematica.logger.error("ServuxEntitiesHandler#decodeClientData(): Entity Data: error reading fullBuffer [{}]", e.getLocalizedMessage());
Litematica.logger.error("ServuxLitematicaHandler#decodeClientData(): Entity Data: error reading fullBuffer [{}]", e.getLocalizedMessage());
}
}
}
default -> Litematica.logger.warn("ServuxEntitiesHandler#decodeClientData(): received unhandled packetType {} of size {} bytes.", packet.getPacketType(), packet.getTotalSize());
default -> Litematica.logger.warn("ServuxLitematicaHandler#decodeClientData(): received unhandled packetType {} of size {} bytes.", packet.getPacketType(), packet.getTotalSize());
}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ public void receivePlayPayload(T payload, ClientPlayNetworking.Context ctx)
{
if (payload.getId().id().equals(CHANNEL_ID))
{
((ClientPlayHandler<?>) ClientPlayHandler.getInstance()).decodeClientData(CHANNEL_ID, ((ServuxEntitiesPacket.Payload) payload).data());
((ClientPlayHandler<?>) ClientPlayHandler.getInstance()).decodeClientData(CHANNEL_ID, ((ServuxLitematicaPacket.Payload) payload).data());
// This allows the data to be "shared" among multiple mods
}
}
Expand All @@ -143,28 +143,28 @@ public void receivePlayPayload(T payload, ClientPlayNetworking.Context ctx)
public void encodeWithSplitter(PacketByteBuf buffer, ClientPlayNetworkHandler handler)
{
// Send each PacketSplitter buffer slice
ServuxEntitiesHandler.INSTANCE.sendPlayPayload(new ServuxEntitiesPacket.Payload(ServuxEntitiesPacket.ResponseC2SData(buffer)));
ServuxLitematicaHandler.INSTANCE.sendPlayPayload(new ServuxLitematicaPacket.Payload(ServuxLitematicaPacket.ResponseC2SData(buffer)));
}

@Override
public <P extends IClientPayloadData> void encodeClientData(P data)
{
ServuxEntitiesPacket packet = (ServuxEntitiesPacket) data;
ServuxLitematicaPacket packet = (ServuxLitematicaPacket) data;

if (packet.getType().equals(ServuxEntitiesPacket.Type.PACKET_C2S_NBT_RESPONSE_START))
if (packet.getType().equals(ServuxLitematicaPacket.Type.PACKET_C2S_NBT_RESPONSE_START))
{
PacketByteBuf buffer = new PacketByteBuf(Unpooled.buffer());
buffer.writeVarInt(packet.getTransactionId());
buffer.writeNbt(packet.getCompound());
PacketSplitter.send(this, buffer, MinecraftClient.getInstance().getNetworkHandler());
}
else if (!ServuxEntitiesHandler.INSTANCE.sendPlayPayload(new ServuxEntitiesPacket.Payload(packet)))
else if (!ServuxLitematicaHandler.INSTANCE.sendPlayPayload(new ServuxLitematicaPacket.Payload(packet)))
{
if (this.failures > MAX_FAILURES)
{
Litematica.logger.warn("encodeClientData(): encountered [{}] sendPayload failures, cancelling any Servux join attempt(s)", MAX_FAILURES);
this.servuxRegistered = false;
ServuxEntitiesHandler.INSTANCE.unregisterPlayReceiver();
ServuxLitematicaHandler.INSTANCE.unregisterPlayReceiver();
EntitiesDataStorage.getInstance().onPacketFailure();
}
else
Expand Down
Loading

0 comments on commit e2ba05e

Please sign in to comment.