Skip to content

Commit

Permalink
PlaceholderAPI support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
T14D3 authored Jan 9, 2025
2 parents 7dd8b6e + 3548c89 commit e3da76c
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 2 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ repositories {
name = "sonatype"
url = "https://oss.sonatype.org/content/groups/public/"
}
maven {
url = "https://repo.extendedclip.com/releases/"
}
maven {
name = "EngineHub"
url = "https://maven.enginehub.org/repo/"
Expand All @@ -28,8 +31,8 @@ repositories {

dependencies {
compileOnly 'io.papermc.paper:paper-api:1.21.1-R0.1-SNAPSHOT'
compileOnly 'me.clip:placeholderapi:2.11.6'
compileOnly 'com.sk89q.worldedit:worldedit-bukkit:7.3.0'
//paperweight.paperDevBundle("1.21.1-R0.1-SNAPSHOT")
implementation(platform("com.intellectualsites.bom:bom-newest:1.52"))
compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Core")
compileOnly("com.fastasyncworldedit:FastAsyncWorldEdit-Bukkit")
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/de/t14d3/zones/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -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. <br>
* Use {@link de.t14d3.zones.PermissionManager#hasPermission} to check player permissions.
*
* @return {@code Map<UUID player, Map<String permission, String value> permissions>}
* @see de.t14d3.zones.PermissionManager#hasPermission
*/
public Map<UUID, Map<String, String>> getMembers() {
return members;
}
Expand Down Expand Up @@ -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<UUID, Map<String, String>> entry : members.entrySet()) {
Map<String, String> 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());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/de/t14d3/zones/Zones.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sk89q.worldedit.WorldEdit;
import de.t14d3.zones.integrations.FAWEIntegration;
import de.t14d3.zones.integrations.WorldEditSession;
import de.t14d3.zones.integrations.PlaceholderAPI;
import de.t14d3.zones.listeners.CommandListener;
import de.t14d3.zones.listeners.PlayerInteractListener;
import de.t14d3.zones.listeners.PlayerQuitListener;
Expand Down Expand Up @@ -97,6 +98,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!");
}
if (getServer().getPluginManager().getPlugin("WorldEdit") != null) {
WorldEdit.getInstance().getEventBus().register(new WorldEditSession(this));
getLogger().info("WorldEdit Integration enabled.");
Expand Down
188 changes: 188 additions & 0 deletions src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
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;

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 @NotNull List<String> 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_<action>_<type>%");
}

@Override
public String onPlaceholderRequest(Player player, @NotNull String params) {
List<Region> 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;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Zones
version: '${version}'
main: de.t14d3.zones.Zones
api-version: '1.21'
softdepend: [ WorldEdit, FastAsyncWorldEdit ]
softdepend: [ WorldEdit, FastAsyncWorldEdit, PlaceholderAPI ]
commands:
zone:
description: Main Command
Expand Down

0 comments on commit e3da76c

Please sign in to comment.