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