diff --git a/src/main/java/com/songoda/epicanchors/anchor/Anchor.java b/src/main/java/com/songoda/epicanchors/anchor/Anchor.java index 98814a3..489bbaf 100644 --- a/src/main/java/com/songoda/epicanchors/anchor/Anchor.java +++ b/src/main/java/com/songoda/epicanchors/anchor/Anchor.java @@ -11,6 +11,7 @@ public class Anchor { private Location location; private int ticksLeft; + private boolean isInfinite; private final int chunkX; private final int chunkZ; @@ -20,6 +21,7 @@ public class Anchor { this.chunkX = location.getBlockX() >> 4; this.chunkZ = location.getBlockZ() >> 4; this.ticksLeft = ticksLeft; + this.isInfinite = (ticksLeft == -99); } public void addTime(String type, Player player) { @@ -96,4 +98,12 @@ public class Anchor { public void setTicksLeft(int ticksLeft) { this.ticksLeft = ticksLeft; } + + public boolean isInfinite() { + return isInfinite; + } + + public void setInfinite(boolean infinite) { + isInfinite = infinite; + } } diff --git a/src/main/java/com/songoda/epicanchors/commands/CommandGive.java b/src/main/java/com/songoda/epicanchors/commands/CommandGive.java index c089023..1535d8b 100644 --- a/src/main/java/com/songoda/epicanchors/commands/CommandGive.java +++ b/src/main/java/com/songoda/epicanchors/commands/CommandGive.java @@ -2,6 +2,7 @@ package com.songoda.epicanchors.commands; import com.songoda.core.commands.AbstractCommand; import com.songoda.epicanchors.EpicAnchors; +import com.songoda.epicanchors.utils.Methods; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -31,16 +32,19 @@ public class CommandGive extends AbstractCommand { return ReturnType.SYNTAX_ERROR; } - try { - Integer.parseInt(args[1]); - } catch (Exception e) { + + + ItemStack itemStack; + + if (Methods.isInt(args[1])) { + itemStack = (Integer.parseInt(args[1]) <= 0) ? instance.makeAnchorItem(-99) : instance.makeAnchorItem(Integer.parseInt(args[1]) * 20 * 60 * 60); + } else if (args[1].toLowerCase().equals("infinite")) { + itemStack = instance.makeAnchorItem(-99); + } else { instance.getLocale().newMessage("&cYou can only use whole numbers...").sendPrefixedMessage(sender); return ReturnType.FAILURE; } - - ItemStack itemStack = instance.makeAnchorItem(Integer.parseInt(args[1]) * 20 * 60 * 60); - if (target != null) { target.getInventory().addItem(itemStack); instance.getLocale().getMessage("command.give.success").sendPrefixedMessage(target); @@ -50,6 +54,7 @@ public class CommandGive extends AbstractCommand { instance.getLocale().getMessage("command.give.success").sendPrefixedMessage(player); } } + return ReturnType.SUCCESS; } @@ -75,7 +80,7 @@ public class CommandGive extends AbstractCommand { @Override public String getSyntax() { - return "/ea give "; + return "/ea give "; } @Override diff --git a/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java b/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java index c05c4bd..33babdf 100644 --- a/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java +++ b/src/main/java/com/songoda/epicanchors/gui/GUIOverview.java @@ -49,22 +49,22 @@ public class GUIOverview extends Gui { setItem(13, GuiUtils.createButtonItem(plugin.makeAnchorItem(anchor.getTicksLeft()), plugin.getLocale().getMessage("interface.anchor.smalltitle").getMessage(), - ChatColor.GRAY + Methods.makeReadable((long) (anchor.getTicksLeft() / 20) * 1000) + " remaining.")); + (anchor.isInfinite()) ? ChatColor.GRAY + "Infinite" : ChatColor.GRAY + Methods.makeReadable((long) (anchor.getTicksLeft() / 20) * 1000) + " remaining.")); if (EconomyManager.isEnabled() && Settings.ADD_TIME_WITH_ECONOMY.getBoolean()) { setButton(15, GuiUtils.createButtonItem(Settings.ECO_ICON.getMaterial(CompatibleMaterial.SUNFLOWER), plugin.getLocale().getMessage("interface.button.addtimewitheconomy").getMessage(), plugin.getLocale().getMessage("interface.button.addtimewitheconomylore") - .processPlaceholder("cost", Methods.formatEconomy(Settings.ECONOMY_COST.getDouble())).getMessage()), // EconomyManager.formatEconomy adds its own prefix/suffix - event -> anchor.addTime("ECO", event.player)); + .processPlaceholder("cost", Methods.formatEconomy(Settings.ECONOMY_COST.getDouble())).getMessage()), // EconomyManager.formatEconomy adds its own prefix/suffix + event -> checkInfiniteAndAlert(anchor, event.player, true)); } if (Settings.ADD_TIME_WITH_XP.getBoolean()) { setButton(11, GuiUtils.createButtonItem(Settings.XP_ICON.getMaterial(CompatibleMaterial.EXPERIENCE_BOTTLE), plugin.getLocale().getMessage("interface.button.addtimewithxp").getMessage(), plugin.getLocale().getMessage("interface.button.addtimewithxplore") - .processPlaceholder("cost", String.valueOf(Settings.XP_COST.getInt())).getMessage()), - event -> anchor.addTime("XP", event.player)); + .processPlaceholder("cost", String.valueOf(Settings.XP_COST.getInt())).getMessage()), + event -> checkInfiniteAndAlert(anchor, event.player, false)); } } @@ -72,7 +72,19 @@ public class GUIOverview extends Gui { private void runTask() { task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { updateItem(13, plugin.getLocale().getMessage("interface.anchor.smalltitle").getMessage(), - ChatColor.GRAY + Methods.makeReadable((long) (anchor.getTicksLeft() / 20) * 1000) + " remaining."); + (anchor.isInfinite()) ? ChatColor.GRAY + "Infinite" : ChatColor.GRAY + Methods.makeReadable((long) (anchor.getTicksLeft() / 20) * 1000) + " remaining."); }, 5L, 5L); } + + private void checkInfiniteAndAlert(Anchor anchor, Player p, boolean eco) { + if (anchor.isInfinite()) { + plugin.getLocale().getMessage("interface.button.infinite").sendPrefixedMessage(p); + } else { + if (eco) { + anchor.addTime("ECO", p); + } else { + anchor.addTime("XP", p); + } + } + } } diff --git a/src/main/java/com/songoda/epicanchors/listeners/BlockListeners.java b/src/main/java/com/songoda/epicanchors/listeners/BlockListeners.java index 4cbe436..dbfa78a 100644 --- a/src/main/java/com/songoda/epicanchors/listeners/BlockListeners.java +++ b/src/main/java/com/songoda/epicanchors/listeners/BlockListeners.java @@ -29,8 +29,12 @@ public class BlockListeners implements Listener { || plugin.getTicksFromItem(item) == 0) return; Anchor anchor = new Anchor(event.getBlock().getLocation(), plugin.getTicksFromItem(item)); - plugin.getAnchorManager().addAnchor(event.getBlock().getLocation(), anchor); + if (plugin.getTicksFromItem(item) == -99) { + anchor.setInfinite(true); + } + + plugin.getAnchorManager().addAnchor(event.getBlock().getLocation(), anchor); plugin.updateHologram(anchor); } diff --git a/src/main/java/com/songoda/epicanchors/tasks/AnchorTask.java b/src/main/java/com/songoda/epicanchors/tasks/AnchorTask.java index a357241..5209833 100644 --- a/src/main/java/com/songoda/epicanchors/tasks/AnchorTask.java +++ b/src/main/java/com/songoda/epicanchors/tasks/AnchorTask.java @@ -110,9 +110,12 @@ public class AnchorTask extends BukkitRunnable { } int ticksLeft = anchor.getTicksLeft(); - anchor.setTicksLeft(ticksLeft - 3); - if (ticksLeft <= 0) { + if (!anchor.isInfinite()) { + anchor.setTicksLeft(ticksLeft - 3); + } + + if (ticksLeft <= 0 && !anchor.isInfinite()) { anchor.bust(); chunk.unload(); return; diff --git a/src/main/java/com/songoda/epicanchors/utils/Methods.java b/src/main/java/com/songoda/epicanchors/utils/Methods.java index a19ecfc..be9a55b 100644 --- a/src/main/java/com/songoda/epicanchors/utils/Methods.java +++ b/src/main/java/com/songoda/epicanchors/utils/Methods.java @@ -19,7 +19,7 @@ public class Methods { String remaining = Methods.makeReadable((ticks2 / 20L) * 1000L); - String name = Settings.NAMETAG.getString().replace("{REMAINING}", remaining); + String name = Settings.NAMETAG.getString().replace("{REMAINING}", (ticks2 <= 0) ? "Infinite" : remaining); String info = ""; if (full) { @@ -156,6 +156,19 @@ public class Methods { return 0; } + public static boolean isInt(String number) { + if (number != null && !number.equals("")) { + try { + Integer.parseInt(number); + return true; + } catch (NumberFormatException var2) { + return false; + } + } else { + return false; + } + } + /** * Formats the specified double into the Economy format specified in the Arconix config. * diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index 717de32..0a08cdb 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -14,6 +14,7 @@ interface: addtimewithxplore: '&7Cost: &a%cost% Levels' addtimewitheconomy: '&aAdd 30 Minutes with ECO' addtimewitheconomylore: '&7Cost: &a$%cost%' + infinite: '&cCannot upgrade an infinite anchor!' anchor: title: ChunkAnchor smalltitle: '&eChunkAnchor'