Skip to content

Commit

Permalink
Added more placeholders, added placeholder tabcomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
T14D3 committed Jan 9, 2025
1 parent 041cf30 commit 3548c89
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 2 deletions.
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
146 changes: 144 additions & 2 deletions src/main/java/de/t14d3/zones/integrations/PlaceholderAPI.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -35,12 +40,149 @@ 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) {
if (params.equalsIgnoreCase("get_here")) {
return plugin.getRegionManager().getRegionsAtAsync(player.getLocation()).join().get(0).getName();
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;
}
}

0 comments on commit 3548c89

Please sign in to comment.