Get Command output from Command block

Discussion in 'Plugin Development' started by XxTimexX, Jul 27, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    XxTimexX

    On mine server, one player was OP-ed in order to help me with something, he is "trustworthy". But while I wasn't watching he hidden quite few Command blocks that contained pex user (his name) add *

    Since I don't want to ban him, nor I want him to abuse those command blocks. I've got an idea to make plugin that would intercept the command and disable the output of the command if it contains specific words such as "pex" or "*".

    Now I tried this:

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
            if (sender instanceof ConsoleCommandSender) {
                if (cmd.getName().equalsIgnoreCase("pex user (his name goes here) add * ")) {
                    System.out.println("Test");
                }
            }
            return true;
        }
    
    And I also tried this:

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String lbl, String[] args) {
            if (sender instanceof BlockCommandSender) {
                CommandBlock cb = (CommandBlock) sender;
                Block b = cb.getBlock();
                if (cb.getCommand().equalsIgnoreCase("pex user (here goes his name) add *")) {
                    b.breakNaturally();
                    System.out.println("Test");
                }
            }
            return true;
        }
    
    So, none of these worked. The plugin did load, and since this is all in main class there is no need to register the command.
     
  2. @XxTimexX Why not just disable command blocks? Do you actually even use them
     
  3. Offline

    XxTimexX

    Yeah, some are pretty important, so I can't disable them.
     
  4. @XxTimexX
    The reason what you tried didn't work is that in your main class, only commands in your plugin.yml are handled. What I suggest you do is use ServerCommandEvent and check for your conditions. ServerCommandEvent may or may not fire when command blocks execute commands, I can't test atm, since I don't have access to a computer.
     
  5. Offline

    XxTimexX

    @AlvinB
    Apparently, it did not work.

    Code:
    @EventHandler
        public void onCommandP(ServerCommandEvent e) {
            if (e.getSender() instanceof BlockCommandSender) {
                if (e.getCommand().equalsIgnoreCase("pex user (his name) add *")) {
                    System.out.println("Test");
                }
            }
        }
    
     
  6. @XxTimexX
    Then I'm not sure.. I guess you could use a Redstone event to check if a commandblock is being triggered.. Although I bet there is probably a better event to use, if there is, someone will most likely point it out.
     
  7. There isn't really a commandblock event: https://bukkit.atlassian.net/browse/BUKKIT-5265

    Use BlockPhysicsEvent instead to check if a commandblock has been powered by redstone (directly or indirectly) and then check the commandblock content and cancel if the command matches.
     
  8. Offline

    XxTimexX

    Everything went right, except for the fact that the console spamms error.

    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_10_R1.block.CraftBlockState cannot be cast to org.bukkit.block.CommandBlock
    
    So I cannot cast the Command block.

    Code:
    @EventHandler
        public void onCmdBlock(BlockPhysicsEvent e) {
            Block b = e.getBlock();
            CommandBlock cb = (CommandBlock) b.getState();
            if (cb.getCommand().equalsIgnoreCase("pex user (his name) add *")) {
                e.setCancelled(true);
            }
        }
    
     
  9. Offline

    ArsenArsen

    You imported the wrong CommandBlock class. You need to import the NMS one
    And also check is it a commandblock first.
     
  10. Offline

    I Al Istannen

    @XxTimexX
    May want to check if the block is a commandblock before casting the state. The BlockPhysicsEvent is called for many different blocks.

    EDIT: Ninjad... @ArsenArsen
     
  11. Nope.
    NMS breaks every version (imports, method/variables change etc.) and there's a good reason to avoid it when not needed.

    @XxTimexX
    You need this CommandBlock with the import org.bukkit.block.CommandBlock
     
Thread Status:
Not open for further replies.

Share This Page