Skip to content

Commit

Permalink
#579 Updated for the stock counter to be updated on any hopper input (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Johanmans10 authored May 14, 2024
1 parent 7dbc1ad commit af02eb3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Listeners.Modules;

import com.Acrobot.Breeze.Utils.InventoryUtil;
import com.Acrobot.Breeze.Utils.MaterialUtil;
import com.Acrobot.Breeze.Utils.QuantityUtil;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
Expand All @@ -10,6 +11,7 @@
import com.Acrobot.ChestShop.Signs.ChestShopSign;
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;
Expand Down Expand Up @@ -124,7 +126,13 @@ public static void onTransaction(final TransactionEvent event) {
}
}

public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInventory) {
/**
* Update the stock counter on the sign's quantity line
* @param sign The sign to update
* @param chestShopInventory The inventory to search in
* @param extraItems The extra items to add in the search
*/
public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInventory, ItemStack... extraItems) {
ItemStack itemTradedByShop = determineItemTradedByShop(sign);
if (itemTradedByShop == null) {
return;
Expand All @@ -139,10 +147,25 @@ public static void updateCounterOnQuantityLine(Sign sign, Inventory chestShopInv

int numTradedItemsInChest = InventoryUtil.getAmount(itemTradedByShop, chestShopInventory);

for (ItemStack extraStack : extraItems) {
if (!MaterialUtil.equals(extraStack, itemTradedByShop)) {
continue;
}

numTradedItemsInChest += extraStack.getAmount();
}

sign.setLine(QUANTITY_LINE, String.format(PRICE_LINE_WITH_COUNT, quantity, numTradedItemsInChest));
sign.update(true);
}

public static void updateCounterOnItemMoveEvent(ItemStack toAdd, InventoryHolder destinationHolder) {
Block shopBlock = ChestShopSign.getShopBlock(destinationHolder);
Sign connectedSign = uBlock.getConnectedSign(shopBlock);

updateCounterOnQuantityLine(connectedSign, destinationHolder.getInventory(), toAdd);
}

public static void removeCounterFromQuantityLine(Sign sign) {
int quantity;
try {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.bukkit.inventory.InventoryHolder;

import java.util.Locale;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -128,6 +129,16 @@ public static boolean isShopBlock(InventoryHolder holder) {
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);
}
Expand Down

0 comments on commit af02eb3

Please sign in to comment.