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

Added "Smart-Filling" mode to Cargo Input Nodes

This commit is contained in:
TheBusyBiscuit 2021-03-25 18:39:07 +01:00
parent cc421e7e33
commit f4bded9491
5 changed files with 65 additions and 27 deletions

View File

@ -27,6 +27,7 @@
#### Additions
* Added Vanilla Auto Crafter
* Added Enhanced Auto Crafter
* Added "Smart-Filling" mode to Cargo Input nodes
#### Changes
* Changed item order in guide for the Villager Rune and Nether Goo (All runes are now grouped together)

View File

@ -135,7 +135,7 @@ abstract class AbstractItemNetwork extends Network {
Optional<Block> target = getAttachedBlock(l);
if (target.isPresent()) {
item = CargoUtils.insert(this, inventories, l.getBlock(), target.get(), item);
item = CargoUtils.insert(this, inventories, l.getBlock(), target.get(), false, item);
if (item == null) {
terminal.replaceExistingItem(request.getSlot(), null);
@ -235,7 +235,7 @@ abstract class AbstractItemNetwork extends Network {
if (menu.getItemInSlot(17) != null) {
Optional<Block> target = getAttachedBlock(bus);
target.ifPresent(block -> menu.replaceExistingItem(17, CargoUtils.insert(this, inventories, bus.getBlock(), block, menu.getItemInSlot(17))));
target.ifPresent(block -> menu.replaceExistingItem(17, CargoUtils.insert(this, inventories, bus.getBlock(), block, false, menu.getItemInSlot(17))));
}
if (menu.getItemInSlot(17) == null) {

View File

@ -10,6 +10,7 @@ import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Location;
@ -152,12 +153,15 @@ class CargoNetworkTask implements Runnable {
}
}
@Nullable
@ParametersAreNonnullByDefault
private ItemStack distributeItem(ItemStack stack, Location inputNode, List<Location> outputNodes) {
ItemStack item = stack;
Deque<Location> destinations = new LinkedList<>(outputNodes);
Config cfg = BlockStorage.getLocationInfo(inputNode);
boolean roundrobin = Objects.equals(cfg.getString("round-robin"), "true");
boolean smartFill = Objects.equals(cfg.getString("smart-fill"), "true");
if (roundrobin) {
roundRobinSort(inputNode, destinations);
@ -167,7 +171,7 @@ class CargoNetworkTask implements Runnable {
Optional<Block> target = network.getAttachedBlock(output);
if (target.isPresent()) {
item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), item);
item = CargoUtils.insert(network, inventories, output.getBlock(), target.get(), smartFill, item);
if (item == null) {
break;

View File

@ -252,7 +252,7 @@ final class CargoUtils {
}
@Nullable
static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, ItemStack stack) {
static ItemStack insert(AbstractItemNetwork network, Map<Location, Inventory> inventories, Block node, Block target, boolean smartFill, ItemStack stack) {
if (!matchesFilter(network, node, stack)) {
return stack;
}
@ -264,7 +264,7 @@ final class CargoUtils {
Inventory inventory = inventories.get(target.getLocation());
if (inventory != null) {
return insertIntoVanillaInventory(stack, inventory);
return insertIntoVanillaInventory(stack, smartFill, inventory);
}
BlockState state = PaperLib.getBlockState(target, false).getState();
@ -272,7 +272,7 @@ final class CargoUtils {
if (state instanceof InventoryHolder) {
inventory = ((InventoryHolder) state).getInventory();
inventories.put(target.getLocation(), inventory);
return insertIntoVanillaInventory(stack, inventory);
return insertIntoVanillaInventory(stack, smartFill, inventory);
}
}
@ -292,18 +292,22 @@ final class CargoUtils {
int maxStackSize = itemInSlot.getType().getMaxStackSize();
int currentAmount = itemInSlot.getAmount();
if (currentAmount < maxStackSize && SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
int amount = currentAmount + stack.getAmount();
if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
if (currentAmount < maxStackSize) {
int amount = currentAmount + stack.getAmount();
itemInSlot.setAmount(Math.min(amount, maxStackSize));
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
} else {
stack = null;
itemInSlot.setAmount(Math.min(amount, maxStackSize));
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
} else {
stack = null;
}
menu.replaceExistingItem(slot, itemInSlot);
return stack;
} else if (smartFill) {
return stack;
}
menu.replaceExistingItem(slot, itemInSlot);
return stack;
}
}
@ -311,7 +315,7 @@ final class CargoUtils {
}
@Nullable
private static ItemStack insertIntoVanillaInventory(@Nonnull ItemStack stack, @Nonnull Inventory inv) {
private static ItemStack insertIntoVanillaInventory(@Nonnull ItemStack stack, boolean smartFill, @Nonnull Inventory inv) {
/*
* If the Inventory does not accept this Item Type, bounce the item back.
* Example: Shulker boxes within shulker boxes (fixes #2662)
@ -337,16 +341,20 @@ final class CargoUtils {
} else {
int maxStackSize = itemInSlot.getType().getMaxStackSize();
if (itemInSlot.getAmount() < maxStackSize && SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
int amount = itemInSlot.getAmount() + stack.getAmount();
if (SlimefunUtils.isItemSimilar(itemInSlot, wrapper, true, false)) {
if (itemInSlot.getAmount() < maxStackSize) {
int amount = itemInSlot.getAmount() + stack.getAmount();
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
itemInSlot.setAmount(Math.min(amount, maxStackSize));
if (amount > maxStackSize) {
stack.setAmount(amount - maxStackSize);
itemInSlot.setAmount(Math.min(amount, maxStackSize));
return stack;
} else {
itemInSlot.setAmount(Math.min(amount, maxStackSize));
return null;
}
} else if (smartFill) {
return stack;
} else {
itemInSlot.setAmount(Math.min(amount, maxStackSize));
return null;
}
}
}

View File

@ -1,5 +1,8 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.cargo;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
@ -14,9 +17,12 @@ import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
public class CargoInputNode extends AbstractFilterNode {
private static final int[] BORDER = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 22, 23, 26, 27, 31, 32, 33, 34, 35, 36, 40, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 };
private static final String ROUND_ROBIN_MODE = "round-robin";
private static final int[] BORDER = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 17, 18, 22, 23, 26, 27, 31, 32, 33, 34, 35, 36, 40, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 };
private static final String ROUND_ROBIN_MODE = "round-robin";
private static final String SMART_FILL_MODE = "smart-fill";
@ParametersAreNonnullByDefault
public CargoInputNode(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput) {
super(category, item, recipeType, recipe, recipeOutput);
}
@ -29,7 +35,9 @@ public class CargoInputNode extends AbstractFilterNode {
@Override
protected void onPlace(BlockPlaceEvent e) {
super.onPlace(e);
BlockStorage.addBlockInfo(e.getBlock(), ROUND_ROBIN_MODE, String.valueOf(false));
BlockStorage.addBlockInfo(e.getBlock(), SMART_FILL_MODE, String.valueOf(false));
}
@Override
@ -52,6 +60,23 @@ public class CargoInputNode extends AbstractFilterNode {
return false;
});
}
String smartFillNode = BlockStorage.getLocationInfo(b.getLocation(), SMART_FILL_MODE);
if (!BlockStorage.hasBlockInfo(b) || smartFillNode == null || smartFillNode.equals(String.valueOf(false))) {
menu.replaceExistingItem(16, new CustomItem(Material.WRITABLE_BOOK, "&7\"Smart-Filling\" Mode: &4\u2718", "", "&e> Click to enable \"Smart-Filling\" Mode", "", "&fIn this mode, the Cargo node will attempt", "&fto keep a constant amount of items", "&fin the inventory. This is not perfect", "&fand will still fill in empty slots that", "&fcome before a stack of a configured item."));
menu.addMenuClickHandler(16, (p, slot, item, action) -> {
BlockStorage.addBlockInfo(b, SMART_FILL_MODE, String.valueOf(true));
updateBlockMenu(menu, b);
return false;
});
} else {
menu.replaceExistingItem(16, new CustomItem(Material.WRITTEN_BOOK, "&7\"Smart-Filling\" Mode: &2\u2714", "", "&e> Click to disable \"Smart-Filling\" Mode", "", "&fIn this mode, the Cargo node will attempt", "&fto keep a constant amount of items", "&fin the inventory. This is not perfect", "&fand will still fill in empty slots that", "&fcome before a stack of a configured item."));
menu.addMenuClickHandler(16, (p, slot, item, action) -> {
BlockStorage.addBlockInfo(b, SMART_FILL_MODE, String.valueOf(false));
updateBlockMenu(menu, b);
return false;
});
}
}
}