From a55aed591b45164f1a71f6298c9a6b569e2182e2 Mon Sep 17 00:00:00 2001 From: Brianna Date: Sun, 20 Oct 2019 11:32:02 -0400 Subject: [PATCH] Fixed some issues with Kit keys. --- .../com/songoda/ultimatekits/key/Key.java | 13 ++++--- .../com/songoda/ultimatekits/kit/Kit.java | 36 ++++++++++--------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/songoda/ultimatekits/key/Key.java b/src/main/java/com/songoda/ultimatekits/key/Key.java index 499d37a..a36b45a 100644 --- a/src/main/java/com/songoda/ultimatekits/key/Key.java +++ b/src/main/java/com/songoda/ultimatekits/key/Key.java @@ -3,7 +3,6 @@ package com.songoda.ultimatekits.key; import com.songoda.core.utils.TextUtils; import com.songoda.ultimatekits.UltimateKits; import com.songoda.ultimatekits.kit.Kit; -import com.songoda.ultimatekits.utils.Methods; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; @@ -19,14 +18,14 @@ public class Key { private String name; // The amount of items this key will give you. -1 is all; - private int amt; + private int amount; // The amount of kit given when the key is used. private int kitAmount; - public Key(String name, int amt, int kitAmount) { + public Key(String name, int amount, int kitAmount) { this.name = name; - this.amt = amt; + this.amount = amount; this.kitAmount = kitAmount; } @@ -58,7 +57,7 @@ public class Key { desc1 = desc1.replace("[", "").replace("]", ""); lore.add(desc1); - if (this.amt == -1) + if (this.amount == -1) lore.add(plugin.getLocale().getMessage("interface.key.description2").getMessage()); else lore.add(plugin.getLocale().getMessage("interface.key.description3").getMessage()); @@ -77,8 +76,8 @@ public class Key { return name; } - public int getAmt() { - return amt; + public int getAmount() { + return amount; } public int getKitAmount() { diff --git a/src/main/java/com/songoda/ultimatekits/kit/Kit.java b/src/main/java/com/songoda/ultimatekits/kit/Kit.java index 94148a6..22d1b98 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/Kit.java +++ b/src/main/java/com/songoda/ultimatekits/kit/Kit.java @@ -115,9 +115,8 @@ public class Kit { plugin.getLocale().getMessage("event.key.success") .processPlaceholder("kit", showableName).sendPrefixedMessage(player); if (player.getInventory().getItemInHand().getAmount() != 1) { - ItemStack is = item; - is.setAmount(is.getAmount() - 1); - player.setItemInHand(is); + item.setAmount(item.getAmount() - 1); + player.setItemInHand(item); } else { player.setItemInHand(null); } @@ -267,19 +266,22 @@ public class Kit { CompatibleSound.ENTITY_PLAYER_LEVELUP.play(player, 0.6F, 15.0F); List innerContents = new ArrayList<>(getContents()); - int amt = innerContents.size(); - int amtToGive = key == null ? amt : key.getAmt(); + int kitSize = innerContents.size(); + // Amount of items from the kit to give to the player. + int itemGiveAmount = key == null ? kitSize : key.getAmount(); + if (itemGiveAmount == -1) itemGiveAmount = kitSize; + if (key != null) itemGiveAmount = itemGiveAmount * key.getKitAmount(); - if (amt != amtToGive || kitAnimation != KitAnimation.NONE) - Collections.shuffle(innerContents); - - return generateRandomItem(innerContents, amtToGive, player, -1); + return generateRandomItem(innerContents, itemGiveAmount, player, -1); } - private boolean generateRandomItem(List innerContents, int amtToGive, Player player, int forceSelect) { + private boolean generateRandomItem(List innerContents, int itemGiveAmount, Player player, int forceSelect) { + if (getContents().size() != itemGiveAmount || kitAnimation != KitAnimation.NONE) + Collections.shuffle(innerContents); + int canChoose = 0; for (KitItem item : innerContents) { - if (amtToGive == 0) break; + if (itemGiveAmount == 0) break; int ch = canChoose++ == forceSelect || item.getChance() == 0 ? 100 : item.getChance(); double rand = Math.random() * 100; if (rand < ch || ch == 100) { @@ -293,13 +295,13 @@ public class Kit { } catch (NumberFormatException ex) { ex.printStackTrace(); } - amtToGive--; + itemGiveAmount--; continue; } else if (item.getContent() instanceof KitContentCommand) { String parsed = ((KitContentCommand) item.getContent()).getCommand(); parsed = parsed.replace("{player}", player.getName()); Bukkit.dispatchCommand(Bukkit.getConsoleSender(), parsed); - amtToGive--; + itemGiveAmount--; continue; } @@ -318,7 +320,7 @@ public class Kit { if (parseStack.getType() == Material.AIR) continue; - amtToGive--; + itemGiveAmount--; if (kitAnimation != KitAnimation.NONE) { // TODO: this is a very bad way to solve this problem. @@ -336,9 +338,9 @@ public class Kit { } } - if (amtToGive != 0 && canChoose != 0 && forceSelect == -1) { - return generateRandomItem(innerContents, amtToGive, player, (int) (Math.random() * canChoose)); - } else if (amtToGive != 0) { + if (itemGiveAmount != 0 && canChoose != 0 && forceSelect == -1) { + return generateRandomItem(innerContents, itemGiveAmount, player, (int) (Math.random() * canChoose)); + } else if (itemGiveAmount != 0) { return false; }