diff --git a/API/src/main/java/com/bgsoftware/wildloaders/api/loaders/LoaderData.java b/API/src/main/java/com/bgsoftware/wildloaders/api/loaders/LoaderData.java index 4569755..984d9d2 100644 --- a/API/src/main/java/com/bgsoftware/wildloaders/api/loaders/LoaderData.java +++ b/API/src/main/java/com/bgsoftware/wildloaders/api/loaders/LoaderData.java @@ -24,6 +24,11 @@ public interface LoaderData { */ ItemStack getLoaderItem(); + /** + * Get the drop item of the chunk loader, with a specific time left. + */ + ItemStack getLoaderItem(long timeLeft); + /** * Set the radius of chunks that the chunk loader will load. * If the radius is 0, it means only one chunk is loaded. A radius of 1, will load 3x3 chunks, etc. diff --git a/src/main/java/com/bgsoftware/wildloaders/Locale.java b/src/main/java/com/bgsoftware/wildloaders/Locale.java index b3cd344..fd91665 100644 --- a/src/main/java/com/bgsoftware/wildloaders/Locale.java +++ b/src/main/java/com/bgsoftware/wildloaders/Locale.java @@ -31,6 +31,14 @@ public final class Locale { public static Locale NO_PLACE_PERMISSION = new Locale("NO_PLACE_PERMISSION"); public static Locale PLACED_LOADER = new Locale("PLACED_LOADER"); public static Locale RECEIVE_SUCCESS = new Locale("RECEIVE_SUCCESS"); + public static Locale TIME_PLACEHOLDER_DAYS = new Locale("TIME_PLACEHOLDER_DAYS"); + public static Locale TIME_PLACEHOLDER_DAY = new Locale("TIME_PLACEHOLDER_DAY"); + public static Locale TIME_PLACEHOLDER_HOURS = new Locale("TIME_PLACEHOLDER_HOURS"); + public static Locale TIME_PLACEHOLDER_HOUR = new Locale("TIME_PLACEHOLDER_HOUR"); + public static Locale TIME_PLACEHOLDER_MINUTES = new Locale("TIME_PLACEHOLDER_MINUTES"); + public static Locale TIME_PLACEHOLDER_MINUTE = new Locale("TIME_PLACEHOLDER_MINUTE"); + public static Locale TIME_PLACEHOLDER_SECONDS = new Locale("TIME_PLACEHOLDER_SECONDS"); + public static Locale TIME_PLACEHOLDER_SECOND = new Locale("TIME_PLACEHOLDER_SECOND"); private Locale(String identifier){ localeMap.put(identifier, this); diff --git a/src/main/java/com/bgsoftware/wildloaders/command/commands/CmdGive.java b/src/main/java/com/bgsoftware/wildloaders/command/commands/CmdGive.java index c0c5d54..6db4373 100644 --- a/src/main/java/com/bgsoftware/wildloaders/command/commands/CmdGive.java +++ b/src/main/java/com/bgsoftware/wildloaders/command/commands/CmdGive.java @@ -87,13 +87,9 @@ public final class CmdGive implements ICommand { LoaderData loaderData = optionalLoaderData.get(); - ItemStack itemStack = loaderData.getLoaderItem(); + ItemStack itemStack = args.length == 5 ? loaderData.getLoaderItem(TimeUtils.fromString(args[4])) : loaderData.getLoaderItem(); itemStack.setAmount(amount); - if(args.length == 5){ - itemStack = plugin.getNMSAdapter().setTag(itemStack, "loader-time", TimeUtils.fromString(args[4])); - } - ItemUtils.addItems(target.getInventory(), target.getLocation(), itemStack); Locale.GIVE_SUCCESS.send(sender, amount, loaderData.getName(), target.getName()); diff --git a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java index 1a55f06..55e7589 100644 --- a/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java +++ b/src/main/java/com/bgsoftware/wildloaders/loaders/WChunkLoader.java @@ -111,8 +111,7 @@ public final class WChunkLoader implements ChunkLoader { @Override public ItemStack getLoaderItem() { - ItemStack itemStack = getLoaderData().getLoaderItem(); - return plugin.getNMSAdapter().setTag(itemStack, "loader-time", getTimeLeft()); + return getLoaderData().getLoaderItem(getTimeLeft()); } @Override diff --git a/src/main/java/com/bgsoftware/wildloaders/loaders/WLoaderData.java b/src/main/java/com/bgsoftware/wildloaders/loaders/WLoaderData.java index 36f42a3..2f74c01 100644 --- a/src/main/java/com/bgsoftware/wildloaders/loaders/WLoaderData.java +++ b/src/main/java/com/bgsoftware/wildloaders/loaders/WLoaderData.java @@ -2,7 +2,12 @@ package com.bgsoftware.wildloaders.loaders; import com.bgsoftware.wildloaders.WildLoadersPlugin; import com.bgsoftware.wildloaders.api.loaders.LoaderData; +import com.bgsoftware.wildloaders.utils.TimeUtils; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; public final class WLoaderData implements LoaderData { @@ -40,7 +45,35 @@ public final class WLoaderData implements LoaderData { @Override public ItemStack getLoaderItem() { - return loaderItem.clone(); + return getLoaderItem(getTimeLeft()); + } + + @Override + public ItemStack getLoaderItem(long timeLeft) { + ItemStack itemStack = loaderItem.clone(); + + ItemMeta itemMeta = itemStack.getItemMeta(); + + if(itemMeta != null){ + String formattedTime = TimeUtils.formatTime(timeLeft); + + if(itemMeta.hasDisplayName()) { + itemMeta.setDisplayName(itemMeta.getDisplayName().replace("{}", formattedTime)); + } + + if(itemMeta.hasLore()){ + List lore = new ArrayList<>(itemMeta.getLore().size()); + + for(String line : itemMeta.getLore()) + lore.add(line.replace("{}", formattedTime)); + + itemMeta.setLore(lore); + } + + itemStack.setItemMeta(itemMeta); + } + + return plugin.getNMSAdapter().setTag(itemStack, "loader-time", timeLeft); } @Override diff --git a/src/main/java/com/bgsoftware/wildloaders/utils/TimeUtils.java b/src/main/java/com/bgsoftware/wildloaders/utils/TimeUtils.java index 3d2d168..dd1c829 100644 --- a/src/main/java/com/bgsoftware/wildloaders/utils/TimeUtils.java +++ b/src/main/java/com/bgsoftware/wildloaders/utils/TimeUtils.java @@ -1,5 +1,6 @@ package com.bgsoftware.wildloaders.utils; +import com.bgsoftware.wildloaders.Locale; import com.google.common.collect.Maps; import java.util.Map; @@ -81,21 +82,36 @@ public final class TimeUtils { return timeUnits; } - public static String[] formatTime(long time){ - String[] result = new String[4]; + public static String formatTime(long time){ + StringBuilder stringBuilder = new StringBuilder(); - result[0] = String.valueOf(time / DAYS); - time = time % DAYS; + if(time >= DAYS){ + long days = time / DAYS; + stringBuilder.append(", ").append(days).append(" ") + .append((days == 1 ? Locale.TIME_PLACEHOLDER_DAY : Locale.TIME_PLACEHOLDER_DAYS).getMessage()); + time = time % DAYS; + } - result[1] = String.valueOf(time / HOURS); - time = time % HOURS; + if(time >= HOURS){ + long hours = time / HOURS; + stringBuilder.append(", ").append(hours).append(" ") + .append((hours == 1 ? Locale.TIME_PLACEHOLDER_HOUR : Locale.TIME_PLACEHOLDER_HOURS).getMessage()); + time = time % HOURS; + } - result[2] = String.valueOf(time / MINUTES); - time = time % MINUTES; + if(time >= MINUTES){ + long minutes = time / MINUTES; + stringBuilder.append(", ").append(minutes).append(" ") + .append((minutes == 1 ? Locale.TIME_PLACEHOLDER_MINUTE : Locale.TIME_PLACEHOLDER_MINUTES).getMessage()); + time = time % MINUTES; + } - result[3] = String.valueOf(time); + if(time > 0){ + stringBuilder.append(", ").append(time).append(" ") + .append((time == 1 ? Locale.TIME_PLACEHOLDER_SECOND : Locale.TIME_PLACEHOLDER_SECONDS).getMessage()); + } - return result; + return stringBuilder.substring(2); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 62c0091..1057201 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -37,7 +37,7 @@ chunkloaders: name: '&6Chunk Loader &7(Place to load)' lore: - '&7Place this chunk loader to keep' - - '&7the chunk loaded in the next 24 hours!' + - '&7the chunk loaded in the next {}!' large_loader: time: 86400 type: BEACON @@ -45,4 +45,4 @@ chunkloaders: name: '&6Large Chunk Loader &7(Place to load)' lore: - '&7Place this chunk loader to keep all chunks' - - '&7in a radius of 2 loaded in the next 24 hours!' \ No newline at end of file + - '&7in a radius of 2 loaded in the next {}!' \ No newline at end of file diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index 80da34e..db03515 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -23,4 +23,12 @@ LIST_LOADERS_FOOTER: '' NO_PERMISSION: '&fUnknown command. Type "/help" for help.' NO_PLACE_PERMISSION: '&a&lWildLoaders &7You are lacking the permission to place chunk loaders.' PLACED_LOADER: '&a&lWildLoaders &7Successfully placed a new chunk-loader at {0}.' -RECEIVE_SUCCESS: '&a&lWildLoaders &7You received x{0} {1} from {2}.' \ No newline at end of file +RECEIVE_SUCCESS: '&a&lWildLoaders &7You received x{0} {1} from {2}.' +TIME_PLACEHOLDER_DAYS: 'days' +TIME_PLACEHOLDER_DAY: 'day' +TIME_PLACEHOLDER_HOURS: 'hours' +TIME_PLACEHOLDER_HOUR: 'hour' +TIME_PLACEHOLDER_MINUTES: 'minutes' +TIME_PLACEHOLDER_MINUTE: 'minute' +TIME_PLACEHOLDER_SECONDS: 'seconds' +TIME_PLACEHOLDER_SECOND: 'second' \ No newline at end of file