Imprv. perm. handling, read only conf data

Improved permission handling (can't say fixed because it doesn't work, unless I forgot to test the last version)
Added ReadOnlyConfigData
This commit is contained in:
Norbi Peti 2019-05-04 03:06:22 +02:00
parent 0d4f3b09dd
commit 497c761cc0
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 70 additions and 7 deletions

View file

@ -109,6 +109,8 @@ public class ConfigData<T> {
} }
public void set(T value) { public void set(T value) {
if (this instanceof ReadOnlyConfigData)
return; //Safety for Discord channel/role data
Object val; Object val;
if (setter != null && value != null) if (setter != null && value != null)
val = setter.apply(value); val = setter.apply(value);

View file

@ -83,6 +83,24 @@ public final class IHaveConfig {
return (ConfigData<T>) data; return (ConfigData<T>) data;
} }
/**
* This method overload may be used with any class. The given default value will be run through the getter.
*
* @param path The path in config to use
* @param primitiveDef The <b>primitive</b> value to use by default
* @param getter A function that converts a primitive representation to the correct value
* @param setter A function that converts a value to a primitive representation
* @param <T> The type of this variable (can be any class)
* @return The data object that can be used to get or set the value
*/
@SuppressWarnings("unchecked")
public <T> ReadOnlyConfigData<T> getReadOnlyDataPrimDef(String path, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter) {
ConfigData<?> data = datamap.get(path);
if (data == null)
datamap.put(path, data = new ReadOnlyConfigData<>(config, path, getter.apply(primitiveDef), primitiveDef, getter, setter, saveAction));
return (ReadOnlyConfigData<T>) data;
}
/** /**
* This method overload should only be used with primitves or String. * This method overload should only be used with primitves or String.
* *

View file

@ -0,0 +1,15 @@
package buttondevteam.lib.architecture;
import org.bukkit.configuration.ConfigurationSection;
import java.util.function.Function;
public class ReadOnlyConfigData<T> extends ConfigData<T> {
ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Function<Object, T> getter, Function<T, Object> setter, Runnable saveAction) {
super(config, path, def, primitiveDef, getter, setter, saveAction);
}
ReadOnlyConfigData(ConfigurationSection config, String path, T def, Object primitiveDef, Runnable saveAction) {
super(config, path, def, primitiveDef, saveAction);
}
}

View file

@ -48,6 +48,9 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface Subcommand { public @interface Subcommand {
/**
* Allowed for OPs only by default
*/
String MOD_GROUP = "mod"; String MOD_GROUP = "mod";
/** /**
* Help text to show players. A usage message will be also shown below it. * Help text to show players. A usage message will be also shown below it.

View file

@ -1,6 +1,7 @@
package buttondevteam.lib.chat; package buttondevteam.lib.chat;
import buttondevteam.core.MainPlugin; import buttondevteam.core.MainPlugin;
import lombok.experimental.var;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
@ -14,20 +15,39 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
@Override @Override
public void registerCommand(ICommand2MC command) { public void registerCommand(ICommand2MC command) {
super.registerCommand(command, '/'); super.registerCommand(command, '/');
val perm = "thorpe.command." + command.getCommandPath().replace(' ', '.'); var perm = "thorpe.command." + command.getCommandPath().replace(' ', '.');
if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset
System.out.println("Adding perm " + perm + " with default: "
+ (modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test
if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset if (Bukkit.getPluginManager().getPermission(perm) == null) //Check needed for plugin reset
Bukkit.getPluginManager().addPermission(new Permission(perm, Bukkit.getPluginManager().addPermission(new Permission(perm,
modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test
for (val method : command.getClass().getMethods()) {
if (!method.isAnnotationPresent(Subcommand.class)) continue;
String pg = permGroup(command, method);
if (pg == null) continue;
perm = "thorpe." + pg;
if (Bukkit.getPluginManager().getPermission(perm) == null) //It may occur multiple times
System.out.println("Adding perm " + perm + " with default: "
+ PermissionDefault.OP); //Do not allow any commands that belong to a group
if (Bukkit.getPluginManager().getPermission(perm) == null) //It may occur multiple times
Bukkit.getPluginManager().addPermission(new Permission(perm,
//pg.equals(Subcommand.MOD_GROUP) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only
PermissionDefault.OP)); //Do not allow any commands that belong to a group
}
} }
@Override @Override
public boolean hasPermission(Command2MCSender sender, ICommand2MC command, Method method) { public boolean hasPermission(Command2MCSender sender, ICommand2MC command, Method method) {
String pg; String pg;
return modOnly(command) String perm = modOnly(command)
? MainPlugin.permission.has(sender.getSender(), "tbmc.admin") ? "tbmc.admin"
: (pg = permGroup(command, method)) != null : (pg = permGroup(command, method)) != null //TODO: This way we can't grant specific perms if it has a perm group
? MainPlugin.permission.has(sender.getSender(), pg) ? "thorpe." + pg
: MainPlugin.permission.has(sender.getSender(), "thorpe.command." + command.getCommandPath().replace(' ', '.')); : "thorpe.command." + command.getCommandPath().replace(' ', '.');
//noinspection deprecation
System.out.println("Has permission " + perm + ": " + MainPlugin.permission.playerHas((String) null, sender.getSender().getName(), perm));
return MainPlugin.permission.playerHas((String) null, sender.getSender().getName(), perm);
} }
/** /**
@ -47,8 +67,13 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
* @return The permission group for the subcommand or null * @return The permission group for the subcommand or null
*/ */
private String permGroup(ICommand2MC command, Method method) { private String permGroup(ICommand2MC command, Method method) {
//System.out.println("Perm group for command " + command.getClass().getSimpleName() + " and method " + method.getName());
val sc = method.getAnnotation(Subcommand.class); val sc = method.getAnnotation(Subcommand.class);
if (sc != null && sc.permGroup().length() > 0) return sc.permGroup(); if (sc != null && sc.permGroup().length() > 0) {
//System.out.println("Returning sc.permGroup(): " + sc.permGroup());
return sc.permGroup();
}
//System.out.println("Returning getAnnForValue(" + command.getClass().getSimpleName() + ", ...): " + getAnnForValue(command.getClass(), CommandClass.class, CommandClass::permGroup, null));
return getAnnForValue(command.getClass(), CommandClass.class, CommandClass::permGroup, null); return getAnnForValue(command.getClass(), CommandClass.class, CommandClass::permGroup, null);
} }