Skip to content

Commit

Permalink
clean up infoServux info line & a few other misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sakura-ryoko committed Jan 17, 2025
1 parent 31a8c62 commit 5e307ce
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 29 deletions.
11 changes: 11 additions & 0 deletions src/main/java/fi/dy/masa/minihud/data/EntitiesDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;

import fi.dy.masa.malilib.config.options.ConfigBoolean;
import fi.dy.masa.malilib.interfaces.IClientTickHandler;
import fi.dy.masa.malilib.interfaces.IDataSyncer;
import fi.dy.masa.malilib.mixin.entity.IMixinAbstractHorseEntity;
Expand Down Expand Up @@ -378,6 +379,16 @@ public void onWorldJoin()
// NO-OP
}

public void onEntityDataSyncToggled(ConfigBoolean config)
{
if (this.hasInValidServux)
{
this.reset(true);
}

// Do something?
}

public void requestMetadata()
{
if (DataStorage.getInstance().hasIntegratedServer() == false &&
Expand Down
134 changes: 131 additions & 3 deletions src/main/java/fi/dy/masa/minihud/data/HudDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import fi.dy.masa.malilib.config.options.ConfigBoolean;
import fi.dy.masa.malilib.gui.GuiBase;
import fi.dy.masa.malilib.network.ClientPlayHandler;
import fi.dy.masa.malilib.network.IPluginClientPlayHandler;
Expand All @@ -37,7 +38,6 @@
import fi.dy.masa.minihud.mixin.IMixinServerRecipeManager;
import fi.dy.masa.minihud.network.ServuxHudHandler;
import fi.dy.masa.minihud.network.ServuxHudPacket;
import fi.dy.masa.minihud.network.ServuxStructuresPacket;
import fi.dy.masa.minihud.renderer.OverlayRendererSpawnChunks;
import fi.dy.masa.minihud.util.DataStorage;

Expand All @@ -48,6 +48,7 @@ public class HudDataManager
private final static ServuxHudHandler<ServuxHudPacket.Payload> HANDLER = ServuxHudHandler.getInstance();
private final MinecraftClient mc = MinecraftClient.getInstance();

private boolean shouldRegister;
private boolean servuxServer;
private boolean hasInValidServux;
private String servuxVersion;
Expand Down Expand Up @@ -144,6 +145,18 @@ public void onWorldPre()
public void onWorldJoin()
{
MiniHUD.printDebug("HudDataStorage#onWorldJoin()");

if (DataStorage.getInstance().hasIntegratedServer() == false)
{
if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
this.requestMetadata();
}
else
{
this.unregisterChannel();
}
}
}

public void onPacketFailure()
Expand Down Expand Up @@ -454,13 +467,61 @@ public void onServerWeatherTick(int clearTime, int rainTime, int thunderTime, bo
//System.out.printf("onServerWeatherTick - c: %d, r: %d, t: %d, iR: %s, iT: %s\n", clearTime, rainTime, thunderTime, isRaining, isThunder);
}

public void onHudDataSyncToggled(ConfigBoolean config)
{
if (this.hasInValidServux)
{
this.reset(true);
}

if (this.hasServuxServer() && !config.getBooleanValue())
{
this.unregisterChannel();
}
else
{
this.shouldRegister = true;
this.registerChannel();
}
}

public void registerChannel()
{
this.shouldRegister = true;

if (!this.hasServuxServer() && !DataStorage.getInstance().hasIntegratedServer() && !this.hasInValidServux)
{
this.onWorldPre();
this.requestMetadata();
}
else
{
this.shouldRegister = false;
}
}

public void requestMetadata()
{
if (!DataStorage.getInstance().hasIntegratedServer())
{
if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue() &&
HANDLER.isPlayRegistered(HANDLER.getPayloadChannel()))
{
NbtCompound nbt = new NbtCompound();
nbt.putString("version", Reference.MOD_STRING);

HANDLER.encodeClientData(ServuxHudPacket.MetadataRequest(nbt));
}
}
}

public boolean receiveMetadata(NbtCompound data)
{
if (!this.servuxServer && !DataStorage.getInstance().hasIntegratedServer())
{
MiniHUD.printDebug("HudDataStorage#receiveMetadata(): received METADATA from Servux");

if (data.getInt("version") != ServuxStructuresPacket.PROTOCOL_VERSION)
if (data.getInt("version") != ServuxHudPacket.PROTOCOL_VERSION)
{
MiniHUD.logger.warn("hudDataChannel: Mis-matched protocol version!");
}
Expand All @@ -477,12 +538,39 @@ public boolean receiveMetadata(NbtCompound data)
this.setIsServuxServer();
this.requestRecipeManager();

return true;
if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
//this.registerChannel();
this.requestRecipeManager();
return true;
}
else
{
this.unregisterChannel();
}
}

return false;
}

