#579 Updated for the stock counter to be updated on any hopper input

This commit is contained in:
Johanmans10 2024-04-30 00:21:27 +02:00
parent 09170f6d51
commit 3820bba66c
5 changed files with 57 additions and 22 deletions

View File

@ -374,8 +374,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>

View File

@ -41,10 +41,6 @@ public class InventoryUtil {
return 0;
}
if (inventory.getType() == null) {
return Integer.MAX_VALUE;
}
HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
int itemAmount = 0;

View File

@ -1,11 +1,20 @@
package com.Acrobot.ChestShop.Listeners.Item;
import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.ItemUtil;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Hopper;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
@ -16,14 +25,25 @@ public class ItemMoveListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public static void onItemMove(InventoryMoveItemEvent event) {
if (event.getSource() == null || getHolder(event.getDestination(), false) instanceof BlockState) {
return;
}
InventoryHolder destinationHolder = getHolder(event.getDestination(), false);
InventoryHolder sourceHolder = getHolder(event.getSource(), false);
if (!ChestShopSign.isShopBlock(getHolder(event.getSource(), false))) {
return;
}
if (!(destinationHolder instanceof BlockState) && ChestShopSign.isShopBlock(sourceHolder)) {
event.setCancelled(true);
} else if (ChestShopSign.isShopBlock(destinationHolder) && sourceHolder instanceof Hopper) {
Block shopBlock = ChestShopSign.getShopBlock(destinationHolder);
Sign connectedSign = uBlock.getConnectedSign(shopBlock);
event.setCancelled(true);
Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9);
tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents()));
tempInv.addItem(event.getItem().clone());
StockCounterModule.updateCounterOnQuantityLine(connectedSign, tempInv);
tempInv.clear();
tempInv.close();
}
}
}

View File

@ -21,6 +21,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -109,25 +110,35 @@ public class ChestShopSign {
return false;
}
if (holder instanceof DoubleChest) {
return isShopChest(((DoubleChest) holder).getLocation().getBlock());
} else if (holder instanceof Chest) {
return isShopChest(((Chest) holder).getBlock());
if (holder instanceof DoubleChest dChest) {
return isShopChest(dChest.getLocation().getBlock());
} else if (holder instanceof Chest chest) {
return isShopChest(chest.getBlock());
} else {
return false;
}
}
public static boolean isShopBlock(InventoryHolder holder) {
if (holder instanceof DoubleChest) {
return isShopBlock(((DoubleChest) holder).getLeftSide())
|| isShopBlock(((DoubleChest) holder).getRightSide());
} else if (holder instanceof BlockState) {
return isShopBlock(((BlockState) holder).getBlock());
if (holder instanceof DoubleChest dChest) {
return isShopBlock(dChest.getLeftSide())
|| isShopBlock(dChest.getRightSide());
} else if (holder instanceof BlockState blockState) {
return isShopBlock(blockState.getBlock());
}
return false;
}
public static Block getShopBlock(InventoryHolder holder) {
if (holder instanceof DoubleChest dChest) {
return Optional.ofNullable(getShopBlock(dChest.getLeftSide()))
.orElse(getShopBlock(dChest.getRightSide()));
} else if (holder instanceof BlockState state) {
return state.getBlock();
}
return null;
}
public static boolean canAccess(Player player, Sign sign) {
return hasPermission(player, Permission.OTHER_NAME_ACCESS, sign);
}

View File

@ -96,4 +96,12 @@ public class ItemUtil {
public static String getSignName(ItemStack itemStack) {
return getName(itemStack, MAXIMUM_SIGN_WIDTH);
}
public static ItemStack[] deepClone(ItemStack[] toClone) {
ItemStack[] cloned = toClone.clone();
for (int i = 0; i < toClone.length; i++) {
cloned[i] = toClone[i].clone();
}
return cloned;
}
}