Skip to content

Commit

Permalink
Some refactoring to the Carpet pubsub packet handler
Browse files Browse the repository at this point in the history
  • Loading branch information
maruohon committed Sep 21, 2022
1 parent d66d9ee commit a37402e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 68 deletions.
25 changes: 16 additions & 9 deletions src/main/java/fi/dy/masa/minihud/config/ConfigCallbacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import fi.dy.masa.minihud.data.DataStorage;
import fi.dy.masa.minihud.feature.Actions;
import fi.dy.masa.minihud.network.carpet.CarpetPubsubPacketHandler;
import fi.dy.masa.minihud.network.servux.ServuxInfoSubDataPacketHandler;
import fi.dy.masa.minihud.renderer.RenderContainer;
import fi.dy.masa.minihud.util.DebugInfoUtils;

Expand Down Expand Up @@ -57,16 +58,23 @@ public static void init()
Configs.Generic.WOOL_COUNTER_TYPES.setValueLoadCallback(DataStorage.getInstance().getWoolCounters()::updateEnabledCounters);
Configs.Generic.WOOL_COUNTER_TYPES.setValueChangeCallback((newValue, oldValue) -> {
DataStorage.getInstance().getWoolCounters().updateEnabledCounters(newValue);
CarpetPubsubPacketHandler.updatePubSubSubscriptions();
CarpetPubsubPacketHandler.INSTANCE.updatePubSubSubscriptions();
});

EventListener pubSubCallback = CarpetPubsubPacketHandler::updatePubSubSubscriptions;
Configs.Generic.WOOL_COUNTER_ENABLE_ALL.addValueChangeListener(pubSubCallback);

InfoLineToggle.CARPET_WOOL_COUNTERS.addValueChangeListener(pubSubCallback);
InfoLineToggle.CHUNK_UNLOAD_ORDER.addValueChangeListener(pubSubCallback);
InfoLineToggle.MOB_CAPS.addValueChangeListener(pubSubCallback);
InfoLineToggle.SERVER_TPS.addValueChangeListener(pubSubCallback);
EventListener carpetCallback = CarpetPubsubPacketHandler.INSTANCE::updatePubSubSubscriptions;
Configs.Generic.WOOL_COUNTER_ENABLE_ALL.addValueChangeListener(carpetCallback);
InfoLineToggle.CARPET_WOOL_COUNTERS.addValueChangeListener(carpetCallback);

// TODO move this to a proper method somewhere which only(?) updates the registration
// TODO for the currently detected server side counterpart(s)
EventListener syncCallback = () -> {
CarpetPubsubPacketHandler.INSTANCE.updatePubSubSubscriptions();
ServuxInfoSubDataPacketHandler.INSTANCE.updateSubscriptions();
};
InfoLineToggle.CHUNK_UNLOAD_ORDER.addValueChangeListener(syncCallback);
InfoLineToggle.MOB_CAPS.addValueChangeListener(syncCallback);
InfoLineToggle.SERVER_TPS.addValueChangeListener(syncCallback);
RendererToggle.CHUNK_UNLOAD_BUCKET.addValueChangeListener(syncCallback);

RendererToggle.DEBUG_COLLISION_BOXES.addValueChangeListener( () -> DebugInfoUtils.toggleDebugRenderer(RendererToggle.DEBUG_COLLISION_BOXES));
RendererToggle.DEBUG_HEIGHT_MAP.addValueChangeListener( () -> DebugInfoUtils.toggleDebugRenderer(RendererToggle.DEBUG_HEIGHT_MAP));
Expand All @@ -76,7 +84,6 @@ public static void init()
RendererToggle.DEBUG_WATER.addValueChangeListener( () -> DebugInfoUtils.toggleDebugRenderer(RendererToggle.DEBUG_WATER));