public void unregisterChannel()
{
if (this.hasServuxServer() || !Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
this.servuxServer = false;

if (!this.hasInValidServux)
{
MiniHUD.printDebug("HudDataManager#unregisterChannel(): for {}", this.servuxVersion != null ? this.servuxVersion : "<unknown>");

HANDLER.unregisterPlayReceiver();
HANDLER.reset(HANDLER.getPayloadChannel());
}
}

this.shouldRegister = false;
}

public void requestSpawnMetadata()
{
if (!DataStorage.getInstance().hasIntegratedServer() && this.hasServuxServer())
Expand All @@ -503,10 +591,24 @@ public void receiveSpawnMetadata(NbtCompound data)
this.setServuxVersion(data.getString("servux"));
this.setWorldSpawn(new BlockPos(data.getInt("spawnPosX"), data.getInt("spawnPosY"), data.getInt("spawnPosZ")));
this.setSpawnChunkRadius(data.getInt("spawnChunkRadius"), true);

if (data.contains("worldSeed", Constants.NBT.TAG_LONG))
{
this.setWorldSeed(data.getLong("worldSeed"));
}

if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
if (!this.hasServuxServer())
{
this.registerChannel();
}
}
else
{
this.unregisterChannel();
this.shouldRegister = false;
}
}
}

Expand Down Expand Up @@ -543,6 +645,19 @@ public void receiveWeatherData(NbtCompound data)
this.isThundering = this.thunderWeatherTimer > 0 && !this.isThundering;
this.isRaining = this.rainWeatherTimer > 0 && !this.isRaining;
}

if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
if (!this.hasServuxServer())
{
this.registerChannel();
}
}
else
{
this.unregisterChannel();
this.shouldRegister = false;
}
}
}

Expand Down Expand Up @@ -603,6 +718,19 @@ public void receiveRecipeManager(NbtCompound data)
{
MiniHUD.logger.warn("receiveRecipeManager: failed to read Recipe Manager from Servux (Collection was empty!)");
}

if (Configs.Generic.HUD_DATA_SYNC.getBooleanValue())
{
if (!this.hasServuxServer())
{
this.registerChannel();
}
}
else
{
this.unregisterChannel();
this.shouldRegister = false;
}
}
}

Expand Down
33 changes: 22 additions & 11 deletions src/main/java/fi/dy/masa/minihud/event/RenderHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,26 @@ else if (type == InfoToggle.SERVUX)
{
if (EntitiesDataManager.getInstance().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux", EntitiesDataManager.getInstance().getServuxVersion());
this.addLineI18n("minihud.info_line.servux",
EntitiesDataManager.getInstance().getServuxVersion());
}
else if (this.getDataStorage().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux", this.getDataStorage().getServuxVersion());
this.addLineI18n("minihud.info_line.servux",
this.getDataStorage().getServuxVersion());
}
else if (this.getDataStorage().hasIntegratedServer() == false)
else if (this.getHudData().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux",
this.getHudData().getServuxVersion());
}
else if (this.getDataStorage().hasIntegratedServer() == false &&
!EntitiesDataManager.getInstance().hasServuxServer() &&
!this.getHudData().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux.not_connected");
}

