mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2024-09-27 14:13:05 +02:00
#579 Updated for the stock counter to be updated on any hopper input
This commit is contained in:
parent
09170f6d51
commit
3820bba66c
4
pom.xml
4
pom.xml
@ -374,8 +374,8 @@
|
|||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>17</source>
|
||||||
<target>1.8</target>
|
<target>17</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
@ -41,10 +41,6 @@ public class InventoryUtil {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (inventory.getType() == null) {
|
|
||||||
return Integer.MAX_VALUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
|
HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
|
||||||
int itemAmount = 0;
|
int itemAmount = 0;
|
||||||
|
|
||||||
|
@ -1,11 +1,20 @@
|
|||||||
package com.Acrobot.ChestShop.Listeners.Item;
|
package com.Acrobot.ChestShop.Listeners.Item;
|
||||||
|
|
||||||
|
import com.Acrobot.ChestShop.Listeners.Modules.StockCounterModule;
|
||||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
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.BlockState;
|
||||||
|
import org.bukkit.block.Hopper;
|
||||||
|
import org.bukkit.block.Sign;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.inventory.InventoryMoveItemEvent;
|
import org.bukkit.event.inventory.InventoryMoveItemEvent;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.InventoryHolder;
|
||||||
|
|
||||||
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
|
import static com.Acrobot.Breeze.Utils.ImplementationAdapter.getHolder;
|
||||||
|
|
||||||
@ -16,14 +25,25 @@ public class ItemMoveListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
|
||||||
public static void onItemMove(InventoryMoveItemEvent event) {
|
public static void onItemMove(InventoryMoveItemEvent event) {
|
||||||
if (event.getSource() == null || getHolder(event.getDestination(), false) instanceof BlockState) {
|
InventoryHolder destinationHolder = getHolder(event.getDestination(), false);
|
||||||
return;
|
InventoryHolder sourceHolder = getHolder(event.getSource(), false);
|
||||||
}
|
|
||||||
|
|
||||||
if (!ChestShopSign.isShopBlock(getHolder(event.getSource(), false))) {
|
if (!(destinationHolder instanceof BlockState) && ChestShopSign.isShopBlock(sourceHolder)) {
|
||||||
return;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import org.bukkit.inventory.Inventory;
|
|||||||
import org.bukkit.inventory.InventoryHolder;
|
import org.bukkit.inventory.InventoryHolder;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -109,25 +110,35 @@ public class ChestShopSign {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (holder instanceof DoubleChest) {
|
if (holder instanceof DoubleChest dChest) {
|
||||||
return isShopChest(((DoubleChest) holder).getLocation().getBlock());
|
return isShopChest(dChest.getLocation().getBlock());
|
||||||
} else if (holder instanceof Chest) {
|
} else if (holder instanceof Chest chest) {
|
||||||
return isShopChest(((Chest) holder).getBlock());
|
return isShopChest(chest.getBlock());
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isShopBlock(InventoryHolder holder) {
|
public static boolean isShopBlock(InventoryHolder holder) {
|
||||||
if (holder instanceof DoubleChest) {
|
if (holder instanceof DoubleChest dChest) {
|
||||||
return isShopBlock(((DoubleChest) holder).getLeftSide())
|
return isShopBlock(dChest.getLeftSide())
|
||||||
|| isShopBlock(((DoubleChest) holder).getRightSide());
|
|| isShopBlock(dChest.getRightSide());
|
||||||
} else if (holder instanceof BlockState) {
|
} else if (holder instanceof BlockState blockState) {
|
||||||
return isShopBlock(((BlockState) holder).getBlock());
|
return isShopBlock(blockState.getBlock());
|
||||||
}
|
}
|
||||||
return false;
|
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) {
|
public static boolean canAccess(Player player, Sign sign) {
|
||||||
return hasPermission(player, Permission.OTHER_NAME_ACCESS, sign);
|
return hasPermission(player, Permission.OTHER_NAME_ACCESS, sign);
|
||||||
}
|
}
|
||||||
|
@ -96,4 +96,12 @@ public class ItemUtil {
|
|||||||
public static String getSignName(ItemStack itemStack) {
|
public static String getSignName(ItemStack itemStack) {
|
||||||
return getName(itemStack, MAXIMUM_SIGN_WIDTH);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user