Add src/net/ardakaz/exec/Exec.java
This commit is contained in:
parent
30b12610f4
commit
e6a1c02381
127
src/net/ardakaz/exec/Exec.java
Normal file
127
src/net/ardakaz/exec/Exec.java
Normal file
@ -0,0 +1,127 @@
|
|||||||
|
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 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;
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user