if (EntitiesDataManager.getInstance().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux.entity_sync",
Expand All @@ -540,6 +550,14 @@ else if (this.getDataStorage().hasIntegratedServer() == false)
this.getHudData().isWorldSpawnKnown() ? StringUtils.translate("minihud.info_line.slime_chunk.yes") : StringUtils.translate("minihud.info_line.slime_chunk.no")
);
}
else if (this.getHudData().hasServuxServer())
{
this.addLineI18n("minihud.info_line.servux.no_structures_hud",
this.getHudData().getSpawnChunkRadius(),
this.getHudData().getWorldSpawn().toShortString(),
this.getHudData().isWorldSpawnKnown() ? StringUtils.translate("minihud.info_line.slime_chunk.yes") : StringUtils.translate("minihud.info_line.slime_chunk.no")
);
}
else if (this.getDataStorage().hasIntegratedServer())
{
this.addLineI18n("minihud.info_line.servux.structures_integrated",
Expand Down Expand Up @@ -1741,7 +1759,7 @@ private boolean isEntityDataValid(@Nonnull NbtCompound nbt)
return true;
}

System.out.printf("isEntityDataValid(): nbt: [%s]\n", nbt.toString());
//System.out.printf("isEntityDataValid(): nbt: [%s]\n", nbt.toString());

for (String key : nbt.getKeys())
{
Expand Down Expand Up @@ -1836,26 +1854,19 @@ public Pair<Entity, NbtCompound> getTargetEntity(World world, MinecraftClient mc
if (pair == null && this.lastEntity != null &&
this.lastEntity.getLeft().getId() == lookedEntity.getId())
{
System.out.print("getTargetEntity() --> LAST 1\n");
pair = this.lastEntity;
}
else if (pair != null && pair.getRight() != null &&
!pair.getRight().isEmpty() &&
this.isEntityDataValid(pair.getRight()))
{
System.out.print("getTargetEntity() --> SAVE 1\n");
this.lastEntity = pair;
}
else if (this.lastEntity != null &&
this.lastEntity.getLeft().getId() == lookedEntity.getId())
{
System.out.print("getTargetEntity() --> LAST 2\n");
pair = this.lastEntity;
}
else
{
System.out.print("getTargetEntity() --> ELSE 1\n");
}

return pair;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fi/dy/masa/minihud/hotkeys/KeyCallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import fi.dy.masa.minihud.config.Configs;
import fi.dy.masa.minihud.config.RendererCallbacks;
import fi.dy.masa.minihud.config.RendererToggle;
import fi.dy.masa.minihud.data.EntitiesDataManager;
import fi.dy.masa.minihud.data.HudDataManager;
import fi.dy.masa.minihud.gui.GuiConfigs;
import fi.dy.masa.minihud.gui.GuiConfigs.ConfigGuiTab;
import fi.dy.masa.minihud.gui.GuiShapeEditor;
Expand All @@ -30,6 +32,9 @@ public static void init()
Configs.Generic.SHAPE_EDITOR.getKeybind().setCallback(callback);
Configs.Generic.INVENTORY_PREVIEW_TOGGLE_SCREEN.getKeybind().setCallback(callback);

Configs.Generic.ENTITY_DATA_SYNC.setValueChangeCallback((config) -> EntitiesDataManager.getInstance().onEntityDataSyncToggled(config));
Configs.Generic.HUD_DATA_SYNC.setValueChangeCallback((config) -> HudDataManager.getInstance().onHudDataSyncToggled(config));

Configs.Colors.BEACON_RANGE_LVL1_OVERLAY_COLOR.setValueChangeCallback((config) -> updateBeaconOverlay());
Configs.Colors.BEACON_RANGE_LVL2_OVERLAY_COLOR.setValueChangeCallback((config) -> updateBeaconOverlay());
Configs.Colors.BEACON_RANGE_LVL3_OVERLAY_COLOR.setValueChangeCallback((config) -> updateBeaconOverlay());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ public void toPacket(PacketByteBuf output)
// Write Packet Buffer (Slice)
try
{
output.writeBytes(this.buffer.readBytes(this.buffer.readableBytes()));
PacketByteBuf serverReplay = new PacketByteBuf(this.buffer.copy());
output.writeBytes(serverReplay.readBytes(serverReplay.readableBytes()));
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ public void toPacket(PacketByteBuf output)
// Write Packet Buffer (Slice)
try
{
output.writeBytes(this.buffer.readBytes(this.buffer.readableBytes()));
PacketByteBuf serverReplay = new PacketByteBuf(this.buffer.copy());
output.writeBytes(serverReplay.readBytes(serverReplay.readableBytes()));
}
catch (Exception e)
{
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/fi/dy/masa/minihud/network/ServuxHudHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,16 @@ public <P extends IClientPayloadData> void decodeClientData(Identifier channel,
this.servuxRegistered = true;
}
}
case PACKET_S2C_SPAWN_DATA ->
{
HudDataManager.getInstance().receiveSpawnMetadata(packet.getCompound());
}
case PACKET_S2C_WEATHER_TICK ->
{
HudDataManager.getInstance().receiveWeatherData(packet.getCompound());
}
case PACKET_S2C_SPAWN_DATA -> HudDataManager.getInstance().receiveSpawnMetadata(packet.getCompound());
case PACKET_S2C_WEATHER_TICK -> HudDataManager.getInstance().receiveWeatherData(packet.getCompound());
case PACKET_S2C_NBT_RESPONSE_DATA ->
{
if (this.readingSessionKey == -1)
{
this.readingSessionKey = Random.create(Util.getMeasuringTimeMs()).nextLong();
}

MiniHUD.printDebug("ServuxHudHandler#decodeClientData(): received Entity Data Packet Slice of size {} (in bytes) // reading session key [{}]", packet.getTotalSize(), this.readingSessionKey);
MiniHUD.printDebug("ServuxHudHandler#decodeClientData(): received Hud 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 @@ -106,7 +100,7 @@ public <P extends IClientPayloadData> void decodeClientData(Identifier channel,
}
catch (Exception e)
{
MiniHUD.logger.error("ServuxHudHandler#decodeClientData(): Entity Data: error reading fullBuffer [{}]", e.getLocalizedMessage());
MiniHUD.logger.error("ServuxHudHandler#decodeClientData(): Hud Data: error reading fullBuffer [{}]", e.getLocalizedMessage());
}
}
}
Expand Down
Loading

0 comments on commit 5e307ce

Please sign in to comment.