Skip to content

Commit

Permalink
Add config options for Default Claim Permissions (#160)
Browse files Browse the repository at this point in the history
* Added permission config for default claim permissions.

* Fixed unused imports and unnecesary semicolons.

* Added changes for pr #160

* Added Function into constructor for pr #160
  • Loading branch information
Paperboypaddy authored Dec 16, 2024
1 parent 56b40ef commit 9d77ca8
Show file tree
Hide file tree
Showing 12 changed files with 500 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/main/java/codes/wasabi/xclaim/api/enums/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@
import codes.wasabi.xclaim.api.Claim;
import codes.wasabi.xclaim.api.enums.permission.PermissionHandler;
import codes.wasabi.xclaim.api.enums.permission.handler.*;
import codes.wasabi.xclaim.config.struct.sub.DefaultPermissionsConfig;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Constructor;
import java.util.function.Function;

public enum Permission {
BUILD("perm-build-name", "perm-build-description", TrustLevel.TRUSTED, BuildBreakHandler.Build.class),
BREAK("perm-break-name", "perm-break-description", TrustLevel.TRUSTED, BuildBreakHandler.Break.class),
ENTER("perm-enter-name", "perm-enter-description", TrustLevel.ALL, EnterHandler.class),
INTERACT("perm-interact-name", "perm-interact-description", TrustLevel.VETERANS, InteractHandler.class),
CHEST_OPEN("perm-chest-open-name", "perm-chest-open-description", TrustLevel.TRUSTED, InteractHandler.Chests.class, (byte) 1),
ENT_PLACE("perm-ent-place-name", "perm-ent-place-description", TrustLevel.VETERANS, InteractHandler.Entities.class, (byte) 1),
VEHICLE_PLACE("perm-vehicle-place-name", "perm-vehicle-place-description", TrustLevel.VETERANS, InteractHandler.Vehicles.class, (byte) 2),
FIRE_USE("perm-fire-use-name", "perm-fire-use-description", TrustLevel.TRUSTED, InteractHandler.Flammable.class, (byte) 1),
ENTITY_DAMAGE_FRIENDLY("perm-entity-damage-friendly-name", "perm-entity-damage-friendly-description", TrustLevel.VETERANS, DamageHandler.Friendly.class),
ENTITY_DAMAGE_HOSTILE("perm-entity-damage-hostile-name", "perm-entity-damage-hostile-description", TrustLevel.VETERANS, DamageHandler.Hostile.class),
ENTITY_DAMAGE_VEHICLE("perm-entity-damage-vehicle-name", "perm-entity-damage-vehicle-description", TrustLevel.VETERANS, DamageHandler.Vehicle.class),
ENTITY_DAMAGE_NL("perm-entity-damage-nl-name", "perm-entity-damage-nl-description", TrustLevel.VETERANS, DamageHandler.NonLiving.class),
ENTITY_DAMAGE_MISC("perm-entity-damage-misc-name", "perm-entity-damage-misc-description", TrustLevel.ALL, DamageHandler.Misc.class),
EXPLODE("perm-explode-name", "perm-explode-description", TrustLevel.TRUSTED, ExplosionHandler.class),
ITEM_DROP("perm-item-drop-name", "perm-item-drop-description", TrustLevel.ALL, DropHandler.class),
MANAGE("perm-manage-name", "perm-manage-description", TrustLevel.NONE),
DELETE("perm-delete-name", "perm-delete-description", TrustLevel.NONE);
BUILD("perm-build-name", "perm-build-description", DefaultPermissionsConfig::defaultBuild, BuildBreakHandler.Build.class),
BREAK("perm-break-name", "perm-break-description", DefaultPermissionsConfig::defaultBreak, BuildBreakHandler.Break.class),
ENTER("perm-enter-name", "perm-enter-description", DefaultPermissionsConfig::defaultEnter, EnterHandler.class),
INTERACT("perm-interact-name", "perm-interact-description", DefaultPermissionsConfig::defaultInteract, InteractHandler.class),
CHEST_OPEN("perm-chest-open-name", "perm-chest-open-description", DefaultPermissionsConfig::defaultChestOpen, InteractHandler.Chests.class, (byte) 1),
ENT_PLACE("perm-ent-place-name", "perm-ent-place-description", DefaultPermissionsConfig::defaultEntPlace, InteractHandler.Entities.class, (byte) 1),
VEHICLE_PLACE("perm-vehicle-place-name", "perm-vehicle-place-description", DefaultPermissionsConfig::defaultVehiclePlace, InteractHandler.Vehicles.class, (byte) 2),
FIRE_USE("perm-fire-use-name", "perm-fire-use-description", DefaultPermissionsConfig::defaultFireUse, InteractHandler.Flammable.class, (byte) 1),
ENTITY_DAMAGE_FRIENDLY("perm-entity-damage-friendly-name", "perm-entity-damage-friendly-description", DefaultPermissionsConfig::defaultEntFriendly, DamageHandler.Friendly.class),
ENTITY_DAMAGE_HOSTILE("perm-entity-damage-hostile-name", "perm-entity-damage-hostile-description", DefaultPermissionsConfig::defaultEntDamageHost, DamageHandler.Hostile.class),
ENTITY_DAMAGE_VEHICLE("perm-entity-damage-vehicle-name", "perm-entity-damage-vehicle-description", DefaultPermissionsConfig::defaultEntDamageVehicle, DamageHandler.Vehicle.class),
ENTITY_DAMAGE_NL("perm-entity-damage-nl-name", "perm-entity-damage-nl-description", DefaultPermissionsConfig::defaultEntDamageNL, DamageHandler.NonLiving.class),
ENTITY_DAMAGE_MISC("perm-entity-damage-misc-name", "perm-entity-damage-misc-description", DefaultPermissionsConfig::defaultEntDamageMisc, DamageHandler.Misc.class),
EXPLODE("perm-explode-name", "perm-explode-description", DefaultPermissionsConfig::defaultExplode, ExplosionHandler.class),
ITEM_DROP("perm-item-drop-name", "perm-item-drop-description", DefaultPermissionsConfig::defaultItemDrop, DropHandler.class),
MANAGE("perm-manage-name", "perm-manage-description", DefaultPermissionsConfig::defaultManage),
DELETE("perm-delete-name", "perm-delete-description", DefaultPermissionsConfig::defaultDelete);

/**
* Wraps #valueOf with legacy support
Expand Down Expand Up @@ -55,34 +57,34 @@ public enum Permission {
break;
default:
throw e;
};
}
}
return p;
}

private final String printName;
private final String description;
private final TrustLevel defaultTrust;
private final Function<DefaultPermissionsConfig, String> computeDefaultTrust;
private final Class<? extends PermissionHandler> handlerClass;
private final byte priority;
Permission(String printName, String description, TrustLevel defaultTrust, Class<? extends PermissionHandler> handlerClass, byte priority) {
Permission(String printName, String description, Function<DefaultPermissionsConfig, String> computeDefaultTrust, Class<? extends PermissionHandler> handlerClass, byte priority) {
this.printName = printName;
this.description = description;
this.defaultTrust = defaultTrust;
this.computeDefaultTrust = computeDefaultTrust;
this.handlerClass = handlerClass;
this.priority = priority;
}

Permission(String printName, String description, TrustLevel defaultTrust, Class<? extends PermissionHandler> handlerClass) {
this(printName, description, defaultTrust, handlerClass, (byte) 0);
Permission(String printName, String description, Function<DefaultPermissionsConfig, String> computeDefaultTrust, Class<? extends PermissionHandler> handlerClass) {
this(printName, description, computeDefaultTrust, handlerClass, (byte) 0);
}

Permission(String printName, String description, TrustLevel defaultTrust, byte priority) {
this(printName, description, defaultTrust, null, priority);
Permission(String printName, String description, Function<DefaultPermissionsConfig, String> computeDefaultTrust, byte priority) {
this(printName, description, computeDefaultTrust, null, priority);
}

Permission(String printName, String description, TrustLevel defaultTrust) {
this(printName, description, defaultTrust, null, (byte) 0);
Permission(String printName, String description, Function<DefaultPermissionsConfig, String> computeDefaultTrust) {
this(printName, description, computeDefaultTrust, null, (byte) 0);
}

public String getDescription() {
Expand All @@ -93,10 +95,6 @@ public String getPrintName() {
return XClaim.lang.get(printName);
}

public TrustLevel getDefaultTrust() {
return defaultTrust;
}

public byte getPriority() {
return priority;
}
Expand All @@ -117,4 +115,10 @@ public boolean hasHandler() {
return ph;
}

public @NotNull TrustLevel getDefaultTrust() {
DefaultPermissionsConfig config = XClaim.mainConfig.defaultPermissions(); // Get the current PermissionsConfig
String rawTrust = computeDefaultTrust.apply(config);// Fetch raw trust level string using the function
return TrustLevel.fromString(rawTrust); // Convert string to TrustLevel
}

}
8 changes: 8 additions & 0 deletions src/main/java/codes/wasabi/xclaim/api/enums/TrustLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ public enum TrustLevel {
public static @NotNull TrustLevel @NotNull [] ascending() {
return new TrustLevel[] { NONE, TRUSTED, VETERANS, ALL };
}

public static @NotNull TrustLevel fromString(@NotNull String raw) {
try {
return TrustLevel.valueOf(raw.toUpperCase());
} catch (IllegalArgumentException e) {
return TrustLevel.NONE; // Default fallback value
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class DefaultingRootConfig extends FilterRootConfig {
private final DefaultingWorldsConfig worlds;
private final DefaultingIntegrationsConfig integrations;
private final DefaultingGuiConfig gui;
private final DefaultingDefaultPermissionsConfig defaultPermissions;
public DefaultingRootConfig(@NotNull RootConfig backing) {
super(backing);
this.autoSave = new DefaultingAutoSaveConfig( backing.autoSave() );
Expand All @@ -21,6 +22,7 @@ public DefaultingRootConfig(@NotNull RootConfig backing) {
this.worlds = new DefaultingWorldsConfig( backing.worlds() );
this.integrations = new DefaultingIntegrationsConfig( backing.integrations() );
this.gui = new DefaultingGuiConfig( backing.gui() );
this.defaultPermissions = new DefaultingDefaultPermissionsConfig( backing.defaultPermissions() );
}

@Override
Expand Down Expand Up @@ -68,4 +70,9 @@ public DefaultingRootConfig(@NotNull RootConfig backing) {
return this.gui;
}

@Override
public @NotNull DefaultingDefaultPermissionsConfig defaultPermissions() {
return this.defaultPermissions;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package codes.wasabi.xclaim.config.impl.defaulting.sub;

import codes.wasabi.xclaim.config.impl.filter.sub.FilterDefaultPermissionsConfig;
import codes.wasabi.xclaim.config.struct.sub.DefaultPermissionsConfig;
import org.jetbrains.annotations.NotNull;

public final class DefaultingDefaultPermissionsConfig extends FilterDefaultPermissionsConfig {

public DefaultingDefaultPermissionsConfig(@NotNull DefaultPermissionsConfig backing) {
super(backing);
}

@Override
public @NotNull String defaultBuild(){
return this.nullFallback(this.backing().defaultBuild(), "TRUSTED");
}

@Override
public @NotNull String defaultBreak(){
return this.nullFallback(this.backing().defaultBreak(), "TRUSTED");
}

@Override
public @NotNull String defaultEnter(){
return this.nullFallback(this.backing().defaultEnter(), "ALL");
}

@Override
public @NotNull String defaultInteract(){
return this.nullFallback(this.backing().defaultInteract(), "VETERANS");
}

@Override
public @NotNull String defaultChestOpen(){
return this.nullFallback(this.backing().defaultChestOpen(), "TRUSTED");
}

@Override
public @NotNull String defaultEntPlace(){
return this.nullFallback(this.backing().defaultEntPlace(), "VETERANS");
}

@Override
public @NotNull String defaultVehiclePlace(){
return this.nullFallback(this.backing().defaultVehiclePlace(), "VETERANS");
}

@Override
public @NotNull String defaultFireUse(){
return this.nullFallback(this.backing().defaultFireUse(), "TRUSTED");
}

@Override
public @NotNull String defaultEntFriendly(){
return this.nullFallback(this.backing().defaultEntFriendly(), "VETERANS");
}

@Override
public @NotNull String defaultEntDamageHost(){
return this.nullFallback(this.backing().defaultEntDamageHost(), "VETERANS");
}

@Override
public @NotNull String defaultEntDamageVehicle(){
return this.nullFallback(this.backing().defaultEntDamageVehicle(), "VETERANS");
}

@Override
public @NotNull String defaultEntDamageNL(){
return this.nullFallback(this.backing().defaultEntDamageNL(), "VETERANS");
}

@Override
public @NotNull String defaultEntDamageMisc(){
return this.nullFallback(this.backing().defaultEntDamageMisc(), "ALL");
}

@Override
public @NotNull String defaultExplode(){
return this.nullFallback(this.backing().defaultExplode(), "TRUSTED");
}

@Override
public @NotNull String defaultItemDrop(){
return this.nullFallback(this.backing().defaultItemDrop(), "ALL");
}

@Override
public @NotNull String defaultManage(){
return this.nullFallback(this.backing().defaultManage(), "NONE");
}

@Override
public @NotNull String defaultDelete(){
return this.nullFallback(this.backing().defaultDelete(), "NONE");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package codes.wasabi.xclaim.config.impl.filter.sub;

import codes.wasabi.xclaim.config.impl.filter.FilterConfig;
import codes.wasabi.xclaim.config.struct.sub.DefaultPermissionsConfig;
import org.jetbrains.annotations.NotNull;

public abstract class FilterDefaultPermissionsConfig extends FilterConfig implements DefaultPermissionsConfig {

public FilterDefaultPermissionsConfig(@NotNull DefaultPermissionsConfig backing) {
super(backing);
}

@Override
protected @NotNull DefaultPermissionsConfig backing() {
return (DefaultPermissionsConfig) super.backing();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public final class TomlRootConfig extends TomlConfig implements RootConfig {
private final TomlWorldsConfig worlds;
private final TomlIntegrationsConfig integrations;
private final TomlGuiConfig gui;
private final TomlDefaultPermissionsConfig defaultPermissions;
public TomlRootConfig(@NotNull Toml table) {
super(table);
this.autoSave = new TomlAutoSaveConfig( this.getTable("auto-save"));
Expand All @@ -22,6 +23,7 @@ public TomlRootConfig(@NotNull Toml table) {
this.worlds = new TomlWorldsConfig( this.getTable("worlds"));
this.integrations = new TomlIntegrationsConfig(this.getTable("integrations"));
this.gui = new TomlGuiConfig( this.getTable("gui"));
this.defaultPermissions = new TomlDefaultPermissionsConfig( this.getTable("default-permissions"));
}

@Override
Expand Down Expand Up @@ -69,4 +71,9 @@ public TomlRootConfig(@NotNull Toml table) {
return this.integrations;
}

@Override
public @NotNull TomlDefaultPermissionsConfig defaultPermissions() {
return this.defaultPermissions;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package codes.wasabi.xclaim.config.impl.toml.sub;

import codes.wasabi.xclaim.config.impl.toml.TomlConfig;
import codes.wasabi.xclaim.config.struct.sub.DefaultPermissionsConfig;
import com.moandjiezana.toml.Toml;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnknownNullability;

public class TomlDefaultPermissionsConfig extends TomlConfig implements DefaultPermissionsConfig {

public TomlDefaultPermissionsConfig(@Nullable Toml table) {
super(table);
}

@Override
public @UnknownNullability String defaultBuild() {
return this.getString("build");
}

@Override
public @UnknownNullability String defaultBreak() {
return this.getString("break");
}

@Override
public @UnknownNullability String defaultEnter() {
return this.getString("enter");
}

@Override
public @UnknownNullability String defaultInteract() {
return this.getString("interact");
}

@Override
public @UnknownNullability String defaultChestOpen() {
return this.getString("chest-open");
}

@Override
public @UnknownNullability String defaultEntPlace() {
return this.getString("ent-place");
}

@Override
public @UnknownNullability String defaultVehiclePlace() {
return this.getString("vehicle_place");
}

@Override
public @UnknownNullability String defaultFireUse() {
return this.getString("fire-use");
}

@Override
public @UnknownNullability String defaultEntFriendly() {
return this.getString("entity-friendly");
}

@Override
public @UnknownNullability String defaultEntDamageHost() {
return this.getString("entity-damage-hostile");
}

@Override
public @UnknownNullability String defaultEntDamageVehicle() {
return this.getString("entity-damage-vehicle");
}

@Override
public @UnknownNullability String defaultEntDamageNL() {
return this.getString("entity-damage-nl");
}

@Override
public @UnknownNullability String defaultEntDamageMisc() {
return this.getString("entity-damage-misc");
}

@Override
public @UnknownNullability String defaultExplode() {
return this.getString("explode");
}

@Override
public @UnknownNullability String defaultItemDrop() {
return this.getString("item-drop");
}

@Override
public @UnknownNullability String defaultManage() {
return this.getString("manage");
}

@Override
public @UnknownNullability String defaultDelete() {
return this.getString("delete");
}
}
Loading

0 comments on commit 9d77ca8

Please sign in to comment.