From 3d5c806116cec43acb15eddeea15eb83c0e03e20 Mon Sep 17 00:00:00 2001 From: jameslfc19 Date: Tue, 14 Jul 2020 18:44:04 +0100 Subject: [PATCH] Fixes AutoCrafting Table now uses all inventories for crafting, rather than one at a time! Fixed missing materials. Fixed Autocraft bug when reloading. --- .../chests/v1_14_R1/MaterialChecker_1_14.java | 1 + .../chests/v1_16_R1/MaterialChecker_1_16.java | 1 + .../interfaces/VirtualCraftingHolder.java | 78 ++++++++++--------- .../chests/listeners/StorageListener.java | 27 ++++--- .../minecraft/chests/misc/Utils.java | 4 + .../storage/abstracts/AbstractStorage.java | 4 +- .../autocraft/AutoCraftingStorage.java | 2 +- 7 files changed, 69 insertions(+), 48 deletions(-) diff --git a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/MaterialChecker_1_14.java b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/MaterialChecker_1_14.java index 66cdc3c..b1c5956 100644 --- a/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/MaterialChecker_1_14.java +++ b/ChestsPlusPlus_1_14/src/main/java/com/jamesdpeters/minecraft/chests/v1_14_R1/MaterialChecker_1_14.java @@ -56,6 +56,7 @@ public class MaterialChecker_1_14 extends MaterialChecker { version_1_14_Items.add(Material.CAULDRON); version_1_14_Items.add(Material.BREWING_STAND); version_1_14_Items.add(Material.HOPPER); + version_1_14_Items.add(Material.TORCH); version_1_14_Ignored_Items = new ArrayList<>(); version_1_14_Ignored_Items.addAll(Tag.BEDS.getValues()); diff --git a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/MaterialChecker_1_16.java b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/MaterialChecker_1_16.java index 0c079dc..2ca5079 100644 --- a/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/MaterialChecker_1_16.java +++ b/ChestsPlusPlus_1_16/src/main/java/com/jamesdpeters/minecraft/chests/v1_16_R1/MaterialChecker_1_16.java @@ -32,6 +32,7 @@ public class MaterialChecker_1_16 extends MaterialChecker { materials.add(Material.CRIMSON_FUNGUS); materials.add(Material.SOUL_CAMPFIRE); materials.add(Material.SOUL_LANTERN); + materials.add(Material.SOUL_TORCH); materials.add(Material.CHAIN); ignoredMaterials = new ArrayList<>(); diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java index f57e6df..a47501d 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/interfaces/VirtualCraftingHolder.java @@ -221,41 +221,33 @@ public class VirtualCraftingHolder implements InventoryHolder { output = hopper.getInventory(); } else { output = getInventory(blockBelow); + //If there is no output crafting isn't possible, so skip this location. if(output == null) continue; //If crafting table is powered output into container is possible. if(!block.isBlockPowered()) continue; } - boolean didCraft = false; - if(craftItem(blockAbove,output)) didCraft = true; - if(craftItemIfHopperSource(block.getRelative(BlockFace.NORTH),output)) didCraft = true; - if(craftItemIfHopperSource(block.getRelative(BlockFace.EAST),output)) didCraft = true; - if(craftItemIfHopperSource(block.getRelative(BlockFace.SOUTH),output)) didCraft = true; - if(craftItemIfHopperSource(block.getRelative(BlockFace.WEST),output)) didCraft = true; + List inventories = new ArrayList<>(); + Utils.addIfNotNull(inventories, getInventory(blockAbove)); + Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.NORTH))); + Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.EAST))); + Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.SOUTH))); + Utils.addIfNotNull(inventories, getInventory(block.getRelative(BlockFace.WEST))); + + boolean didCraft = craftItem(inventories,output); //Play sound if crafting occured. - if(didCraft) if(location.getLocation().getWorld() != null) { - location.getLocation().getWorld().playSound(location.getLocation(), Sound.BLOCK_DISPENSER_DISPENSE, 0.25f, 1f); - if(output.getHolder() instanceof VirtualInventoryHolder){ - ((VirtualInventoryHolder) output.getHolder()).getStorage().updateDisplayItem(); + if(didCraft) { + if (location.getLocation().getWorld() != null) { + location.getLocation().getWorld().playSound(location.getLocation(), Sound.BLOCK_DISPENSER_DISPENSE, 0.25f, 1f); + if (output.getHolder() instanceof VirtualInventoryHolder) { + ((VirtualInventoryHolder) output.getHolder()).getStorage().updateDisplayItem(); + } } } } } - private boolean craftItemIfHopperSource(Block sourceBlock, Inventory output){ - if(sourceBlock.getState() instanceof Hopper){ - return craftItem(sourceBlock, output); - } - return false; - } - - private boolean craftItem(Block sourceBlock, Inventory output){ - Inventory source = getInventory(sourceBlock); - if(source == null) return false; - return craftItem(source, output); - } - private Inventory getInventory(Block block){ Inventory inventory = null; if(block.getState() instanceof Container){ @@ -274,22 +266,34 @@ public class VirtualCraftingHolder implements InventoryHolder { return inventory; } - private boolean craftItem(Inventory inputInventory, Inventory output){ - boolean sameInv = inputInventory.equals(output); + private boolean craftItem(List inputs, Inventory output){ + boolean sameInv = false; + Inventory sameInventory = null; + List tempInvs = new ArrayList<>(); + + for (Inventory inv : inputs) { + Inventory tempInv = Utils.copyInventory(inv); + tempInvs.add(tempInv); + if(inv.equals(output)){ + sameInv = true; + sameInventory = tempInv; + } + } - Inventory tempInv = Utils.copyInventory(inputInventory); for(ItemStack[] choices : recipeChoices){ if(choices == null) continue; boolean foundMatch = false; for(ItemStack choice : choices){ - int index = tempInv.first(choice.getType()); - if(index != -1){ - ItemStack item = tempInv.getItem(index); - if(item != null) { - item.setAmount(item.getAmount() - 1); - tempInv.setItem(index, item); - foundMatch = true; - break; + for(Inventory tempInv : tempInvs) { + int index = tempInv.first(choice.getType()); + if (index != -1) { + ItemStack item = tempInv.getItem(index); + if (item != null) { + item.setAmount(item.getAmount() - 1); + tempInv.setItem(index, item); + foundMatch = true; + break; + } } } } @@ -299,12 +303,14 @@ public class VirtualCraftingHolder implements InventoryHolder { //If we reach here there are enough materials so check for space in the Hopper and update inventory. //Check if output and input are the same inventory to avoid duplication. - Inventory tempOutput = sameInv ? tempInv : Utils.copyInventory(output); + Inventory tempOutput = sameInv ? sameInventory : Utils.copyInventory(output); HashMap map = tempOutput.addItem(result.clone()); //If result fits into output copy over the temporary inventories. if(map.isEmpty()){ - moveTempInv(tempInv,inputInventory); + for(int i=0; i filterList(List list, String phrase){ return list.stream().filter(s -> s.contains(phrase)).collect(Collectors.toList()); } + + public static void addIfNotNull(List list, T value){ + if(value != null) list.add(value); + } } diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java index cf09945..8da462a 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/abstracts/AbstractStorage.java @@ -34,6 +34,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; public abstract class AbstractStorage implements ConfigurationSerializable { @@ -79,6 +80,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { locationInfoList = (List) map.get("locationInfo"); locationInfoList.removeAll(Collections.singletonList(null)); locationInfoList.removeIf(locationInfo -> locationInfo.getLocation() == null); + locationInfoList = locationInfoList.stream().distinct().collect(Collectors.toList()); } //Read owners UUID and find the player for that ID. @@ -132,7 +134,7 @@ public abstract class AbstractStorage implements ConfigurationSerializable { } private int startSignChangeTask(){ - return Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, this::updateSign, 1, 1); + return Bukkit.getScheduler().scheduleSyncRepeatingTask(ChestsPlusPlus.PLUGIN, this::updateSign, 1, 5); } private void updateSign(){ diff --git a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java index 28dc7df..baf4a4c 100644 --- a/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java +++ b/ChestsPlusPlus_Main/src/main/java/com/jamesdpeters/minecraft/chests/storage/autocraft/AutoCraftingStorage.java @@ -93,7 +93,7 @@ public class AutoCraftingStorage extends AbstractStorage implements Configuratio @Override protected Inventory initInventory(){ - virtualCraftingHolder = new VirtualCraftingHolder(this); + if(virtualCraftingHolder == null) virtualCraftingHolder = new VirtualCraftingHolder(this); if(recipeSerializable != null) { Recipe recipe = recipeSerializable.getRecipe();