From 70f5c2f2cca861e0aa4db4dae113d39c21a0a9e9 Mon Sep 17 00:00:00 2001 From: iTwins Date: Sun, 3 Sep 2023 03:57:41 +0200 Subject: [PATCH 1/6] added network signal insulators --- .../slimefun4/api/network/Network.java | 43 +++++++++++++++++-- .../api/network/NetworkComponent.java | 5 +++ .../core/networks/cargo/CargoNet.java | 1 + .../core/networks/energy/EnergyNet.java | 1 + .../energy/EnergyNetComponentType.java | 6 +++ .../implementation/SlimefunItems.java | 2 + .../items/cargo/CargoInsulator.java | 22 ++++++++++ .../items/electric/EnergyInsulator.java | 42 ++++++++++++++++++ .../setup/SlimefunItemSetup.java | 10 +++++ .../slimefun4/utils/HeadTexture.java | 2 + 10 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java index 49fb36520e..f19d216af3 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/Network.java @@ -57,6 +57,7 @@ public abstract class Network { private final Queue nodeQueue = new ArrayDeque<>(); protected final Set regulatorNodes = new HashSet<>(); protected final Set connectorNodes = new HashSet<>(); + protected final Set insulatorNodes = new HashSet<>(); protected final Set terminusNodes = new HashSet<>(); /** @@ -121,7 +122,7 @@ protected Network(@Nonnull NetworkManager manager, @Nonnull Location regulator) * @return The size of this {@link Network} */ public int getSize() { - return regulatorNodes.size() + connectorNodes.size() + terminusNodes.size(); + return regulatorNodes.size() + connectorNodes.size() + terminusNodes.size() + insulatorNodes.size(); } /** @@ -181,6 +182,8 @@ public boolean connectsTo(@Nonnull Location l) { return NetworkComponent.CONNECTOR; } else if (terminusNodes.contains(l)) { return NetworkComponent.TERMINUS; + } else if (insulatorNodes.contains(l)) { + return NetworkComponent.INSULATOR; } return null; @@ -196,12 +199,15 @@ private void discoverStep() { NetworkComponent classification = classifyLocation(l); if (classification != currentAssignment) { - if (currentAssignment == NetworkComponent.REGULATOR || currentAssignment == NetworkComponent.CONNECTOR) { + if (currentAssignment == NetworkComponent.REGULATOR || currentAssignment == NetworkComponent.CONNECTOR || classification == NetworkComponent.INSULATOR) { // Requires a complete rebuild of the network, so we just throw the current one away. manager.unregisterNetwork(this); return; } else if (currentAssignment == NetworkComponent.TERMINUS) { terminusNodes.remove(l); + } else if (currentAssignment == NetworkComponent.INSULATOR) { + insulatorNodes.remove(l); + updateNeighbors(l); } if (classification == NetworkComponent.REGULATOR) { @@ -226,8 +232,13 @@ private void discoverStep() { } private void discoverNeighbors(@Nonnull Location l, double xDiff, double yDiff, double zDiff) { - for (int i = getRange() + 1; i > 0; i--) { + for (int i = 1; i <= getRange(); i++) { Location newLocation = l.clone().add(i * xDiff, i * yDiff, i * zDiff); + if (classifyLocation(newLocation) == NetworkComponent.INSULATOR) { + positions.add(BlockPosition.getAsLong(newLocation)); + insulatorNodes.add(newLocation); + return; + } addLocationToNetwork(newLocation); } } @@ -241,6 +252,32 @@ private void discoverNeighbors(@Nonnull Location l) { discoverNeighbors(l, 0.0, 0.0, -1.0); } + private void updateNeighbors(@Nonnull Location l, double xDiff, double yDiff, double zDiff) { + for (int i = 1; i <= getRange(); i++) { + Location newLocation = l.clone().add(i * xDiff, i * yDiff, i * zDiff); + NetworkComponent classification = classifyLocation(newLocation); + if (connectsTo(newLocation) && classification == NetworkComponent.CONNECTOR || classification == NetworkComponent.REGULATOR) { + discoverNeighbors(newLocation); + } + } + } + + /** + * Make all the nodes that are connected to this network and + * in range of this location rediscover their neighbors. + * + * @param l + * The location to search around + */ + private void updateNeighbors(@Nonnull Location l) { + updateNeighbors(l, 1.0, 0.0, 0.0); + updateNeighbors(l, -1.0, 0.0, 0.0); + updateNeighbors(l, 0.0, 1.0, 0.0); + updateNeighbors(l, 0.0, -1.0, 0.0); + updateNeighbors(l, 0.0, 0.0, 1.0); + updateNeighbors(l, 0.0, 0.0, -1.0); + } + /** * This method runs the network visualizer which displays a {@link Particle} on * every {@link Location} that this {@link Network} is connected to. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/NetworkComponent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/NetworkComponent.java index a741ceadde..50d801801c 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/NetworkComponent.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/NetworkComponent.java @@ -19,6 +19,11 @@ public enum NetworkComponent { */ CONNECTOR, + /** + * This represents a node that stops {@link NetworkComponent}s from connecting + */ + INSULATOR, + /** * This represents the main component of the {@link Network}. * This node is responsible for managing the {@link Network}. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java index 7ef1da5cdb..d756fc82cf 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java @@ -100,6 +100,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) { case "CARGO_NODE_INPUT", "CARGO_NODE_OUTPUT", "CARGO_NODE_OUTPUT_ADVANCED" -> NetworkComponent.TERMINUS; + case "CARGO_INSULATOR" -> NetworkComponent.INSULATOR; default -> null; }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java index cb7c2c910c..e382ec216d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java @@ -108,6 +108,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) { CAPACITOR -> NetworkComponent.CONNECTOR; case CONSUMER, GENERATOR -> NetworkComponent.TERMINUS; + case INSULATOR -> NetworkComponent.INSULATOR; default -> null; }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNetComponentType.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNetComponentType.java index 8340260dcb..8899e37127 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNetComponentType.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNetComponentType.java @@ -47,6 +47,12 @@ public enum EnergyNetComponentType { */ CONNECTOR, + /** + * An Insulator can be placed between {@link EnergyNetComponent}s + * to stop them from connecting + */ + INSULATOR, + /** * A fallback value to use when a {@link Block} cannot be classified as any of the * other options. diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java index d65a67bd34..7481a0446a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/SlimefunItems.java @@ -767,6 +767,7 @@ private SlimefunItems() {} public static final SlimefunItemStack ENERGY_REGULATOR = new SlimefunItemStack("ENERGY_REGULATOR", HeadTexture.ENERGY_REGULATOR, "&6Energy Regulator", "", "&fCore Component of an Energy Network"); public static final SlimefunItemStack ENERGY_CONNECTOR = new SlimefunItemStack("ENERGY_CONNECTOR", HeadTexture.ENERGY_CONNECTOR, "&eEnergy Connector", LoreBuilder.range(6), "", "&fPlace this between machines", "&fand generators to connect them", "&fto your regulator."); + public static final SlimefunItemStack ENERGY_INSULATOR = new SlimefunItemStack("ENERGY_INSULATOR", HeadTexture.ENERGY_INSULATOR, "&7Energy Insulator", "", "&fPlace this between machines, generators", "&fand connectors to stop", "&fthem from connecting."); public static final SlimefunItemStack DEBUG_FISH = new SlimefunItemStack("DEBUG_FISH", Material.SALMON, "&3How much is the Fish?", "", "&eRight Click &fany Block to view it's BlockData", "&eLeft Click &fto break a Block", "&eShift + Left Click &fany Block to erase it's BlockData", "&eShift + Right Click &fto place a Placeholder Block"); public static final SlimefunItemStack NETHER_ICE = new SlimefunItemStack("NETHER_ICE", HeadTexture.NETHER_ICE, "&eNether Ice", "", LoreBuilder.radioactive(Radioactivity.MODERATE), LoreBuilder.HAZMAT_SUIT_REQUIRED); @@ -776,6 +777,7 @@ private SlimefunItems() {} // Cargo public static final SlimefunItemStack CARGO_MANAGER = new SlimefunItemStack("CARGO_MANAGER", HeadTexture.CARGO_MANAGER, "&6Cargo Manager", "", "&fCore Component of an Item Transport Network"); public static final SlimefunItemStack CARGO_CONNECTOR_NODE = new SlimefunItemStack("CARGO_NODE", HeadTexture.CARGO_CONNECTOR_NODE, "&7Cargo Node &c(Connector)", "", "&fCargo Connector Pipe"); + public static final SlimefunItemStack CARGO_INSULATOR = new SlimefunItemStack("CARGO_INSULATOR", HeadTexture.CARGO_INSULATOR, "&7Cargo Insulator", "", "&fCargo Signal Insulator"); public static final SlimefunItemStack CARGO_INPUT_NODE = new SlimefunItemStack("CARGO_NODE_INPUT", HeadTexture.CARGO_INPUT_NODE, "&7Cargo Node &c(Input)", "", "&fCargo Input Pipe"); public static final SlimefunItemStack CARGO_OUTPUT_NODE = new SlimefunItemStack("CARGO_NODE_OUTPUT", HeadTexture.CARGO_OUTPUT_NODE, "&7Cargo Node &c(Output)", "", "&fCargo Output Pipe"); public static final SlimefunItemStack CARGO_OUTPUT_NODE_2 = new SlimefunItemStack("CARGO_NODE_OUTPUT_ADVANCED", HeadTexture.CARGO_OUTPUT_NODE, "&6Advanced Cargo Node &c(Output)", "", "&fCargo Output Pipe"); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java new file mode 100644 index 0000000000..d96eaade22 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java @@ -0,0 +1,22 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.cargo; + +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; + +/** + * The {@link CargoInsulator} stops {@link CargoNode}s and {@link CargoConnectorNode}s + * from connecting when placed in between them. + * + * @author iTwins + */ +public class CargoInsulator extends SlimefunItem { + + public CargoInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe); + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java new file mode 100644 index 0000000000..3dac28291b --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java @@ -0,0 +1,42 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.electric; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; +import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; +import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNetComponentType; + +/** + * This {@link EnergyNetComponent} is node that stops {@link EnergyNetComponent}s from connecting + * when placed in between them. + * + * @author iTwins + * + * @see EnergyNet + * @see EnergyNetComponent + */ +public class EnergyInsulator extends SlimefunItem implements EnergyNetComponent { + + public EnergyInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe); + } + + @Nonnull + @Override + public EnergyNetComponentType getEnergyComponentType() { + return EnergyNetComponentType.INSULATOR; + } + + @Override + public int getCapacity() { + return 0; + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java index 9ade30e843..21912f9518 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/SlimefunItemSetup.java @@ -74,12 +74,14 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.AdvancedCargoOutputNode; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoConnectorNode; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoInputNode; +import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoInsulator; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoManager; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoOutputNode; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.ReactorAccessPort; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.TrashCan; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.Capacitor; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyConnector; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyInsulator; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyRegulator; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets.JetBoots; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets.Jetpack; @@ -1560,6 +1562,10 @@ public static void setup(@Nonnull Slimefun plugin) { new SlimefunItemStack(SlimefunItems.ENERGY_CONNECTOR, 8)) .register(plugin); + new EnergyInsulator(itemGroups.electricity, SlimefunItems.ENERGY_INSULATOR, RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[] {SlimefunItems.PLASTIC_SHEET, new ItemStack(Material.TERRACOTTA), SlimefunItems.PLASTIC_SHEET, new ItemStack(Material.TERRACOTTA), SlimefunItems.ENERGY_CONNECTOR, new ItemStack(Material.TERRACOTTA), SlimefunItems.PLASTIC_SHEET, new ItemStack(Material.TERRACOTTA), SlimefunItems.PLASTIC_SHEET}) + .register(plugin); + new SlimefunItem(itemGroups.misc, SlimefunItems.DUCT_TAPE, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {SlimefunItems.ALUMINUM_DUST, SlimefunItems.ALUMINUM_DUST, SlimefunItems.ALUMINUM_DUST, new ItemStack(Material.SLIME_BALL), new ItemStack(Material.WHITE_WOOL), new ItemStack(Material.SLIME_BALL), new ItemStack(Material.PAPER), new ItemStack(Material.PAPER), new ItemStack(Material.PAPER)}, new SlimefunItemStack(SlimefunItems.DUCT_TAPE, 2)) @@ -2557,6 +2563,10 @@ public int getCapacity() { new SlimefunItemStack(SlimefunItems.CARGO_CONNECTOR_NODE, 4)) .register(plugin); + new CargoInsulator(itemGroups.cargo, SlimefunItems.CARGO_INSULATOR, RecipeType.ENHANCED_CRAFTING_TABLE, + new ItemStack[] {SlimefunItems.LEAD_INGOT, SlimefunItems.TIN_INGOT, SlimefunItems.LEAD_INGOT, SlimefunItems.TIN_INGOT, SlimefunItems.CARGO_CONNECTOR_NODE, SlimefunItems.TIN_INGOT, SlimefunItems.LEAD_INGOT, SlimefunItems.TIN_INGOT, SlimefunItems.LEAD_INGOT}) + .register(plugin); + new CargoInputNode(itemGroups.cargo, SlimefunItems.CARGO_INPUT_NODE, RecipeType.ENHANCED_CRAFTING_TABLE, new ItemStack[] {null, new ItemStack(Material.HOPPER), null, SlimefunItems.BILLON_INGOT, SlimefunItems.CARGO_CONNECTOR_NODE, SlimefunItems.BILLON_INGOT, null, new ItemStack(Material.HOPPER), null}, new SlimefunItemStack(SlimefunItems.CARGO_INPUT_NODE, 2)) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java index a4caf14774..18e771fda0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java @@ -69,11 +69,13 @@ public enum HeadTexture { ELECTRIC_PRESS("8d5cf92bc79ec19f4106441affff1406a1367010dcafb197dd94cfca1a6de0fc"), ENERGY_REGULATOR("d78f2b7e5e75639ea7fb796c35d364c4df28b4243e66b76277aadcd6261337"), ENERGY_CONNECTOR("1085e098756b995b00241644089c55a8f9acde35b9a37785d5e057a923613b"), + ENERGY_INSULATOR("7f44bee34a0ead8af93260181747e573456abb54385bf234102ef14c4f2216d6"), NETHER_ICE("3ce2dad9baf7eaba7e80d4d0f9fac0aab01a76b12fb71c3d2af2a16fdd4c7383"), ENRICHED_NETHER_ICE("7c818aa13aabc7294838d21caac057e97bd8c89641a0c0f8a55442ff4e27"), NETHER_ICE_COOLANT_CELL("8d3cd412555f897016213e5d6c7431b448b9e5644e1b19ec51b5316f35840e0"), CARGO_MANAGER("e510bc85362a130a6ff9d91ff11d6fa46d7d1912a3431f751558ef3c4d9c2"), CARGO_CONNECTOR_NODE("07b7ef6fd7864865c31c1dc87bed24ab5973579f5c6638fecb8dedeb443ff0"), + CARGO_INSULATOR("ac8962e46ee8ba5517f3f524175391fca35af4ca3c01995a8da2c8898573c50c"), CARGO_INPUT_NODE("16d1c1a69a3de9fec962a77bf3b2e376dd25c873a3d8f14f1dd345dae4c4"), CARGO_OUTPUT_NODE("55b21fd480c1c43bf3b9f842c869bdc3bc5acc2599bf2eb6b8a1c95dce978f"), FILLED_CAN("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"), From d6863c6cc58f420a3b5d4ae5480c74edf350eee2 Mon Sep 17 00:00:00 2001 From: iTwins Date: Sun, 3 Sep 2023 04:10:15 +0200 Subject: [PATCH 2/6] fixed annotations --- .../slimefun4/implementation/items/cargo/CargoInsulator.java | 3 +++ .../implementation/items/electric/EnergyInsulator.java | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java index d96eaade22..2330acbe8f 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java @@ -1,5 +1,7 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.cargo; +import javax.annotation.ParametersAreNonnullByDefault; + import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; @@ -15,6 +17,7 @@ */ public class CargoInsulator extends SlimefunItem { + @ParametersAreNonnullByDefault public CargoInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java index 3dac28291b..7450b1cf56 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java @@ -2,6 +2,7 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; @@ -24,13 +25,13 @@ */ public class EnergyInsulator extends SlimefunItem implements EnergyNetComponent { + @ParametersAreNonnullByDefault public EnergyInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe); } - @Nonnull @Override - public EnergyNetComponentType getEnergyComponentType() { + public @Nonnull EnergyNetComponentType getEnergyComponentType() { return EnergyNetComponentType.INSULATOR; } From 97dee496ebed3699473cb9af7b1c7010077026c8 Mon Sep 17 00:00:00 2001 From: iTwins Date: Thu, 7 Sep 2023 00:09:25 +0200 Subject: [PATCH 3/6] added toggling functionality for insulators --- .../api/network/SignalInsulator.java | 135 ++++++++++++++++++ .../core/networks/cargo/CargoNet.java | 3 +- .../core/networks/energy/EnergyNet.java | 3 +- .../items/cargo/CargoInsulator.java | 4 +- .../items/electric/EnergyInsulator.java | 5 +- 5 files changed, 143 insertions(+), 7 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java new file mode 100644 index 0000000000..7f704206cd --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java @@ -0,0 +1,135 @@ +package io.github.thebusybiscuit.slimefun4.api.network; + +import java.util.HashMap; +import java.util.Optional; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import com.google.common.base.Preconditions; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.inventory.ItemStack; + +import io.github.bakedlibs.dough.common.ChatColors; +import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; +import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; +import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; +import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; +import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler; +import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoInsulator; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyInsulator; + +import me.mrCookieSlime.Slimefun.api.BlockStorage; + +/** + * This class holds the base functionality for toggling + * a network insulator. + * + * @author iTwins + * + * @see Network + * @see EnergyInsulator + * @see CargoInsulator + */ +public abstract class SignalInsulator extends SlimefunItem { + + private static final HashMap enabledStates = new HashMap<>(); + + @ParametersAreNonnullByDefault + public SignalInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(itemGroup, item, recipeType, recipe); + } + + @ParametersAreNonnullByDefault + public SignalInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, @Nullable ItemStack recipeOutput) { + super(itemGroup, item, recipeType, recipe, recipeOutput); + } + + /** + * This method checks {@link BlockStorage} to see whether + * the {@link EnergyInsulator} at the given location is enabled. + * + * @param location + * The {@link Location} to check + * @return + * Whether the {@link EnergyInsulator} at the given location is enabled + * or false if there is no {@link EnergyInsulator}. + */ + public static boolean isEnabled(@Nonnull Location location) { + Preconditions.checkArgument(location != null, "The Location can not be null."); + + return Boolean.parseBoolean(BlockStorage.getLocationInfo(location, "enabled")); + } + + /** + * This method writes the enabled state of this {@link EnergyInsulator} to {@link BlockStorage} + * + * @param location + * The {@link Location} of the {@link SignalInsulator} + * @param enabled + * The boolean value to write + */ + public static void setEnabled(@Nonnull Location location, boolean enabled) { + Preconditions.checkArgument(location != null, "The Location can not be null."); + + BlockStorage.getLocationInfo(location).setValue("enabled", String.valueOf(enabled)); + } + + @Override + public void preRegister() { + addItemHandler(onPlace()); + addItemHandler(onRightClick()); + addItemHandler(onBreak()); + } + + private @Nonnull BlockPlaceHandler onPlace() { + return new BlockPlaceHandler(false) { + @Override + public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { + enabledStates.put(e.getBlock().getLocation(), true); + } + }; + } + + private @Nonnull BlockUseHandler onRightClick() { + return e -> { + Optional optionalBlock = e.getClickedBlock(); + if (optionalBlock.isEmpty()) { + return; + } + + Location location = optionalBlock.get().getLocation(); + boolean newState = !isEnabled(location); + setEnabled(location, newState); + + if (newState) { + e.getPlayer().sendMessage(ChatColors.color("&7Enabled: " + "&2\u2714")); + } else { + e.getPlayer().sendMessage(ChatColors.color("&7Enabled: " + "&4\u2718")); + } + + for (Network network : Slimefun.getNetworkManager().getNetworksFromLocation(location, EnergyNet.class)) { + network.markDirty(location); + } + }; + } + + private @Nonnull BlockBreakHandler onBreak() { + return new SimpleBlockBreakHandler() { + @Override + public void onBlockBreak(@Nonnull Block b) { + enabledStates.remove(b.getLocation()); + } + }; + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java index d756fc82cf..3ede166fc4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/cargo/CargoNet.java @@ -21,6 +21,7 @@ import io.github.thebusybiscuit.slimefun4.api.network.NetworkComponent; import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; +import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoInsulator; import me.mrCookieSlime.Slimefun.api.BlockStorage; @@ -100,7 +101,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) { case "CARGO_NODE_INPUT", "CARGO_NODE_OUTPUT", "CARGO_NODE_OUTPUT_ADVANCED" -> NetworkComponent.TERMINUS; - case "CARGO_INSULATOR" -> NetworkComponent.INSULATOR; + case "CARGO_INSULATOR" -> CargoInsulator.isEnabled(l) ? NetworkComponent.INSULATOR : null; default -> null; }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java index e382ec216d..86c7d4887b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/networks/energy/EnergyNet.java @@ -25,6 +25,7 @@ import io.github.thebusybiscuit.slimefun4.core.attributes.HologramOwner; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems; +import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyInsulator; import io.github.thebusybiscuit.slimefun4.utils.NumberUtils; import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config; @@ -108,7 +109,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) { CAPACITOR -> NetworkComponent.CONNECTOR; case CONSUMER, GENERATOR -> NetworkComponent.TERMINUS; - case INSULATOR -> NetworkComponent.INSULATOR; + case INSULATOR -> EnergyInsulator.isEnabled(l) ? NetworkComponent.INSULATOR : null; default -> null; }; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java index 2330acbe8f..664273d4d4 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/cargo/CargoInsulator.java @@ -5,8 +5,8 @@ import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.network.SignalInsulator; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; /** @@ -15,7 +15,7 @@ * * @author iTwins */ -public class CargoInsulator extends SlimefunItem { +public class CargoInsulator extends SignalInsulator { @ParametersAreNonnullByDefault public CargoInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java index 7450b1cf56..a9bab7b50b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/EnergyInsulator.java @@ -1,14 +1,13 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.electric; import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup; -import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; +import io.github.thebusybiscuit.slimefun4.api.network.SignalInsulator; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; import io.github.thebusybiscuit.slimefun4.core.attributes.EnergyNetComponent; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; @@ -23,7 +22,7 @@ * @see EnergyNet * @see EnergyNetComponent */ -public class EnergyInsulator extends SlimefunItem implements EnergyNetComponent { +public class EnergyInsulator extends SignalInsulator implements EnergyNetComponent { @ParametersAreNonnullByDefault public EnergyInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { From 40e4b3c975eb41a541d4d51227826e87d4f470d0 Mon Sep 17 00:00:00 2001 From: iTwins Date: Thu, 7 Sep 2023 01:10:32 +0200 Subject: [PATCH 4/6] remove enabled state cache --- .../slimefun4/api/network/SignalInsulator.java | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java index 7f704206cd..872a1f043b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java @@ -42,8 +42,6 @@ */ public abstract class SignalInsulator extends SlimefunItem { - private static final HashMap enabledStates = new HashMap<>(); - @ParametersAreNonnullByDefault public SignalInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { super(itemGroup, item, recipeType, recipe); @@ -86,16 +84,14 @@ public static void setEnabled(@Nonnull Location location, boolean enabled) { @Override public void preRegister() { - addItemHandler(onPlace()); - addItemHandler(onRightClick()); - addItemHandler(onBreak()); + addItemHandler(onPlace(), onRightClick()); } private @Nonnull BlockPlaceHandler onPlace() { return new BlockPlaceHandler(false) { @Override public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { - enabledStates.put(e.getBlock().getLocation(), true); + setEnabled(e.getBlock().getLocation(), true); } }; } @@ -123,13 +119,4 @@ public void onPlayerPlace(@Nonnull BlockPlaceEvent e) { }; } - private @Nonnull BlockBreakHandler onBreak() { - return new SimpleBlockBreakHandler() { - @Override - public void onBlockBreak(@Nonnull Block b) { - enabledStates.remove(b.getLocation()); - } - }; - } - } From 0d7afa329d0f94d721e74d2a5a99a91210b400ab Mon Sep 17 00:00:00 2001 From: iTwins Date: Thu, 7 Sep 2023 01:12:23 +0200 Subject: [PATCH 5/6] removed unused imports --- .../thebusybiscuit/slimefun4/api/network/SignalInsulator.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java index 872a1f043b..bd00976c77 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/network/SignalInsulator.java @@ -1,6 +1,5 @@ package io.github.thebusybiscuit.slimefun4.api.network; -import java.util.HashMap; import java.util.Optional; import javax.annotation.Nonnull; @@ -19,12 +18,10 @@ import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem; import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack; import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType; -import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler; import io.github.thebusybiscuit.slimefun4.core.networks.energy.EnergyNet; import io.github.thebusybiscuit.slimefun4.implementation.Slimefun; -import io.github.thebusybiscuit.slimefun4.implementation.handlers.SimpleBlockBreakHandler; import io.github.thebusybiscuit.slimefun4.implementation.items.cargo.CargoInsulator; import io.github.thebusybiscuit.slimefun4.implementation.items.electric.EnergyInsulator; From 4ba1253bbf59ed8c3195019283002d543721b274 Mon Sep 17 00:00:00 2001 From: Jeroen <48769316+iTwins@users.noreply.github.com> Date: Fri, 8 Dec 2023 23:21:23 +0100 Subject: [PATCH 6/6] new texture for the cargo insulator Co-authored-by: JustAHuman-xD <65748158+JustAHuman-xD@users.noreply.github.com> --- .../io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java index 18e771fda0..3c0177da96 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/HeadTexture.java @@ -75,7 +75,7 @@ public enum HeadTexture { NETHER_ICE_COOLANT_CELL("8d3cd412555f897016213e5d6c7431b448b9e5644e1b19ec51b5316f35840e0"), CARGO_MANAGER("e510bc85362a130a6ff9d91ff11d6fa46d7d1912a3431f751558ef3c4d9c2"), CARGO_CONNECTOR_NODE("07b7ef6fd7864865c31c1dc87bed24ab5973579f5c6638fecb8dedeb443ff0"), - CARGO_INSULATOR("ac8962e46ee8ba5517f3f524175391fca35af4ca3c01995a8da2c8898573c50c"), + CARGO_INSULATOR("c1620b325a36677d556d6b7588ae2d340fdc76bc99d77db4268422748e72c093"), CARGO_INPUT_NODE("16d1c1a69a3de9fec962a77bf3b2e376dd25c873a3d8f14f1dd345dae4c4"), CARGO_OUTPUT_NODE("55b21fd480c1c43bf3b9f842c869bdc3bc5acc2599bf2eb6b8a1c95dce978f"), FILLED_CAN("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"),