From 0ee91ab60927f40be2c5d5f37bfc8a2c490163e6 Mon Sep 17 00:00:00 2001 From: calmilamsy Date: Thu, 24 Apr 2025 16:04:38 +0100 Subject: [PATCH 1/5] Update to latest stapi --- build.gradle.kts | 7 ++----- gradle.properties | 9 ++++----- gradle/wrapper/gradle-wrapper.properties | 2 +- .../machineessentials/MachineEssentials.java | 19 +++++++++---------- .../electric/base/ElectricBlockEntity.java | 2 +- .../base/ElectricWireBlockEntity.java | 1 - .../machineessentials/network/Network.java | 2 -- .../network/NetworkManager.java | 7 ++++++- .../network/NetworkType.java | 1 - .../network/NetworkWalker.java | 1 - 10 files changed, 23 insertions(+), 28 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b94702d..7bbc88c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,13 +1,11 @@ -import org.gradle.internal.extensions.stdlib.toDefaultLowerCase import java.io.BufferedReader import java.io.InputStreamReader import java.net.URI -import java.net.URL plugins { id("maven-publish") - id("fabric-loom") version "1.8.10" - id("babric-loom-extension") version "1.8.5" + id("fabric-loom") version "1.9.2" + id("babric-loom-extension") version "1.9.4" } //noinspection GroovyUnusedAssignment @@ -74,7 +72,6 @@ dependencies { modImplementation("net.modificationstation:StationAPI:${project.properties["stapi_version"]}") modImplementation("net.glasslauncher.mods:ModMenu:${project.properties["modmenu_version"]}") - modImplementation("net.glasslauncher.mods:glass-networking:${project.properties["glassnetworking_version"]}") modImplementation("net.glasslauncher.mods:GlassConfigAPI:${project.properties["gcapi_version"]}") modImplementation("net.glasslauncher.mods:AlwaysMoreItems:${project.properties["alwaysmoreitems_version"]}") } diff --git a/gradle.properties b/gradle.properties index ff9016f..177a0c4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,10 +15,9 @@ maven_group=net.teamterminus archives_base_name=machine_essentials # Dependencies -stapi_version=2.0-alpha.2.4 +stapi_version=2.0.0-alpha.4 # Extra Dependencies -gcapi_version=3.0.2 -glassnetworking_version=1.0.3 -alwaysmoreitems_version=1.2.0 -modmenu_version=1.8.5-beta.9 +gcapi_version=3.1.0 +alwaysmoreitems_version=1.5.3 +modmenu_version=1.8.5-beta.11 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index e1adfb4..e48eca5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/net/teamterminus/machineessentials/MachineEssentials.java b/src/main/java/net/teamterminus/machineessentials/MachineEssentials.java index 399eb16..3b5d053 100644 --- a/src/main/java/net/teamterminus/machineessentials/MachineEssentials.java +++ b/src/main/java/net/teamterminus/machineessentials/MachineEssentials.java @@ -7,28 +7,27 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockView; import net.modificationstation.stationapi.api.event.mod.InitEvent; -import net.modificationstation.stationapi.api.mod.entrypoint.Entrypoint; +import net.modificationstation.stationapi.api.mod.entrypoint.EntrypointManager; import net.modificationstation.stationapi.api.util.Namespace; -import net.modificationstation.stationapi.api.util.Null; import net.modificationstation.stationapi.api.util.math.Direction; import net.modificationstation.stationapi.api.world.StationFlatteningWorld; import org.apache.logging.log4j.Logger; +import java.lang.invoke.MethodHandles; import java.util.*; public class MachineEssentials { + static { + EntrypointManager.registerLookup(MethodHandles.lookup()); + } - @Entrypoint.Namespace - public static final Namespace NAMESPACE = Null.get(); - - @Entrypoint.Logger - public static final Logger LOGGER = Null.get(); + @SuppressWarnings("UnstableApiUsage") + public static final Namespace NAMESPACE = Namespace.resolve(); - @Entrypoint.Instance - public static final MachineEssentials INSTANCE = Null.get(); + public static final Logger LOGGER = NAMESPACE.getLogger("MachineEssentials"); @EventListener - public static void init(InitEvent event) { + private static void init(InitEvent event) { LOGGER.info("Machine Essentials initialized."); } diff --git a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java index 2573bf5..1f609a5 100644 --- a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java +++ b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java @@ -8,8 +8,8 @@ import net.teamterminus.machineessentials.energy.electric.api.Electric; import net.teamterminus.machineessentials.energy.electric.api.HasVoltageTier; import net.teamterminus.machineessentials.energy.electric.api.VoltageTier; -import net.teamterminus.machineessentials.network.NetworkComponent; import net.teamterminus.machineessentials.network.Network; +import net.teamterminus.machineessentials.network.NetworkComponent; import net.teamterminus.machineessentials.network.NetworkType; import net.teamterminus.machineessentials.util.AveragingCounter; import net.teamterminus.machineessentials.util.BlockEntityInit; diff --git a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java index 52297be..e7ba3e3 100644 --- a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java +++ b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java @@ -1,7 +1,6 @@ package net.teamterminus.machineessentials.energy.electric.base; import lombok.Getter; -import lombok.Setter; import net.minecraft.block.entity.BlockEntity; import net.modificationstation.stationapi.api.util.math.Direction; import net.modificationstation.stationapi.api.util.math.Vec3i; diff --git a/src/main/java/net/teamterminus/machineessentials/network/Network.java b/src/main/java/net/teamterminus/machineessentials/network/Network.java index 7d53045..11e3b9c 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/Network.java +++ b/src/main/java/net/teamterminus/machineessentials/network/Network.java @@ -1,8 +1,6 @@ package net.teamterminus.machineessentials.network; import com.google.common.collect.Maps; - - import net.minecraft.block.Block; import net.minecraft.block.entity.BlockEntity; import net.minecraft.nbt.NbtCompound; diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java index a4f695d..774e540 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java @@ -11,6 +11,7 @@ import net.modificationstation.stationapi.api.event.world.BlockSetEvent; import net.modificationstation.stationapi.api.event.world.WorldEvent; import net.modificationstation.stationapi.api.mod.entrypoint.Entrypoint; +import net.modificationstation.stationapi.api.mod.entrypoint.EntrypointManager; import net.modificationstation.stationapi.api.mod.entrypoint.EventBusPolicy; import net.modificationstation.stationapi.api.util.math.Vec3i; import net.modificationstation.stationapi.api.world.StationFlatteningWorld; @@ -19,6 +20,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.lang.invoke.MethodHandles; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -30,6 +32,9 @@ */ @Entrypoint(eventBus = @EventBusPolicy(registerInstance = false)) public class NetworkManager { + static { + EntrypointManager.registerLookup(MethodHandles.lookup()); + } private static final Map> NETS = new HashMap<>(); private static final AtomicInteger ID_PROVIDER = new AtomicInteger(0); @@ -47,7 +52,7 @@ public static NetworkManager getInstance() { } @EventListener - public void blockChanged(BlockSetEvent event) { + private void blockChanged(BlockSetEvent event) { if(event.blockState == States.AIR.get()){ removeBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); } else { diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkType.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkType.java index 644fc16..82ec237 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkType.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkType.java @@ -1,6 +1,5 @@ package net.teamterminus.machineessentials.network; -import net.modificationstation.stationapi.api.util.Identifier; import org.jetbrains.annotations.NotNull; /** diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java index f770935..0658fc5 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java @@ -8,7 +8,6 @@ import net.modificationstation.stationapi.api.util.math.Vec3i; import net.teamterminus.machineessentials.MachineEssentials; - import java.util.*; /** From 8d1306c4215c015b738aed7138a71a7bf2533709 Mon Sep 17 00:00:00 2001 From: calmilamsy Date: Fri, 25 Apr 2025 16:34:16 +0100 Subject: [PATCH 2/5] Fix bad save code --- .../network/NetworkManager.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java index 774e540..b46c685 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java @@ -16,10 +16,7 @@ import net.modificationstation.stationapi.api.util.math.Vec3i; import net.modificationstation.stationapi.api.world.StationFlatteningWorld; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; +import java.io.*; import java.lang.invoke.MethodHandles; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -78,11 +75,18 @@ private static void initNetsEvent(WorldEvent.Init event) { private static void saveNetsEvent(WorldEvent.Save event) { try { File file = event.world.dimensionData.getWorldPropertiesFile("networks"); - NbtCompound tag = NbtIo.readCompressed(new FileInputStream(file)); - NetworkManager.netsToTag(event.world,tag); + NbtCompound tag; + if (file.exists()) { + tag = NbtIo.readCompressed(new FileInputStream(file)); + NetworkManager.netsToTag(event.world, tag); + } + else { + tag = new NbtCompound(); + file.createNewFile(); + } NbtIo.writeCompressed(tag, new FileOutputStream(file)); } - catch (FileNotFoundException e) { + catch (IOException e) { e.printStackTrace(); } } From fd095bf806c5df2bf749393239109971f4d6f4d2 Mon Sep 17 00:00:00 2001 From: calmilamsy Date: Thu, 1 May 2025 13:47:49 +0100 Subject: [PATCH 3/5] Why --- .../energy/electric/base/ElectricBlock.java | 6 ------ .../energy/electric/base/ElectricBlockEntity.java | 3 +-- .../energy/electric/base/ElectricWireBlockEntity.java | 3 +-- .../machineessentials/util/BlockEntityInit.java | 5 ----- 4 files changed, 2 insertions(+), 15 deletions(-) delete mode 100644 src/main/java/net/teamterminus/machineessentials/util/BlockEntityInit.java diff --git a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlock.java b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlock.java index 518cd88..91467db 100644 --- a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlock.java +++ b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlock.java @@ -4,7 +4,6 @@ import net.minecraft.world.World; import net.modificationstation.stationapi.api.template.block.TemplateBlockWithEntity; import net.modificationstation.stationapi.api.util.Identifier; -import net.teamterminus.machineessentials.util.BlockEntityInit; public abstract class ElectricBlock extends TemplateBlockWithEntity { @@ -12,9 +11,4 @@ public ElectricBlock(Identifier identifier, Material material) { super(identifier, material); } - @Override - public void onPlaced(World world, int x, int y, int z) { - super.onPlaced(world, x, y, z); - ((BlockEntityInit) world.getBlockEntity(x, y, z)).init(); - } } diff --git a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java index 1f609a5..e448ddc 100644 --- a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java +++ b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricBlockEntity.java @@ -12,9 +12,8 @@ import net.teamterminus.machineessentials.network.NetworkComponent; import net.teamterminus.machineessentials.network.NetworkType; import net.teamterminus.machineessentials.util.AveragingCounter; -import net.teamterminus.machineessentials.util.BlockEntityInit; -public abstract class ElectricBlockEntity extends BlockEntity implements BlockEntityInit, NetworkComponent, HasVoltageTier, Electric { +public abstract class ElectricBlockEntity extends BlockEntity implements NetworkComponent, HasVoltageTier, Electric { /** * Prefer using getEnergy() instead when possible, this field might not always represent the real energy level! diff --git a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java index e7ba3e3..13407fb 100644 --- a/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java +++ b/src/main/java/net/teamterminus/machineessentials/energy/electric/base/ElectricWireBlockEntity.java @@ -11,9 +11,8 @@ import net.teamterminus.machineessentials.network.Network; import net.teamterminus.machineessentials.network.NetworkType; import net.teamterminus.machineessentials.util.AveragingCounter; -import net.teamterminus.machineessentials.util.BlockEntityInit; -public abstract class ElectricWireBlockEntity extends BlockEntity implements BlockEntityInit, ElectricWire { +public abstract class ElectricWireBlockEntity extends BlockEntity implements ElectricWire { public Network energyNet; @Getter diff --git a/src/main/java/net/teamterminus/machineessentials/util/BlockEntityInit.java b/src/main/java/net/teamterminus/machineessentials/util/BlockEntityInit.java deleted file mode 100644 index ad8d5e6..0000000 --- a/src/main/java/net/teamterminus/machineessentials/util/BlockEntityInit.java +++ /dev/null @@ -1,5 +0,0 @@ -package net.teamterminus.machineessentials.util; - -public interface BlockEntityInit { - void init(); -} From aefa52943932384b35b1805efc92c399ad28ffdd Mon Sep 17 00:00:00 2001 From: calmilamsy Date: Thu, 1 May 2025 13:51:48 +0100 Subject: [PATCH 4/5] MAAAARTIN FIX YOUR SETTINGS --- .../network/NetworkManager.java | 590 +++++++++--------- 1 file changed, 295 insertions(+), 295 deletions(-) diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java index b46c685..2b55d68 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java @@ -29,299 +29,299 @@ */ @Entrypoint(eventBus = @EventBusPolicy(registerInstance = false)) public class NetworkManager { - static { - EntrypointManager.registerLookup(MethodHandles.lookup()); - } - - private static final Map> NETS = new HashMap<>(); - private static final AtomicInteger ID_PROVIDER = new AtomicInteger(0); - private static final NetworkManager INSTANCE = new NetworkManager(); - - public NetworkManager() {} - - public static int getNetID(World world, int x, int y, int z) { - Network net = getNet(world, x, y, z); - return net == null ? -1 : net.hashCode(); - } - - public static NetworkManager getInstance() { - return INSTANCE; - } - - @EventListener - private void blockChanged(BlockSetEvent event) { - if(event.blockState == States.AIR.get()){ - removeBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); - } else { - addBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); - } - } - - @EventListener - private static void initNetsEvent(WorldEvent.Init event) { - File file = event.world.dimensionData.getWorldPropertiesFile("networks"); - if (file.exists()) { - try { - NbtCompound tag = NbtIo.readCompressed(new FileInputStream(file)); - NetworkManager.netsFromTag(event.world, tag); - } - catch (FileNotFoundException e) { - e.printStackTrace(); - } - } - } - - @EventListener - private static void saveNetsEvent(WorldEvent.Save event) { - try { - File file = event.world.dimensionData.getWorldPropertiesFile("networks"); - NbtCompound tag; - if (file.exists()) { - tag = NbtIo.readCompressed(new FileInputStream(file)); - NetworkManager.netsToTag(event.world, tag); - } - else { - tag = new NbtCompound(); - file.createNewFile(); - } - NbtIo.writeCompressed(tag, new FileOutputStream(file)); - } - catch (IOException e) { - e.printStackTrace(); - } - } - - public static void addBlock(BlockChangeInfo blockChanged) { - int x = blockChanged.pos.getX(); - int y = blockChanged.pos.getY(); - int z = blockChanged.pos.getZ(); - World world = blockChanged.world; - - if(!canBeNet(blockChanged.state.getBlock())) { - return; - } - - NetworkComponentBlock component = (NetworkComponentBlock) blockChanged.state.getBlock(); - - Set nets = NETS.computeIfAbsent(world.dimension.id, i -> new HashSet<>()); - - //check for nets around this one - Set sideNets = new HashSet<>(); - for (Network net: nets) { - for (Vec3i offset: OFFSETS) { - int px = x + offset.getX(); - int py = y + offset.getY(); - int pz = z + offset.getZ(); - if (net.existsOnPos(px, py, pz)) { - sideNets.add(net); - } - } - } - - Network net = null; - int size = sideNets.size(); - //no nets around, create one - if (size == 0) { - net = new Network(world,component.getType()); - net.addBlock(x, y, z); - for (Vec3i offset: OFFSETS) { - int px = x + offset.getX(); - int py = y + offset.getY(); - int pz = z + offset.getZ(); - if (canBeNet(world, px, py, pz)) { - net.addBlock(px, py, pz); - } - } - if (net.getSize() > 1) { - nets.add(net); - } - } - else if (size == 1) { - Network potentialNet = sideNets.stream().findAny().get(); - if(potentialNet.isOfSameType(component)){ - potentialNet.addBlock(x, y, z); - net = potentialNet; - } - } - else { //multiple nets around - Network[] netsArray = sideNets.toArray(new Network[size]); - Network main = null; - for (Network network : netsArray) { - if(network.isOfSameType(component)){ - main = network; - main.addBlock(x, y, z); - for (Network otherNet : netsArray) { - if(otherNet == main){ - continue; - } - if(otherNet.isOfSameType(main)){ - main.mergeNetwork(otherNet); - nets.remove(otherNet); - } - } - net = main; - break; - } - } - } - - if(net == null && getNet(world, x, y, z) == null) { - net = new Network(world,component.getType()); - net.addBlock(x, y, z); - for (Vec3i offset: OFFSETS) { - int px = x + offset.getX(); - int py = y + offset.getY(); - int pz = z + offset.getZ(); - if (canBeNet(world, px, py, pz)) { - net.addBlock(px, py, pz); - } - } - if (net.getSize() > 1) { - nets.add(net); - } - } - - //add surrounding blocks to net if type matches - for (Vec3i offset : OFFSETS) { - int px = x + offset.getX(); - int py = y + offset.getY(); - int pz = z + offset.getZ(); - if (canBeNet(world, px, py, pz) && getNet(world, px, py, pz) == null && net != null) { - NetworkComponentBlock sideComponent = (NetworkComponentBlock) world.getBlockState(px, py, pz).getBlock(); - if(net.isOfSameType(sideComponent)){ - net.addBlock(px, py, pz); - } - } - } - } - - public static void removeBlock(BlockChangeInfo blockChanged) { - int x = blockChanged.pos.getX(); - int y = blockChanged.pos.getY(); - int z = blockChanged.pos.getZ(); - World world = blockChanged.world; - Set nets = NETS.get(world.dimension.id); - - if (nets == null) { - return; - } - - Network target = null; - for (Network net: nets) { - if (net.existsOnPos(x, y, z)) { - target = net; - break; - } - } - - if (target != null) { - List sideNets = target.removeBlock(x, y, z); - if (sideNets != null) { - nets.remove(target); - nets.addAll(sideNets); - } - else if (target.getSize() < 2) { - nets.remove(target); - } - } - } - - public static int getUID() { - return ID_PROVIDER.getAndIncrement(); - } - - public static void netsToTag(World world, NbtCompound root) { - Set nets = NETS.get(world.dimension.id); - NbtCompound dimTag = new NbtCompound(); - root.put("dim" + world.dimension.id, dimTag); - - if (nets == null) { - return; - } - - NbtList netsList = new NbtList(); - dimTag.put("nets", netsList); - nets.forEach(network -> { - netsList.add(network.toTag()); - }); - } - - public static void netsToTag(int dim, NbtCompound root) { - Set nets = NETS.get(dim); - NbtCompound dimTag = new NbtCompound(); - root.put("dim" + dim, dimTag); - - if (nets == null) { - return; - } - - NbtList netsList = new NbtList(); - dimTag.put("nets", netsList); - nets.forEach(network -> { - netsList.add(network.toTag()); - }); - } - - public static void netsFromTag(World world, NbtCompound root) { - Set nets = new HashSet<>(); - NETS.put(world.dimension.id, nets); - - NbtCompound dimTag = root.getCompound("dim" + world.dimension.id); - if (dimTag == null) { - return; - } - - NbtList netsList = dimTag.getList("nets"); - final int size = netsList.size(); - for (int i = 0; i < size; i++) { - Network net = Network.fromTag(world, (NbtCompound) netsList.get(i)); - if (net.getSize() > 1) { - nets.add(net); - } - } - } - - public static boolean canBeNet(StationFlatteningWorld world, int x, int y, int z) { - Block block = world.getBlockState(x,y,z).getBlock(); - return canBeNet(block); - } - - public static boolean canBeNet(Block block) { - return block instanceof NetworkComponentBlock; - } - - private static Network getNet(World world, int x, int y, int z) { - Set nets = NETS.get(world.dimension.id); - if (nets != null) { - for (Network net: nets) { - if (net.existsOnPos(x, y, z)) { - return net; - } - } - } - return null; - } - - public static void updateAllNets(){ - NETS.forEach((dimId, nets)->{ - for (Network net : nets) { - net.update(); - } - }); - } - - public static class BlockChangeInfo { - - public BlockState state; - public int meta; - public World world; - public Vec3i pos; - - public BlockChangeInfo(World world, Vec3i pos, BlockState state, int meta) { - this.state = state; - this.meta = meta; - this.world = world; - this.pos = pos; - } - } + static { + EntrypointManager.registerLookup(MethodHandles.lookup()); + } + + private static final Map> NETS = new HashMap<>(); + private static final AtomicInteger ID_PROVIDER = new AtomicInteger(0); + private static final NetworkManager INSTANCE = new NetworkManager(); + + public NetworkManager() {} + + public static int getNetID(World world, int x, int y, int z) { + Network net = getNet(world, x, y, z); + return net == null ? -1 : net.hashCode(); + } + + public static NetworkManager getInstance() { + return INSTANCE; + } + + @EventListener + private void blockChanged(BlockSetEvent event) { + if(event.blockState == States.AIR.get()){ + removeBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); + } else { + addBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); + } + } + + @EventListener + private static void initNetsEvent(WorldEvent.Init event) { + File file = event.world.dimensionData.getWorldPropertiesFile("networks"); + if (file.exists()) { + try { + NbtCompound tag = NbtIo.readCompressed(new FileInputStream(file)); + NetworkManager.netsFromTag(event.world, tag); + } + catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + + @EventListener + private static void saveNetsEvent(WorldEvent.Save event) { + try { + File file = event.world.dimensionData.getWorldPropertiesFile("networks"); + NbtCompound tag; + if (file.exists()) { + tag = NbtIo.readCompressed(new FileInputStream(file)); + NetworkManager.netsToTag(event.world, tag); + } + else { + tag = new NbtCompound(); + file.createNewFile(); + } + NbtIo.writeCompressed(tag, new FileOutputStream(file)); + } + catch (IOException e) { + e.printStackTrace(); + } + } + + public static void addBlock(BlockChangeInfo blockChanged) { + int x = blockChanged.pos.getX(); + int y = blockChanged.pos.getY(); + int z = blockChanged.pos.getZ(); + World world = blockChanged.world; + + if(!canBeNet(blockChanged.state.getBlock())) { + return; + } + + NetworkComponentBlock component = (NetworkComponentBlock) blockChanged.state.getBlock(); + + Set nets = NETS.computeIfAbsent(world.dimension.id, i -> new HashSet<>()); + + //check for nets around this one + Set sideNets = new HashSet<>(); + for (Network net: nets) { + for (Vec3i offset: OFFSETS) { + int px = x + offset.getX(); + int py = y + offset.getY(); + int pz = z + offset.getZ(); + if (net.existsOnPos(px, py, pz)) { + sideNets.add(net); + } + } + } + + Network net = null; + int size = sideNets.size(); + //no nets around, create one + if (size == 0) { + net = new Network(world,component.getType()); + net.addBlock(x, y, z); + for (Vec3i offset: OFFSETS) { + int px = x + offset.getX(); + int py = y + offset.getY(); + int pz = z + offset.getZ(); + if (canBeNet(world, px, py, pz)) { + net.addBlock(px, py, pz); + } + } + if (net.getSize() > 1) { + nets.add(net); + } + } + else if (size == 1) { + Network potentialNet = sideNets.stream().findAny().get(); + if(potentialNet.isOfSameType(component)){ + potentialNet.addBlock(x, y, z); + net = potentialNet; + } + } + else { //multiple nets around + Network[] netsArray = sideNets.toArray(new Network[size]); + Network main = null; + for (Network network : netsArray) { + if(network.isOfSameType(component)){ + main = network; + main.addBlock(x, y, z); + for (Network otherNet : netsArray) { + if(otherNet == main){ + continue; + } + if(otherNet.isOfSameType(main)){ + main.mergeNetwork(otherNet); + nets.remove(otherNet); + } + } + net = main; + break; + } + } + } + + if(net == null && getNet(world, x, y, z) == null) { + net = new Network(world,component.getType()); + net.addBlock(x, y, z); + for (Vec3i offset: OFFSETS) { + int px = x + offset.getX(); + int py = y + offset.getY(); + int pz = z + offset.getZ(); + if (canBeNet(world, px, py, pz)) { + net.addBlock(px, py, pz); + } + } + if (net.getSize() > 1) { + nets.add(net); + } + } + + //add surrounding blocks to net if type matches + for (Vec3i offset : OFFSETS) { + int px = x + offset.getX(); + int py = y + offset.getY(); + int pz = z + offset.getZ(); + if (canBeNet(world, px, py, pz) && getNet(world, px, py, pz) == null && net != null) { + NetworkComponentBlock sideComponent = (NetworkComponentBlock) world.getBlockState(px, py, pz).getBlock(); + if(net.isOfSameType(sideComponent)){ + net.addBlock(px, py, pz); + } + } + } + } + + public static void removeBlock(BlockChangeInfo blockChanged) { + int x = blockChanged.pos.getX(); + int y = blockChanged.pos.getY(); + int z = blockChanged.pos.getZ(); + World world = blockChanged.world; + Set nets = NETS.get(world.dimension.id); + + if (nets == null) { + return; + } + + Network target = null; + for (Network net: nets) { + if (net.existsOnPos(x, y, z)) { + target = net; + break; + } + } + + if (target != null) { + List sideNets = target.removeBlock(x, y, z); + if (sideNets != null) { + nets.remove(target); + nets.addAll(sideNets); + } + else if (target.getSize() < 2) { + nets.remove(target); + } + } + } + + public static int getUID() { + return ID_PROVIDER.getAndIncrement(); + } + + public static void netsToTag(World world, NbtCompound root) { + Set nets = NETS.get(world.dimension.id); + NbtCompound dimTag = new NbtCompound(); + root.put("dim" + world.dimension.id, dimTag); + + if (nets == null) { + return; + } + + NbtList netsList = new NbtList(); + dimTag.put("nets", netsList); + nets.forEach(network -> { + netsList.add(network.toTag()); + }); + } + + public static void netsToTag(int dim, NbtCompound root) { + Set nets = NETS.get(dim); + NbtCompound dimTag = new NbtCompound(); + root.put("dim" + dim, dimTag); + + if (nets == null) { + return; + } + + NbtList netsList = new NbtList(); + dimTag.put("nets", netsList); + nets.forEach(network -> { + netsList.add(network.toTag()); + }); + } + + public static void netsFromTag(World world, NbtCompound root) { + Set nets = new HashSet<>(); + NETS.put(world.dimension.id, nets); + + NbtCompound dimTag = root.getCompound("dim" + world.dimension.id); + if (dimTag == null) { + return; + } + + NbtList netsList = dimTag.getList("nets"); + final int size = netsList.size(); + for (int i = 0; i < size; i++) { + Network net = Network.fromTag(world, (NbtCompound) netsList.get(i)); + if (net.getSize() > 1) { + nets.add(net); + } + } + } + + public static boolean canBeNet(StationFlatteningWorld world, int x, int y, int z) { + Block block = world.getBlockState(x,y,z).getBlock(); + return canBeNet(block); + } + + public static boolean canBeNet(Block block) { + return block instanceof NetworkComponentBlock; + } + + private static Network getNet(World world, int x, int y, int z) { + Set nets = NETS.get(world.dimension.id); + if (nets != null) { + for (Network net: nets) { + if (net.existsOnPos(x, y, z)) { + return net; + } + } + } + return null; + } + + public static void updateAllNets(){ + NETS.forEach((dimId, nets)->{ + for (Network net : nets) { + net.update(); + } + }); + } + + public static class BlockChangeInfo { + + public BlockState state; + public int meta; + public World world; + public Vec3i pos; + + public BlockChangeInfo(World world, Vec3i pos, BlockState state, int meta) { + this.state = state; + this.meta = meta; + this.world = world; + this.pos = pos; + } + } } From f5963eb1dd39aafe3f81bd7f755285aeee5ab1fb Mon Sep 17 00:00:00 2001 From: calmilamsy Date: Thu, 1 May 2025 13:53:37 +0100 Subject: [PATCH 5/5] Fix bad if formatting --- .../network/NetworkManager.java | 16 ++++++++-------- .../machineessentials/network/NetworkWalker.java | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java index 2b55d68..47d9d5e 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkManager.java @@ -50,7 +50,7 @@ public static NetworkManager getInstance() { @EventListener private void blockChanged(BlockSetEvent event) { - if(event.blockState == States.AIR.get()){ + if (event.blockState == States.AIR.get()){ removeBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); } else { addBlock(new BlockChangeInfo(event.world, new Vec3i(event.x, event.y, event.z), event.blockState, event.blockMeta)); @@ -97,7 +97,7 @@ public static void addBlock(BlockChangeInfo blockChanged) { int z = blockChanged.pos.getZ(); World world = blockChanged.world; - if(!canBeNet(blockChanged.state.getBlock())) { + if (!canBeNet(blockChanged.state.getBlock())) { return; } @@ -138,7 +138,7 @@ public static void addBlock(BlockChangeInfo blockChanged) { } else if (size == 1) { Network potentialNet = sideNets.stream().findAny().get(); - if(potentialNet.isOfSameType(component)){ + if (potentialNet.isOfSameType(component)){ potentialNet.addBlock(x, y, z); net = potentialNet; } @@ -147,14 +147,14 @@ else if (size == 1) { Network[] netsArray = sideNets.toArray(new Network[size]); Network main = null; for (Network network : netsArray) { - if(network.isOfSameType(component)){ + if (network.isOfSameType(component)){ main = network; main.addBlock(x, y, z); for (Network otherNet : netsArray) { - if(otherNet == main){ + if (otherNet == main){ continue; } - if(otherNet.isOfSameType(main)){ + if (otherNet.isOfSameType(main)){ main.mergeNetwork(otherNet); nets.remove(otherNet); } @@ -165,7 +165,7 @@ else if (size == 1) { } } - if(net == null && getNet(world, x, y, z) == null) { + if (net == null && getNet(world, x, y, z) == null) { net = new Network(world,component.getType()); net.addBlock(x, y, z); for (Vec3i offset: OFFSETS) { @@ -188,7 +188,7 @@ else if (size == 1) { int pz = z + offset.getZ(); if (canBeNet(world, px, py, pz) && getNet(world, px, py, pz) == null && net != null) { NetworkComponentBlock sideComponent = (NetworkComponentBlock) world.getBlockState(px, py, pz).getBlock(); - if(net.isOfSameType(sideComponent)){ + if (net.isOfSameType(sideComponent)){ net.addBlock(px, py, pz); } } diff --git a/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java b/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java index 71e1744..993a8b5 100644 --- a/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java +++ b/src/main/java/net/teamterminus/machineessentials/network/NetworkWalker.java @@ -46,7 +46,7 @@ public NetworkWalker(World world, Vec3i source, int walkedBlocks, List List createNetworkPaths(World world, Vec3i source) { - if(world.getBlockEntity(source.getX(), source.getY(), source.getZ()) instanceof NetworkComponent) { + if (world.getBlockEntity(source.getX(), source.getY(), source.getZ()) instanceof NetworkComponent) { NetworkWalker walker = new NetworkWalker<>(world, source, 1, new ArrayList<>()); walker.traverse(); return walker.isFailed() ? null : walker.routes; @@ -72,7 +72,7 @@ public void traverse() { * @param max The maximum amount of blocks to traverse */ public void traverse(int max) { - if(used) { + if (used) { throw new IllegalStateException("Walker already used!"); } root = this;