Solved Get Command WITHOUT Overriding Other Commands

Discussion in 'Plugin Development' started by Xp10d3, Sep 10, 2020.

  1. Offline

    Xp10d3

    Basically I'm creating a custom logging system. I want it to be custom and not use other plugins (for a few reasons, one including that I want to learn more about Bukkit API), however I'm confused on how to check if a player sent a command WITHOUT overriding another plugin's command or Bukkit's default commands. So for example, here is the ban command:
    Code:java
    1.  
    2. if (cmd.getName().equalsIgnoreCase("ban") || cmd.getName().equalsIgnoreCase("banip")) {
    3. if (args.length == 2) {
    4. Player target = Bukkit.getServer().getPlayer(args[0]);
    5. //Bukkit.getServer().banIP(target.getUniqueId().toString());
    6. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". Reason for ban: " + args[2] + ". Logging to ban log...");
    7. core.banLog("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". Reason for ban: " + args[2]);
    8. } else if (args.length == 1) {
    9. Player target = Bukkit.getServer().getPlayer(args[0]);
    10. //Bukkit.getServer().banIP(target.getUniqueId().toString());
    11. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". Logging to ban log...");
    12. core.banLog("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". There was no reason for the ban.");
    13. }
    14. }
    15.  

    Now, everything should work, but I want this to run IN ADDITION to either Bukkit's default /ban command or Essential's /ban command. However, there are many issues.
    Code:
    1. If I register the command in my constructor OR my main class, it will likely override other plugin's commands.
    2. If I DON'T register the command, nothing will happen and it won't log anything.
    3. I'm confused how to solve this.
    4. I'm not an expert in Java :P
    
    Commands class:
    Code:java
    1.  
    2. package eltik.endran.blockres;
    3.  
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6. import java.util.UUID;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandExecutor;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13.  
    14. public class Commands implements CommandExecutor {
    15.  
    16. Core core = Core.getPlugin(Core.class);
    17.  
    18. public Commands(Core core) {
    19. this.core = core;
    20. //Bukkit.getPluginCommand("eshop").setExecutor(this);
    21. }
    22.  
    23. Date now = new Date();
    24. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    25.  
    26. @Override
    27. public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
    28. if (!(sender instanceof Player)) {
    29. sender.sendMessage("You must be a player to access this command!");
    30. core.logToCommands("[" + format.format(now) + "]" + " Something other then a player tried to run a command. Sender was: " + sender);
    31. return false;
    32. }
    33.  
    34. if (sender instanceof Player) {
    35. core.logToCommands("Player sent command. Command: " + cmd + ". Player: " + sender.getName());
    36. }
    37.  
    38. Player player = (Player) sender;
    39. UUID uuid = player.getUniqueId();
    40. if (cmd.getName().equalsIgnoreCase("ban") || cmd.getName().equalsIgnoreCase("banip")) {
    41. if (args.length == 2) {
    42. Player target = Bukkit.getServer().getPlayer(args[0]);
    43. //Bukkit.getServer().banIP(target.getUniqueId().toString());
    44. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". Reason for ban: " + args[2] + ". Logging to ban log...");
    45. core.banLog("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". Reason for ban: " + args[2]);
    46. } else if (args.length == 1) {
    47. Player target = Bukkit.getServer().getPlayer(args[0]);
    48. //Bukkit.getServer().banIP(target.getUniqueId().toString());
    49. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". Logging to ban log...");
    50. core.banLog("[" + format.format(now) + "]" + "Player " + uuid + " banned " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". There was no reason for the ban.");
    51. }
    52. }
    53.  
    54. if (cmd.getName().equalsIgnoreCase("kick")) {
    55. if (args.length == 2) {
    56. Player target = Bukkit.getServer().getPlayer(args[0]);
    57. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " kicked " + target.getName() + ". Reason for kick: " + args[2] + ". Logging to kick log...");
    58. core.kickLog("[" + format.format(now) + "]" + "Player " + uuid + " kick " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". Reason for kick: " + args[2]);
    59. } else if (args.length == 1) {
    60. Player target = Bukkit.getServer().getPlayer(args[0]);
    61. core.logToCommands("[" + format.format(now) + "]" + "Player " + uuid + " kick " + target.getName() + ". Logging to kick log...");
    62. core.kickLog("[" + format.format(now) + "]" + "Player " + uuid + " kick " + target.getName() + ". STATS: Experience of player:" + target.getExp() + ". Custom name of player: " + target.getCustomName() + ". Display name: " + target.getDisplayName() + ". First played " + target.getFirstPlayed() + ". Health: " + target.getHealth() + ". Last played " + target.getLastPlayed() + ". Target was online: " + target.isOnline() + ". There was no reason for the kick.");
    63. }
    64. }
    65. return false;
    66. }
    67. }
    68.  

    You probably don't need this, but my main class:
     
  2. Offline

    KarimAKL

    @Xp10d3 You're registering a new command. What you want is listening to the PlayerCommandPreprocessEvent.
     
    Xp10d3 likes this.
  3. Offline

    Xp10d3

    Thanks! I'll try that. How would I get the second/third argument of the command?
    EDIT: Never mind command.split is a thing lol. Marking as solved. Thanks karim :D
     
    Last edited: Sep 10, 2020
    KarimAKL likes this.

Share This Page