mirror of
https://github.com/JamesPeters98/ChestsPlusPlus.git
synced 2025-01-25 09:41:32 +01:00
Fixes
AutoCrafting Table now uses all inventories for crafting, rather than one at a time! Fixed missing materials. Fixed Autocraft bug when reloading.
This commit is contained in:
parent
455d1392ce
commit
3d5c806116
@ -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());
|
||||
|
@ -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<>();
|
||||
|
@ -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<Inventory> 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<Inventory> inputs, Inventory output){
|
||||
boolean sameInv = false;
|
||||
Inventory sameInventory = null;
|
||||
List<Inventory> 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<tempInvs.size(); i++) {
|
||||
moveTempInv(tempInvs.get(i), inputs.get(i));
|
||||
}
|
||||
if(!sameInv) moveTempInv(tempOutput, output);
|
||||
return true;
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StorageListener implements Listener {
|
||||
@ -121,17 +122,23 @@ public class StorageListener implements Listener {
|
||||
String playerUUID = itemMeta.getPersistentDataContainer().get(Values.playerUUID, PersistentDataType.STRING);
|
||||
String storageID = itemMeta.getPersistentDataContainer().get(Values.storageID, PersistentDataType.STRING);
|
||||
|
||||
BlockFace blockFace = storageType.onStoragePlacedBlockFace(event.getPlayer(),event.getBlockPlaced());
|
||||
Block signSpace = event.getBlockPlaced().getRelative(blockFace);
|
||||
if(signSpace.getType() != Material.AIR){
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if(storageType.hasPermissionToAdd(event.getPlayer())){
|
||||
storageType.createStorageFacing(event.getPlayer(), event.getBlockPlaced(), storageID, blockFace,false);
|
||||
if(playerUUID != null && storageID != null) {
|
||||
OfflinePlayer owner = Bukkit.getOfflinePlayer(UUID.fromString(playerUUID));
|
||||
|
||||
BlockFace blockFace = storageType.onStoragePlacedBlockFace(event.getPlayer(), event.getBlockPlaced());
|
||||
Block signSpace = event.getBlockPlaced().getRelative(blockFace);
|
||||
if (signSpace.getType() != Material.AIR) {
|
||||
event.setCancelled(true);
|
||||
return;
|
||||
}
|
||||
if (storageType.hasPermissionToAdd(event.getPlayer())) {
|
||||
storageType.createStorageFacing(event.getPlayer(), owner, event.getBlockPlaced(), storageID, blockFace, false);
|
||||
// storageType.add(event.getPlayer(), storageID, event.getBlockPlaced().getLocation(), event.getPlayer().)
|
||||
} else {
|
||||
Messages.NO_PERMISSION(event.getPlayer());
|
||||
} else {
|
||||
event.setCancelled(true);
|
||||
Messages.NO_PERMISSION(event.getPlayer());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -185,4 +185,8 @@ public class Utils {
|
||||
public static List<String> filterList(List<String> list, String phrase){
|
||||
return list.stream().filter(s -> s.contains(phrase)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static <T> void addIfNotNull(List<T> list, T value){
|
||||
if(value != null) list.add(value);
|
||||
}
|
||||
}
|
||||
|
@ -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<LocationInfo>) 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(){
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user