Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add network signal insulators #3963

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public abstract class Network {
private final Queue<Location> nodeQueue = new ArrayDeque<>();
protected final Set<Location> regulatorNodes = new HashSet<>();
protected final Set<Location> connectorNodes = new HashSet<>();
protected final Set<Location> insulatorNodes = new HashSet<>();
protected final Set<Location> terminusNodes = new HashSet<>();

/**
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
iTwins marked this conversation as resolved.
Show resolved Hide resolved
}

return null;
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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);
Comment on lines +256 to +257
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly confused by this. What is this method supposed to do?

As it is written I don't see it ever being useful to pass anything but one unitary value (with sign) and two zeros, like you do below, therefore the method only updatesNeighbors in one direction.
But then the question arises on whether this format even makes sense, it would be a lot more readable (though not as compact) if it accepted a BlockFace at that point.

Was the idea just to avoid having to "decode" what versor each BlockFace refers to?

Copy link
Contributor Author

@iTwins iTwins Dec 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method checks if any Locations need to be added to the network when an insulator is broken.

It indeed is intended to only check the 6 directions in which a network can connect. The reason I went with this signature is because the method is similar to discoverNeighbors and that used the same way of passing the arguments. If you'd rather have the method take a BlockFace I would be happy to change it. Then perhaps discoverNeighbors should also be changed to take a BlockFace for consistency sake.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my god I had not even noticed the other one. If you could make both accept a blockface that would be awesome

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.github.thebusybiscuit.slimefun4.api.network;

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.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.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 {

@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(), onRightClick());
}

private @Nonnull BlockPlaceHandler onPlace() {
return new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(@Nonnull BlockPlaceEvent e) {
setEnabled(e.getBlock().getLocation(), true);
}
};
}

private @Nonnull BlockUseHandler onRightClick() {
return e -> {
Optional<Block> 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);
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -100,6 +101,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) {
case "CARGO_NODE_INPUT",
"CARGO_NODE_OUTPUT",
"CARGO_NODE_OUTPUT_ADVANCED" -> NetworkComponent.TERMINUS;
case "CARGO_INSULATOR" -> CargoInsulator.isEnabled(l) ? NetworkComponent.INSULATOR : null;
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +109,7 @@ public NetworkComponent classifyLocation(@Nonnull Location l) {
CAPACITOR -> NetworkComponent.CONNECTOR;
case CONSUMER,
GENERATOR -> NetworkComponent.TERMINUS;
case INSULATOR -> EnergyInsulator.isEnabled(l) ? NetworkComponent.INSULATOR : null;
default -> null;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.network.SignalInsulator;
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 SignalInsulator {

@ParametersAreNonnullByDefault
public CargoInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(itemGroup, item, recipeType, recipe);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import org.bukkit.inventory.ItemStack;

import io.github.thebusybiscuit.slimefun4.api.items.ItemGroup;
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;
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 SignalInsulator implements EnergyNetComponent {

@ParametersAreNonnullByDefault
public EnergyInsulator(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(itemGroup, item, recipeType, recipe);
}

@Override
public @Nonnull EnergyNetComponentType getEnergyComponentType() {
return EnergyNetComponentType.INSULATOR;
}

@Override
public int getCapacity() {
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
Loading