RendererToggle.BEACON_RANGE.addValueChangeListener(beaconUpdateCallback);
RendererToggle.CHUNK_UNLOAD_BUCKET.addValueChangeListener(pubSubCallback);
RendererToggle.LIGHT_LEVEL.addValueChangeListener(lightLevelUpdateCallback);
RendererToggle.STRUCTURE_BOUNDING_BOXES.addValueChangeListener(DataStorage.getInstance().getStructureStorage()::requestStructureDataUpdates);;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fi/dy/masa/minihud/data/DataStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public void reset()
if (this.mc.world != null)
{
this.structureStorage.requestStructureDataUpdates();
CarpetPubsubPacketHandler.updatePubSubSubscriptions();
CarpetPubsubPacketHandler.INSTANCE.updatePubSubSubscriptions();
ServuxInfoSubDataPacketHandler.INSTANCE.updateSubscriptions();
}
else
{
this.spawnerPositions.clear();
this.waterFallPositions.clear();
CarpetPubsubPacketHandler.unsubscribeAll();
CarpetPubsubPacketHandler.INSTANCE.unsubscribeAll();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,11 @@ public class CarpetPubsubPacketHandler extends BasePacketHandler
public static final String NODE_SERVER_TPS = "minecraft.performance.tps";
public static final String NODE_SERVER_MSPT = "minecraft.performance.mspt";

private static final HashSet<String> SUBSCRIPTIONS = new HashSet<>();
private static final HashSet<String> PER_DIMENSION_SUBSCRIPTIONS = new HashSet<>();
protected final HashSet<String> subscriptions = new HashSet<>();
protected final HashSet<String> perDimensionSubscriptions = new HashSet<>();
protected final Map<String, NodeType> nodeTypes;

private static final Map<String, NodeType> NODE_TYPES;

private static class NodeType
{
public final int typeId;
public final Consumer<PacketBuffer> handler;

public NodeType(int typeId, Consumer<PacketBuffer> handler)
{
this.typeId = typeId;
this.handler = handler;
}

private static NodeType create(int typeId, Consumer<PacketBuffer> handler)
{
return new NodeType(typeId, handler);
}
}

static
{
NODE_TYPES = registerTypes();
}

private static Map<String, NodeType> registerTypes()
protected static Map<String, NodeType> registerTypes()
{
ImmutableMap.Builder<String, NodeType> builder = ImmutableMap.builder();
DataStorage data = DataStorage.getInstance();
Expand Down Expand Up @@ -115,10 +92,11 @@ private static Map<String, NodeType> registerTypes()
return builder.build();
}

private CarpetPubsubPacketHandler()
protected CarpetPubsubPacketHandler()
{
this.registerToServer = true;
this.usePacketSplitter = true;
this.nodeTypes = registerTypes();
}

@Override
Expand Down Expand Up @@ -151,7 +129,7 @@ public void onPacketReceived(PacketBuffer buf)

protected boolean readNodeData(String node, int type, PacketBuffer buf)
{
NodeType expectedType = NODE_TYPES.getOrDefault(node, null);
NodeType expectedType = this.nodeTypes.getOrDefault(node, null);

if (expectedType != null && expectedType.typeId == type)
{
Expand All @@ -162,33 +140,33 @@ protected boolean readNodeData(String node, int type, PacketBuffer buf)
return false;
}

public static void subscribe(String... nodes)
public void subscribe(String... nodes)
{
subscribe(Arrays.asList(nodes));
this.subscribe(Arrays.asList(nodes));
}

public static void subscribe(Collection<String> nodes)
public void subscribe(Collection<String> nodes)
{
updateSubscriptions(PACKET_C2S_SUBSCRIBE, nodes);
this.updateSubscriptions(PACKET_C2S_SUBSCRIBE, nodes);
}

public static void unsubscribeAll()
public void unsubscribeAll()
{
unsubscribe(SUBSCRIPTIONS);
SUBSCRIPTIONS.clear();
this.unsubscribe(this.subscriptions);
this.subscriptions.clear();
}

public static void unsubscribe(String... nodes)
public void unsubscribe(String... nodes)
{
unsubscribe(Arrays.asList(nodes));
this.unsubscribe(Arrays.asList(nodes));
}

public static void unsubscribe(Collection<String> nodes)
public void unsubscribe(Collection<String> nodes)
{
updateSubscriptions(PACKET_C2S_UNSUBSCRIBE, nodes);
this.updateSubscriptions(PACKET_C2S_UNSUBSCRIBE, nodes);
}

private static void updateSubscriptions(int updateType, Collection<String> nodes)
protected void updateSubscriptions(int updateType, Collection<String> nodes)
{
NetHandlerPlayClient handler = GameUtils.getClient().getConnection();

Expand All @@ -198,15 +176,15 @@ private static void updateSubscriptions(int updateType, Collection<String> nodes

for (String node : nodes)
{
if (updateType == PACKET_C2S_SUBSCRIBE && SUBSCRIPTIONS.contains(node) == false)
if (updateType == PACKET_C2S_SUBSCRIBE && this.subscriptions.contains(node) == false)
{
actionableNodes.add(node);
SUBSCRIPTIONS.add(node);
this.subscriptions.add(node);
}
else if (updateType == PACKET_C2S_UNSUBSCRIBE && SUBSCRIPTIONS.contains(node))
else if (updateType == PACKET_C2S_UNSUBSCRIBE && this.subscriptions.contains(node))
{
actionableNodes.add(node);
SUBSCRIPTIONS.remove(node);
this.subscriptions.remove(node);
}
}

Expand Down Expand Up @@ -234,7 +212,7 @@ else if (updateType == PACKET_C2S_UNSUBSCRIBE && SUBSCRIPTIONS.contains(node))
}
}

public static void updatePubSubSubscriptions()
public void updatePubSubSubscriptions()
{
World world = GameUtils.getClientWorld();

Expand All @@ -245,8 +223,8 @@ public static void updatePubSubSubscriptions()
DimensionType dimType = world.provider.getDimensionType();

// First unsubscribe from any previous subscriptions
Set<String> unSubs = new HashSet<>(PER_DIMENSION_SUBSCRIPTIONS);
PER_DIMENSION_SUBSCRIPTIONS.clear();
Set<String> unSubs = new HashSet<>(this.perDimensionSubscriptions);
this.perDimensionSubscriptions.clear();

if (InfoLineToggle.SERVER_TPS.getBooleanValue())
{
Expand All @@ -271,7 +249,7 @@ public static void updatePubSubSubscriptions()
if (InfoLineToggle.MOB_CAPS.getBooleanValue())
{
List<String> mobCaps = getMobCapNodeNames(dimType);
addPerDimensionSubs(mobCaps, newSubs, unSubs);
this.addPerDimensionSubs(mobCaps, newSubs, unSubs);
}

if ((InfoLineToggle.CHUNK_UNLOAD_ORDER.getBooleanValue()
Expand All @@ -280,7 +258,7 @@ public static void updatePubSubSubscriptions()
&& Configs.Generic.CHUNK_UNLOAD_BUCKET_HASH_SIZE.getBooleanValue())
{
String node = getDroppedChunksHashSizeNodeName(dimType);
addPerDimensionSubs(Collections.singletonList(node), newSubs, unSubs);
this.addPerDimensionSubs(Collections.singletonList(node), newSubs, unSubs);
}

if (unSubs.isEmpty() == false || newSubs.isEmpty() == false)
Expand All @@ -289,12 +267,12 @@ public static void updatePubSubSubscriptions()

if (unSubs.isEmpty() == false)
{
unsubscribe(unSubs);
this.unsubscribe(unSubs);
}

if (newSubs.isEmpty() == false)
{
subscribe(newSubs);
this.subscribe(newSubs);
}
}
else
Expand All @@ -304,19 +282,19 @@ public static void updatePubSubSubscriptions()
}
}

private static void addPerDimensionSubs(List<String> nodes, Set<String> newsubs, Set<String> unsubs)
protected void addPerDimensionSubs(List<String> nodes, Set<String> newsubs, Set<String> unsubs)
{
newsubs.addAll(nodes);
nodes.forEach(unsubs::remove);
PER_DIMENSION_SUBSCRIPTIONS.addAll(nodes);
this.perDimensionSubscriptions.addAll(nodes);
}

private static String getDroppedChunksHashSizeNodeName(DimensionType dimType)
protected static String getDroppedChunksHashSizeNodeName(DimensionType dimType)
{
return "minecraft." + dimType.getName() + ".chunk_loading.dropped_chunks.hash_size";
}

private static List<String> getMobCapNodeNames(DimensionType dimensionType)
protected static List<String> getMobCapNodeNames(DimensionType dimensionType)
{
List<String> nodes = new ArrayList<>();
String prefix = "minecraft." + dimensionType.getName() + ".mob_cap.";
Expand All @@ -331,7 +309,7 @@ private static List<String> getMobCapNodeNames(DimensionType dimensionType)
return nodes;
}

private static List<String> getWoolCounterNodeNames(boolean enabledOnly)
protected static List<String> getWoolCounterNodeNames(boolean enabledOnly)
{
List<String> nodes = new ArrayList<>();
WoolCounters wc = DataStorage.getInstance().getWoolCounters();
Expand All @@ -346,4 +324,21 @@ private static List<String> getWoolCounterNodeNames(boolean enabledOnly)

return nodes;
}

public static class NodeType
{
public final int typeId;
public final Consumer<PacketBuffer> handler;

public NodeType(int typeId, Consumer<PacketBuffer> handler)
{
this.typeId = typeId;
this.handler = handler;
}

public static NodeType create(int typeId, Consumer<PacketBuffer> handler)
{
return new NodeType(typeId, handler);
}
}
}

0 comments on commit a37402e

Please sign in to comment.