From ccb43d06616a2c018b85ab3e1aae420c51a7b98b Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 23 Apr 2020 14:16:06 +0100 Subject: [PATCH] Do placeholders better. - Still allow parsing of placeholder if no plot is required and plot is null. - Properly check if the player is null before getting the plot, and if the plot is null - These were still issues before my initial "fix" city --- .../bukkit/placeholder/Placeholders.java | 103 +++++++----------- 1 file changed, 40 insertions(+), 63 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java index c5666fd4b..1cf38cab7 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -69,7 +69,10 @@ public class Placeholders extends PlaceholderExpansion { @Override public String onPlaceholderRequest(Player p, String identifier) { final PlotPlayer pl = PlotPlayer.get(p.getName()); - final Plot plot = pl.getCurrentPlot(); + + if (pl == null) { + return ""; + } if (identifier.startsWith("has_plot_")) { if (identifier.split("has_plot_").length != 2) @@ -89,15 +92,35 @@ public class Placeholders extends PlaceholderExpansion { return String.valueOf(pl.getPlotCount(identifier)); } + switch (identifier) { + case "currentplot_world": { + return p.getWorld().getName(); + } + case "has_plot": { + return (pl.getPlotCount() > 0) ? + PlaceholderAPIPlugin.booleanTrue() : + PlaceholderAPIPlugin.booleanFalse(); + } + case "allowed_plot_count": { + return String.valueOf(pl.getAllowedPlots()); + } + case "plot_count": { + return String.valueOf(pl.getPlotCount()); + } + } + + Plot plot = pl.getCurrentPlot(); + + if (plot == null) { + return ""; + } + switch (identifier) { case "currentplot_alias": { - return (pl.getCurrentPlot() != null) ? pl.getCurrentPlot().getAlias() : ""; + return plot.getAlias(); } case "currentplot_owner": { - if (pl.getCurrentPlot() == null) { - return ""; - } - final Set o = pl.getCurrentPlot().getOwners(); + final Set o = plot.getOwners(); if (o == null || o.isEmpty()) { return ""; } @@ -114,94 +137,48 @@ public class Placeholders extends PlaceholderExpansion { name = Bukkit.getOfflinePlayer(uid).getName(); return name != null ? name : "unknown"; } - case "currentplot_world": { - return p.getWorld().getName(); - } - case "has_plot": { - return (pl.getPlotCount() > 0) ? - PlaceholderAPIPlugin.booleanTrue() : - PlaceholderAPIPlugin.booleanFalse(); - } - case "allowed_plot_count": { - return String.valueOf(pl.getAllowedPlots()); - } - case "plot_count": { - return String.valueOf(pl.getPlotCount()); - } case "currentplot_members": { - if (pl.getCurrentPlot() == null) { - return ""; - } - if (pl.getCurrentPlot().getMembers() == null - && pl.getCurrentPlot().getTrusted() == null) { + if (plot.getMembers() == null && plot.getTrusted() == null) { return "0"; } - return String.valueOf( - pl.getCurrentPlot().getMembers().size() + pl.getCurrentPlot().getTrusted() - .size()); + return String.valueOf(plot.getMembers().size() + plot.getTrusted().size()); } case "currentplot_members_added": { - if (pl.getCurrentPlot() == null) { - return ""; - } - if (pl.getCurrentPlot().getMembers() == null) { + if (plot.getMembers() == null) { return "0"; } - return String.valueOf(pl.getCurrentPlot().getMembers().size()); + return String.valueOf(plot.getMembers().size()); } case "currentplot_members_trusted": { - if (pl.getCurrentPlot() == null) { - return ""; - } - if (pl.getCurrentPlot().getTrusted() == null) { + if (plot.getTrusted() == null) { return "0"; } return String.valueOf(plot.getTrusted().size()); } case "currentplot_members_denied": { - if (pl.getCurrentPlot() == null) { - return ""; - } - if (pl.getCurrentPlot().getDenied() == null) { + if (plot.getDenied() == null) { return "0"; } - return String.valueOf(pl.getCurrentPlot().getDenied().size()); + return String.valueOf(plot.getDenied().size()); } case "has_build_rights": { - return (pl.getCurrentPlot() != null) ? - ((pl.getCurrentPlot().isAdded(pl.getUUID())) ? - PlaceholderAPIPlugin.booleanTrue() : - PlaceholderAPIPlugin.booleanFalse()) : - ""; + return plot.isAdded(pl.getUUID()) ? + PlaceholderAPIPlugin.booleanTrue() : + PlaceholderAPIPlugin.booleanFalse(); } case "currentplot_x": { - if (pl.getCurrentPlot() == null) { - return ""; - } return String.valueOf(plot.getId().getX()); } case "currentplot_y": { - if (pl.getCurrentPlot() == null) { - return ""; - } return String.valueOf(plot.getId().getY()); } case "currentplot_xy": { - if (pl.getCurrentPlot() == null) { - return ""; - } - return pl.getCurrentPlot().getId().getX() + ";" + pl.getCurrentPlot().getId().getY(); + return plot.getId().getX() + ";" + plot.getId().getY(); } case "currentplot_rating": { - if (pl.getCurrentPlot() == null) { - return ""; - } return String.valueOf(plot.getAverageRating()); } case "currentplot_biome": { - if (pl.getCurrentPlot() == null) { - return ""; - } return plot.getBiomeSynchronous() + ""; } default: