Skip to content

Commit

Permalink
Fixed hopper transactions not always being logged (fixes #490)
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Jan 25, 2024
1 parent f7fea2b commit af1d440
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
Expand All @@ -30,11 +28,7 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I
}
}

ItemStack[] containerState = null;
if (!ConfigHandler.isPaper) {
containerState = Util.getContainerState(sourceHolder.getInventory().getContents());
}
ItemStack[] sourceContainer = containerState;
ItemStack[] sourceContainer = Util.getContainerState(sourceHolder.getInventory().getContents());
ItemStack movedItem = item.clone();

final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
Expand All @@ -44,48 +38,10 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I
return;
}

boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
int itemHash = Util.getItemStackHashCode(item);
boolean abort = false;

if (ConfigHandler.isPaper) {
for (ItemStack itemStack : sourceHolder.getInventory().getContents()) {
if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) {
if (itemHash != Util.getItemStackHashCode(movedItem) || destinationHolder.getInventory().firstEmpty() == -1 || destinationHolder.getInventory() instanceof BrewerInventory || destinationHolder.getInventory() instanceof FurnaceInventory) {
abort = true;
}

break;
}
}

/*
for (ItemStack itemStack : sourceHolder.getInventory().getContents()) {
if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) {
abort = true;
break;
}
}
if (abort) {
for (ItemStack itemStack : destinationHolder.getInventory().getContents()) {
if (itemStack != null && Util.getItemStackHashCode(itemStack) == Util.getItemStackHashCode(movedItem)) {
if (itemHash == Util.getItemStackHashCode(itemStack) && destinationHolder.getInventory().firstEmpty() > -1) {
abort = false;
}
break;
}
}
}
*/
}
else {
ItemStack[] sourceContents = sourceHolder.getInventory().getContents();
boolean addedInventory = Util.addedContainer(sourceContainer, sourceContents);
if (addedInventory) {
abort = true;
}
boolean addedInventory = Util.canAddContainer(sourceContainer, movedItem, sourceHolder.getInventory().getMaxStackSize());
if (!addedInventory) {
abort = true;
}

if (abort) {
Expand All @@ -104,6 +60,7 @@ static void processHopperPull(Location location, InventoryHolder sourceHolder, I
ConfigHandler.hopperAbort.remove(loggingChestId);
}

boolean hopperTransactions = Config.getConfig(location.getWorld()).HOPPER_TRANSACTIONS;
if (!hopperTransactions) {
List<Object> list = ConfigHandler.transactingChest.get(location.getWorld().getUID().toString() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ());
if (list != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
Expand All @@ -30,11 +28,7 @@ static void processHopperPush(Location location, InventoryHolder sourceHolder, I
}
}

ItemStack[] containerState = null;
if (!ConfigHandler.isPaper) {
containerState = Util.getContainerState(destinationHolder.getInventory().getContents());
}
ItemStack[] destinationContainer = containerState;
ItemStack[] destinationContainer = Util.getContainerState(destinationHolder.getInventory().getContents());
ItemStack movedItem = item.clone();

final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
Expand All @@ -44,26 +38,10 @@ static void processHopperPush(Location location, InventoryHolder sourceHolder, I
return;
}

int itemHash = Util.getItemStackHashCode(item);
boolean abort = false;

if (ConfigHandler.isPaper) {
for (ItemStack itemStack : sourceHolder.getInventory().getContents()) {
if (itemStack != null && Util.getItemStackHashCode(itemStack) == itemHash) {
if (itemHash != Util.getItemStackHashCode(movedItem) || destinationHolder.getInventory().firstEmpty() == -1 || destinationHolder.getInventory() instanceof BrewerInventory || destinationHolder.getInventory() instanceof FurnaceInventory) {
abort = true;
}

break;
}
}
}
else {
ItemStack[] destinationContents = destinationHolder.getInventory().getContents();
boolean addedInventory = Util.addedContainer(destinationContainer, destinationContents);
if (!addedInventory) {
abort = true;
}
boolean addedInventory = Util.canAddContainer(destinationContainer, movedItem, destinationHolder.getInventory().getMaxStackSize());
if (!addedInventory) {
abort = true;
}

if (abort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ else if (inventoryHolder instanceof DoubleChest) {
return false;
}

private static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) {
static void onInventoryInteractAsync(Player player, Inventory inventory, boolean enderChest) {
if (inventory == null) {
return;
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/coreprotect/utility/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,30 @@ public static boolean addedContainer(ItemStack[] oldContainer, ItemStack[] newCo
return false;
}

/* return true if item can be added to container */
public static boolean canAddContainer(ItemStack[] container, ItemStack item, int forceMaxStack) {
for (ItemStack containerItem : container) {
if (containerItem == null || containerItem.getType() == Material.AIR) {
return true;
}

int maxStackSize = containerItem.getMaxStackSize();
if (forceMaxStack > 0 && (forceMaxStack < maxStackSize || maxStackSize == -1)) {
maxStackSize = forceMaxStack;
}

if (maxStackSize == -1) {
maxStackSize = 1;
}

if (containerItem.isSimilar(item) && containerItem.getAmount() < maxStackSize) {
return true;
}
}

return false;
}

public static int getArtId(String name, boolean internal) {
int id = -1;
name = name.toLowerCase(Locale.ROOT).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static List<List<Map<String, Object>>> seralize(ItemStack item, Material
List<Map<String, Object>> list = new ArrayList<>();
List<Object> modifiers = new ArrayList<>();

if (item.hasItemMeta() && item.getItemMeta() != null) {
if (item != null && item.hasItemMeta() && item.getItemMeta() != null) {
ItemMeta itemMeta = item.getItemMeta().clone();

if (itemMeta.hasAttributeModifiers()) {
Expand Down

0 comments on commit af1d440

Please sign in to comment.