Skip to content
Open
Changes from 1 commit
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
47 changes: 46 additions & 1 deletion 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 @@ -210,12 +211,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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indents look... off. Are you using tabs perhaps?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to change the default in IDE... will fix

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 < 27; j++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be better, clearer, and more future-proof to use a foreach loop instead.

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 @@ -224,6 +259,7 @@ public void run()
bestMatchStack = itemInSlot;
bestMatchSlot = i;
bestMatchStackSize = stackSize;
bestMatchShulker = false;
}

if(bestMatchStackSize == 1) break;
Expand All @@ -233,7 +269,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 Down