Skip to content

Commit

Permalink
Global type registers, better tabcompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
T14D3 committed Jan 2, 2025
1 parent 906b719 commit d50162d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 13 deletions.
7 changes: 7 additions & 0 deletions src/main/java/de/t14d3/zones/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public boolean isMember(UUID uuid) {
return this.members.containsKey(uuid);
}

public boolean isAdmin(UUID uuid) {
if (this.members.containsKey(uuid) && this.members.get(uuid).containsKey("role")) {
return this.members.get(uuid).get("role").equals("admin") || this.members.get(uuid).get("role").equals("owner");
}
return false; // Default to false
}

public Map<String, String> getMemberPermissions(UUID uuid) {
return this.members.get(uuid);
}
Expand Down
8 changes: 8 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 de.t14d3.zones.listeners.PlayerInteractListener;
import de.t14d3.zones.listeners.PlayerQuitListener;
import de.t14d3.zones.utils.BeaconUtils;
import de.t14d3.zones.utils.Types;
import de.t14d3.zones.utils.Utils;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
Expand All @@ -17,6 +18,7 @@

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

Expand All @@ -28,6 +30,7 @@ public final class Zones extends JavaPlugin {
private BeaconUtils beaconUtils;
public Map<UUID, Pair<Location, Location>> selection = new HashMap<>();
private Map<String, String> messages;
public List<String> types;

@Override
@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -71,6 +74,11 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);
getLogger().info("Zones plugin has been enabled and regions are loaded.");

// Populate Types
Types types = new Types();
types.populateTypes();
this.types = types.allTypes;

// Register command executor and tab completer
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Expand Down
47 changes: 34 additions & 13 deletions src/main/java/de/t14d3/zones/listeners/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import de.t14d3.zones.Region;
import de.t14d3.zones.RegionManager;
import de.t14d3.zones.Zones;
import de.t14d3.zones.utils.Actions;
import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import it.unimi.dsi.fastutil.Pair;
Expand Down Expand Up @@ -101,22 +102,42 @@ public void execute(CommandSourceStack stack, String[] args) {
// TODO: Implement proper set command handling, non-functional for now
if (args[0].equalsIgnoreCase("set")) {
if (args.length == 2) {
List<String> builder = new ArrayList<>();
regionManager.regions().forEach((regionKey, region) -> {
if (pm.hasPermission(player.getUniqueId(), "role", "owner", region)) {
builder.add(regionKey);
List<String> suggestions = new ArrayList<>();
for (String regionKey : regionManager.regions().keySet()) {
if (regionKey.toLowerCase().startsWith(args[1].toLowerCase())
&& regionManager.regions().get(regionKey).isAdmin(player.getUniqueId())) {
suggestions.add(regionKey);
}
});
return builder;
} else if (args.length == 3) {
String regionKey = args[1];
Region region = regionManager.regions().get(regionKey);
if (region == null) {
return List.of("");
}
if (pm.hasPermission(player.getUniqueId(), "role", "owner", region)) {
return List.of("role", "name", "min", "max", "members");
return suggestions;
}
if (args.length == 3) {
List<String> suggestions = new ArrayList<>();
for (OfflinePlayer offlinePlayer : Bukkit.getOfflinePlayers()) {
if (offlinePlayer.getName() != null && offlinePlayer.getName().toLowerCase().startsWith(args[2].toLowerCase())) {
suggestions.add(offlinePlayer.getName());
}
}
return suggestions;
}
if (args.length == 4) {
List<String> suggestions = new ArrayList<>();
for (Actions action : Actions.values()) {
if (action.name().toLowerCase().startsWith(args[3].toLowerCase())) {
suggestions.add(action.name());
}
}
return suggestions;
}
if (args.length >= 5) {
List<String> suggestions = new ArrayList<>();
List<String> types = plugin.types;
for (String value : types) {
if (value.toLowerCase().startsWith(args[4].toLowerCase())) {
suggestions.add(value);
}
}
return suggestions;
}
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/de/t14d3/zones/utils/Types.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package de.t14d3.zones.utils;

import org.bukkit.Material;
import org.bukkit.block.Container;
import org.bukkit.block.data.Powerable;
import org.bukkit.entity.EntityType;

import java.util.ArrayList;
import java.util.List;


public class Types {

/**
* List of all possible permission types/values.
* Includes all Block Materials and EntityTypes, their negated forms
* and the wildcards "true" and "false".
*/
public List<String> allTypes = new ArrayList<>();
public List<String> blockTypes = new ArrayList<>();
public List<String> entityTypes = new ArrayList<>();
public List<String> containerTypes = new ArrayList<>();
public List<String> redstoneTypes = new ArrayList<>();


public void populateTypes() {
for (Material material : Material.values()) {
if (material.isBlock()) {
allTypes.add(material.name());
allTypes.add("!" + material.name());
}
}
for (EntityType entityType : EntityType.values()) {
allTypes.add(entityType.name());
allTypes.add("!" + entityType.name());
}
allTypes.add("true");
allTypes.add("false");

// Populate blockTypes
for (Material material : Material.values()) {
if (material.isBlock()) {
blockTypes.add(material.name());
blockTypes.add("!" + material.name());
}
}
blockTypes.add("true");
blockTypes.add("false");

// Populate entityTypes
for (EntityType entityType : EntityType.values()) {
entityTypes.add(entityType.name());
entityTypes.add("!" + entityType.name());
}
entityTypes.add("true");
entityTypes.add("false");

// Populate containerTypes
for (Material material : Material.values()) {
if (material.isBlock() && material.createBlockData().createBlockState() instanceof Container) {
containerTypes.add(material.name());
containerTypes.add("!" + material.name());
}
}
containerTypes.add("true");
containerTypes.add("false");

// Populate redstoneTypes
for (Material material : Material.values()) {
if (material.isBlock() && material.createBlockData().createBlockState() instanceof Powerable) {
redstoneTypes.add(material.name());
redstoneTypes.add("!" + material.name());
}
}
redstoneTypes.add("true");
redstoneTypes.add("false");
}
}

0 comments on commit d50162d

Please sign in to comment.