Skip to content

Commit

Permalink
some code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jun 12, 2024
1 parent 1f98643 commit 75aae75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@ public boolean isEmpty()
@Override
public void toPacket(PacketByteBuf output)
{
output.writeVarInt(this.getPacketType());

if (this.packetType.equals(Type.PACKET_S2C_STRUCTURE_DATA))
{
// Write Packet Buffer
try
{
output.writeVarInt(this.getPacketType());
output.writeBytes(this.buffer.readBytes(this.buffer.readableBytes()));
}
catch (Exception e)
Expand All @@ -115,7 +116,6 @@ public void toPacket(PacketByteBuf output)
// Write NBT
try
{
output.writeVarInt(this.getPacketType());
output.writeNbt(this.nbt);
}
catch (Exception e)
Expand Down
46 changes: 13 additions & 33 deletions src/main/java/fi/dy/masa/minihud/util/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public class DataStorage

private static final DataStorage INSTANCE = new DataStorage();
private final MobCapDataHandler mobCapData = new MobCapDataHandler();
//private final static ServuxStructuresHandlerOld<ServuxStructuresPayloadOld> OLD_HANDLER = ServuxStructuresHandlerOld.getInstance();
private final static ServuxStructuresHandler<ServuxStructuresPacket.Payload> HANDLER = ServuxStructuresHandler.getInstance();
private boolean worldSeedValid = false;
private boolean carpetServer = false;
Expand All @@ -74,7 +73,8 @@ public class DataStorage
private int structureDataTimeout = 30 * 20;
private boolean serverTPSValid;
private boolean hasSyncedTime;
private String serverVersion;
private String servuxVersion;
private int servuxTimeout;
private boolean hasStructureDataFromServer;
private boolean structureRendererNeedsUpdate;
private boolean structuresNeedUpdating;
Expand All @@ -95,7 +95,6 @@ public class DataStorage
private final PriorityBlockingQueue<ChunkTask> taskQueue = Queues.newPriorityBlockingQueue();
private final Thread workerThread;
private final ThreadWorker worker;
private int timeout;

private DataStorage()
{
Expand All @@ -111,19 +110,12 @@ public static DataStorage getInstance()

public void onGameInit()
{
//ClientPlayHandler.getInstance().registerClientPlayHandler(OLD_HANDLER);
//OLD_HANDLER.registerPlayPayload(ServuxStructuresPayloadOld.TYPE, ServuxStructuresPayloadOld.CODEC, IPluginClientPlayHandler.BOTH_CLIENT);

ClientPlayHandler.getInstance().registerClientPlayHandler(HANDLER);
HANDLER.registerPlayPayload(ServuxStructuresPacket.Payload.ID, ServuxStructuresPacket.Payload.CODEC, IPluginClientPlayHandler.BOTH_CLIENT);
}

//public Identifier getNetworkChannelOld() { return ServuxStructuresHandlerOld.CHANNEL_ID; }

public Identifier getNetworkChannel() { return ServuxStructuresHandler.CHANNEL_ID; }

//public IPluginClientPlayHandler<ServuxStructuresPayloadOld> getPacketHandlerOld() { return OLD_HANDLER; }

public IPluginClientPlayHandler<ServuxStructuresPacket.Payload> getPacketHandler() { return HANDLER; }

public MobCapDataHandler getMobCapData()
Expand All @@ -149,9 +141,6 @@ public void reset(boolean isLogout)
MiniHUD.logger.warn("Interrupted whilst waiting for worker thread to die", e);
}
*/
//OLD_HANDLER.reset(this.getNetworkChannel());
//OLD_HANDLER.resetFailures(this.getNetworkChannel());

HANDLER.reset(this.getNetworkChannel());
HANDLER.resetFailures(this.getNetworkChannel());

Expand Down Expand Up @@ -181,7 +170,7 @@ public void reset(boolean isLogout)
this.lastStructureUpdatePos = null;
this.structures.clear();
this.clearTasks();
this.timeout = -1;
this.servuxTimeout = -1;

ShapeManager.INSTANCE.clear();
OverlayRendererBeaconRange.INSTANCE.clear();
Expand Down Expand Up @@ -223,15 +212,15 @@ public void setIsServuxServer()
}
}

public void setServerVersion(String ver)
public void setServuxVersion(String ver)
{
if (ver != null && ver.isEmpty() == false)
{
this.serverVersion = ver;
this.servuxVersion = ver;
}
else
{
this.serverVersion = "unknown";
this.servuxVersion = "unknown";
}
}

Expand All @@ -246,7 +235,6 @@ public void onWorldPre()
{
if (this.hasIntegratedServer == false)
{
//OLD_HANDLER.registerPlayReceiver(ServuxStructuresPayloadOld.TYPE, OLD_HANDLER::receivePlayPayload);
HANDLER.registerPlayReceiver(ServuxStructuresPacket.Payload.ID, HANDLER::receivePlayPayload);
}
}
Expand Down Expand Up @@ -308,10 +296,8 @@ public void requestSpawnMetadata()
if (this.hasIntegratedServer == false && this.hasServuxServer())
{
NbtCompound nbt = new NbtCompound();
//nbt.putInt("packetType", ServuxStructuresHandlerOld.PACKET_C2S_REQUEST_SPAWN_METADATA);
nbt.putString("version", Reference.MOD_STRING);

//OLD_HANDLER.encodeNbtCompound(nbt);
HANDLER.encodeStructuresPacket(new ServuxStructuresPacket(ServuxStructuresPacket.Type.PACKET_C2S_REQUEST_SPAWN_METADATA, nbt));
}
}
Expand Down Expand Up @@ -873,18 +859,17 @@ public void registerStructureChannel()
MiniHUD.printDebug("DataStorage#registerStructureChannel(): sending STRUCTURES_REGISTER to Servux");

NbtCompound nbt = new NbtCompound();
//nbt.putInt("packetType", ServuxStructuresHandlerOld.PACKET_C2S_STRUCTURES_REGISTER);
nbt.putString("version", Reference.MOD_STRING);

//OLD_HANDLER.encodeNbtCompound(nbt);
HANDLER.encodeStructuresPacket(new ServuxStructuresPacket(ServuxStructuresPacket.Type.PACKET_C2S_STRUCTURES_REGISTER, nbt));
}
}
else
{
this.shouldRegisterStructureChannel = false;
}
// QuickCarpet doesn't exist for 1.20.5
// QuickCarpet doesn't exist for 1.20.5+,
// Will re-add if they update it
}

public boolean receiveServuxMetadata(NbtCompound data)
Expand All @@ -898,8 +883,8 @@ public boolean receiveServuxMetadata(NbtCompound data)
{
MiniHUD.logger.warn("structureChannel: Mis-matched protocol version!");
}
this.timeout = data.getInt("timeout");
this.setServerVersion(data.getString("servux"));
this.servuxTimeout = data.getInt("timeout");
this.setServuxVersion(data.getString("servux"));
this.setWorldSpawn(new BlockPos(data.getInt("spawnPosX"), data.getInt("spawnPosY"), data.getInt("spawnPosZ")));
this.setSpawnChunkRadius(data.getInt("spawnChunkRadius"), true);
this.setIsServuxServer();
Expand All @@ -924,7 +909,7 @@ public void receiveSpawnMetadata(NbtCompound data)
{
MiniHUD.printDebug("DataStorage#receiveSpawnMetadata(): from Servux");

this.setServerVersion(data.getString("servux"));
this.setServuxVersion(data.getString("servux"));
this.setWorldSpawn(new BlockPos(data.getInt("spawnPosX"), data.getInt("spawnPosY"), data.getInt("spawnPosZ")));
this.setSpawnChunkRadius(data.getInt("spawnChunkRadius"), true);

Expand All @@ -942,12 +927,7 @@ public void unregisterStructureChannel()
this.servuxServer = false;
if (this.hasInValidServux == false)
{
MiniHUD.printDebug("DataStorage#unregisterStructureChannel(): for {}", this.serverVersion != null ? this.serverVersion : "<unknown>");

//NbtCompound nbt = new NbtCompound();
//nbt.putInt("packetType", ServuxStructuresHandlerOld.PACKET_C2S_STRUCTURES_UNREGISTER);
//OLD_HANDLER.encodeNbtCompound(nbt);
//OLD_HANDLER.reset(this.getNetworkChannel());
MiniHUD.printDebug("DataStorage#unregisterStructureChannel(): for {}", this.servuxVersion != null ? this.servuxVersion : "<unknown>");

HANDLER.encodeStructuresPacket(new ServuxStructuresPacket(ServuxStructuresPacket.Type.PACKET_C2S_STRUCTURES_UNREGISTER, new NbtCompound()));
HANDLER.reset(this.getNetworkChannel());
Expand Down Expand Up @@ -1013,7 +993,7 @@ public void addOrUpdateStructuresFromServer(NbtList structures, boolean isServux

if (structures.getHeldType() == Constants.NBT.TAG_COMPOUND)
{
this.structureDataTimeout = this.timeout + 300;
this.structureDataTimeout = this.servuxTimeout + 300;

long currentTime = this.mc.world.getTime();
final int count = structures.size();
Expand Down

0 comments on commit 75aae75

Please sign in to comment.