From 010040dc229b1a793b2418f8f825daf7a4f6d600 Mon Sep 17 00:00:00 2001 From: T14D3 <73843330+T14D3@users.noreply.github.com> Date: Mon, 6 Jan 2025 15:04:19 +0100 Subject: [PATCH 1/2] Initial PlaceholderAPI support --- build.gradle | 4 ++ src/main/java/de/t14d3/zones/Zones.java | 6 +++ .../zones/integrations/PlaceholderAPI.java | 46 +++++++++++++++++++ src/main/resources/plugin.yml | 1 + 4 files changed, 57 insertions(+) create mode 100644 src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java diff --git a/build.gradle b/build.gradle index 1da8823..2d834e4 100644 --- a/build.gradle +++ b/build.gradle @@ -20,11 +20,15 @@ repositories { name = "sonatype" url = "https://oss.sonatype.org/content/groups/public/" } + maven { + url = "https://repo.extendedclip.com/releases/" + } } dependencies { //compileOnly("io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT") paperweight.paperDevBundle("1.21.1-R0.1-SNAPSHOT") + compileOnly 'me.clip:placeholderapi:2.11.6' } def targetJavaVersion = 21 diff --git a/src/main/java/de/t14d3/zones/Zones.java b/src/main/java/de/t14d3/zones/Zones.java index c47e26c..0a473e9 100644 --- a/src/main/java/de/t14d3/zones/Zones.java +++ b/src/main/java/de/t14d3/zones/Zones.java @@ -1,5 +1,6 @@ package de.t14d3.zones; +import de.t14d3.zones.integrations.PlaceholderAPI; import de.t14d3.zones.listeners.CommandListener; import de.t14d3.zones.listeners.PlayerInteractListener; import de.t14d3.zones.listeners.PlayerQuitListener; @@ -91,6 +92,11 @@ public void onEnable() { getLogger().info("Zones have been saved."); }, 20L, getConfig().getInt("zone-saving.period", 60) * 20L); } + // PlaceholderAPI integration + if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) { + new PlaceholderAPI(this).register(); + getLogger().info("PlaceholderAPI hooked!"); + } getLogger().info("Zones plugin has been enabled! Loaded " + regionManager.loadedRegions.size() + " regions."); } diff --git a/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java b/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java new file mode 100644 index 0000000..bbe4ae3 --- /dev/null +++ b/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java @@ -0,0 +1,46 @@ +package de.t14d3.zones.integrations; + +import de.t14d3.zones.Zones; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class PlaceholderAPI extends PlaceholderExpansion { + private final Zones plugin; + + public PlaceholderAPI(Zones plugin) { + this.plugin = plugin; + } + + @Override + @NotNull + public String getAuthor() { + return "T14D3"; + } + + @Override + @NotNull + public String getIdentifier() { + return "zones"; + } + + @Override + @NotNull + public String getVersion() { + return plugin.getPluginMeta().getVersion(); + } + + @Override + public boolean persist() { + return true; + } + + @Override + public String onPlaceholderRequest(Player player, @NotNull String params) { + if (params.equalsIgnoreCase("get_here")) { + return plugin.getRegionManager().getRegionsAtAsync(player.getLocation()).join().get(0).getName(); + } + + return null; + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0808ca4..a9b2e5c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -2,6 +2,7 @@ name: Zones version: '${version}' main: de.t14d3.zones.Zones api-version: '1.21' +softdepend: [ "PlaceholderAPI" ] commands: zone: description: Main Command From 3548c8911064b986b596f0ac358597176a909731 Mon Sep 17 00:00:00 2001 From: T14D3 <73843330+T14D3@users.noreply.github.com> Date: Thu, 9 Jan 2025 17:57:54 +0100 Subject: [PATCH 2/2] Added more placeholders, added placeholder tabcomplete --- src/main/java/de/t14d3/zones/Region.java | 18 +++ .../zones/integrations/PlaceholderAPI.java | 146 +++++++++++++++++- 2 files changed, 162 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/t14d3/zones/Region.java b/src/main/java/de/t14d3/zones/Region.java index b3c0f41..24e8e51 100644 --- a/src/main/java/de/t14d3/zones/Region.java +++ b/src/main/java/de/t14d3/zones/Region.java @@ -111,6 +111,13 @@ void setMax(BlockVector max) { this.max.set(max.getBlockX(), max.getBlockY(), max.getBlockZ()); } + /** + * Get the members of this region and their permissions.
+ * Use {@link de.t14d3.zones.PermissionManager#hasPermission} to check player permissions. + * + * @return {@code Map permissions>} + * @see de.t14d3.zones.PermissionManager#hasPermission + */ public Map> getMembers() { return members; } @@ -190,6 +197,17 @@ void addMemberPermission(UUID uuid, String permission, String value, RegionManag regionManager.saveRegion(key, this); // Ensure changes are saved } + public @Nullable UUID getOwner() { + for (Map.Entry> entry : members.entrySet()) { + Map map = entry.getValue(); + if (map.containsKey("role") && map.get("role").equalsIgnoreCase("owner")) { + return entry.getKey(); + } + } + return null; + } + + JsonObject getAsJson() { JsonObject json = new JsonObject(); json.addProperty("name", getName()); diff --git a/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java b/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java index bbe4ae3..56fd31e 100644 --- a/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java +++ b/src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java @@ -1,10 +1,15 @@ package de.t14d3.zones.integrations; +import de.t14d3.zones.Region; import de.t14d3.zones.Zones; +import de.t14d3.zones.utils.Actions; import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.List; + public class PlaceholderAPI extends PlaceholderExpansion { private final Zones plugin; @@ -35,12 +40,149 @@ public boolean persist() { return true; } + @Override + public @NotNull List getPlaceholders() { + return List.of("%zones_get_name%", + "%zones_get_key%", + "%zones_get_members%", + "%zones_get_owner%", + "%zones_get_min%", + "%zones_get_min_x%", + "%zones_get_min_y%", + "%zones_get_min_z%", + "%zones_get_max%", + "%zones_get_max_x%", + "%zones_get_max_y%", + "%zones_get_max_z%", + "%zones_is_member%", + "%zones_can_place_hand%", + "%zones_can_break_target%", + "%zones_can__%"); + } + @Override public String onPlaceholderRequest(Player player, @NotNull String params) { - if (params.equalsIgnoreCase("get_here")) { - return plugin.getRegionManager().getRegionsAtAsync(player.getLocation()).join().get(0).getName(); + List regions = plugin.getRegionManager().getRegionsAtAsync(player.getLocation()).join(); + if (params.equalsIgnoreCase("get_name")) { + String name = ""; + if (!regions.isEmpty()) { + name = regions.get(0).getName(); + } + return name; + } + if (params.equalsIgnoreCase("get_key")) { + String key = ""; + if (!regions.isEmpty()) { + key = regions.get(0).getKey(); + } + return key; } + if (params.equalsIgnoreCase("get_members")) { + final String[] members = {""}; + regions.get(0).getMembers().keySet().forEach(uuid -> { + String member = Bukkit.getOfflinePlayer(uuid).getName() != null ? Bukkit.getOfflinePlayer(uuid).getName() : uuid.toString(); + if (members[0].isEmpty()) { + members[0] = member; + } else { + members[0] = members[0] + ", " + member; + } + } + ); + return members[0]; + } + if (params.equalsIgnoreCase("get_owner")) { + String owner = ""; + if (!regions.isEmpty()) { + if (regions.get(0).getOwner() != null) { + owner = Bukkit.getOfflinePlayer(regions.get(0).getOwner()).getName() != null + ? Bukkit.getOfflinePlayer(regions.get(0).getOwner()).getName() : ""; + } + } + return owner; + } + if (params.equalsIgnoreCase("get_min")) { + if (!regions.isEmpty()) { + return regions.get(0).getMinString(); + } + return ""; + } + if (params.equalsIgnoreCase("get_min_x")) { + if (!regions.isEmpty()) { + return String.valueOf(regions.get(0).getMin().getBlockX()); + } + return ""; + } + if (params.equalsIgnoreCase("get_min_y")) { + int minY = 0; + if (!regions.isEmpty()) { + minY = regions.get(0).getMin().getBlockY(); + } + return String.valueOf(minY); + } + if (params.equalsIgnoreCase("get_min_z")) { + if (!regions.isEmpty()) { + return String.valueOf(regions.get(0).getMin().getBlockZ()); + } + return ""; + } + if (params.equalsIgnoreCase("get_max")) { + if (!regions.isEmpty()) { + return regions.get(0).getMaxString(); + } + return ""; + } + if (params.equalsIgnoreCase("get_max_x")) { + if (!regions.isEmpty()) { + return String.valueOf(regions.get(0).getMax().getBlockX()); + } + return ""; + } + if (params.equalsIgnoreCase("get_max_y")) { + if (!regions.isEmpty()) { + return String.valueOf(regions.get(0).getMax().getBlockY()); + } + return ""; + } + if (params.equalsIgnoreCase("get_max_z")) { + if (!regions.isEmpty()) { + return String.valueOf(regions.get(0).getMax().getBlockZ()); + } + return ""; + } + if (params.equalsIgnoreCase("is_member")) { + if (!regions.isEmpty()) { + return regions.get(0).isMember(player.getUniqueId()) ? "true" : "false"; + } + return "false"; + } + if (params.equalsIgnoreCase("can_place_hand")) { + if (!regions.isEmpty()) { + return plugin.getPermissionManager().canInteract(player.getLocation(), player.getUniqueId(), Actions.PLACE, player.getInventory().getItemInMainHand().getType().name()) ? "true" : "false"; + } + return "false"; + } + if (params.equalsIgnoreCase("can_break_target")) { + if (!regions.isEmpty()) { + return plugin.getPermissionManager().canInteract( + player.getLocation(), + player.getUniqueId(), + Actions.BREAK, + player.getTargetBlockExact(5) != null + ? player.getTargetBlockExact(5).getType().name() : "") + ? "true" : "false"; + } + return "false"; + } + if (params.startsWith("can_")) { + String action = params.split("_")[1].toUpperCase(); + // There's definitely a better way to do this, but it works and I'm too lazy to find a better one + String type = params.substring(params.indexOf('_', params.indexOf('_') + 1) + 1); + if (!regions.isEmpty()) { + return plugin.getPermissionManager().canInteract(player.getLocation(), player.getUniqueId(), Actions.valueOf(action), type) ? "true" : "false"; + } + return "false"; + } return null; } }