Initial code
This commit is contained in:
		
							
								
								
									
										92
									
								
								src/net/ardakaz/exec/Exec.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								src/net/ardakaz/exec/Exec.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
			
		||||
package net.ardakaz.exec;
 | 
			
		||||
 | 
			
		||||
import me.clip.placeholderapi.PlaceholderAPI;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.command.Command;
 | 
			
		||||
import org.bukkit.command.CommandExecutor;
 | 
			
		||||
import org.bukkit.command.CommandSender;
 | 
			
		||||
import org.bukkit.configuration.file.FileConfiguration;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.plugin.java.JavaPlugin;
 | 
			
		||||
 | 
			
		||||
public class Exec extends JavaPlugin {
 | 
			
		||||
	
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onEnable() {
 | 
			
		||||
    	saveDefaultConfig();
 | 
			
		||||
    	this.getCommand("exec").setExecutor(new ExecCommand());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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;
 | 
			
		||||
            }
 | 
			
		||||
        	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;
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Args to script
 | 
			
		||||
            String cmdToRun = script.replace("%args%", execArgs);
 | 
			
		||||
 | 
			
		||||
            for(int i = 0; i < args.length; ++i) {
 | 
			
		||||
               cmdToRun = cmdToRun.replace("%arg_" + (i + 1) + "%", args[i]);
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
            // Placeholders to script
 | 
			
		||||
            cmdToRun = PlaceholderAPI.setPlaceholders(player, cmdToRun);
 | 
			
		||||
            
 | 
			
		||||
            // Execution
 | 
			
		||||
            getLogger().info("Executing " + cmdToRun);
 | 
			
		||||
            try {
 | 
			
		||||
                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;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user