diff options
author | KHobbits <rob@khobbits.co.uk> | 2014-06-25 19:03:31 +0100 |
---|---|---|
committer | KHobbits <rob@khobbits.co.uk> | 2014-06-25 19:03:31 +0100 |
commit | f6b2524a8c1151948d4a24b27dfae9872a449ec1 (patch) | |
tree | 25fdc8cd65bdb259dc731af3383a9b858107539a | |
parent | 84a03630797dd808df9cf3993e86a915bb6f125c (diff) | |
download | Essentials-f6b2524a8c1151948d4a24b27dfae9872a449ec1.tar Essentials-f6b2524a8c1151948d4a24b27dfae9872a449ec1.tar.gz Essentials-f6b2524a8c1151948d4a24b27dfae9872a449ec1.tar.lz Essentials-f6b2524a8c1151948d4a24b27dfae9872a449ec1.tar.xz Essentials-f6b2524a8c1151948d4a24b27dfae9872a449ec1.zip |
Kit cleanup and refactor.
4 files changed, 104 insertions, 59 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Kit.java b/Essentials/src/com/earth2me/essentials/Kit.java index 3a6b99dae..6152e8396 100644 --- a/Essentials/src/com/earth2me/essentials/Kit.java +++ b/Essentials/src/com/earth2me/essentials/Kit.java @@ -21,6 +21,24 @@ import org.bukkit.inventory.ItemStack; public class Kit { + final IEssentials ess; + final String kitName; + final Map<String, Object> kit; + final Trade charge; + + public Kit(final String kitName, final IEssentials ess) throws Exception + { + this.kitName = kitName; + this.ess = ess; + this.kit = ess.getSettings().getKit(kitName); + this.charge = new Trade("kit-" + kitName, new Trade("kit-kit", ess), ess); + + if (kit == null) + { + throw new Exception(tl("kitNotFound")); + } + } + //TODO: Convert this to use one of the new text classes? public static String listKits(final IEssentials ess, final User user) throws Exception { @@ -43,9 +61,9 @@ public class Kit { cost = tl("kitCost", NumberUtil.displayCurrency(costPrice, ess)); } - final Map<String, Object> kit = ess.getSettings().getKit(kitItem); - if (Kit.getNextUse(user, kitItem, kit) != 0) + Kit kit = new Kit(kitItem, ess); + if (kit.getNextUse(user) != 0) { name = tl("kitDelay", name); } @@ -62,14 +80,26 @@ public class Kit } - public static void checkTime(final User user, final String kitName, final Map<String, Object> els) throws Exception + public String getName() { - final Calendar time = new GregorianCalendar(); - long nextUse = getNextUse(user, kitName, els); + return kitName; + } + + public void checkPerms(final User user) throws Exception + { + if (!user.isAuthorized("essentials.kits." + kitName)) + { + throw new Exception(tl("noKitPermission", "essentials.kits." + kitName)); + } + } + + public void checkDelay(final User user) throws Exception + { + long nextUse = getNextUse(user); if (nextUse == 0L) { - user.setKitTimestamp(kitName, time.getTimeInMillis()); + return; } else if (nextUse < 0L) { @@ -83,7 +113,23 @@ public class Kit } } - public static long getNextUse(final User user, final String kitName, final Map<String, Object> els) throws Exception + public void checkAffordable(final User user) throws Exception + { + charge.isAffordableFor(user); + } + + public void setTime(final User user) throws Exception + { + final Calendar time = new GregorianCalendar(); + user.setKitTimestamp(kitName, time.getTimeInMillis()); + } + + public void chargeUser(final User user) throws Exception + { + charge.charge(user); + } + + public long getNextUse(final User user) throws Exception { if (user.isAuthorized("essentials.kit.exemptdelay")) { @@ -96,7 +142,7 @@ public class Kit try { // Make sure delay is valid - delay = els.containsKey("delay") ? ((Number)els.get("delay")).doubleValue() : 0.0d; + delay = kit.containsKey("delay") ? ((Number)kit.get("delay")).doubleValue() : 0.0d; } catch (Exception e) { @@ -134,7 +180,7 @@ public class Kit } } - public static List<String> getItems(final IEssentials ess, final User user, final String kitName, final Map<String, Object> kit) throws Exception + public List<String> getItems(final User user) throws Exception { if (kit == null) { @@ -166,7 +212,12 @@ public class Kit } } - public static void expandItems(final IEssentials ess, final User user, final List<String> items) throws Exception + public void expandItems(final User user) throws Exception + { + expandItems(user, getItems(user)); + } + + public void expandItems(final User user, final List<String> items) throws Exception { try { @@ -187,11 +238,12 @@ public class Kit final String[] parts = kitItem.split(" +"); final ItemStack parseStack = ess.getItemDb().get(parts[0], parts.length > 1 ? Integer.parseInt(parts[1]) : 1); - - if (parseStack.getType() == Material.AIR) { + + if (parseStack.getType() == Material.AIR) + { continue; } - + final MetaItemStack metaStack = new MetaItemStack(parseStack); if (parts.length > 2) @@ -213,10 +265,12 @@ public class Kit for (ItemStack itemStack : overfilled.values()) { int spillAmount = itemStack.getAmount(); - if (!allowOversizedStacks) { - itemStack.setAmount(spillAmount < itemStack.getMaxStackSize() ? spillAmount : itemStack.getMaxStackSize()); + if (!allowOversizedStacks) + { + itemStack.setAmount(spillAmount < itemStack.getMaxStackSize() ? spillAmount : itemStack.getMaxStackSize()); } - while (spillAmount > 0) { + while (spillAmount > 0) + { user.getWorld().dropItemNaturally(user.getLocation(), itemStack); spillAmount -= itemStack.getAmount(); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java index cb14bd0be..046745dcb 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandkit.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandkit.java @@ -3,12 +3,11 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.CommandSource; import static com.earth2me.essentials.I18n.tl; import com.earth2me.essentials.Kit; -import com.earth2me.essentials.Trade; import com.earth2me.essentials.User; import com.earth2me.essentials.utils.StringUtil; +import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.Map; import org.bukkit.Server; @@ -57,9 +56,8 @@ public class Commandkit extends EssentialsCommand for (final String kitName : kits) { - final Map<String, Object> kit = ess.getSettings().getKit(kitName); - final List<String> items = Kit.getItems(ess, userTo, kitName, kit); - Kit.expandItems(ess, userTo, items); + final Kit kit = new Kit(kitName, ess); + kit.expandItems(userTo); sender.sendMessage(tl("kitGiveTo", kitName, userTo.getDisplayName())); userTo.sendMessage(tl("kitReceive", kitName)); @@ -67,49 +65,42 @@ public class Commandkit extends EssentialsCommand } } - private void giveKits(User userTo, User userFrom, String kitNames) throws Exception + private void giveKits(final User userTo, final User userFrom, final String kitNames) throws Exception { if (kitNames.isEmpty()) { - throw new Exception(tl("kitError2")); + throw new Exception(tl("kitNotFound")); } - String[] kits = kitNames.split(","); + String[] kitList = kitNames.split(","); - for (final String kitName : kits) - { - giveKit(userTo, userFrom, kitName); - } - } + List<Kit> kits = new ArrayList<Kit>(); - private void giveKit(User userTo, User userFrom, String kitName) throws Exception - { - if (kitName.isEmpty()) + for (final String kitName : kitList) { - throw new Exception(tl("kitError2")); - } - - final Map<String, Object> kit = ess.getSettings().getKit(kitName); + if (kitName.isEmpty()) + { + throw new Exception(tl("kitNotFound")); + } - if (!userFrom.isAuthorized("essentials.kits." + kitName)) - { - throw new Exception(tl("noKitPermission", "essentials.kits." + kitName)); + Kit kit = new Kit(kitName, ess); + kit.checkPerms(userFrom); + kit.checkDelay(userFrom); + kit.checkAffordable(userFrom); + kits.add(kit); } - final List<String> items = Kit.getItems(ess, userTo, kitName, kit); - - final Trade charge = new Trade("kit-" + kitName, new Trade("kit-kit", ess), ess); - charge.isAffordableFor(userFrom); - - Kit.checkTime(userFrom, kitName, kit); - Kit.expandItems(ess, userTo, items); + for (final Kit kit : kits) + { + kit.setTime(userFrom); + kit.expandItems(userTo); + kit.chargeUser(userTo); - charge.charge(userFrom); + if (!userFrom.equals(userTo)) + { + userFrom.sendMessage(tl("kitGiveTo", kit.getName(), userTo.getDisplayName())); + } - if (!userFrom.equals(userTo)) - { - userFrom.sendMessage(tl("kitGiveTo", kitName, userTo.getDisplayName())); + userTo.sendMessage(tl("kitReceive", kit.getName())); } - - userTo.sendMessage(tl("kitReceive", kitName)); } } diff --git a/Essentials/src/com/earth2me/essentials/signs/SignKit.java b/Essentials/src/com/earth2me/essentials/signs/SignKit.java index dc181953b..c956ef104 100644 --- a/Essentials/src/com/earth2me/essentials/signs/SignKit.java +++ b/Essentials/src/com/earth2me/essentials/signs/SignKit.java @@ -59,10 +59,11 @@ public class SignKit extends EssentialsSign charge.isAffordableFor(player); try { - final Map<String, Object> kit = ess.getSettings().getKit(kitName); - Kit.checkTime(player, kitName, kit); - final List<String> items = Kit.getItems(ess, player, kitName, kit); - Kit.expandItems(ess, player, items); + final Kit kit = new Kit(kitName, ess); + kit.checkDelay(player); + kit.setTime(player); + kit.expandItems(player); + charge.charge(player); Trade.log("Sign", "Kit", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess); } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index 67ef6c28e..d637014eb 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -122,9 +122,8 @@ public class EssentialsSpawnPlayerListener implements Listener { try { - final Map<String, Object> kit = ess.getSettings().getKit(kitName.toLowerCase(Locale.ENGLISH)); - final List<String> items = Kit.getItems(ess, user, kitName, kit); - Kit.expandItems(ess, user, items); + final Kit kit = new Kit(kitName.toLowerCase(Locale.ENGLISH), ess); + kit.expandItems(user); } catch (Exception ex) { |