From 43a0cc57325ecbbfe490b3b19ba157d9d855ab72 Mon Sep 17 00:00:00 2001 From: Brianna Date: Thu, 30 Apr 2020 14:56:34 -0400 Subject: [PATCH] Redesigned the KitItem processing system. --- .../ultimatekits/gui/AnimatedKitGui.java | 11 +++++-- .../com/songoda/ultimatekits/kit/Kit.java | 31 +++---------------- .../ultimatekits/kit/type/KitContent.java | 3 ++ .../kit/type/KitContentCommand.java | 10 ++++++ .../kit/type/KitContentEconomy.java | 16 ++++++++++ .../ultimatekits/kit/type/KitContentItem.java | 22 +++++++++++++ 6 files changed, 63 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/songoda/ultimatekits/gui/AnimatedKitGui.java b/src/main/java/com/songoda/ultimatekits/gui/AnimatedKitGui.java index f6e5137..331ca1f 100644 --- a/src/main/java/com/songoda/ultimatekits/gui/AnimatedKitGui.java +++ b/src/main/java/com/songoda/ultimatekits/gui/AnimatedKitGui.java @@ -98,15 +98,20 @@ public class AnimatedKitGui extends Gui { // should we try to wrap it up? if (finish) { ItemStack item = getItem(13); + KitItem kitItem = items.stream().filter(i -> i.getItem().isSimilar(item)).findFirst().orElse(null); if(item == null) { done = true; // idk. } else if (item.isSimilar(give)) { if (!done) { done = true; if (!Settings.AUTO_EQUIP_ARMOR_ROULETTE.getBoolean() || !ArmorType.equip(player, give)) { - Map overfilled = player.getInventory().addItem(give); - for (ItemStack item2 : overfilled.values()) { - player.getWorld().dropItemNaturally(player.getLocation(), item2); + + ItemStack processedItem = kitItem.getContent().process(player); + if (processedItem != null) { + Map overfilled = player.getInventory().addItem(give); + for (ItemStack item2 : overfilled.values()) { + player.getWorld().dropItemNaturally(player.getLocation(), item2); + } } } diff --git a/src/main/java/com/songoda/ultimatekits/kit/Kit.java b/src/main/java/com/songoda/ultimatekits/kit/Kit.java index fe82c43..3a68148 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/Kit.java +++ b/src/main/java/com/songoda/ultimatekits/kit/Kit.java @@ -322,37 +322,14 @@ public class Kit { double rand = Math.random() * 100; if (rand < ch || ch == 100) { - if (item.getContent() instanceof KitContentEconomy) { - try { - EconomyManager.deposit(player, ((KitContentEconomy) item.getContent()).getAmount()); - plugin.getLocale().getMessage("event.claim.eco") - .processPlaceholder("amt", Methods.formatEconomy(((KitContentEconomy) item.getContent()).getAmount())) - .sendPrefixedMessage(player); - } catch (NumberFormatException ex) { - ex.printStackTrace(); - } - 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); + if (item.getContent() instanceof KitContentEconomy + || item.getContent() instanceof KitContentCommand) { + item.getContent().process(player); itemGiveAmount--; continue; } - ItemStack parseStack = ((KitContentItem) item.getContent()).getItemStack(); - - if (parseStack.hasItemMeta() && parseStack.getItemMeta().hasLore()) { - ItemMeta meta = parseStack.getItemMeta(); - List newLore = new ArrayList<>(); - for (String str : parseStack.getItemMeta().getLore()) { - str = str.replace("{PLAYER}", player.getName()).replace("", player.getName()); - newLore.add(str); - } - meta.setLore(newLore); - parseStack.setItemMeta(meta); - } + ItemStack parseStack = item.getContent().process(player); if (parseStack.getType() == Material.AIR) continue; diff --git a/src/main/java/com/songoda/ultimatekits/kit/type/KitContent.java b/src/main/java/com/songoda/ultimatekits/kit/type/KitContent.java index 542fa44..4aa1661 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/type/KitContent.java +++ b/src/main/java/com/songoda/ultimatekits/kit/type/KitContent.java @@ -1,5 +1,6 @@ package com.songoda.ultimatekits.kit.type; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; public interface KitContent { @@ -8,4 +9,6 @@ public interface KitContent { ItemStack getItemForDisplay(); + ItemStack process(Player player); + } diff --git a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentCommand.java b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentCommand.java index ff51991..2b5c5e6 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentCommand.java +++ b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentCommand.java @@ -1,7 +1,9 @@ package com.songoda.ultimatekits.kit.type; import com.songoda.ultimatekits.UltimateKits; +import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -40,4 +42,12 @@ public class KitContentCommand implements KitContent { stack.setItemMeta(meta); return stack; } + + @Override + public ItemStack process(Player player) { + String parsed = command; + parsed = parsed.replace("{player}", player.getName()); + Bukkit.dispatchCommand(Bukkit.getConsoleSender(), parsed); + return null; + } } diff --git a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentEconomy.java b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentEconomy.java index a164962..00a9129 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentEconomy.java +++ b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentEconomy.java @@ -1,7 +1,10 @@ package com.songoda.ultimatekits.kit.type; +import com.songoda.core.hooks.EconomyManager; import com.songoda.ultimatekits.UltimateKits; +import com.songoda.ultimatekits.utils.Methods; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -42,4 +45,17 @@ public class KitContentEconomy implements KitContent { parseStack.setItemMeta(meta); return parseStack; } + + @Override + public ItemStack process(Player player) { + try { + EconomyManager.deposit(player, amount); + UltimateKits.getInstance().getLocale().getMessage("event.claim.eco") + .processPlaceholder("amt", Methods.formatEconomy(amount)) + .sendPrefixedMessage(player); + } catch (NumberFormatException ex) { + ex.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java index 39b6853..cea5b62 100644 --- a/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java +++ b/src/main/java/com/songoda/ultimatekits/kit/type/KitContentItem.java @@ -1,7 +1,12 @@ package com.songoda.ultimatekits.kit.type; import com.songoda.ultimatekits.UltimateKits; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; public class KitContentItem implements KitContent { @@ -28,4 +33,21 @@ public class KitContentItem implements KitContent { public ItemStack getItemForDisplay() { return itemStack.clone(); } + + @Override + public ItemStack process(Player player) { + ItemStack parseStack = itemStack; + + if (parseStack.hasItemMeta() && parseStack.getItemMeta().hasLore()) { + ItemMeta meta = parseStack.getItemMeta(); + List newLore = new ArrayList<>(); + for (String str : parseStack.getItemMeta().getLore()) { + str = str.replace("{PLAYER}", player.getName()).replace("", player.getName()); + newLore.add(str); + } + meta.setLore(newLore); + parseStack.setItemMeta(meta); + } + return parseStack; + } }