Added placeholders for time left for loaders

This commit is contained in:
OmerBenGera 2020-10-28 19:24:01 +02:00
parent f7633b3759
commit 0765174e5a
8 changed files with 86 additions and 21 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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());

View File

@ -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

View File

@ -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<String> 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

View File

@ -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);
}
}

View File

@ -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!'
- '&7in a radius of 2 loaded in the next {}!'

View File

@ -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}.'
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'