This commit is contained in:
Johan 2024-05-02 22:58:04 +02:00 committed by GitHub
commit a52e6c3423
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 50 additions and 18 deletions

View File

@ -1,15 +1,11 @@
package com.Acrobot.Breeze.Utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.Acrobot.ChestShop.Configuration.Properties;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.*;
/**
* @author Acrobot
*/

View File

@ -1,11 +1,15 @@
package com.Acrobot.ChestShop.Listeners.Item;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.block.BlockState;
import org.bukkit.block.Hopper;
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.InventoryHolder;
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
@ -16,14 +20,15 @@ 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 (Properties.USE_STOCK_COUNTER && ChestShopSign.isShopBlock(destinationHolder) && sourceHolder instanceof Hopper) {
StockCounterModule.updateCounterOnItemMoveEvent(event.getItem(), destinationHolder);
}
event.setCancelled(true);
}
}

View File

@ -8,8 +8,10 @@ import com.Acrobot.ChestShop.Events.ItemParseEvent;
import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Events.TransactionEvent;
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.Container;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
@ -143,6 +145,20 @@ public class StockCounterModule implements Listener {
sign.update(true);
}
public static void updateCounterOnItemMoveEvent(ItemStack toAdd, InventoryHolder destinationHolder) {
Block shopBlock = ChestShopSign.getShopBlock(destinationHolder);
Sign connectedSign = uBlock.getConnectedSign(shopBlock);
Inventory tempInv = Bukkit.createInventory(null, destinationHolder.getInventory().getSize() + 9);
tempInv.setContents(ItemUtil.deepClone(destinationHolder.getInventory().getContents()));
tempInv.addItem(toAdd.clone());
updateCounterOnQuantityLine(connectedSign, tempInv);
tempInv.clear();
tempInv.close();
}
public static void removeCounterFromQuantityLine(Sign sign) {
int quantity;
try {

View File

@ -11,16 +11,13 @@ import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Bukkit;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Sign;
import org.bukkit.block.*;
import org.bukkit.entity.Player;
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;
@ -128,6 +125,16 @@ public class ChestShopSign {
return false;
}
public static Block getShopBlock(InventoryHolder holder) {
if (holder instanceof DoubleChest) {
return Optional.ofNullable(getShopBlock(((DoubleChest) holder).getLeftSide()))
.orElse(getShopBlock(((DoubleChest) holder).getRightSide()));
} else if (holder instanceof BlockState) {
return ((BlockState) holder).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;
}
}