Skip to content
Open
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
49 changes: 47 additions & 2 deletions src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.*;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.projectiles.ProjectileSource;

Expand Down Expand Up @@ -217,12 +218,46 @@ public void run()
if(currentStack != null) return;

ItemStack bestMatchStack = null;
ItemStack bestShulkerStack = null;
int bestMatchSlot = -1;
int bestMatchShulkerSlot = -1;
int bestMatchStackSize = Integer.MAX_VALUE;
boolean bestMatchShulker = false;
ShulkerBox bestShulker = null;
for(int i = 0; i < 36; i++)
{
ItemStack itemInSlot = this.targetInventory.getItem(i);
if(itemInSlot == null) continue;

// check if item is a Shulker Box
if(itemInSlot.getItemMeta() instanceof BlockStateMeta) {
BlockStateMeta im = (BlockStateMeta)itemInSlot.getItemMeta();
if(im.getBlockState() instanceof ShulkerBox) {
ShulkerBox shulker = (ShulkerBox) im.getBlockState();

for(int j = 0; j < shulker.getInventory().getSize(); j++) {
ItemStack itemInShulkerSlot = shulker.getInventory().getItem(j);
if(itemInShulkerSlot == null) continue;
if(itemsAreSimilar(itemInShulkerSlot, this.stackToReplace))
{
int stackSize = itemInShulkerSlot.getAmount();
if(stackSize < bestMatchStackSize)
{
bestMatchStack = itemInShulkerSlot;
bestShulkerStack = itemInSlot;
bestMatchSlot = i;
bestMatchShulkerSlot = j;
bestMatchShulker = true;
bestMatchStackSize = stackSize;
bestShulker = shulker;
}

if(bestMatchStackSize == 1) break;
}
}
}
}

if(itemsAreSimilar(itemInSlot, this.stackToReplace))
{
int stackSize = itemInSlot.getAmount();
Expand All @@ -231,6 +266,7 @@ public void run()
bestMatchStack = itemInSlot;
bestMatchSlot = i;
bestMatchStackSize = stackSize;
bestMatchShulker = false;
}

if(bestMatchStackSize == 1) break;
Expand All @@ -240,7 +276,16 @@ public void run()
if(bestMatchStack == null) return;

this.targetInventory.setItem(this.slotToRefill, bestMatchStack);
this.targetInventory.clear(bestMatchSlot);
if(bestMatchShulker) {
bestShulker.getInventory().clear(bestMatchShulkerSlot);
BlockStateMeta metaShulker = (BlockStateMeta) bestShulkerStack.getItemMeta();
metaShulker.setBlockState(bestShulker);
bestShulkerStack.setItemMeta(metaShulker);
this.targetInventory.setItem(bestMatchSlot,bestShulkerStack);
}
else {
this.targetInventory.clear(bestMatchSlot);
}

PlayerData playerData = PlayerData.FromPlayer(player);
if(!playerData.isGotRestackInfo())
Expand All @@ -250,7 +295,7 @@ public void run()
}
}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onBlockDamage(BlockDamageEvent event)
{
Expand Down