Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
ChestShop-authors#579 Updated for the stock counter to be updated on …
Browse files Browse the repository at this point in the history
…any hopper input
  • Loading branch information
Johanmans10 committed Apr 29, 2024
1 parent 09170f6 commit 3820bba
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ public static int getAmount(ItemStack item, Inventory inventory) {
return 0;
}

if (inventory.getType() == null) {
return Integer.MAX_VALUE;
}

HashMap<Integer, ? extends ItemStack> items = inventory.all(item.getType());
int itemAmount = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

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

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);

event.setCancelled(true);
tempInv.clear();
tempInv.close();
}
}


}
29 changes: 20 additions & 9 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 @@ -109,25 +110,35 @@ public static boolean isShopChest(InventoryHolder holder) {
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);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/Acrobot/ChestShop/Utils/ItemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ public static String getName(ItemStack itemStack, int maxWidth) {
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;
}
}

0 comments on commit 3820bba

Please sign in to comment.