Skip to content

Commit

Permalink
sync from sakura-ryoko/minihud
Browse files Browse the repository at this point in the history
  • Loading branch information
TexBlock committed Jul 9, 2024
1 parent 52b6d8d commit dcdb6d9
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 104 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
## Change
- port to 1.20.6
- sync from `sakura-ryoko/minihud`
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
neoForge "net.neoforged:neoforge:${project.neoforge_version}"

modImplementation "maven.modrinth:mafglib:${project.malilib_version}"
modImplementation "maven.modrinth:forged-networking-api:0.1.0+mc1.20.6"
modImplementation("maven.modrinth:neonetwork:${project.neonetwork_version}")

implementation "com.google.code.findbugs:jsr305:3.0.2"
}
Expand Down
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ loom.platform=neoforge

# Base properties
minecraft_version=1.20.6
neoforge_version=20.6.113-beta
neoforge_version=20.6.119
yarn_mappings=1.20.6+build.3
yarn_patch=1.20.5+build.3
yarn_patch=1.20.6+build.4

# Mod Properties
mod_version=0.1.2
mod_version=0.1.3
maven_group=org.thinkingstudio.bocchud
archives_base_name=BoccHUD
mod_id=bocchud
Expand All @@ -19,4 +19,5 @@ loom.platform=neoforge
curseforge_id=916504

# Dependencies
malilib_version=0.1.8-mc1.20.6
malilib_version=0.1.12-mc1.20.6
neonetwork_version=0.1.5+mc1.20.6
5 changes: 3 additions & 2 deletions src/main/java/fi/dy/masa/minihud/Reference.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package fi.dy.masa.minihud;

import fi.dy.masa.malilib.util.StringUtils;
import net.minecraft.MinecraftVersion;
import fi.dy.masa.malilib.util.StringUtils;

public class Reference
{
public static final String MOD_ID = "bocchud";
public static final String MOD_NAME = "BoccHUD";
public static final String MOD_VERSION = StringUtils.getModVersionString(MOD_ID);
public static final String MC_VERSION = MinecraftVersion.CURRENT.getName();
public static final String MOD_STRING = MOD_ID+"-"+MC_VERSION+"-"+MOD_VERSION;
public static final String MOD_TYPE = "neoforge";
public static final String MOD_STRING = MOD_ID + "-" + MOD_TYPE + "-" + MC_VERSION + "-" + MOD_VERSION;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package fi.dy.masa.minihud.network;

import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import org.thinkingstudio.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.util.Identifier;
import fi.dy.masa.malilib.network.ClientPlayHandler;
import net.minecraft.util.Util;
import net.minecraft.util.math.random.Random;
import fi.dy.masa.malilib.network.IPluginClientPlayHandler;
import fi.dy.masa.malilib.network.PacketSplitter;
import fi.dy.masa.malilib.util.Constants;
import fi.dy.masa.minihud.MiniHUD;
import fi.dy.masa.minihud.util.DataStorage;
Expand All @@ -16,29 +20,23 @@
@OnlyIn(Dist.CLIENT)
public abstract class ServuxStructuresHandler<T extends CustomPayload> implements IPluginClientPlayHandler<T>
{
private final static ServuxStructuresHandler<ServuxStructuresPayload> INSTANCE = new ServuxStructuresHandler<>()
private final static ServuxStructuresHandler<ServuxStructuresPacket.Payload> INSTANCE = new ServuxStructuresHandler<>()
{
@Override
public void receive(ServuxStructuresPayload payload, ClientPlayNetworking.Context context)
public void receive(ServuxStructuresPacket.Payload payload, ClientPlayNetworking.Context context)
{
ServuxStructuresHandler.INSTANCE.receivePlayPayload(payload, context);
}
};
public static ServuxStructuresHandler<ServuxStructuresPayload> getInstance() { return INSTANCE; }
public static ServuxStructuresHandler<ServuxStructuresPacket.Payload> getInstance() { return INSTANCE; }

public static final Identifier CHANNEL_ID = new Identifier("servux", "structures");
public static final int PROTOCOL_VERSION = 2;
public static final int PACKET_S2C_METADATA = 1;
public static final int PACKET_S2C_STRUCTURE_DATA = 2;
public static final int PACKET_C2S_STRUCTURES_REGISTER = 3;
public static final int PACKET_C2S_STRUCTURES_UNREGISTER = 4;
public static final int PACKET_S2C_SPAWN_METADATA = 10;
public static final int PACKET_C2S_REQUEST_SPAWN_METADATA = 11;
public static final Identifier CHANNEL_ID = Identifier.of("servux", "structures");

private boolean servuxRegistered;
private boolean payloadRegistered = false;
private int failures = 0;
private static final int MAX_FAILURES = 4;
private long readingSessionKey = -1;

@Override
public Identifier getPayloadChannel() { return CHANNEL_ID; }
Expand All @@ -63,27 +61,57 @@ public void setPlayRegistered(Identifier channel)
}
}

@Override
public void decodeNbtCompound(Identifier channel, NbtCompound data)
public void decodeStructuresPacket(Identifier channel, ServuxStructuresPacket packet)
{
switch (data.getInt("packetType"))
if (channel.equals(CHANNEL_ID) == false)
{
case PACKET_S2C_METADATA ->
return;
}
switch (packet.getType())
{
case PACKET_S2C_STRUCTURE_DATA ->
{
if (DataStorage.getInstance().receiveServuxMetadata(data))
if (this.readingSessionKey == -1)
{
this.servuxRegistered = true;
this.readingSessionKey = Random.create(Util.getMeasuringTimeMs()).nextLong();
}

PacketByteBuf fullPacket = PacketSplitter.receive(this, this.readingSessionKey, packet.getBuffer());

if (fullPacket != null)
{
try
{
NbtCompound nbt = fullPacket.readNbt();
this.readingSessionKey = -1;

if (nbt != null)
{
NbtList structures = nbt.getList("Structures", Constants.NBT.TAG_COMPOUND);
//MiniHUD.printDebug("decodeStructuresPacket(): received Structures Data of size {} (in bytes) // structures [{}]", nbt.getSizeInBytes(), structures.size());

DataStorage.getInstance().addOrUpdateStructuresFromServer(structures, this.servuxRegistered);
}
else
{
MiniHUD.logger.warn("decodeStructuresPacket(): Structures Data: error reading fullBuffer NBT is NULL");
}
}
catch (Exception e)
{
MiniHUD.logger.error("decodeStructuresPacket(): Structures Data: error reading fullBuffer [{}]", e.getLocalizedMessage());
}
}
}
case PACKET_S2C_SPAWN_METADATA -> DataStorage.getInstance().receiveSpawnMetadata(data);
case PACKET_S2C_STRUCTURE_DATA ->
case PACKET_S2C_METADATA ->
{
MiniHUD.printDebug("ServuxStructuresHandler#decodeNbtCompound(): received Structures Data payload of size {} (in bytes)", data.getSizeInBytes());

NbtList structures = data.getList("Structures", Constants.NBT.TAG_COMPOUND);
DataStorage.getInstance().addOrUpdateStructuresFromServer(structures, this.servuxRegistered);
if (DataStorage.getInstance().receiveServuxMetadata(packet.getCompound()))
{
this.servuxRegistered = true;
}
}
default -> MiniHUD.logger.warn("ServuxStructuresHandler#decodeNbtCompound(): received unhandled packetType {} of size {} bytes.", data.getInt("packetType"), data.getSizeInBytes());
case PACKET_S2C_SPAWN_METADATA -> DataStorage.getInstance().receiveSpawnMetadata(packet.getCompound());
default -> MiniHUD.logger.warn("decodeStructuresPacket(): received unhandled packetType {} of size {} bytes.", packet.getPacketType(), packet.getTotalSize());
}
}

Expand All @@ -92,18 +120,16 @@ public void reset(Identifier channel)
{
if (channel.equals(CHANNEL_ID) && this.servuxRegistered)
{
MiniHUD.printDebug("reset() called for {}", channel.toString());

this.servuxRegistered = false;
this.failures = 0;
this.readingSessionKey = -1;
}
}

public void resetFailures(Identifier channel)
{
if (channel.equals(CHANNEL_ID) && this.failures > 0)
{
MiniHUD.printDebug("resetFailures() called for {}", channel.toString());
this.failures = 0;
}
}
Expand All @@ -113,18 +139,23 @@ public void receivePlayPayload(T payload, ClientPlayNetworking.Context ctx)
{
if (payload.getId().id().equals(CHANNEL_ID))
{
((ClientPlayHandler<?>) ClientPlayHandler.getInstance()).decodeNbtCompound(CHANNEL_ID, ((ServuxStructuresPayload) payload).data());
ServuxStructuresHandler.INSTANCE.decodeStructuresPacket(CHANNEL_ID, ((ServuxStructuresPacket.Payload) payload).data());
}
}

@Override
public void encodeNbtCompound(NbtCompound data)
public void encodeWithSplitter(PacketByteBuf buffer, ClientPlayNetworkHandler handler)
{
// NO-OP
}

public void encodeStructuresPacket(ServuxStructuresPacket packet)
{
if (ServuxStructuresHandler.INSTANCE.sendPlayPayload(new ServuxStructuresPayload(data)) == false)
if (ServuxStructuresHandler.INSTANCE.sendPlayPayload(new ServuxStructuresPacket.Payload(packet)) == false)
{
if (this.failures > MAX_FAILURES)
{
MiniHUD.logger.warn("encodeNbtCompound: encountered [{}] sendPayload failures, cancelling any Servux join attempt(s)", MAX_FAILURES);
MiniHUD.logger.warn("encodeStructuresPacket(): encountered [{}] sendPayload failures, cancelling any Servux join attempt(s)", MAX_FAILURES);
this.servuxRegistered = false;
ServuxStructuresHandler.INSTANCE.unregisterPlayReceiver();
DataStorage.getInstance().onPacketFailure();
Expand Down
Loading

0 comments on commit dcdb6d9

Please sign in to comment.