Skip to content

Commit

Permalink
remove needless singleton structure from TransmitterNetworkRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Dec 24, 2024
1 parent ddf6ef1 commit c2b6075
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/main/java/mekanism/client/MekanismClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static void resetDimensionChange() {
Mekanism.activeVibrators.clear();
RadiationManager.get().resetClient();
RenderSPS.clearBoltRenderers();
TransmitterNetworkRegistry.getInstance().clearClientNetworks();
TransmitterNetworkRegistry.clearClientNetworks();
RenderTickHandler.clearQueued();

for (IModModule module : Mekanism.modulesLoaded) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/mekanism/common/Mekanism.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ private void commonSetup(FMLCommonSetupEvent event) {

NeoForge.EVENT_BUS.register(RadiationManager.get());

//Register with TransmitterNetworkRegistry
TransmitterNetworkRegistry.initiate();

//Fake player info
logger.info("Fake player readout: UUID = {}, name = {}", gameProfile.getId(), gameProfile.getName());
logger.info("Mod loaded.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public void handleUpdateTag(@NotNull CompoundTag tag, @NotNull HolderLookup.Prov
//Nothing needs to be done
return;
}
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getInstance().getClientNetwork(networkID);
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getClientNetwork(networkID);
if (clientNetwork == null) {
NETWORK network = createEmptyNetworkWithID(networkID);
network.register();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/item/ItemNetworkReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public InteractionResult useOn(UseOnContext context) {
return InteractionResult.CONSUME;
} else if (player.isShiftKeyDown() && MekanismAPI.debug) {
displayBorder(player, MekanismLang.DEBUG_TITLE, true);
for (Component component : TransmitterNetworkRegistry.getInstance().toComponents()) {
for (Component component : TransmitterNetworkRegistry.toComponents()) {
player.sendSystemMessage(TextComponentUtil.build(EnumColor.DARK_GRAY, component));
}
displayEndBorder(player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ protected void adoptAllAndRegister(Collection<NETWORK> networks) {

public void register() {
if (isRemote()) {
TransmitterNetworkRegistry.getInstance().addClientNetwork(getUUID(), this);
TransmitterNetworkRegistry.addClientNetwork(getUUID(), this);
} else {
TransmitterNetworkRegistry.getInstance().registerNetwork(this);
TransmitterNetworkRegistry.registerNetwork(this);
}
}

Expand All @@ -184,9 +184,9 @@ public void deregister() {
acceptorCache.deregister();
transmitterValidator = null;
if (isRemote()) {
TransmitterNetworkRegistry.getInstance().removeClientNetwork(this);
TransmitterNetworkRegistry.removeClientNetwork(this);
} else {
TransmitterNetworkRegistry.getInstance().removeNetwork(this);
TransmitterNetworkRegistry.removeNetwork(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,109 +29,96 @@
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.level.ChunkTicketLevelUpdatedEvent;
import net.neoforged.neoforge.event.tick.ServerTickEvent;
import org.jetbrains.annotations.Nullable;

@EventBusSubscriber(modid = Mekanism.MODID)
public class TransmitterNetworkRegistry {

private static final TransmitterNetworkRegistry INSTANCE = new TransmitterNetworkRegistry();
private static boolean loaderRegistered = false;
private final Multimap<Chunk3D, Transmitter<?, ?, ?>> transmitters = HashMultimap.create();
private Object2BooleanMap<Chunk3D> changedTicketChunks = new Object2BooleanOpenHashMap<>();
private final Set<DynamicNetwork<?, ?, ?>> networks = new ObjectOpenHashSet<>();
private final Map<UUID, DynamicNetwork<?, ?, ?>> clientNetworks = new Object2ObjectOpenHashMap<>();
private Map<GlobalPos, Transmitter<?, ?, ?>> newOrphanTransmitters = new Object2ObjectOpenHashMap<>();
private Set<Transmitter<?, ?, ?>> invalidTransmitters = new ObjectOpenHashSet<>();
private Set<DynamicNetwork<?, ?, ?>> networksToChange = new ObjectOpenHashSet<>();
private static final Multimap<Chunk3D, Transmitter<?, ?, ?>> transmitters = HashMultimap.create();
private static Object2BooleanMap<Chunk3D> changedTicketChunks = new Object2BooleanOpenHashMap<>();
private static final Set<DynamicNetwork<?, ?, ?>> networks = new ObjectOpenHashSet<>();
private static final Map<UUID, DynamicNetwork<?, ?, ?>> clientNetworks = new Object2ObjectOpenHashMap<>();
private static Map<GlobalPos, Transmitter<?, ?, ?>> newOrphanTransmitters = new Object2ObjectOpenHashMap<>();
private static Set<Transmitter<?, ?, ?>> invalidTransmitters = new ObjectOpenHashSet<>();
private static Set<DynamicNetwork<?, ?, ?>> networksToChange = new ObjectOpenHashSet<>();

public void addClientNetwork(UUID networkID, DynamicNetwork<?, ?, ?> network) {
public static void addClientNetwork(UUID networkID, DynamicNetwork<?, ?, ?> network) {
if (!clientNetworks.containsKey(networkID)) {
clientNetworks.put(networkID, network);
}
}

@Nullable
public DynamicNetwork<?, ?, ?> getClientNetwork(UUID networkID) {
public static DynamicNetwork<?, ?, ?> getClientNetwork(UUID networkID) {
return clientNetworks.get(networkID);
}

public void removeClientNetwork(DynamicNetwork<?, ?, ?> network) {
public static void removeClientNetwork(DynamicNetwork<?, ?, ?> network) {
clientNetworks.remove(network.getUUID());
}

public void clearClientNetworks() {
public static void clearClientNetworks() {
clientNetworks.clear();
}

public static void initiate() {
if (!loaderRegistered) {
loaderRegistered = true;
NeoForge.EVENT_BUS.register(INSTANCE);
}
}

public static void reset() {
getInstance().networks.clear();
getInstance().networksToChange.clear();
getInstance().invalidTransmitters.clear();
getInstance().newOrphanTransmitters.clear();
getInstance().transmitters.clear();
getInstance().changedTicketChunks.clear();
networks.clear();
networksToChange.clear();
invalidTransmitters.clear();
newOrphanTransmitters.clear();
transmitters.clear();
changedTicketChunks.clear();
}

public static void trackTransmitter(Transmitter<?, ?, ?> transmitter) {
getInstance().transmitters.put(transmitter.getTileChunk(), transmitter);
transmitters.put(transmitter.getTileChunk(), transmitter);
}

public static void untrackTransmitter(Transmitter<?, ?, ?> transmitter) {
getInstance().transmitters.remove(transmitter.getTileChunk(), transmitter);
transmitters.remove(transmitter.getTileChunk(), transmitter);
}

public static void invalidateTransmitter(Transmitter<?, ?, ?> transmitter) {
TransmitterNetworkRegistry registry = getInstance();
registry.invalidTransmitters.add(transmitter);
invalidTransmitters.add(transmitter);
GlobalPos coord = transmitter.getTileGlobalPos();
Transmitter<?, ?, ?> removed = registry.newOrphanTransmitters.remove(coord);
Transmitter<?, ?, ?> removed = newOrphanTransmitters.remove(coord);
if (removed != null && removed != transmitter) {
Mekanism.logger.error("Different orphan transmitter was registered at location during removal! {}", coord);
registry.newOrphanTransmitters.put(coord, transmitter);//put it back?
newOrphanTransmitters.put(coord, transmitter);//put it back?
}
}

public static void registerOrphanTransmitter(Transmitter<?, ?, ?> transmitter) {
if (!getInstance().invalidTransmitters.remove(transmitter)) {
if (!invalidTransmitters.remove(transmitter)) {
//If we weren't an invalid transmitter, then we need to add it as a new orphan, otherwise removing it is good enough
// as if it was an orphan before it still will be one, and if it wasn't then it still will be part of the network it
// was in.
GlobalPos pos = transmitter.getTileGlobalPos();
Transmitter<?, ?, ?> previous = getInstance().newOrphanTransmitters.put(pos, transmitter);
Transmitter<?, ?, ?> previous = newOrphanTransmitters.put(pos, transmitter);
if (previous != null && previous != transmitter && previous.isValid()) {
Mekanism.logger.error("Different orphan transmitter was already registered at location! {}", pos);
}
}
}

public static void registerChangedNetwork(DynamicNetwork<?, ?, ?> network) {
getInstance().networksToChange.add(network);
networksToChange.add(network);
}

public static TransmitterNetworkRegistry getInstance() {
return INSTANCE;
}

public void registerNetwork(DynamicNetwork<?, ?, ?> network) {
public static void registerNetwork(DynamicNetwork<?, ?, ?> network) {
networks.add(network);
}

public void removeNetwork(DynamicNetwork<?, ?, ?> network) {
public static void removeNetwork(DynamicNetwork<?, ?, ?> network) {
networks.remove(network);
networksToChange.remove(network);
}

@SubscribeEvent
public void onTick(ServerTickEvent.Post event) {
public static void onTick(ServerTickEvent.Post event) {
handleChangedChunks();
removeInvalidTransmitters();
assignOrphans();
Expand All @@ -144,7 +131,7 @@ public void onTick(ServerTickEvent.Post event) {
}

@SubscribeEvent
public void onTicketLevelChange(ChunkTicketLevelUpdatedEvent event) {
public static void onTicketLevelChange(ChunkTicketLevelUpdatedEvent event) {
int newTicketLevel = event.getNewTicketLevel();
int oldTicketLevel = event.getOldTicketLevel();
boolean loaded;
Expand Down Expand Up @@ -174,7 +161,7 @@ public void onTicketLevelChange(ChunkTicketLevelUpdatedEvent event) {
}
}

private void handleChangedChunks() {
private static void handleChangedChunks() {
if (!changedTicketChunks.isEmpty()) {
Object2BooleanMap<Chunk3D> changed = changedTicketChunks;
changedTicketChunks = new Object2BooleanOpenHashMap<>();
Expand All @@ -195,7 +182,7 @@ private void handleChangedChunks() {
}
}

private void removeInvalidTransmitters() {
private static void removeInvalidTransmitters() {
if (!invalidTransmitters.isEmpty()) {
//Ensure we copy the invalid transmitters, so that when we iterate and remove invalid ones
// and add still valid ones as orphans, we actually add them as orphans rather than try
Expand All @@ -211,7 +198,7 @@ private void removeInvalidTransmitters() {
}
}

private <NETWORK extends DynamicNetwork<?, NETWORK, TRANSMITTER>, TRANSMITTER extends Transmitter<?, NETWORK, TRANSMITTER>>
private static <NETWORK extends DynamicNetwork<?, NETWORK, TRANSMITTER>, TRANSMITTER extends Transmitter<?, NETWORK, TRANSMITTER>>
void removeInvalidTransmitter(Transmitter<?, NETWORK, TRANSMITTER> invalid) {
if (!invalid.isOrphan() || !invalid.isValid()) {
NETWORK n = invalid.getTransmitterNetwork();
Expand All @@ -227,7 +214,7 @@ void removeInvalidTransmitter(Transmitter<?, NETWORK, TRANSMITTER> invalid) {
}
}

private void assignOrphans() {
private static void assignOrphans() {
if (!newOrphanTransmitters.isEmpty()) {
Map<GlobalPos, Transmitter<?, ?, ?>> orphanTransmitters = newOrphanTransmitters;
newOrphanTransmitters = new Object2ObjectOpenHashMap<>();
Expand All @@ -244,7 +231,7 @@ private void assignOrphans() {
}
}

private void commitChanges() {
private static void commitChanges() {
if (!networksToChange.isEmpty()) {
Set<DynamicNetwork<?, ?, ?>> networks = networksToChange;
networksToChange = new ObjectOpenHashSet<>();
Expand All @@ -259,7 +246,7 @@ public String toString() {
return "Network Registry:\n" + networks;
}

public Component[] toComponents() {
public static Component[] toComponents() {
Component[] components = new Component[networks.size()];
int i = 0;
for (DynamicNetwork<?, ?, ?> network : networks) {
Expand Down Expand Up @@ -351,4 +338,7 @@ private void iterate(Map<GlobalPos, Transmitter<?, ?, ?>> orphanTransmitters, Bl
}
}
}

private TransmitterNetworkRegistry() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CustomPacketPayload.Type<PacketChemicalNetworkContents> type() {
public void handle(IPayloadContext context) {
//Note: We set the information even if opaque transmitters is true in case the client turns the config setting off
// so that they will have the proper information to then render
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getInstance().getClientNetwork(networkID);
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getClientNetwork(networkID);
if (clientNetwork instanceof ChemicalNetwork network) {
network.setLastChemical(chemical);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public CustomPacketPayload.Type<PacketFluidNetworkContents> type() {
public void handle(IPayloadContext context) {
//Note: We set the information even if opaque transmitters is true in case the client turns the config setting off
// so that they will have the proper information to then render
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getInstance().getClientNetwork(networkID);
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getClientNetwork(networkID);
if (clientNetwork instanceof FluidNetwork network) {
network.setLastFluid(fluid);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CustomPacketPayload.Type<PacketNetworkScale> type() {
public void handle(IPayloadContext context) {
//Note: We set the information even if opaque transmitters is true in case the client turns the config setting off
// so that they will have the proper information to then render
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getInstance().getClientNetwork(networkID);
DynamicNetwork<?, ?, ?> clientNetwork = TransmitterNetworkRegistry.getClientNetwork(networkID);
if (clientNetwork instanceof DynamicBufferedNetwork<?, ?, ?, ?> network) {
network.currentScale = scale;
}
Expand Down

0 comments on commit c2b6075

Please sign in to comment.