Internally rename "variables" to "placeholders"

This commit is contained in:
filoghost 2020-07-05 12:39:52 +02:00
parent 035b445418
commit 25e7dbbaf7
17 changed files with 180 additions and 176 deletions

View File

@ -18,7 +18,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.util.Colors; import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class BroadcastAction extends Action { public class BroadcastAction extends Action {

View File

@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.hook.BungeeCordHook; import me.filoghost.chestcommands.hook.BungeeCordHook;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class ChangeServerAction extends Action { public class ChangeServerAction extends Action {

View File

@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class ConsoleCommandAction extends Action { public class ConsoleCommandAction extends Action {

View File

@ -22,7 +22,7 @@ import me.filoghost.chestcommands.hook.BarAPIHook;
import me.filoghost.chestcommands.parsing.NumberParser; import me.filoghost.chestcommands.parsing.NumberParser;
import me.filoghost.chestcommands.util.Colors; import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.util.Strings; import me.filoghost.chestcommands.util.Strings;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class DragonBarAction extends Action { public class DragonBarAction extends Action {

View File

@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class OpCommandAction extends Action { public class OpCommandAction extends Action {

View File

@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action;
import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.ChestCommands;
import me.filoghost.chestcommands.menu.InternalIconMenu; 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.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;

View File

@ -16,7 +16,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class PlayerCommandAction extends Action { public class PlayerCommandAction extends Action {

View File

@ -17,7 +17,7 @@ package me.filoghost.chestcommands.action;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.util.Colors; import me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
public class SendMessageAction extends Action { public class SendMessageAction extends Action {

View File

@ -26,10 +26,10 @@ public enum PlaceholderAPIHook implements PluginHook {
return PlaceholderAPI.containsPlaceholders(message); return PlaceholderAPI.containsPlaceholders(message);
} }
public static String setPlaceholders(String message, Player executor) { public static String setPlaceholders(String message, Player viewer) {
INSTANCE.checkEnabledState(); INSTANCE.checkEnabledState();
return PlaceholderAPI.setPlaceholders(executor, message); return PlaceholderAPI.setPlaceholders(viewer, message);
} }
} }

View File

@ -18,8 +18,8 @@ import me.filoghost.chestcommands.api.Icon;
import me.filoghost.chestcommands.util.Log; import me.filoghost.chestcommands.util.Log;
import me.filoghost.chestcommands.util.Preconditions; import me.filoghost.chestcommands.util.Preconditions;
import me.filoghost.chestcommands.util.collection.CollectionUtils; import me.filoghost.chestcommands.util.collection.CollectionUtils;
import me.filoghost.chestcommands.variable.RelativeString; import me.filoghost.chestcommands.placeholder.RelativeString;
import me.filoghost.chestcommands.variable.RelativeStringList; import me.filoghost.chestcommands.placeholder.RelativeStringList;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Color; import org.bukkit.Color;
@ -57,7 +57,7 @@ public abstract class BaseConfigurableIcon implements Icon {
private List<Pattern> bannerPatterns; private List<Pattern> bannerPatterns;
private boolean placeholdersEnabled; 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) { public BaseConfigurableIcon(Material material) {
Preconditions.checkArgumentNotAir(material, "material"); Preconditions.checkArgumentNotAir(material, "material");
@ -70,9 +70,9 @@ public abstract class BaseConfigurableIcon implements Icon {
return false; return false;
} }
return (name == null || !name.hasVariables()) return (name == null || !name.hasPlaceholders())
&& (lore == null || !lore.hasVariables()) && (lore == null || !lore.hasPlaceholders())
&& (skullOwner == null || !skullOwner.hasVariables()); && (skullOwner == null || !skullOwner.hasPlaceholders());
} }
public void setMaterial(Material material) { public void setMaterial(Material material) {
@ -324,7 +324,6 @@ public abstract class BaseConfigurableIcon implements Icon {
if (shouldCacheRendering()) { if (shouldCacheRendering()) {
// If there are no variables, cache the item
cachedRendering = itemStack; cachedRendering = itemStack;
} }

View File

@ -1,36 +1,36 @@
/* /*
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package me.filoghost.chestcommands.variable; package me.filoghost.chestcommands.placeholder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class CachedGetters { public class CachedGetters {
private static long lastOnlinePlayersRefresh; private static long lastOnlinePlayersRefresh;
private static int onlinePlayers; private static int onlinePlayers;
public static int getOnlinePlayers() { public static int getOnlinePlayers() {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) { if (lastOnlinePlayersRefresh == 0 || now - lastOnlinePlayersRefresh > 1000) {
// getOnlinePlayers() could be expensive if called frequently // getOnlinePlayers() could be expensive if called frequently
lastOnlinePlayersRefresh = now; lastOnlinePlayersRefresh = now;
onlinePlayers = Bukkit.getOnlinePlayers().size(); onlinePlayers = Bukkit.getOnlinePlayers().size();
} }
return onlinePlayers; return onlinePlayers;
} }
} }

View File

@ -1,69 +1,74 @@
/* /*
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package me.filoghost.chestcommands.variable; package me.filoghost.chestcommands.placeholder;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import me.filoghost.chestcommands.hook.VaultEconomyHook; import me.filoghost.chestcommands.hook.VaultEconomyHook;
public enum Variable { public enum Placeholder {
PLAYER("{player}") { PLAYER("{player}") {
public String getReplacement(Player executor) { @Override
return executor.getName(); public String getReplacement(Player executor) {
} return executor.getName();
}, }
},
ONLINE("{online}") {
public String getReplacement(Player executor) { ONLINE("{online}") {
return String.valueOf(CachedGetters.getOnlinePlayers()); @Override
} public String getReplacement(Player executor) {
}, return String.valueOf(CachedGetters.getOnlinePlayers());
}
MAX_PLAYERS("{max_players}") { },
public String getReplacement(Player executor) {
return String.valueOf(Bukkit.getMaxPlayers()); MAX_PLAYERS("{max_players}") {
} @Override
}, 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)); MONEY("{money}") {
} else { @Override
return "[ECONOMY PLUGIN NOT FOUND]"; 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(); },
}
}; WORLD("{world}") {
@Override
private final String text; public String getReplacement(Player executor) {
return executor.getWorld().getName();
private Variable(String text) { }
this.text = text; };
}
private final String text;
public String getText() {
return text; Placeholder(String text) {
} this.text = text;
}
public abstract String getReplacement(Player executor);
} public String getText() {
return text;
}
public abstract String getReplacement(Player executor);
}

View File

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

View File

@ -1,11 +1,11 @@
package me.filoghost.chestcommands.variable; package me.filoghost.chestcommands.placeholder;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class RelativeString { public class RelativeString {
private final String string; private final String string;
private final boolean hasVariables; private final boolean hasPlaceholders;
public static RelativeString of(String string) { public static RelativeString of(String string) {
if (string != null) { if (string != null) {
@ -17,7 +17,7 @@ public class RelativeString {
private RelativeString(String string) { private RelativeString(String string) {
this.string = string; this.string = string;
this.hasVariables = VariableManager.hasVariables(string); this.hasPlaceholders = PlaceholderManager.hasPlaceholders(string);
} }
public String getRawValue() { public String getRawValue() {
@ -25,15 +25,15 @@ public class RelativeString {
} }
public String getValue(Player player) { public String getValue(Player player) {
if (hasVariables) { if (hasPlaceholders) {
return VariableManager.setVariables(string, player); return PlaceholderManager.replacePlaceholders(string, player);
} else { } else {
return string; return string;
} }
} }
public boolean hasVariables() { public boolean hasPlaceholders() {
return hasVariables; return hasPlaceholders;
} }
} }

View File

@ -1,4 +1,4 @@
package me.filoghost.chestcommands.variable; package me.filoghost.chestcommands.placeholder;
import java.util.List; import java.util.List;
@ -12,17 +12,17 @@ public class RelativeStringList {
private final ImmutableList<String> originalList; private final ImmutableList<String> originalList;
private final List<RelativeString> relativeList; private final List<RelativeString> relativeList;
private final boolean hasVariables; private final boolean hasPlaceholders;
public RelativeStringList(List<String> list) { public RelativeStringList(List<String> list) {
if (list != null) { if (list != null) {
this.originalList = ImmutableList.copyOf(list); this.originalList = ImmutableList.copyOf(list);
this.relativeList = CollectionUtils.transform(list, RelativeString::of); this.relativeList = CollectionUtils.transform(list, RelativeString::of);
this.hasVariables = this.relativeList.stream().anyMatch(RelativeString::hasVariables); this.hasPlaceholders = this.relativeList.stream().anyMatch(RelativeString::hasPlaceholders);
} else { } else {
this.originalList = null; this.originalList = null;
this.relativeList = null; this.relativeList = null;
this.hasVariables = false; this.hasPlaceholders = false;
} }
} }
@ -31,15 +31,15 @@ public class RelativeStringList {
} }
public List<String> getValue(Player player) { public List<String> getValue(Player player) {
if (hasVariables) { if (hasPlaceholders) {
return CollectionUtils.transform(relativeList, element -> element.getValue(player)); return CollectionUtils.transform(relativeList, element -> element.getValue(player));
} else { } else {
return originalList; return originalList;
} }
} }
public boolean hasVariables() { public boolean hasPlaceholders() {
return hasVariables; return hasPlaceholders;
} }
} }

View File

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

View File

@ -21,7 +21,7 @@ menu-settings:
# OPTIONAL # OPTIONAL
# How frequently the menu will be refreshed, in seconds. # 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 auto-refresh: 5
# OPTIONAL # OPTIONAL