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 me.filoghost.chestcommands.util.Colors;
import me.filoghost.chestcommands.variable.RelativeString;
import me.filoghost.chestcommands.placeholder.RelativeString;
public class BroadcastAction extends Action {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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 <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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 <https://www.gnu.org/licenses/>.
*/
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);
}

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

View File

@ -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<String> originalList;
private final List<RelativeString> relativeList;
private final boolean hasVariables;
private final boolean hasPlaceholders;
public RelativeStringList(List<String> 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<String> 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;
}
}

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