From af7db08036c4769544004888070e832a8d4a7f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 13:14:02 +0200 Subject: [PATCH 01/11] Add new placeholder system --- .../bukkit/placeholder/Placeholders.java | 130 +--------- .../com/plotsquared/core/PlotSquared.java | 9 + .../java/com/plotsquared/core/plot/Plot.java | 2 +- .../core/plot/flag/GlobalFlagContainer.java | 10 +- .../core/util/EventDispatcher.java | 8 +- .../core/util/placeholders/Placeholder.java | 63 +++++ .../placeholders/PlaceholderRegistry.java | 230 ++++++++++++++++++ .../placeholders/PlotFlagPlaceholder.java | 75 ++++++ .../placeholders/PlotSpecificPlaceholder.java | 59 +++++ 9 files changed, 455 insertions(+), 131 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java create mode 100644 Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java 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 c61b29f37..783d80257 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -27,17 +27,10 @@ package com.plotsquared.bukkit.placeholder; import com.plotsquared.core.PlotSquared; import com.plotsquared.core.player.PlotPlayer; -import com.plotsquared.core.plot.Plot; -import com.plotsquared.core.plot.flag.GlobalFlagContainer; -import com.plotsquared.core.plot.flag.PlotFlag; -import com.plotsquared.core.util.MainUtil; import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; -import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import java.util.UUID; - public class Placeholders extends PlaceholderExpansion { public Placeholders() { @@ -70,6 +63,7 @@ public class Placeholders extends PlaceholderExpansion { return ""; } + // PAPI specific ones that don't translate well over into other placeholder APIs if (identifier.startsWith("has_plot_")) { identifier = identifier.substring("has_plot_".length()); if (identifier.isEmpty()) @@ -88,126 +82,8 @@ 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 plot.getAlias(); - } - case "currentplot_owner": { - final UUID plotOwner = plot.getOwnerAbs(); - if (plotOwner == null) { - return ""; - } - - try { - return MainUtil.getName(plotOwner, false); - } catch (final Exception ignored) {} - - final String name = Bukkit.getOfflinePlayer(plotOwner).getName(); - return name != null ? name : "unknown"; - } - case "currentplot_members": { - if (plot.getMembers() == null && plot.getTrusted() == null) { - return "0"; - } - return String.valueOf(plot.getMembers().size() + plot.getTrusted().size()); - } - case "currentplot_members_added": { - if (plot.getMembers() == null) { - return "0"; - } - return String.valueOf(plot.getMembers().size()); - } - case "currentplot_members_trusted": { - if (plot.getTrusted() == null) { - return "0"; - } - return String.valueOf(plot.getTrusted().size()); - } - case "currentplot_members_denied": { - if (plot.getDenied() == null) { - return "0"; - } - return String.valueOf(plot.getDenied().size()); - } - case "has_build_rights": { - return plot.isAdded(pl.getUUID()) ? - PlaceholderAPIPlugin.booleanTrue() : - PlaceholderAPIPlugin.booleanFalse(); - } - case "currentplot_x": { - return String.valueOf(plot.getId().getX()); - } - case "currentplot_y": { - return String.valueOf(plot.getId().getY()); - } - case "currentplot_xy": { - return plot.getId().getX() + ";" + plot.getId().getY(); - } - case "currentplot_rating": { - return String.valueOf(plot.getAverageRating()); - } - case "currentplot_biome": { - return plot.getBiomeSynchronous() + ""; - } - default: - break; - } - if (identifier.startsWith("currentplot_localflag_")) { - return getFlagValue(plot, identifier.substring("currentplot_localflag_".length()), - false); - } - if (identifier.startsWith("currentplot_flag_")) { - return getFlagValue(plot, identifier.substring("currentplot_flag_".length()), true); - } - return ""; + // PlotSquared placeholders + return PlotSquared.get().getPlaceholderRegistry().getPlaceholderValue(identifier, pl); } - /** - * Return the flag value from its name on the current plot. - * If the flag doesn't exist it returns an empty string. - * If the flag exists but it is not set on current plot and the parameter inherit is set to true, - * it returns the default value. - * - * @param plot Current plot where the player is - * @param flagName Name of flag to get from current plot - * @param inherit Define if it returns only the flag set on currentplot or also inherited flag - * @return The value of flag serialized in string - */ - private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { - if (flagName.isEmpty()) - return ""; - final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); - if (flag == null) - return ""; - - if (inherit) { - return plot.getFlag(flag).toString(); - } else { - final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); - return (plotFlag != null) ? plotFlag.getValue().toString() : ""; - } - } } diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index e9048f726..3451b0a1c 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -62,6 +62,7 @@ import com.plotsquared.core.plot.PlotManager; import com.plotsquared.core.plot.comment.CommentManager; import com.plotsquared.core.plot.expiration.ExpireManager; import com.plotsquared.core.plot.expiration.ExpiryTask; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; import com.plotsquared.core.plot.world.DefaultPlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotArea; @@ -82,6 +83,7 @@ import com.plotsquared.core.util.SetupUtils; import com.plotsquared.core.util.StringMan; import com.plotsquared.core.util.WorldUtil; import com.plotsquared.core.util.logger.ILogger; +import com.plotsquared.core.util.placeholders.PlaceholderRegistry; import com.plotsquared.core.util.query.PlotQuery; import com.plotsquared.core.util.task.TaskManager; import com.plotsquared.core.uuid.UUIDPipeline; @@ -171,6 +173,7 @@ public class PlotSquared { private File storageFile; @Getter private PlotAreaManager plotAreaManager; @Getter private EventDispatcher eventDispatcher; + @Getter private PlaceholderRegistry placeholderRegistry; /** * Initialize PlotSquared with the desired Implementation class. @@ -194,6 +197,9 @@ public class PlotSquared { // ConfigurationSerialization.registerClass(BlockBucket.class, "BlockBucket"); + // Setup the global flag container + GlobalFlagContainer.setup(); + try { new ReflectionUtils(this.IMP.getNMSPackage()); try { @@ -209,6 +215,7 @@ public class PlotSquared { "PlotSquared-" + platform + ".jar"); } } + TaskManager.IMP = this.IMP.getTaskManager(); // World Util. Has to be done before config files are loaded @@ -262,6 +269,8 @@ public class PlotSquared { startExpiryTasks(); // Create Event utility class eventDispatcher = new EventDispatcher(); + // Create placeholder registry + placeholderRegistry = new PlaceholderRegistry(eventDispatcher); // create Hybrid utility class HybridUtils.manager = this.IMP.initHybridUtils(); // Inventory utility class diff --git a/Core/src/main/java/com/plotsquared/core/plot/Plot.java b/Core/src/main/java/com/plotsquared/core/plot/Plot.java index 2adca5334..ac1766e93 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/Plot.java +++ b/Core/src/main/java/com/plotsquared/core/plot/Plot.java @@ -2256,7 +2256,7 @@ public class Plot { * * @return The plot alias */ - public String getAlias() { + @NotNull public String getAlias() { if (this.settings == null) { return ""; } diff --git a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java index 4d7fdd396..3c44a8b5a 100644 --- a/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java +++ b/Core/src/main/java/com/plotsquared/core/plot/flag/GlobalFlagContainer.java @@ -25,6 +25,7 @@ */ package com.plotsquared.core.plot.flag; +import com.google.common.base.Preconditions; import com.plotsquared.core.plot.flag.implementations.AnalysisFlag; import com.plotsquared.core.plot.flag.implementations.AnimalAttackFlag; import com.plotsquared.core.plot.flag.implementations.AnimalCapFlag; @@ -114,7 +115,13 @@ import java.util.Map; public final class GlobalFlagContainer extends FlagContainer { - @Getter private static final GlobalFlagContainer instance = new GlobalFlagContainer(); + @Getter private static GlobalFlagContainer instance; + + public static void setup() { + Preconditions.checkState(instance == null, "Cannot setup the container twice"); + instance = new GlobalFlagContainer(); + } + private static Map> stringClassMap; private GlobalFlagContainer() { @@ -124,6 +131,7 @@ public final class GlobalFlagContainer extends FlagContainer { } }); stringClassMap = new HashMap<>(); + // Register all default flags here // Boolean flags this.addFlag(ExplosionFlag.EXPLOSION_FALSE); diff --git a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java index cb554b754..23d5dd15f 100644 --- a/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java +++ b/Core/src/main/java/com/plotsquared/core/util/EventDispatcher.java @@ -80,9 +80,9 @@ import java.util.UUID; public class EventDispatcher { - private EventBus eventBus = new EventBus("PlotSquaredEvents"); + private final EventBus eventBus = new EventBus("PlotSquaredEvents"); - private List listeners = new ArrayList<>(); + private final List listeners = new ArrayList<>(); public void registerListener(Object listener) { eventBus.register(listener); @@ -100,6 +100,10 @@ public class EventDispatcher { } } + public void callGenericEvent(@NotNull final Object event) { + eventBus.post(event); + } + public void callEvent(@NotNull final PlotEvent event) { eventBus.post(event); } diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java new file mode 100644 index 000000000..38e28a586 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/Placeholder.java @@ -0,0 +1,63 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.core.util.placeholders; + +import com.google.common.base.Preconditions; +import com.plotsquared.core.player.PlotPlayer; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.jetbrains.annotations.NotNull; + +/** + * A placeholder is a keyed value that gets replaced by a {@link PlotPlayer player}-specific value at runtime + */ +@EqualsAndHashCode(of = "key") @ToString(of = "key") +public abstract class Placeholder { + + private final String key; + + public Placeholder(@NotNull final String key) { + this.key = Preconditions.checkNotNull(key, "Key may not be null"); + } + + /** + * Get the value of the placeholder for a particular player + * + * @param player Player + * @return Placeholder value. Return {@code ""} if no placeholder value can be returned + */ + @NotNull public abstract String getValue(@NotNull final PlotPlayer player); + + /** + * Get the placeholder key + * + * @return Placeholder key + */ + @NotNull public final String getKey() { + return this.key; + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java new file mode 100644 index 000000000..243ca0909 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java @@ -0,0 +1,230 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.core.util.placeholders; + +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; +import com.plotsquared.core.util.EventDispatcher; +import com.plotsquared.core.util.MainUtil; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.Collections; +import java.util.Locale; +import java.util.Map; +import java.util.UUID; +import java.util.function.BiFunction; + +/** + * Registry that contains {@link Placeholder placeholders} + */ +public final class PlaceholderRegistry { + + private final Map placeholders; + private final EventDispatcher eventDispatcher; + + public PlaceholderRegistry(@NotNull final EventDispatcher eventDispatcher) { + this.placeholders = Maps.newHashMap(); + this.eventDispatcher = eventDispatcher; + this.registerDefault(); + } + + private void registerDefault() { + final GlobalFlagContainer globalFlagContainer = GlobalFlagContainer.getInstance(); + for (final PlotFlag flag : globalFlagContainer.getRecognizedPlotFlags()) { + this.registerPlaceholder(new PlotFlagPlaceholder(flag, true)); + this.registerPlaceholder(new PlotFlagPlaceholder(flag, false)); + } + GlobalFlagContainer.getInstance().subscribe((flag, type) -> { + this.registerPlaceholder(new PlotFlagPlaceholder(flag, true)); + this.registerPlaceholder(new PlotFlagPlaceholder(flag, false)); + }); + this.createPlaceholder("currentplot_world", player -> player.getLocation().getWorld()); + this.createPlaceholder("has_plot", player -> player.getPlotCount() > 0 ? "true" : "false"); + this.createPlaceholder("allowed_plot_count", player -> Integer.toString(player.getAllowedPlots())); + this.createPlaceholder("plot_count", player -> Integer.toString(player.getPlotCount())); + this.createPlaceholder("currentplot_alias", (player, plot) -> plot.getAlias()); + this.createPlaceholder("currentplot_owner", (player, plot) -> { + final UUID plotOwner = plot.getOwnerAbs(); + if (plotOwner == null) { + return ""; + } + + try { + return MainUtil.getName(plotOwner, false); + } catch (final Exception ignored) {} + + return "unknown"; + }); + this.createPlaceholder("currentplot_members", (player, plot) -> { + if (plot.getMembers() == null && plot.getTrusted() == null) { + return "0"; + } + return String.valueOf(plot.getMembers().size() + plot.getTrusted().size()); + }); + this.createPlaceholder("currentplot_members_added", (player, plot) -> { + if (plot.getMembers() == null) { + return "0"; + } + return String.valueOf(plot.getMembers().size()); + }); + this.createPlaceholder("currentplot_members_trusted", (player, plot) -> { + if (plot.getTrusted() == null) { + return "0"; + } + return String.valueOf(plot.getTrusted().size()); + }); + this.createPlaceholder("currentplot_members_denied", (player, plot) -> { + if (plot.getDenied() == null) { + return "0"; + } + return String.valueOf(plot.getDenied().size()); + }); + this.createPlaceholder("has_build_rights", (player, plot) -> + plot.isAdded(player.getUUID()) ? "true" : "false"); + this.createPlaceholder("currentplot_x", (player, plot) -> Integer.toString(plot.getId().getX())); + this.createPlaceholder("currentplot_y", (player, plot) -> Integer.toString(plot.getId().getY())); + this.createPlaceholder("currentplot_xy", (player, plot) -> plot.getId().toString()); + this.createPlaceholder("currentplot_rating", (player, plot) -> Double.toString(plot.getAverageRating())); + this.createPlaceholder("currentplot_biome", (player, plot) -> plot.getBiomeSynchronous().toString()); + } + + /** + * Create a functional placeholder + * + * @param key Placeholder key + * @param placeholderFunction Placeholder generator. Cannot return null + */ + @SuppressWarnings("ALL") public void createPlaceholder(@NotNull final String key, + @NotNull final Function, String> placeholderFunction) { + this.registerPlaceholder(new Placeholder(key) { + @Override @NotNull public String getValue(@NotNull final PlotPlayer player) { + return placeholderFunction.apply(player); + } + }); + } + + /** + * Create a functional placeholder + * + * @param key Placeholder key + * @param placeholderFunction Placeholder generator. Cannot return null + */ + public void createPlaceholder(@NotNull final String key, + @NotNull final BiFunction, Plot, String> placeholderFunction) { + this.registerPlaceholder(new PlotSpecificPlaceholder(key) { + @Override @NotNull public String getValue(@NotNull final PlotPlayer player, @NotNull final Plot plot) { + return placeholderFunction.apply(player, plot); + } + }); + } + + /** + * Register a placeholder + * + * @param placeholder Placeholder instance + */ + public void registerPlaceholder(@NotNull final Placeholder placeholder) { + final Placeholder previous = this.placeholders + .put(placeholder.getKey().toLowerCase(Locale.ENGLISH), + Preconditions.checkNotNull(placeholder, "Placeholder may not be null")); + if (previous != null) { + this.eventDispatcher.callGenericEvent(new PlaceholderAddedEvent(placeholder)); + } + } + + /** + * Get a placeholder instance from its key + * + * @param key Placeholder key + * @return Placeholder value + */ + @Nullable public Placeholder getPlaceholder(@NotNull final String key) { + return this.placeholders.get( + Preconditions.checkNotNull(key, "Key may not be null").toLowerCase(Locale.ENGLISH)); + } + + /** + * Get the placeholder value evaluated for a player, and catch and deal with any problems + * occurring while doing so + * + * @param key Placeholder key + * @param player Player to evaluate for + * @return Replacement value + */ + @NotNull public String getPlaceholderValue(@NotNull final String key, + @NotNull final PlotPlayer player) { + final Placeholder placeholder = getPlaceholder(key); + if (placeholder == null) { + return ""; + } + String placeholderValue = ""; + try { + placeholderValue = placeholder.getValue(player); + // If a placeholder for some reason decides to be disobedient, we catch it here + if (placeholderValue == null) { + new RuntimeException(String + .format("Placeholder '%s' returned null for player '%s'", placeholder.getKey(), + player.getName())).printStackTrace(); + } + } catch (final Exception exception) { + new RuntimeException(String + .format("Placeholder '%s' failed to evalulate for player '%s'", + placeholder.getKey(), player.getName()), exception).printStackTrace(); + } + return placeholderValue; + } + + /** + * Get all placeholders + * + * @return Unmodifiable collection of placeholders + */ + @NotNull public Collection getPlaceholders() { + return Collections.unmodifiableCollection(this.placeholders.values()); + } + + + /** + * Event called when a new {@link Placeholder} has been added + */ + @RequiredArgsConstructor + @Getter + public static class PlaceholderAddedEvent { + + private final Placeholder placeholder; + + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java new file mode 100644 index 000000000..40b963d06 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -0,0 +1,75 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.core.util.placeholders; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; +import org.jetbrains.annotations.NotNull; + +public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { + + private final PlotFlag flag; + private final boolean local; + + public PlotFlagPlaceholder(@NotNull final PlotFlag flag, final boolean local) { + super(String.format("currentplot_%sflag_%s", local ? "local": "", flag.getName())); + this.flag = flag; + this.local = local; + } + + @Override @NotNull public String getValue(@NotNull final PlotPlayer player, @NotNull final Plot plot) { + return this.getFlagValue(plot, this.flag.getName(), !this.local); + } + + /** + * Return the flag value from its name on the current plot. + * If the flag doesn't exist it returns an empty string. + * If the flag exists but it is not set on current plot and the parameter inherit is set to true, + * it returns the default value. + * + * @param plot Current plot where the player is + * @param flagName Name of flag to get from current plot + * @param inherit Define if it returns only the flag set on currentplot or also inherited flag + * @return The value of flag serialized in string + */ + private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { + if (flagName.isEmpty()) + return ""; + final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); + if (flag == null) + return ""; + + if (inherit) { + return plot.getFlag(flag).toString(); + } else { + final PlotFlag plotFlag = plot.getFlagContainer().queryLocal(flag.getClass()); + return (plotFlag != null) ? plotFlag.getValue().toString() : ""; + } + } + +} diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java new file mode 100644 index 000000000..92285238d --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotSpecificPlaceholder.java @@ -0,0 +1,59 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.core.util.placeholders; + +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.plot.Plot; +import org.jetbrains.annotations.NotNull; + +/** + * A {@link Placeholder placeholder} that requires a {@link com.plotsquared.core.plot.Plot plot} + */ +public abstract class PlotSpecificPlaceholder extends Placeholder { + + public PlotSpecificPlaceholder(@NotNull final String key) { + super(key); + } + + @Override @NotNull public final String getValue(@NotNull final PlotPlayer player) { + final Plot plot = player.getCurrentPlot(); + if (plot == null) { + return ""; + } + return this.getValue(player, plot); + } + + /** + * Get the value of the placeholder for the {@link PlotPlayer player} in a specific {@link Plot plot} + * + * @param player Player that the placeholder is evaluated for + * @param plot Plot that the player is in + * @return Placeholder value, or {@code ""} if the placeholder does not apply + */ + @NotNull public abstract String getValue(@NotNull final PlotPlayer player, + @NotNull final Plot plot); + +} From 1908588fcbd4c35ad16b1f17f6870a0e34db0cbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 13:17:21 +0200 Subject: [PATCH 02/11] Change expansion author and version --- .../com/plotsquared/bukkit/placeholder/Placeholders.java | 6 +++--- 1 file changed, 3 insertions(+), 3 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 783d80257..0c3b8bfad 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java @@ -45,7 +45,7 @@ public class Placeholders extends PlaceholderExpansion { } @Override public String getAuthor() { - return "NotMyFault"; + return "IntellectualSites"; } @Override public String getIdentifier() { @@ -53,11 +53,11 @@ public class Placeholders extends PlaceholderExpansion { } @Override public String getVersion() { - return "2.5"; + return "3"; } @Override public String onPlaceholderRequest(Player p, String identifier) { - final PlotPlayer pl = PlotSquared.imp().getPlayerManager().getPlayerIfExists(p.getUniqueId()); + final PlotPlayer pl = PlotSquared.imp().getPlayerManager().getPlayerIfExists(p.getUniqueId()); if (pl == null) { return ""; From 34655b8d4114ce6190d2974758becaebcc41308b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:41:08 +0200 Subject: [PATCH 03/11] Add (untested) MVdWPlaceholderAPI support --- Bukkit/build.gradle | 2 + .../com/plotsquared/bukkit/BukkitMain.java | 10 ++- .../bukkit/placeholder/MVdWPlaceholders.java | 76 +++++++++++++++++++ ...laceholders.java => PAPIPlaceholders.java} | 4 +- Bukkit/src/main/resources/plugin.yml | 2 +- 5 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java rename Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/{Placeholders.java => PAPIPlaceholders.java} (97%) diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index e206fbe5b..fa23da2ff 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -14,6 +14,7 @@ repositories { maven { url = "https://ci.ender.zone/plugin/repository/everything/" } maven { url = "https://mvn.intellectualsites.com/content/repositories/snapshots" } maven { url = "https://repo.wea-ondara.net/repository/public/" } + maven { url = "http://repo.mvdw-software.be/content/groups/public/" } mavenLocal() } @@ -41,6 +42,7 @@ dependencies { implementation("net.alpenblock:BungeePerms:4.0-dev-106") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile('com.sk89q:squirrelid:1.0.0-SNAPSHOT'){ transitive = false } + compile('be.maximvdw:MVdWPlaceholderAPI:2.1.1-SNAPSHOT'){ transitive = false } } sourceCompatibility = 1.8 diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index c03cdd694..66bc5ed3c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -36,8 +36,9 @@ import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.managers.BukkitWorldManager; import com.plotsquared.bukkit.managers.HyperverseWorldManager; import com.plotsquared.bukkit.managers.MultiverseWorldManager; +import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; import com.plotsquared.bukkit.placeholder.PlaceholderFormatter; -import com.plotsquared.bukkit.placeholder.Placeholders; +import com.plotsquared.bukkit.placeholder.PAPIPlaceholders; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.queue.BukkitLocalQueue; import com.plotsquared.bukkit.schematic.BukkitSchematicHandler; @@ -363,7 +364,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< } if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { - new Placeholders().register(); + new PAPIPlaceholders().register(); if (Settings.Enabled_Components.EXTERNAL_PLACEHOLDERS) { ChatFormatter.formatters.add(new PlaceholderFormatter()); } @@ -373,6 +374,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< .debug(Captions.PREFIX + "&6PlaceholderAPI is not in use. Hook deactivated."); } + if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { + new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); + PlotSquared.log(Captions.PREFIX + "&cPlotSquared hooked into MVdWPlaceholderAPI"); + } + this.startMetrics(); if (Settings.Enabled_Components.WORLDS) { TaskManager.IMP.taskRepeat(this::unload, 20); diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java new file mode 100644 index 000000000..5bd835de0 --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java @@ -0,0 +1,76 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.bukkit.placeholder; + +import be.maximvdw.placeholderapi.PlaceholderAPI; +import com.google.common.eventbus.Subscribe; +import com.plotsquared.bukkit.util.BukkitUtil; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.player.PlotPlayer; +import com.plotsquared.core.util.placeholders.Placeholder; +import com.plotsquared.core.util.placeholders.PlaceholderRegistry; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +/** + * Placeholder support for MVdWPlaceholderAPI + */ +public class MVdWPlaceholders { + + private final Plugin plugin; + private final PlaceholderRegistry registry; + + public MVdWPlaceholders(@NotNull final Plugin plugin, + @NotNull final PlaceholderRegistry registry) { + this.plugin = plugin; + this.registry = registry; + for (final Placeholder placeholder : registry.getPlaceholders()) { + this.addPlaceholder(placeholder); + } + PlotSquared.get().getEventDispatcher().registerListener(this); + } + + @Subscribe public void onNewPlaceholder(@NotNull final + PlaceholderRegistry.PlaceholderAddedEvent event) { + this.addPlaceholder(event.getPlaceholder()); + } + + private void addPlaceholder(@NotNull final Placeholder placeholder) { + PlaceholderAPI.registerPlaceholder(plugin, String.format("plotsquared_%s", placeholder.getKey()), + placeholderReplaceEvent -> { + if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) { + return ""; + } + final PlotPlayer player = BukkitUtil.getPlayer(placeholderReplaceEvent.getPlayer()); + if (player == null) { + return ""; + } + return registry.getPlaceholderValue(placeholderReplaceEvent.getPlaceholder(), player); + }); + } + +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java similarity index 97% rename from Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java rename to Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java index 0c3b8bfad..52d30d5ed 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/Placeholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/PAPIPlaceholders.java @@ -31,9 +31,9 @@ import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.entity.Player; -public class Placeholders extends PlaceholderExpansion { +public class PAPIPlaceholders extends PlaceholderExpansion { - public Placeholders() { + public PAPIPlaceholders() { } @Override public boolean persist() { diff --git a/Bukkit/src/main/resources/plugin.yml b/Bukkit/src/main/resources/plugin.yml index 41967220c..2dd7edbac 100644 --- a/Bukkit/src/main/resources/plugin.yml +++ b/Bukkit/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ load: STARTUP description: "Easy, yet powerful Plot World generation and management." authors: [Citymonstret, Empire92, MattBDev, dordsor21, NotMyFault, SirYwell] website: https://www.spigotmc.org/resources/77506/ -softdepend: [Vault, PlaceholderAPI, Essentials, LuckPerms, BungeePerms] +softdepend: [Vault, PlaceholderAPI, Essentials, LuckPerms, BungeePerms, MVdWPlaceholderAPI] loadbefore: [MultiWorld, Multiverse-Core] depend: [WorldEdit] database: false From 64f5580eddbdcf1e0e10e06bed8d373f4cc43fe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:46:52 +0200 Subject: [PATCH 04/11] Fix code styling issue --- .../core/util/placeholders/PlotFlagPlaceholder.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java index 40b963d06..51d075d26 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -57,13 +57,14 @@ public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { * @param inherit Define if it returns only the flag set on currentplot or also inherited flag * @return The value of flag serialized in string */ - private String getFlagValue(final Plot plot, final String flagName, final boolean inherit) { - if (flagName.isEmpty()) + @NotNull private String getFlagValue(@NotNull final Plot plot, @NotNull final String flagName, final boolean inherit) { + if (flagName.isEmpty()) { return ""; + } final PlotFlag flag = GlobalFlagContainer.getInstance().getFlagFromString(flagName); - if (flag == null) + if (flag == null) { return ""; - + } if (inherit) { return plot.getFlag(flag).toString(); } else { From 5d2d4ac12b1a243890373da8a8386d805e962f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 14 Jul 2020 20:47:15 +0200 Subject: [PATCH 05/11] fix language issue --- .../plotsquared/core/util/placeholders/PlotFlagPlaceholder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java index 51d075d26..4a3d1876a 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlotFlagPlaceholder.java @@ -54,7 +54,7 @@ public final class PlotFlagPlaceholder extends PlotSpecificPlaceholder { * * @param plot Current plot where the player is * @param flagName Name of flag to get from current plot - * @param inherit Define if it returns only the flag set on currentplot or also inherited flag + * @param inherit Define if it returns only the flag set on the current plot or also inherited flags * @return The value of flag serialized in string */ @NotNull private String getFlagValue(@NotNull final Plot plot, @NotNull final String flagName, final boolean inherit) { From a52249513e17dc65e31d9493e3afac735800ae23 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Wed, 15 Jul 2020 12:05:42 +0200 Subject: [PATCH 06/11] Update MVdW version --- Bukkit/build.gradle | 2 +- Bukkit/pom.xml | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Bukkit/build.gradle b/Bukkit/build.gradle index fa23da2ff..61842d3ce 100644 --- a/Bukkit/build.gradle +++ b/Bukkit/build.gradle @@ -42,7 +42,7 @@ dependencies { implementation("net.alpenblock:BungeePerms:4.0-dev-106") compile("se.hyperver.hyperverse:Core:0.6.0-SNAPSHOT"){ transitive = false } compile('com.sk89q:squirrelid:1.0.0-SNAPSHOT'){ transitive = false } - compile('be.maximvdw:MVdWPlaceholderAPI:2.1.1-SNAPSHOT'){ transitive = false } + compile('be.maximvdw:MVdWPlaceholderAPI:3.1.1-SNAPSHOT'){ transitive = false } } sourceCompatibility = 1.8 diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index cde998b9a..c70676567 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -45,7 +45,7 @@ io.papermc paperlib - 1.0.2 + 1.0.4 compile @@ -84,6 +84,18 @@ + + be.maximvdw + MVdWPlaceholderAPI + 3.1.1-SNAPSHOT + compile + + + * + * + + + com.sk89q.worldedit worldedit-core From e8c155763be355172f28b54c5ff3545d5182406f Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Wed, 15 Jul 2020 12:44:07 +0200 Subject: [PATCH 07/11] Register placeholders --- Bukkit/pom.xml | 2 +- .../src/main/java/com/plotsquared/bukkit/BukkitMain.java | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index c70676567..07e1a9ca7 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -87,7 +87,7 @@ be.maximvdw MVdWPlaceholderAPI - 3.1.1-SNAPSHOT + 2.1.1-SNAPSHOT compile diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 66bc5ed3c..5ee38078c 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -369,14 +369,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< ChatFormatter.formatters.add(new PlaceholderFormatter()); } PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into PlaceholderAPI"); - } else { - PlotSquared - .debug(Captions.PREFIX + "&6PlaceholderAPI is not in use. Hook deactivated."); } - if (Bukkit.getPluginManager().isPluginEnabled("MVdWPlaceholderAPI")) { + if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); - PlotSquared.log(Captions.PREFIX + "&cPlotSquared hooked into MVdWPlaceholderAPI"); + PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); } this.startMetrics(); From 2d9cf8b7597bb7b8684fce35263813b7194874de Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:04:17 +0200 Subject: [PATCH 08/11] Get placeholders working --- .../com/plotsquared/bukkit/BukkitMain.java | 12 ++++---- .../bukkit/listener/ServerListener.java | 29 +++++++++++++++++++ .../bukkit/placeholder/MVdWPlaceholders.java | 6 ++-- .../java/com/plotsquared/core/IPlotMain.java | 5 ++++ .../com/plotsquared/core/PlotSquared.java | 1 + .../placeholders/PlaceholderRegistry.java | 2 +- 6 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 5ee38078c..fc3689dcd 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -31,12 +31,12 @@ import com.plotsquared.bukkit.listener.ChunkListener; import com.plotsquared.bukkit.listener.EntitySpawnListener; import com.plotsquared.bukkit.listener.PaperListener; import com.plotsquared.bukkit.listener.PlayerEvents; +import com.plotsquared.bukkit.listener.ServerListener; import com.plotsquared.bukkit.listener.SingleWorldListener; import com.plotsquared.bukkit.listener.WorldEvents; import com.plotsquared.bukkit.managers.BukkitWorldManager; import com.plotsquared.bukkit.managers.HyperverseWorldManager; import com.plotsquared.bukkit.managers.MultiverseWorldManager; -import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; import com.plotsquared.bukkit.placeholder.PlaceholderFormatter; import com.plotsquared.bukkit.placeholder.PAPIPlaceholders; import com.plotsquared.bukkit.player.BukkitPlayerManager; @@ -371,11 +371,6 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into PlaceholderAPI"); } - if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { - new MVdWPlaceholders(this, PlotSquared.get().getPlaceholderRegistry()); - PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); - } - this.startMetrics(); if (Settings.Enabled_Components.WORLDS) { TaskManager.IMP.taskRepeat(this::unload, 20); @@ -1052,6 +1047,11 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain< getServer().getPluginManager().registerEvents(new WorldEvents(), this); } + @Override + public void registerServerEvents() { + getServer().getPluginManager().registerEvents(new ServerListener(this), this); + } + @NotNull @Override public IndependentPlotGenerator getDefaultGenerator() { return new HybridGen(); } diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java new file mode 100644 index 000000000..c6f8b072e --- /dev/null +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -0,0 +1,29 @@ +package com.plotsquared.bukkit.listener; + +import com.plotsquared.bukkit.BukkitMain; +import com.plotsquared.bukkit.placeholder.MVdWPlaceholders; +import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.server.ServerLoadEvent; + +public class ServerListener implements Listener { + + private final BukkitMain plugin; + + public ServerListener(BukkitMain plugin) { + this.plugin = plugin; + } + + @EventHandler public void onServerLoad(ServerLoadEvent event) { + if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { + new MVdWPlaceholders(this.plugin, PlotSquared.get().getPlaceholderRegistry()); + PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); + } else { + System.out.println("Nope"); + } + + } +} diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java index 5bd835de0..599a1122b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/placeholder/MVdWPlaceholders.java @@ -41,6 +41,7 @@ import org.jetbrains.annotations.NotNull; */ public class MVdWPlaceholders { + private static final String PREFIX = "plotsquared_"; private final Plugin plugin; private final PlaceholderRegistry registry; @@ -60,7 +61,7 @@ public class MVdWPlaceholders { } private void addPlaceholder(@NotNull final Placeholder placeholder) { - PlaceholderAPI.registerPlaceholder(plugin, String.format("plotsquared_%s", placeholder.getKey()), + PlaceholderAPI.registerPlaceholder(plugin, PREFIX + String.format("%s", placeholder.getKey()), placeholderReplaceEvent -> { if (!placeholderReplaceEvent.isOnline() || placeholderReplaceEvent.getPlayer() == null) { return ""; @@ -69,7 +70,8 @@ public class MVdWPlaceholders { if (player == null) { return ""; } - return registry.getPlaceholderValue(placeholderReplaceEvent.getPlaceholder(), player); + String key = placeholderReplaceEvent.getPlaceholder().substring(PREFIX.length()); + return registry.getPlaceholderValue(key, player); }); } diff --git a/Core/src/main/java/com/plotsquared/core/IPlotMain.java b/Core/src/main/java/com/plotsquared/core/IPlotMain.java index a38045acb..5dcc6d5a1 100644 --- a/Core/src/main/java/com/plotsquared/core/IPlotMain.java +++ b/Core/src/main/java/com/plotsquared/core/IPlotMain.java @@ -270,6 +270,11 @@ public interface IPlotMain

extends ILogger { */ void registerWorldEvents(); + /** + * Register events related to the server + */ + void registerServerEvents(); + /** * Usually HybridGen * diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 3451b0a1c..f1287dbcb 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -262,6 +262,7 @@ public class PlotSquared { this.IMP.registerPlayerEvents(); } // Required + this.IMP.registerServerEvents(); this.IMP.registerWorldEvents(); if (Settings.Enabled_Components.CHUNK_PROCESSOR) { this.IMP.registerChunkProcessor(); diff --git a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java index 243ca0909..d18859b2e 100644 --- a/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java +++ b/Core/src/main/java/com/plotsquared/core/util/placeholders/PlaceholderRegistry.java @@ -159,7 +159,7 @@ public final class PlaceholderRegistry { final Placeholder previous = this.placeholders .put(placeholder.getKey().toLowerCase(Locale.ENGLISH), Preconditions.checkNotNull(placeholder, "Placeholder may not be null")); - if (previous != null) { + if (previous == null) { this.eventDispatcher.callGenericEvent(new PlaceholderAddedEvent(placeholder)); } } From 10ab28c1ec3a44b9b947349c39e3feaec338b359 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:08:00 +0200 Subject: [PATCH 09/11] Add missing license headers --- .../bukkit/listener/ServerListener.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java index c6f8b072e..245393ba0 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -1,3 +1,28 @@ +/* + * _____ _ _ _____ _ + * | __ \| | | | / ____| | | + * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| | + * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` | + * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| | + * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_| + * | | + * |_| + * PlotSquared plot management system for Minecraft + * Copyright (C) 2020 IntellectualSites + * + * 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 com.plotsquared.bukkit.listener; import com.plotsquared.bukkit.BukkitMain; From 97a356c4a072ee1b96ed76162b218f8309746962 Mon Sep 17 00:00:00 2001 From: Hannes Greule Date: Thu, 16 Jul 2020 18:13:02 +0200 Subject: [PATCH 10/11] Very attentive reviewers, can recommend --- .../java/com/plotsquared/bukkit/listener/ServerListener.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java index 245393ba0..c74db87d4 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/ServerListener.java @@ -46,9 +46,6 @@ public class ServerListener implements Listener { if (Bukkit.getPluginManager().getPlugin("MVdWPlaceholderAPI") != null) { new MVdWPlaceholders(this.plugin, PlotSquared.get().getPlaceholderRegistry()); PlotSquared.log(Captions.PREFIX + "&6PlotSquared hooked into MVdWPlaceholderAPI"); - } else { - System.out.println("Nope"); } - } } From ff3a94933aada14eb7b495e5c58ad4011a4c7e43 Mon Sep 17 00:00:00 2001 From: N0tMyFaultOG Date: Thu, 16 Jul 2020 18:43:52 +0200 Subject: [PATCH 11/11] Bump version --- Bukkit/pom.xml | 4 ++-- build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Bukkit/pom.xml b/Bukkit/pom.xml index 07e1a9ca7..bba553386 100644 --- a/Bukkit/pom.xml +++ b/Bukkit/pom.xml @@ -21,7 +21,7 @@ com.plotsquared PlotSquared-Core - 5.12.5 + 5.13.0 compile @@ -87,7 +87,7 @@ be.maximvdw MVdWPlaceholderAPI - 2.1.1-SNAPSHOT + 3.1.1-SNAPSHOT compile diff --git a/build.gradle b/build.gradle index a0bae9c0c..bfd4ef86f 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ ext { git = Grgit.open(dir: new File(rootDir.toString() + "/.git")) } -def ver = "5.12.5" +def ver = "5.13.0" def versuffix = "" ext { if (project.hasProperty("versionsuffix")) {