From e38bc14e860c5c50a2fc5d46ae87a0a8f9e0b19f Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 2 Mar 2021 13:54:20 -0800 Subject: [PATCH] Sometimes I hate being dyslexic, fixing illegal stack sizes in smelting.. again --- .../nossr50/listeners/InventoryListener.java | 2 +- .../skills/smelting/SmeltingManager.java | 52 +++++++------------ 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java index c110de94c..c3a0aa838 100644 --- a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java @@ -117,7 +117,7 @@ public class InventoryListener implements Listener { //Profile doesn't exist if(offlineProfile != null) { //Process smelting - offlineProfile.getSmeltingManager().smeltProcessing(event, furnace); + offlineProfile.getSmeltingManager().smeltProcessing(event); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java index d5b203c42..f6b52646b 100644 --- a/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/smelting/SmeltingManager.java @@ -11,7 +11,6 @@ import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.random.RandomChanceUtil; import com.gmail.nossr50.util.skills.RankUtils; import com.gmail.nossr50.util.skills.SkillActivationType; -import org.bukkit.block.Furnace; import org.bukkit.event.inventory.FurnaceBurnEvent; import org.bukkit.event.inventory.FurnaceSmeltEvent; import org.bukkit.inventory.ItemStack; @@ -111,48 +110,37 @@ public class SmeltingManager extends SkillManager { } } - public void smeltProcessing(@NotNull FurnaceSmeltEvent furnaceSmeltEvent, @NotNull Furnace furnace) { - ItemStack sourceItemStack = furnaceSmeltEvent.getSource(); - ItemStack resultItemStack = furnaceSmeltEvent.getResult(); + public void smeltProcessing(@NotNull FurnaceSmeltEvent furnaceSmeltEvent) { + applyXpGain(Smelting.getResourceXp(furnaceSmeltEvent.getSource()), XPGainReason.PVE, XPGainSource.PASSIVE); //Add XP - applyXpGain(Smelting.getResourceXp(sourceItemStack), XPGainReason.PVE, XPGainSource.PASSIVE); //Add XP - - - processDoubleSmelt(furnaceSmeltEvent, resultItemStack, furnace); + processDoubleSmelt(furnaceSmeltEvent); } - private void processDoubleSmelt(@NotNull FurnaceSmeltEvent furnaceSmeltEvent, @NotNull ItemStack resultItemStack, @NotNull Furnace furnace) { - int itemLimit = resultItemStack.getMaxStackSize(); - - //Check for viewers - if(furnace.getInventory().getViewers().size() > 0) { - itemLimit = itemLimit - 1; //Lower max size to prevent exploit, the exploit involves an open window and a stack at 63 which is about to double smelt, instructions to replicate below - /* - Momshroom02/26/2021 - 1) have max (or close to it) smelting. - 2) put more than half a stack of an ore in a furnace and wait for it to smelt. - 3) have your inv full except for one items worth of whatever the furnace product is. (so like a stack of 63 iron and the rest of your inv is full). - 4) shift click on the furnace output to remove only one item-- leaving 63. - 5) let one more item smelt so the furnace appears to have 64 - 6) manually drag (don't shift click) the stack of product out of the furnace- it'll be 65 items - when you drop that and pick it up with only one free space in your inv, it'll look like there's more than one left on the ground, but for me, there was only one, and the "dupe" was visual only-- idk about VK's player. - */ - } - - //TODO: Permission check work around, could store it as NBT on the furnace - //We don't do permission checks because this can be for an offline player and Bukkit has nothing to grab permissions for offline players + private void processDoubleSmelt(@NotNull FurnaceSmeltEvent furnaceSmeltEvent) { + ItemStack resultItemStack = furnaceSmeltEvent.getResult(); + /* + doubleSmeltCondition should be equal to the max + */ //Process double smelt if (Config.getInstance().getDoubleDropsEnabled(PrimarySkillType.SMELTING, resultItemStack.getType()) - && resultItemStack.getAmount() < itemLimit + && canDoubleSmeltItemStack(resultItemStack) //Effectively two less than max stack size && isSecondSmeltSuccessful()) { - ItemStack newResult = resultItemStack.clone(); - newResult.setAmount(Math.min(resultItemStack.getAmount() + 1, itemLimit)); //Don't go over max stack limits - furnaceSmeltEvent.setResult(newResult); + ItemStack doubleSmeltStack = resultItemStack.clone(); //TODO: Necessary? + doubleSmeltStack.setAmount(resultItemStack.getAmount() + 1); //Add one + furnaceSmeltEvent.setResult(doubleSmeltStack); //Set result } } + private boolean canDoubleSmeltItemStack(@NotNull ItemStack itemStack) { + int resultAmount = itemStack.getAmount(); //Amount before double smelt + int itemLimit = itemStack.getMaxStackSize(); + int doubleSmeltCondition = itemLimit - 2; //Don't double smelt if it would cause an illegal stack size + + return resultAmount <= doubleSmeltCondition; + } + public int vanillaXPBoost(int experience) { return experience * getVanillaXpMultiplier(); }