Add PlaceholderAPI integration

This commit is contained in:
Gabriele C 2019-04-24 11:37:51 +02:00
parent ec31e18c1f
commit cbe2655c2b
16 changed files with 165 additions and 95 deletions

View File

@ -32,6 +32,11 @@
<id>bstats-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
</repository>
<repository>
<id>placeholderapi-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<dependencies>
@ -56,6 +61,13 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.9.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit-lite</artifactId>

View File

@ -21,6 +21,7 @@ import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import com.gmail.filoghost.chestcommands.bridge.PlaceholderAPIBridge;
import org.bstats.bukkit.MetricsLite;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
@ -94,6 +95,10 @@ public class ChestCommands extends JavaPlugin {
getLogger().info("Hooked BarAPI");
}
if (PlaceholderAPIBridge.setupPlugin()) {
getLogger().info("Hooked PlaceholderAPI");
}
if (settings.update_notifications) {
new SimpleUpdater(this, 56919).checkForUpdates(new ResponseHandler() {

View File

@ -14,15 +14,11 @@
*/
package com.gmail.filoghost.chestcommands.api;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.Set;
import com.gmail.filoghost.chestcommands.internal.VariableManager;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
@ -35,7 +31,6 @@ import org.bukkit.inventory.meta.LeatherArmorMeta;
import org.bukkit.inventory.meta.SkullMeta;
import com.gmail.filoghost.chestcommands.ChestCommands;
import com.gmail.filoghost.chestcommands.internal.Variable;
import com.gmail.filoghost.chestcommands.util.Utils;
public class Icon {
@ -53,9 +48,9 @@ public class Icon {
protected boolean closeOnClick;
private ClickHandler clickHandler;
private Set<Variable> nameVariables;
private Map<Integer, Set<Variable>> loreVariables;
private boolean nameHasVariables;
private boolean[] loreLinesWithVariables;
private ItemStack cachedItem; // When there are no variables, we don't recreate the item
public Icon() {
@ -64,7 +59,7 @@ public class Icon {
}
public boolean hasVariables() {
return nameVariables != null || loreVariables != null;
return nameHasVariables || loreLinesWithVariables != null;
}
public void setMaterial(Material material) {
@ -107,20 +102,7 @@ public class Icon {
public void setName(String name) {
this.name = name;
this.nameVariables = null; // Reset the variables
if (name != null) {
for (Variable variable : Variable.values()) {
if (name.contains(variable.getText())) {
if (nameVariables == null) {
nameVariables = new HashSet<Variable>();
}
nameVariables.add(variable);
}
}
}
this.nameHasVariables = VariableManager.hasVariables(name);
}
public boolean hasName() {
@ -135,26 +117,16 @@ public class Icon {
public void setLore(List<String> lore) {
this.lore = lore;
this.loreVariables = null; // Reset the variables
this.loreLinesWithVariables = null;
if (lore != null) {
for (int i = 0; i < lore.size(); i++) {
for (Variable variable : Variable.values()) {
if (lore.get(i).contains(variable.getText())) {
if (loreVariables == null) {
loreVariables = new HashMap<Integer, Set<Variable>>();
}
Set<Variable> lineVariables = loreVariables.get(i);
if (lineVariables == null) {
lineVariables = new HashSet<Variable>();
loreVariables.put(i, lineVariables);
}
lineVariables.add(variable);
if (VariableManager.hasVariables(lore.get(i))) {
if (this.loreLinesWithVariables == null) {
this.loreLinesWithVariables = new boolean[lore.size()];
}
loreLinesWithVariables[i] = true;
break;
}
}
}
@ -229,10 +201,8 @@ public class Icon {
String name = this.name;
if (pov != null && nameVariables != null) {
for (Variable nameVariable : nameVariables) {
name = name.replace(nameVariable.getText(), nameVariable.getReplacement(pov));
}
if (pov != null && nameHasVariables) {
name = VariableManager.setVariables(name, pov);
}
if (name.isEmpty()) {
@ -254,18 +224,12 @@ public class Icon {
output = Utils.newArrayList();
if (pov != null && loreVariables != null) {
if (pov != null && loreLinesWithVariables != null) {
for (int i = 0; i < lore.size(); i++) {
String line = lore.get(i);
Set<Variable> lineVariables = loreVariables.get(i);
if (lineVariables != null) {
for (Variable lineVariable : lineVariables) {
line = line.replace(lineVariable.getText(), lineVariable.getReplacement(pov));
}
if(loreLinesWithVariables[i]) {
line = VariableManager.setVariables(line, pov);
}
output.add(line);
}
} else {

View File

@ -0,0 +1,44 @@
package com.gmail.filoghost.chestcommands.bridge;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class PlaceholderAPIBridge {
private static PlaceholderAPIPlugin placeholderAPI;
public static boolean setupPlugin() {
Plugin placeholderPlugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI");
if (placeholderPlugin == null) {
return false;
}
placeholderAPI = (PlaceholderAPIPlugin) placeholderPlugin;
return true;
}
public static boolean hasValidPlugin() {
return placeholderAPI != null;
}
public static boolean hasPlaceholders(String message) {
if (!hasValidPlugin()) {
throw new IllegalStateException("PlaceholderAPI plugin was not found!");
}
return PlaceholderAPI.containsPlaceholders(message);
}
public static String setPlaceholders(String message, Player executor) {
if (!hasValidPlugin()) {
throw new IllegalStateException("PlaceholderAPI plugin was not found!");
}
return PlaceholderAPI.setPlaceholders(executor, message);
}
}

View File

@ -52,6 +52,4 @@ public class CommandsClickHandler implements ClickHandler {
return closeOnClick;
}
}

View File

@ -132,7 +132,6 @@ public class ExtendedIconMenu extends IconMenu {
}
}
public void sendNoPermissionMessage(CommandSender sender) {
String noPermMessage = ChestCommands.getLang().no_open_permission;
if (noPermMessage != null && !noPermMessage.isEmpty()) {

View File

@ -0,0 +1,32 @@
package com.gmail.filoghost.chestcommands.internal;
import com.gmail.filoghost.chestcommands.bridge.PlaceholderAPIBridge;
import org.bukkit.entity.Player;
public class VariableManager {
public static boolean hasVariables(String message) {
for(Variable variable : Variable.values()) {
if(message.contains(variable.getText())) {
return true;
}
}
if(PlaceholderAPIBridge.hasValidPlugin() && PlaceholderAPIBridge.hasPlaceholders(message)) {
return true;
}
return false;
}
public static String setVariables(String message, Player executor) {
for(Variable variable : Variable.values()) {
if(message.contains(variable.getText())) {
message = message.replace(variable.getText(), variable.getReplacement(executor));
}
}
if(PlaceholderAPIBridge.hasValidPlugin()) {
message = PlaceholderAPIBridge.setPlaceholders(message, executor);
}
return message;
}
}

View File

@ -14,40 +14,23 @@
*/
package com.gmail.filoghost.chestcommands.internal.icon;
import java.util.ArrayList;
import java.util.List;
import com.gmail.filoghost.chestcommands.internal.VariableManager;
import org.bukkit.entity.Player;
import com.gmail.filoghost.chestcommands.config.AsciiPlaceholders;
import com.gmail.filoghost.chestcommands.internal.Variable;
public abstract class IconCommand {
protected String command;
private List<Variable> containedVariables;
protected boolean hasVariables;
public IconCommand(String command) {
this.command = AsciiPlaceholders.placeholdersToSymbols(command).trim();
this.containedVariables = new ArrayList<Variable>();
for (Variable variable : Variable.values()) {
if (command.contains(variable.getText())) {
containedVariables.add(variable);
}
}
this.hasVariables = VariableManager.hasVariables(command);
}
public String getParsedCommand(Player executor) {
if (containedVariables.isEmpty()) {
return command;
}
String commandCopy = command;
for (Variable variable : containedVariables) {
commandCopy = commandCopy.replace(variable.getText(), variable.getReplacement(executor));
}
return commandCopy;
return hasVariables ? VariableManager.setVariables(command, executor) : command;
}
public abstract void execute(Player player);

View File

@ -29,7 +29,6 @@ public class BroadcastIconCommand extends IconCommand {
@Override
public void execute(Player player) {
Bukkit.broadcastMessage(getParsedCommand(player));
}
}

View File

@ -27,21 +27,29 @@ public class DragonBarIconCommand extends IconCommand {
public DragonBarIconCommand(String command) {
super(command);
if(!hasVariables) {
parseBar(super.command);
}
}
private void parseBar(String command) {
seconds = 1;
message = command;
String[] split = command.split("\\|", 2); // Max of 2 pieces
if (split.length > 1 && Utils.isValidPositiveInteger(split[0].trim())) {
seconds = Integer.parseInt(split[0].trim());
message = split[1].trim();
}
message = Utils.addColors(message);
}
@Override
public void execute(Player player) {
if(hasVariables) {
parseBar(getParsedCommand(player));
}
if (BarAPIBridge.hasValidPlugin()) {
BarAPIBridge.setMessage(player, message, seconds);
}

View File

@ -29,11 +29,16 @@ public class GiveIconCommand extends IconCommand {
public GiveIconCommand(String command) {
super(command);
if (!hasVariables) {
parseItem(super.command);
}
}
private void parseItem(String command) {
try {
ItemStackReader reader = new ItemStackReader(command, true);
itemToGive = reader.createStack();
errorMessage = null;
} catch (FormatException e) {
errorMessage = ChatColor.RED + "Invalid item to give: " + e.getMessage();
}
@ -41,6 +46,9 @@ public class GiveIconCommand extends IconCommand {
@Override
public void execute(Player player) {
if (hasVariables) {
parseItem(getParsedCommand(player));
}
if (errorMessage != null) {
player.sendMessage(errorMessage);
return;

View File

@ -28,17 +28,25 @@ public class GiveMoneyIconCommand extends IconCommand {
public GiveMoneyIconCommand(String command) {
super(command);
if (!hasVariables) {
parseMoney(super.command);
}
}
private void parseMoney(String command) {
if (!Utils.isValidPositiveDouble(command)) {
errorMessage = ChatColor.RED + "Invalid money amount: " + command;
return;
}
errorMessage = null;
moneyToGive = Double.parseDouble(command);
}
@Override
public void execute(Player player) {
if (hasVariables) {
parseMoney(getParsedCommand(player));
}
if (errorMessage != null) {
player.sendMessage(errorMessage);
return;

View File

@ -30,7 +30,8 @@ public class OpenIconCommand extends IconCommand {
@Override
public void execute(final Player player) {
final ExtendedIconMenu menu = ChestCommands.getFileNameToMenuMap().get(command.toLowerCase());
String target = hasVariables ? getParsedCommand(player) : command;
final ExtendedIconMenu menu = ChestCommands.getFileNameToMenuMap().get(target.toLowerCase());
if (menu != null) {
/*

View File

@ -27,7 +27,7 @@ public class ServerIconCommand extends IconCommand {
@Override
public void execute(Player player) {
BungeeCordUtils.connect(player, command);
BungeeCordUtils.connect(player, hasVariables ? getParsedCommand(player) : command);
}
}

View File

@ -30,24 +30,30 @@ public class SoundIconCommand extends IconCommand {
public SoundIconCommand(String command) {
super(command);
if (!hasVariables) {
parseSound(super.command);
}
}
private void parseSound(String command) {
errorMessage = null;
pitch = 1.0f;
volume = 1.0f;
String[] split = command.split(",");
sound = Utils.matchSound(split[0]);
if (sound == null) {
errorMessage = ChatColor.RED + "Invalid sound \"" + split[0].trim() + "\".";
return;
}
if (split.length > 1) {
try {
pitch = Float.parseFloat(split[1].trim());
} catch (NumberFormatException e) { }
}
if (split.length > 2) {
try {
volume = Float.parseFloat(split[2].trim());
@ -57,6 +63,9 @@ public class SoundIconCommand extends IconCommand {
@Override
public void execute(Player player) {
if (hasVariables) {
parseSound(getParsedCommand(player));
}
if (errorMessage != null) {
player.sendMessage(errorMessage);
return;

View File

@ -1,9 +1,9 @@
name: ChestCommands
main: com.gmail.filoghost.chestcommands.ChestCommands
version: ${project.version}
api-version: 1.13
api-version: 1.14
softdepend: [Vault, BarAPI]
softdepend: [Vault, BarAPI, PlaceholderAPI]
commands:
chestcommands: