From bf83c53fa8c63c892d38ee5e77a6ab6fc1ec8f44 Mon Sep 17 00:00:00 2001 From: MattAKAFred Date: Mon, 29 Jun 2020 20:56:35 -0700 Subject: [PATCH 1/4] Auto refill from shulker boxes in inventory Searches shulker inventory and replaces items if they appear in a shulker box Probably wouldn't hurt to have a setting to toggle - outside the scope of this PR --- .../AutomaticInventory/AIEventHandler.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java index 1a317c4..692c713 100644 --- a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java +++ b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java @@ -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; @@ -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; + 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++) { + 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(); @@ -224,6 +259,7 @@ public void run() bestMatchStack = itemInSlot; bestMatchSlot = i; bestMatchStackSize = stackSize; + bestMatchShulker = false; } if(bestMatchStackSize == 1) break; @@ -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()) From 8bcafb727067caa088050979656a7cc3f196092f Mon Sep 17 00:00:00 2001 From: MattAKAFred Date: Wed, 1 Jul 2020 08:33:06 -0700 Subject: [PATCH 2/4] Some whitespace cleanup, for loop adjustment --- .../AutomaticInventory/AIEventHandler.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java index 692c713..2fbf01f 100644 --- a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java +++ b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java @@ -211,24 +211,24 @@ public void run() if(currentStack != null) return; ItemStack bestMatchStack = null; - ItemStack bestShulkerStack = null; + ItemStack bestShulkerStack = null; int bestMatchSlot = -1; - int bestMatchShulkerSlot = -1; + int bestMatchShulkerSlot = -1; int bestMatchStackSize = Integer.MAX_VALUE; - boolean bestMatchShulker = false; - ShulkerBox bestShulker = null; + 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 + + // 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++) { + for(int j = 0; j < shulker.getInventory().getSize(); j++) { ItemStack itemInShulkerSlot = shulker.getInventory().getItem(j); if(itemInShulkerSlot == null) continue; if(itemsAreSimilar(itemInShulkerSlot, this.stackToReplace)) @@ -259,7 +259,7 @@ public void run() bestMatchStack = itemInSlot; bestMatchSlot = i; bestMatchStackSize = stackSize; - bestMatchShulker = false; + bestMatchShulker = false; } if(bestMatchStackSize == 1) break; @@ -288,7 +288,7 @@ public void run() } } } - + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onBlockDamage(BlockDamageEvent event) { From b5b1e7fe9219f8b16d9b43be5922f6350230876f Mon Sep 17 00:00:00 2001 From: MattAKAFred Date: Mon, 29 Jun 2020 20:56:35 -0700 Subject: [PATCH 3/4] Auto refill from shulker boxes in inventory Searches shulker inventory and replaces items if they appear in a shulker box Probably wouldn't hurt to have a setting to toggle - outside the scope of this PR --- .../AutomaticInventory/AIEventHandler.java | 47 ++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java index 7ddeab4..7a6ff0e 100644 --- a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java +++ b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java @@ -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; @@ -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 < 27; 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(); @@ -231,6 +266,7 @@ public void run() bestMatchStack = itemInSlot; bestMatchSlot = i; bestMatchStackSize = stackSize; + bestMatchShulker = false; } if(bestMatchStackSize == 1) break; @@ -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()) From ee6f3347f561944fda60abe2b20664cf21477f18 Mon Sep 17 00:00:00 2001 From: MattAKAFred Date: Wed, 1 Jul 2020 08:33:06 -0700 Subject: [PATCH 4/4] Some whitespace cleanup, for loop adjustment --- .../AutomaticInventory/AIEventHandler.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java index 7a6ff0e..40ef426 100644 --- a/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java +++ b/src/me/ryanhamshire/AutomaticInventory/AIEventHandler.java @@ -218,24 +218,24 @@ public void run() if(currentStack != null) return; ItemStack bestMatchStack = null; - ItemStack bestShulkerStack = null; + ItemStack bestShulkerStack = null; int bestMatchSlot = -1; - int bestMatchShulkerSlot = -1; + int bestMatchShulkerSlot = -1; int bestMatchStackSize = Integer.MAX_VALUE; - boolean bestMatchShulker = false; - ShulkerBox bestShulker = null; + 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 + + // 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++) { + for(int j = 0; j < shulker.getInventory().getSize(); j++) { ItemStack itemInShulkerSlot = shulker.getInventory().getItem(j); if(itemInShulkerSlot == null) continue; if(itemsAreSimilar(itemInShulkerSlot, this.stackToReplace)) @@ -266,7 +266,7 @@ public void run() bestMatchStack = itemInSlot; bestMatchSlot = i; bestMatchStackSize = stackSize; - bestMatchShulker = false; + bestMatchShulker = false; } if(bestMatchStackSize == 1) break; @@ -295,7 +295,7 @@ public void run() } } } - + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) public void onBlockDamage(BlockDamageEvent event) {