From 25e7dbbaf7301fe2e103c51bb7cb0ed31cfd68d0 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 5 Jul 2020 12:39:52 +0200 Subject: [PATCH] Internally rename "variables" to "placeholders" --- .../chestcommands/action/BroadcastAction.java | 2 +- .../action/ChangeServerAction.java | 2 +- .../action/ConsoleCommandAction.java | 2 +- .../chestcommands/action/DragonBarAction.java | 2 +- .../chestcommands/action/OpCommandAction.java | 2 +- .../chestcommands/action/OpenMenuAction.java | 2 +- .../action/PlayerCommandAction.java | 2 +- .../action/SendMessageAction.java | 2 +- .../hook/PlaceholderAPIHook.java | 4 +- .../icon/BaseConfigurableIcon.java | 13 +- .../CachedGetters.java | 72 ++++----- .../Placeholder.java} | 143 +++++++++--------- .../placeholder/PlaceholderManager.java | 39 +++++ .../RelativeString.java | 14 +- .../RelativeStringList.java | 14 +- .../variable/VariableManager.java | 39 ----- Plugin/src/main/resources/menu/example.yml | 2 +- 17 files changed, 180 insertions(+), 176 deletions(-) rename Plugin/src/main/java/me/filoghost/chestcommands/{variable => placeholder}/CachedGetters.java (93%) rename Plugin/src/main/java/me/filoghost/chestcommands/{variable/Variable.java => placeholder/Placeholder.java} (90%) create mode 100644 Plugin/src/main/java/me/filoghost/chestcommands/placeholder/PlaceholderManager.java rename Plugin/src/main/java/me/filoghost/chestcommands/{variable => placeholder}/RelativeString.java (59%) rename Plugin/src/main/java/me/filoghost/chestcommands/{variable => placeholder}/RelativeStringList.java (73%) delete mode 100644 Plugin/src/main/java/me/filoghost/chestcommands/variable/VariableManager.java diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/BroadcastAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/BroadcastAction.java index 8d80646..4ad9bba 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/BroadcastAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/BroadcastAction.java @@ -18,7 +18,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import me.filoghost.chestcommands.util.Colors; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class BroadcastAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/ChangeServerAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/ChangeServerAction.java index faf7b0f..efe9d3f 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/ChangeServerAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/ChangeServerAction.java @@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action; import org.bukkit.entity.Player; import me.filoghost.chestcommands.hook.BungeeCordHook; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class ChangeServerAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/ConsoleCommandAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/ConsoleCommandAction.java index 5535a31..21a7ffa 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/ConsoleCommandAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/ConsoleCommandAction.java @@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class ConsoleCommandAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java index b48d6b3..953e083 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/DragonBarAction.java @@ -22,7 +22,7 @@ import me.filoghost.chestcommands.hook.BarAPIHook; import me.filoghost.chestcommands.parsing.NumberParser; import me.filoghost.chestcommands.util.Colors; import me.filoghost.chestcommands.util.Strings; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class DragonBarAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpCommandAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpCommandAction.java index d67e175..8f60de1 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpCommandAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpCommandAction.java @@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action; import org.bukkit.entity.Player; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class OpCommandAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java index e08f04a..8cb94af 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/OpenMenuAction.java @@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action; import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.menu.InternalIconMenu; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/PlayerCommandAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/PlayerCommandAction.java index ad7b45b..6aa3fc5 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/PlayerCommandAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/PlayerCommandAction.java @@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action; import org.bukkit.entity.Player; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class PlayerCommandAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/action/SendMessageAction.java b/Plugin/src/main/java/me/filoghost/chestcommands/action/SendMessageAction.java index c579e6f..53d6a2b 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/action/SendMessageAction.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/action/SendMessageAction.java @@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action; import org.bukkit.entity.Player; import me.filoghost.chestcommands.util.Colors; -import me.filoghost.chestcommands.variable.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeString; public class SendMessageAction extends Action { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/hook/PlaceholderAPIHook.java b/Plugin/src/main/java/me/filoghost/chestcommands/hook/PlaceholderAPIHook.java index 0a0ead6..1859861 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/hook/PlaceholderAPIHook.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/hook/PlaceholderAPIHook.java @@ -26,10 +26,10 @@ public enum PlaceholderAPIHook implements PluginHook { return PlaceholderAPI.containsPlaceholders(message); } - public static String setPlaceholders(String message, Player executor) { + public static String setPlaceholders(String message, Player viewer) { INSTANCE.checkEnabledState(); - return PlaceholderAPI.setPlaceholders(executor, message); + return PlaceholderAPI.setPlaceholders(viewer, message); } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/icon/BaseConfigurableIcon.java b/Plugin/src/main/java/me/filoghost/chestcommands/icon/BaseConfigurableIcon.java index 565d6b5..979927b 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/icon/BaseConfigurableIcon.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/icon/BaseConfigurableIcon.java @@ -18,8 +18,8 @@ import me.filoghost.chestcommands.api.Icon; import me.filoghost.chestcommands.util.Log; import me.filoghost.chestcommands.util.Preconditions; import me.filoghost.chestcommands.util.collection.CollectionUtils; -import me.filoghost.chestcommands.variable.RelativeString; -import me.filoghost.chestcommands.variable.RelativeStringList; +import me.filoghost.chestcommands.placeholder.RelativeString; +import me.filoghost.chestcommands.placeholder.RelativeStringList; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Color; @@ -57,7 +57,7 @@ public abstract class BaseConfigurableIcon implements Icon { private List bannerPatterns; private boolean placeholdersEnabled; - protected ItemStack cachedRendering; // Cache the rendered item when there are no variables and values haven't changed + protected ItemStack cachedRendering; // Cache the rendered item when possible and if state hasn't changed public BaseConfigurableIcon(Material material) { Preconditions.checkArgumentNotAir(material, "material"); @@ -70,9 +70,9 @@ public abstract class BaseConfigurableIcon implements Icon { return false; } - return (name == null || !name.hasVariables()) - && (lore == null || !lore.hasVariables()) - && (skullOwner == null || !skullOwner.hasVariables()); + return (name == null || !name.hasPlaceholders()) + && (lore == null || !lore.hasPlaceholders()) + && (skullOwner == null || !skullOwner.hasPlaceholders()); } public void setMaterial(Material material) { @@ -324,7 +324,6 @@ public abstract class BaseConfigurableIcon implements Icon { if (shouldCacheRendering()) { - // If there are no variables, cache the item cachedRendering = itemStack; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/variable/CachedGetters.java b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/CachedGetters.java similarity index 93% rename from Plugin/src/main/java/me/filoghost/chestcommands/variable/CachedGetters.java rename to Plugin/src/main/java/me/filoghost/chestcommands/placeholder/CachedGetters.java index 73d1de0..f905120 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/variable/CachedGetters.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/CachedGetters.java @@ -1,36 +1,36 @@ -/* - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package me.filoghost.chestcommands.variable; - -import org.bukkit.Bukkit; - -public class CachedGetters { - - private static long lastOnlinePlayersRefresh; - private static int onlinePlayers; - - - public static int getOnlinePlayers() { - long now = System.currentTimeMillis(); - if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) { - // getOnlinePlayers() could be expensive if called frequently - lastOnlinePlayersRefresh = now; - onlinePlayers = Bukkit.getOnlinePlayers().size(); - } - - return onlinePlayers; - } - -} +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package me.filoghost.chestcommands.placeholder; + +import org.bukkit.Bukkit; + +public class CachedGetters { + + private static long lastOnlinePlayersRefresh; + private static int onlinePlayers; + + + public static int getOnlinePlayers() { + long now = System.currentTimeMillis(); + if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) { + // getOnlinePlayers() could be expensive if called frequently + lastOnlinePlayersRefresh = now; + onlinePlayers = Bukkit.getOnlinePlayers().size(); + } + + return onlinePlayers; + } + +} diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/variable/Variable.java b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/Placeholder.java similarity index 90% rename from Plugin/src/main/java/me/filoghost/chestcommands/variable/Variable.java rename to Plugin/src/main/java/me/filoghost/chestcommands/placeholder/Placeholder.java index a2e09a4..3482a61 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/variable/Variable.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/Placeholder.java @@ -1,69 +1,74 @@ -/* - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package me.filoghost.chestcommands.variable; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - -import me.filoghost.chestcommands.hook.VaultEconomyHook; - -public enum Variable { - - PLAYER("{player}") { - public String getReplacement(Player executor) { - return executor.getName(); - } - }, - - ONLINE("{online}") { - public String getReplacement(Player executor) { - return String.valueOf(CachedGetters.getOnlinePlayers()); - } - }, - - MAX_PLAYERS("{max_players}") { - public String getReplacement(Player executor) { - return String.valueOf(Bukkit.getMaxPlayers()); - } - }, - - MONEY("{money}") { - public String getReplacement(Player executor) { - if (VaultEconomyHook.INSTANCE.isEnabled()) { - return VaultEconomyHook.formatMoney(VaultEconomyHook.getMoney(executor)); - } else { - return "[ECONOMY PLUGIN NOT FOUND]"; - } - } - }, - - WORLD("{world}") { - public String getReplacement(Player executor) { - return executor.getWorld().getName(); - } - }; - - private final String text; - - private Variable(String text) { - this.text = text; - } - - public String getText() { - return text; - } - - public abstract String getReplacement(Player executor); -} +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package me.filoghost.chestcommands.placeholder; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +import me.filoghost.chestcommands.hook.VaultEconomyHook; + +public enum Placeholder { + + PLAYER("{player}") { + @Override + public String getReplacement(Player executor) { + return executor.getName(); + } + }, + + ONLINE("{online}") { + @Override + public String getReplacement(Player executor) { + return String.valueOf(CachedGetters.getOnlinePlayers()); + } + }, + + MAX_PLAYERS("{max_players}") { + @Override + public String getReplacement(Player executor) { + return String.valueOf(Bukkit.getMaxPlayers()); + } + }, + + MONEY("{money}") { + @Override + public String getReplacement(Player executor) { + if (VaultEconomyHook.INSTANCE.isEnabled()) { + return VaultEconomyHook.formatMoney(VaultEconomyHook.getMoney(executor)); + } else { + return "[ECONOMY PLUGIN NOT FOUND]"; + } + } + }, + + WORLD("{world}") { + @Override + public String getReplacement(Player executor) { + return executor.getWorld().getName(); + } + }; + + private final String text; + + Placeholder(String text) { + this.text = text; + } + + public String getText() { + return text; + } + + public abstract String getReplacement(Player executor); +} diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/PlaceholderManager.java b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/PlaceholderManager.java new file mode 100644 index 0000000..4eda536 --- /dev/null +++ b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/PlaceholderManager.java @@ -0,0 +1,39 @@ +package me.filoghost.chestcommands.placeholder; + +import org.bukkit.entity.Player; + +import me.filoghost.chestcommands.hook.PlaceholderAPIHook; + +public class PlaceholderManager { + + public static boolean hasPlaceholders(String message) { + if(message == null) { + return false; + } + for (Placeholder placeholder : Placeholder.values()) { + if (message.contains(placeholder.getText())) { + return true; + } + } + if (PlaceholderAPIHook.INSTANCE.isEnabled() && PlaceholderAPIHook.hasPlaceholders(message)) { + return true; + } + return false; + } + + public static String replacePlaceholders(String message, Player viewer) { + if (message == null) { + return null; + } + for (Placeholder placeholder : Placeholder.values()) { + if (message.contains(placeholder.getText())) { + message = message.replace(placeholder.getText(), placeholder.getReplacement(viewer)); + } + } + if (PlaceholderAPIHook.INSTANCE.isEnabled()) { + message = PlaceholderAPIHook.setPlaceholders(message, viewer); + } + return message; + } + +} diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeString.java b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeString.java similarity index 59% rename from Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeString.java rename to Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeString.java index ba0f59c..a4e754e 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeString.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeString.java @@ -1,11 +1,11 @@ -package me.filoghost.chestcommands.variable; +package me.filoghost.chestcommands.placeholder; import org.bukkit.entity.Player; public class RelativeString { private final String string; - private final boolean hasVariables; + private final boolean hasPlaceholders; public static RelativeString of(String string) { if (string != null) { @@ -17,7 +17,7 @@ public class RelativeString { private RelativeString(String string) { this.string = string; - this.hasVariables = VariableManager.hasVariables(string); + this.hasPlaceholders = PlaceholderManager.hasPlaceholders(string); } public String getRawValue() { @@ -25,15 +25,15 @@ public class RelativeString { } public String getValue(Player player) { - if (hasVariables) { - return VariableManager.setVariables(string, player); + if (hasPlaceholders) { + return PlaceholderManager.replacePlaceholders(string, player); } else { return string; } } - public boolean hasVariables() { - return hasVariables; + public boolean hasPlaceholders() { + return hasPlaceholders; } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeStringList.java b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeStringList.java similarity index 73% rename from Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeStringList.java rename to Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeStringList.java index cd1ae39..14ff906 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/variable/RelativeStringList.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/placeholder/RelativeStringList.java @@ -1,4 +1,4 @@ -package me.filoghost.chestcommands.variable; +package me.filoghost.chestcommands.placeholder; import java.util.List; @@ -12,17 +12,17 @@ public class RelativeStringList { private final ImmutableList originalList; private final List relativeList; - private final boolean hasVariables; + private final boolean hasPlaceholders; public RelativeStringList(List list) { if (list != null) { this.originalList = ImmutableList.copyOf(list); this.relativeList = CollectionUtils.transform(list, RelativeString::of); - this.hasVariables = this.relativeList.stream().anyMatch(RelativeString::hasVariables); + this.hasPlaceholders = this.relativeList.stream().anyMatch(RelativeString::hasPlaceholders); } else { this.originalList = null; this.relativeList = null; - this.hasVariables = false; + this.hasPlaceholders = false; } } @@ -31,15 +31,15 @@ public class RelativeStringList { } public List getValue(Player player) { - if (hasVariables) { + if (hasPlaceholders) { return CollectionUtils.transform(relativeList, element -> element.getValue(player)); } else { return originalList; } } - public boolean hasVariables() { - return hasVariables; + public boolean hasPlaceholders() { + return hasPlaceholders; } } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/variable/VariableManager.java b/Plugin/src/main/java/me/filoghost/chestcommands/variable/VariableManager.java deleted file mode 100644 index 07f75e2..0000000 --- a/Plugin/src/main/java/me/filoghost/chestcommands/variable/VariableManager.java +++ /dev/null @@ -1,39 +0,0 @@ -package me.filoghost.chestcommands.variable; - -import org.bukkit.entity.Player; - -import me.filoghost.chestcommands.hook.PlaceholderAPIHook; - -public class VariableManager { - - public static boolean hasVariables(String message) { - if(message == null) { - return false; - } - for (Variable variable : Variable.values()) { - if (message.contains(variable.getText())) { - return true; - } - } - if (PlaceholderAPIHook.INSTANCE.isEnabled() && PlaceholderAPIHook.hasPlaceholders(message)) { - return true; - } - return false; - } - - public static String setVariables(String message, Player executor) { - if (message == null) { - return null; - } - for (Variable variable : Variable.values()) { - if (message.contains(variable.getText())) { - message = message.replace(variable.getText(), variable.getReplacement(executor)); - } - } - if (PlaceholderAPIHook.INSTANCE.isEnabled()) { - message = PlaceholderAPIHook.setPlaceholders(message, executor); - } - return message; - } - -} diff --git a/Plugin/src/main/resources/menu/example.yml b/Plugin/src/main/resources/menu/example.yml index e12fe95..c075935 100644 --- a/Plugin/src/main/resources/menu/example.yml +++ b/Plugin/src/main/resources/menu/example.yml @@ -21,7 +21,7 @@ menu-settings: # OPTIONAL # How frequently the menu will be refreshed, in seconds. - # Useful if you have variables in icon descriptions. + # Useful if you have placeholders in icon descriptions. auto-refresh: 5 # OPTIONAL