1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added a config option to delete excess cargo network items

This commit is contained in:
TheBusyBiscuit 2020-11-15 16:29:45 +01:00
parent adc36fe7bd
commit 0cdd21f6bb
7 changed files with 66 additions and 14 deletions

View File

@ -30,6 +30,7 @@
* (API) Added PlayerPreResearchEvent
* Added a config option to disable network visualizations
* (API) Added CoolerFeedPlayerEvent
* Added a config option to delete excess cargo network items
#### Changes
* Removed 1.13 support

View File

@ -14,6 +14,7 @@ import org.bukkit.Server;
import io.github.thebusybiscuit.cscorelib2.config.Config;
import io.github.thebusybiscuit.slimefun4.api.network.Network;
import io.github.thebusybiscuit.slimefun4.core.networks.cargo.CargoNet;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.NetworkListener;
/**
@ -30,6 +31,7 @@ public class NetworkManager {
private final int maxNodes;
private final boolean enableVisualizer;
private final boolean deleteExcessItems;
private final List<Network> networks = new LinkedList<>();
/**
@ -37,14 +39,29 @@ public class NetworkManager {
*
* @param maxStepSize
* The maximum amount of nodes a {@link Network} can have
* @param enableVisualizer
* Whether the {@link Network} visualizer is enabled
* @param deleteExcessItems
* Whether excess items from a {@link CargoNet} should be voided
*/
public NetworkManager(int maxStepSize, boolean enableVisualizer) {
public NetworkManager(int maxStepSize, boolean enableVisualizer, boolean deleteExcessItems) {
Validate.isTrue(maxStepSize > 0, "The maximal Network size must be above zero!");
this.enableVisualizer = enableVisualizer;
this.deleteExcessItems = deleteExcessItems;
maxNodes = maxStepSize;
}
/**
* This creates a new {@link NetworkManager} with the given capacity.
*
* @param maxStepSize
* The maximum amount of nodes a {@link Network} can have
*/
public NetworkManager(int maxStepSize) {
this(maxStepSize, true, false);
}
/**
* This method returns the limit of nodes a {@link Network} can have.
* This value is read from the {@link Config} file.
@ -64,6 +81,16 @@ public class NetworkManager {
return enableVisualizer;
}
/**
* This returns whether excess items from a {@link CargoNet} should be voided
* instead of being dropped to the ground.
*
* @return Whether to delete excess items
*/
public boolean isItemDeletionEnabled() {
return deleteExcessItems;
}
/**
* This returns a {@link List} of every {@link Network} on the {@link Server}.
*

View File

@ -15,6 +15,7 @@ import org.bukkit.block.Block;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.core.networks.NetworkManager;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
@ -37,6 +38,7 @@ import me.mrCookieSlime.Slimefun.api.inventory.DirtyChestMenu;
*/
class CargoNetworkTask implements Runnable {
private final NetworkManager manager;
private final CargoNet network;
private final Map<Location, Inventory> inventories = new HashMap<>();
@ -49,6 +51,7 @@ class CargoNetworkTask implements Runnable {
@ParametersAreNonnullByDefault
CargoNetworkTask(CargoNet network, Map<Location, Integer> inputs, Map<Integer, List<Location>> outputs, Set<Location> chestTerminalInputs, Set<Location> chestTerminalOutputs) {
this.network = network;
this.manager = SlimefunPlugin.getNetworkManager();
this.inputs = inputs;
this.outputs = outputs;
@ -115,7 +118,7 @@ class CargoNetworkTask implements Runnable {
// Try to add the item into another available slot then
ItemStack rest = inv.addItem(stack).get(0);
if (rest != null) {
if (rest != null && !manager.isItemDeletionEnabled()) {
// If the item still couldn't be inserted, simply drop it on the ground
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), rest);
}
@ -126,7 +129,7 @@ class CargoNetworkTask implements Runnable {
if (menu != null) {
if (menu.getItemInSlot(previousSlot) == null) {
menu.replaceExistingItem(previousSlot, stack);
} else {
} else if (!manager.isItemDeletionEnabled()) {
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), stack);
}
}

View File

@ -218,7 +218,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
networkSize = 1;
}
networkManager = new NetworkManager(networkSize, config.getBoolean("networks.enable-visualizer"));
networkManager = new NetworkManager(networkSize, config.getBoolean("networks.enable-visualizer"), config.getBoolean("networks.delete-excess-items"));
// Setting up bStats
new Thread(metricsService::start, "Slimefun Metrics").start();
@ -305,7 +305,7 @@ public final class SlimefunPlugin extends JavaPlugin implements SlimefunAddon {
private void onUnitTestStart() {
local = new LocalizationService(this, "", null);
gpsNetwork = new GPSNetwork();
networkManager = new NetworkManager(200, true);
networkManager = new NetworkManager(200);
command.register();
registry.load(config);
}

View File

@ -38,6 +38,7 @@ networks:
max-size: 200
cargo-ticker-delay: 0
enable-visualizer: true
delete-excess-items: false
items:
talismans: true

View File

@ -25,7 +25,7 @@ class TestNetworkListener {
private static SlimefunPlugin plugin;
private static NetworkListener listener;
private static NetworkManager manager = new NetworkManager(80, false);
private static NetworkManager manager = new NetworkManager(80);
private static ServerMock server;
@BeforeAll

View File

@ -11,6 +11,8 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import be.seeseemelk.mockbukkit.MockBukkit;
@ -37,23 +39,41 @@ class TestNetworkManager {
@Test
@DisplayName("Test illegal network size arguments")
void testIllegalNetworkSize() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new NetworkManager(-100, false));
Assertions.assertThrows(IllegalArgumentException.class, () -> new NetworkManager(0, false));
Assertions.assertThrows(IllegalArgumentException.class, () -> new NetworkManager(-100));
Assertions.assertThrows(IllegalArgumentException.class, () -> new NetworkManager(0));
}
@Test
@DisplayName("Test maximum network size")
void testGetMaxNetworkSize() {
int size = 50;
NetworkManager manager = new NetworkManager(size, false);
NetworkManager manager = new NetworkManager(size);
Assertions.assertEquals(size, manager.getMaxSize());
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
@DisplayName("Test visualizer setting")
void testVisualizerSetting(boolean enabled) {
NetworkManager manager = new NetworkManager(200, enabled, false);
Assertions.assertEquals(enabled, manager.isVisualizerEnabled());
}
@ParameterizedTest
@ValueSource(booleans = { true, false })
@DisplayName("Test item deletion setting")
void testItemDeletionSetting(boolean enabled) {
NetworkManager manager = new NetworkManager(200, true, enabled);
Assertions.assertEquals(enabled, manager.isItemDeletionEnabled());
}
@Test
@DisplayName("Test network list")
void testGetNetworkList() {
NetworkManager manager = new NetworkManager(10, false);
NetworkManager manager = new NetworkManager(10);
World world = server.addSimpleWorld("Simple Network World");
Location loc = new Location(world, 0, 100, 0);
@ -82,7 +102,7 @@ class TestNetworkManager {
@Test
@DisplayName("Test getting a network at a location")
void testGetNetworkAtLocation() {
NetworkManager manager = new NetworkManager(10, false);
NetworkManager manager = new NetworkManager(10);
World world = server.addSimpleWorld("Simple Network World");
Location loc = new Location(world, 0, 100, 0);
Location loc2 = new Location(world, 0, 200, 0);
@ -104,7 +124,7 @@ class TestNetworkManager {
@Test
@DisplayName("Test getting all networks at a location")
void testGetNetworksAtLocation() {
NetworkManager manager = new NetworkManager(10, false);
NetworkManager manager = new NetworkManager(10);
World world = server.addSimpleWorld("Simple Network World");
Location loc = new Location(world, 0, 100, 0);
Location loc2 = new Location(world, 0, 200, 0);
@ -120,7 +140,7 @@ class TestNetworkManager {
@Test
@DisplayName("Test a single node network")
void testSingleNodeNetwork() {
NetworkManager manager = new NetworkManager(1, false);
NetworkManager manager = new NetworkManager(1);
World world = server.addSimpleWorld("Simple Network World");
Location loc = new Location(world, 0, 100, 0);
@ -134,7 +154,7 @@ class TestNetworkManager {
@Test
@DisplayName("Test networks connecting via corners")
void testCornerConnection() {
NetworkManager manager = new NetworkManager(100, false);
NetworkManager manager = new NetworkManager(100);
World world = server.addSimpleWorld("Simple Network World");
Map<Location, NetworkComponent> map = new HashMap<>();