Permission and config improvements, fixes #66

Merged
NorbiPeti merged 13 commits from dev into master 2019-06-06 18:39:52 +00:00
4 changed files with 32 additions and 20 deletions
Showing only changes of commit b1ea027d1c - Show all commits

View file

@ -18,6 +18,7 @@
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="module" module-name="ButtonCore (1) (com.github.TBMCPlugins.ButtonCore)" />
<orderEntry type="library" name="Maven: org.reflections:reflections:0.9.10" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:annotations:2.0.1" level="project" />
<orderEntry type="library" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />

View file

@ -132,12 +132,10 @@ public abstract class Component<TP extends JavaPlugin> {
if (component.enabled == enabled) return; //Don't do anything
if (component.enabled = enabled) {
updateConfig(component.getPlugin(), component);
System.out.println("Enabling component " + component.getClassName());
component.enable();
if (ButtonPlugin.configGenAllowed(component))
IHaveConfig.pregenConfig(component, null);
} else {
System.out.println("Disabling component " + component.getClassName());
component.disable();
component.plugin.saveConfig();
TBMCChatAPI.RemoveCommands(component);

View file

@ -1,5 +1,6 @@
package buttondevteam.lib.chat;
import buttondevteam.core.MainPlugin;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.ThorpeUtils;
import buttondevteam.lib.player.ChromaGamerBase;
@ -9,7 +10,9 @@ import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.experimental.var;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.scheduler.BukkitTask;
import java.io.InputStreamReader;
import java.lang.annotation.ElementType;
@ -20,9 +23,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.*;
import java.util.function.Function;
/**
@ -52,6 +53,7 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
* Allowed for OPs only by default
*/
String MOD_GROUP = "mod";
/**
* Help text to show players. A usage message will be also shown below it.
*/
@ -76,6 +78,31 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
public String[] helpText;
}
protected static class SubcommandHelpData<T extends ICommand2> extends SubcommandData<T> {
private final TreeSet<String> ht = new TreeSet<>();
private BukkitTask task;
public SubcommandHelpData(Method method, T command, String[] helpText) {
super(method, command, helpText);
}
public void addSubcommand(String command) {
ht.add(command);
if (task == null)
task = Bukkit.getScheduler().runTask(MainPlugin.Instance, () -> {
helpText = new String[ht.size() + 1]; //This will only run after the server is started List<E> list = new ArrayList<E>(size());
helpText[0] = "§6---- Subcommands ----"; //TODO: There may be more to the help text
int i = 1;
for (Iterator<String> iterator = ht.iterator();
iterator.hasNext() && i < helpText.length; i++) {
String e = iterator.next();
helpText[i] = e;
}
task = null; //Run again, if needed
});
}
}
@RequiredArgsConstructor
protected static class ParamConverter<T> {
public final Function<String, T> converter;
@ -165,10 +192,8 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
continue;
} else if (Number.class.isAssignableFrom(cl) || cl.isPrimitive()) {
try {
//System.out.println("Converting "+param+" param to "+cl.getSimpleName());
//noinspection unchecked
Number n = ThorpeUtils.convertNumber(NumberFormat.getInstance().parse(param), (Class<? extends Number>) cl);
//System.out.println(n.getClass().getSimpleName()+" with value "+n);
params.add(n);
} catch (ParseException e) {
sender.sendMessage("§c'" + param + "' is not a number.");
@ -186,7 +211,6 @@ public abstract class Command2<TC extends ICommand2, TP extends Command2Sender>
}
params.add(cparam);
}
//System.out.println("Our params: "+params);
try {
val ret = sd.method.invoke(sd.command, params.toArray()); //I FORGOT TO TURN IT INTO AN ARRAY (for a long time)
if (ret instanceof Boolean) {

View file

@ -6,7 +6,6 @@ import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
@ -20,9 +19,6 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
public void registerCommand(ICommand2MC command) {
super.registerCommand(command, '/');
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
Bukkit.getPluginManager().addPermission(new Permission(perm,
modOnly(command) ? PermissionDefault.OP : PermissionDefault.TRUE)); //Allow commands by default, unless it's mod only - TODO: Test
@ -31,9 +27,6 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
String pg = permGroup(command, method);
if (pg.length() == 0) 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
@ -54,10 +47,9 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
for (String perm : perms) {
if (perm != null) {
if (p) { //Use OfflinePlayer to avoid fetching player data
if (sender.getSender() instanceof Player)
if (sender.getSender() instanceof OfflinePlayer)
p = MainPlugin.permission.playerHas(null, (OfflinePlayer) sender.getSender(), perm);
else
//p = MainPlugin.permission.groupHas((String) null, MainPlugin.Instance.unconnPermGroup().get(), perm);
p = MainPlugin.permission.playerHas(null, Bukkit.getOfflinePlayer(new UUID(0, 0)), perm);
} else break; //If any of the permissions aren't granted then don't allow
}
@ -82,13 +74,10 @@ public class Command2MC extends Command2<ICommand2MC, Command2MCSender> {
* @return The permission group for the subcommand or empty string
*/
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);
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, "");
}