0.2 update
This commit is contained in:
parent
7cdea1dafe
commit
6ce69ccce4
@ -1,127 +1,92 @@
|
||||
package net.ardakaz.exec;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Iterator;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandMap;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.command.defaults.BukkitCommand;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Exec extends JavaPlugin {
|
||||
private File configFile;
|
||||
private FileConfiguration config;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
this.getCommand("exec").setExecutor(new ExecCommand());
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
this.loadConfig();
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
private void registerCommands() {
|
||||
try {
|
||||
Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
|
||||
bukkitCommandMap.setAccessible(true);
|
||||
CommandMap commandMap = (CommandMap)bukkitCommandMap.get(Bukkit.getServer());
|
||||
if (commandMap instanceof SimpleCommandMap) {
|
||||
Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands");
|
||||
knownCommandsField.setAccessible(true);
|
||||
FileConfiguration config = this.getConfig();
|
||||
if (config.contains("commands")) {
|
||||
Iterator var6 = config.getConfigurationSection("commands").getKeys(false).iterator();
|
||||
|
||||
while(var6.hasNext()) {
|
||||
String cmd = (String)var6.next();
|
||||
final String script = config.getString("commands." + cmd + ".script");
|
||||
String description = config.getString("commands." + cmd + ".description", "");
|
||||
final String permission = config.getString("commands." + cmd + ".permission", "");
|
||||
String usage = config.getString("commands." + cmd + ".usage", "");
|
||||
BukkitCommand dynamicCommand = new BukkitCommand(cmd) {
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
return (Exec.this.new ExecCommand(script, permission)).onCommand(sender, this, label, args);
|
||||
}
|
||||
};
|
||||
dynamicCommand.setDescription(description);
|
||||
dynamicCommand.setPermission(permission);
|
||||
dynamicCommand.setUsage(usage);
|
||||
commandMap.register(cmd, dynamicCommand);
|
||||
}
|
||||
class ExecCommand implements CommandExecutor {
|
||||
|
||||
private FileConfiguration config;
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
Player player = null;
|
||||
if (sender instanceof Player) {
|
||||
player = (Player) sender;
|
||||
}
|
||||
}
|
||||
|
||||
Iterator var14 = Bukkit.getOnlinePlayers().iterator();
|
||||
|
||||
while(var14.hasNext()) {
|
||||
Player player = (Player)var14.next();
|
||||
player.updateCommands();
|
||||
}
|
||||
} catch (Exception var12) {
|
||||
var12.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void loadConfig() {
|
||||
if (!this.getDataFolder().exists()) {
|
||||
this.getDataFolder().mkdirs();
|
||||
}
|
||||
|
||||
this.configFile = new File(this.getDataFolder(), "config.yml");
|
||||
if (!this.configFile.exists()) {
|
||||
this.saveResource("config.yml", false);
|
||||
}
|
||||
|
||||
this.reloadConfig();
|
||||
this.config = this.getConfig();
|
||||
}
|
||||
|
||||
class ExecCommand implements CommandExecutor {
|
||||
private final String script;
|
||||
private final String permission;
|
||||
|
||||
public ExecCommand(String script, String permission) {
|
||||
this.script = script;
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
Player player = null;
|
||||
if (sender instanceof Player) {
|
||||
player = (Player)sender;
|
||||
}
|
||||
|
||||
if (!this.permission.isEmpty() && player != null && !player.hasPermission(this.permission)) {
|
||||
player.sendMessage(String.valueOf(ChatColor.RED) + "You do not have a permission to execute this command.");
|
||||
return true;
|
||||
} else {
|
||||
String argString = String.join(" ", args);
|
||||
String cmdToRun = this.script.replace("%args%", argString);
|
||||
this.config = getConfig();
|
||||
|
||||
if (args.length == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (args.length > 1) {
|
||||
sb.append(args[1]);
|
||||
}
|
||||
for (int i = 2; i < args.length; i++) {
|
||||
sb.append(" " + args[i]);
|
||||
}
|
||||
String execCommand = args[0];
|
||||
String execArgs = sb.toString();
|
||||
|
||||
if (execCommand.equals("reload")) {
|
||||
// Reload command
|
||||
reloadConfig();
|
||||
player.sendMessage(String.valueOf(ChatColor.GREEN) + "Config reloaded.");
|
||||
return true;
|
||||
}
|
||||
|
||||
String script = config.getString("commands." + execCommand + ".script");
|
||||
String permission = config.getString("commands." + execCommand + ".permission");
|
||||
|
||||
if (script == null) {
|
||||
player.sendMessage(String.valueOf(ChatColor.RED) + "Unknown command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Permission check
|
||||
if (!permission.isEmpty() && player != null && !player.hasPermission(permission)) {
|
||||
player.sendMessage(String.valueOf(ChatColor.RED) + "You do not have a permission to execute this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Placeholders to script
|
||||
String cmdToRun = PlaceholderAPI.setPlaceholders(player, script);
|
||||
|
||||
// Args to script
|
||||
cmdToRun = cmdToRun.replace("%args%", execArgs);
|
||||
|
||||
for(int i = 0; i < args.length; ++i) {
|
||||
cmdToRun = cmdToRun.replace("%arg_" + (i + 1) + "%", args[i]);
|
||||
}
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||
cmdToRun = PlaceholderAPI.setPlaceholders(player, cmdToRun);
|
||||
}
|
||||
|
||||
|
||||
// Execution
|
||||
getLogger().info("Executing " + cmdToRun);
|
||||
try {
|
||||
Runtime.getRuntime().exec(cmdToRun);
|
||||
} catch (IOException var9) {
|
||||
sender.sendMessage(String.valueOf(ChatColor.RED) + "Exec encountered an internal exception while executing this command.");
|
||||
var9.printStackTrace();
|
||||
Runtime.getRuntime().exec(cmdToRun);
|
||||
} catch (IOException e) {
|
||||
sender.sendMessage(ChatColor.RED + "Script file not found. Please report this to an administrator.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user