Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading