Skip to content

Commit

Permalink
Move command handling to Brigadier (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
T14D3 authored Jan 16, 2025
2 parents 97fa233 + 626e4b3 commit 41d6002
Show file tree
Hide file tree
Showing 18 changed files with 700 additions and 175 deletions.
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'de.t14d3'
version = '0.1.5'
version = '0.1.6'

repositories {
mavenCentral()
Expand Down Expand Up @@ -70,6 +70,9 @@ processResources {
filesMatching('plugin.yml') {
expand props
}
filesMatching('paper-plugin.yml') {
expand props
}
}

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

import de.t14d3.zones.brigadier.Command;
import de.t14d3.zones.utils.Flags;
import de.t14d3.zones.utils.Utils;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.bootstrap.PluginProviderContext;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

@SuppressWarnings("UnstableApiUsage")
public class PaperBootstrap implements PluginBootstrap {

private Zones zones;
private Flags flags;

@Override
public void bootstrap(BootstrapContext context) {
flags = new Flags();
for (Map.Entry<String, String> entry : Utils.defaultFlags().entrySet()) {
flags.registerFlag(entry.getKey(), entry.getValue());
}
Command cmd = new Command(this);
LifecycleEventManager<@NotNull BootstrapContext> eventManager = context.getLifecycleManager();
eventManager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
final Commands commands = event.registrar();
commands.register(cmd.node());
});
}

@Override
public @NotNull JavaPlugin createPlugin(@NotNull PluginProviderContext context) {
zones = new Zones(this);
return zones;
}

public Flags getFlags() {
return flags;
}


}
24 changes: 9 additions & 15 deletions src/main/java/de/t14d3/zones/PermissionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ private boolean calculatePermission(String who, String permission, String type,
return hasPermission(who, permission, type, region.getParentRegion(this.regionManager));
}
if (permissions.containsKey("group")) {
if (who.startsWith(":group-") && !Zones.getInstance().getConfig().getBoolean("allow-group-recursion", false)) {
if (who.startsWith("+group-") && !Zones.getInstance().getConfig().getBoolean("allow-group-recursion", false)) {
Zones.getInstance().getLogger().severe("Recursive group permissions detected!! Groups are not allowed to contain other groups!");
Zones.getInstance().getLogger().severe("Group '" + who.substring(7) + "' contains 'group' permission entry in region '" + region.getKey() + "'");
Zones.getInstance().getLogger().severe("If you are 100% sure this is fine, add 'allow-group-recursion: true' to your config.yml");
return false;
}
for (String group : permissions.get("group").split(",")) {
return hasPermission(":group-" + group, permission, type, region);
return hasPermission("+group-" + group, permission, type, region);
}
}
// Nothing found, deny access
Expand All @@ -203,38 +203,32 @@ private boolean calculatePermission(String who, String permission, String type,
// Analyze permission values
boolean explicitAllow = false;
boolean explicitDeny = false;
boolean result = false;

if (value != null) {
for (String permittedValue : value.split(",")) {
permittedValue = permittedValue.trim(); // Trim whitespace

// Check for wildcard allow
if ("*".equals(permittedValue) || "true".equalsIgnoreCase(permittedValue)) {
explicitAllow = true;
result = true;
}
// Check for wildcard deny
else if ("! *".equals(permittedValue) || "false".equalsIgnoreCase(permittedValue)) {
explicitDeny = true;
else if ("!*".equals(permittedValue) || "false".equalsIgnoreCase(permittedValue)) {
result = false;
}
// Check for specific type allow
else if (permittedValue.equalsIgnoreCase(type)) {
explicitAllow = true;
result = true;
}
// Check for specific type deny
else if (permittedValue.equalsIgnoreCase("!" + type)) {
explicitDeny = true;
result = false;
}
}
}

// Determine final access based on explicit allow/deny flags
if (explicitDeny) {
return false;
} else if (explicitAllow) {
return true;
}
// Deny by default
return false;
return result;
}

public boolean isAdmin(String who, Region region) {
Expand Down
25 changes: 24 additions & 1 deletion src/main/java/de/t14d3/zones/Region.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,39 @@ public Map<String, Map<String, String>> getMembers() {
return members;
}

/**
* Get the names of all groups in this region
*
* @return List of group names
* @since 0.1.5
*/
public List<String> getGroupNames() {
List<String> groupNames = new ArrayList<>();
for (Map.Entry<String, Map<String, String>> entry : members.entrySet()) {
if (entry.getKey().startsWith(":group-")) {
if (entry.getKey().startsWith("+group-")) {
groupNames.add(entry.getKey());
}
}
return groupNames;
}

/**
* Get the members of a group in this region
*
* @param group Group name
* @return List of members
* @since 0.1.6
*/
public List<String> getGroupMembers(String group) {
List<String> groupMembers = new ArrayList<>();
for (Map.Entry<String, Map<String, String>> entry : members.entrySet()) {
if (entry.getValue().containsKey("group") && entry.getValue().get("group").contains(group.substring(7))) {
groupMembers.add(entry.getKey());
}
}
return groupMembers;
}

void setMembers(Map<String, Map<String, String>> members, RegionManager regionManager, String key) {
this.members = members;
regionManager.saveRegion(key, this); // Ensure changes are saved
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/de/t14d3/zones/Zones.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import de.t14d3.zones.listeners.PlayerInteractListener;
import de.t14d3.zones.listeners.PlayerQuitListener;
import de.t14d3.zones.utils.*;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.plugin.lifecycle.event.LifecycleEventManager;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import it.unimi.dsi.fastutil.Pair;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BoundingBox;

Expand All @@ -37,9 +33,14 @@ public final class Zones extends JavaPlugin {
private Types types;
private Messages messages;
private static Zones instance;
private CommandListener commandListener;
private PaperBootstrap bootstrap;

public Zones(PaperBootstrap bootstrap) {
this.bootstrap = bootstrap;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public void onEnable() {
instance = this;
// Initialize PermissionManager first without RegionManager
Expand Down Expand Up @@ -85,13 +86,8 @@ public void onEnable() {
types = new Types();
types.populateTypes();

// Register command executor and tab completer
LifecycleEventManager<Plugin> manager = this.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
final Commands commands = event.registrar();
commands.register("zone", new CommandListener(this, regionManager));
});

this.commandListener = new CommandListener(this, regionManager);
// Register saving task
if (getSavingMode() == Utils.SavingModes.PERIODIC) {
getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
Expand Down Expand Up @@ -154,4 +150,8 @@ public Utils.SavingModes getSavingMode() {
public static Zones getInstance() {
return instance;
}

public CommandListener getCommandListener() {
return commandListener;
}
}
Loading

0 comments on commit 41d6002

Please sign in to comment.