added permission support for commands

This commit is contained in:
Erik Bročko 2018-09-05 18:26:21 +02:00
parent 8795e6d9cc
commit 91460c8367
3 changed files with 29 additions and 2 deletions

View File

@ -55,4 +55,8 @@ public class MarwCore extends JavaPlugin {
return this.features.get(name); return this.features.get(name);
} }
public String getPermissionPath() {
return "marw.core";
}
} }

View File

@ -5,11 +5,13 @@ import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import cz.marwland.mc.core.MarwCore; import cz.marwland.mc.core.MarwCore;
import net.md_5.bungee.api.ChatColor;
public abstract class Feature implements Listener { public abstract class Feature implements Listener {
protected final MarwCore plugin; protected final MarwCore plugin;
@ -20,6 +22,9 @@ public abstract class Feature implements Listener {
public Feature(MarwCore plugin, String name) { public Feature(MarwCore plugin, String name) {
this.plugin = plugin; this.plugin = plugin;
this.name = name; this.name = name;
if (name == null) {
this.name = this.getClass().getSimpleName().toLowerCase();
}
Method commandMap; Method commandMap;
try { try {
commandMap = plugin.getServer().getClass().getMethod("getCommandMap"); commandMap = plugin.getServer().getClass().getMethod("getCommandMap");
@ -27,7 +32,6 @@ public abstract class Feature implements Listener {
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void onEnable() { public void onEnable() {
@ -74,4 +78,15 @@ public abstract class Feature implements Listener {
return plugin.getConfigManager().getConfig(path); return plugin.getConfigManager().getConfig(path);
} }
public String getPermissionPath() {
return plugin.getPermissionPath() + "." + this.getName();
}
public boolean permissionMissingCheck(CommandSender sender, String permission) {
if (sender.hasPermission(permission))
return true;
sender.sendMessage(ChatColor.RED + "You don't have permission " + ChatColor.YELLOW + permission + ChatColor.RED + "!");
return false;
}
} }

View File

@ -26,7 +26,7 @@ public class Borders extends Feature {
EntryManager emgr = new EntryManager(); EntryManager emgr = new EntryManager();
public Borders(MarwCore plugin) { public Borders(MarwCore plugin) {
super(plugin, "heil"); super(plugin, null);
// String name, String description, String usageMessage, List<String> aliases) // String name, String description, String usageMessage, List<String> aliases)
this.addCommand(new BukkitCommand( this.addCommand(new BukkitCommand(
"worldborder", "worldborder",
@ -35,6 +35,9 @@ public class Borders extends Feature {
Arrays.asList() ) { Arrays.asList() ) {
@Override @Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) { public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!permissionMissingCheck(sender, this.getPermission()))
return false;
if (args.length < 2) { if (args.length < 2) {
sender.sendMessage( sender.sendMessage(
ChatColor.YELLOW + "Format: " + ChatColor.YELLOW + "Format: " +
@ -121,6 +124,11 @@ public class Borders extends Feature {
Collections.sort(hints, String.CASE_INSENSITIVE_ORDER); Collections.sort(hints, String.CASE_INSENSITIVE_ORDER);
return hints; return hints;
} }
@Override
public String getPermission() {
return getPermissionPath() + "." + this.getName().toLowerCase();
}
}); });
emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR, new Vector(0, 0, 0))); emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR, new Vector(0, 0, 0)));
// emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE)); // emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE));