Bukkit
  1. Bukkit
  2. BUKKIT-4603

There needs to be a CommandBlockEditEvent event

    Details

      Description

      I believe that Bukkit should implement the ability for plugins to change what happens when a command block is edited by a player(for example, that event would get fired as soon as the player clicks 'Done' after changing/entering a command into the command block after Right-Clicking it). Then we could change what happens depending on what game mode the player is in, survival, creative, or adventure, what they typed, etc. I think that would open up the ability to make some very cool minigames that involve what player can correctly guess the right command to progress further into the game or win, etc.

      EDIT: Also, the maker of the plugin would be responsible for checking the command to be sure it isn't something like "/op @p" or "/kill @a", etc. Also, I took the liberty of making my own class for this, in case you need a better idea of what I'm hoping for:

      CommandBlockEditEvent.java
      package org.bukkit.event.block;
      
      import org.bukkit.block.Block;
      import org.bukkit.entity.Player;
      import org.bukkit.event.Cancellable;
      import org.bukkit.event.HandlerList;
      
      /**
       * @author Brian_Entei(Of course, you can change/delete this)
       *
       */
      public class CommandBlockEditEvent extends BlockEvent implements Cancellable {
          private static final HandlerList handlers = new HandlerList();
          private boolean cancel = false;
          private final Player player;
          private String newCmd;
      	private final String oldCmd;
      
          public CommandBlockEditEvent(final Block theBlock, final Player thePlayer, final String olderCmd, final String newerCmd) {
              super(theBlock);
              this.player = thePlayer;
              this.oldCmd = olderCmd;
              this.newCmd = newerCmd;
          }
      
          /**
           * Gets the player changing the command block's command involved in this event.
           *
           * @return The Player involved in this event.
           */
          public Player getPlayer() {
              return player;
          }
      
          /**
           * Gets the command that will replace the command block's command in this event.
           *
           * @return The String command from the command block being set.
           */
          public String getNewCommand() {
              return this.newCmd;
          }
      
          /**
           * Gets the command that will be replaced from the command block in this event.
           * Note that if this event is cancelled, this command will be the one that remains in the command block.
           *
           * @return The String command from the command block being set.
           */
          public String getOldCommand() {
              return this.oldCmd;
          }
      
          /**
           * Sets the command for the command block involved in this event
           *
           * @param newCmd command to set
           */
          public void setNewCommand(String newCmd) {
              this.newCmd = newCmd;
          }
      
          @Override
      	public boolean isCancelled() {
              return cancel;
          }
      
          @Override
      	public void setCancelled(boolean cancel) {
              this.cancel = cancel;
          }
      
          @Override
          public HandlerList getHandlers() {
              return handlers;
          }
      
          public static HandlerList getHandlerList() {
              return handlers;
          }
      }
      

      ---------------------------------------------------------------------------

      Then, you could use that event like this:

      	@EventHandler(priority=EventPriority.HIGHEST)
      	public void onCommandBlockEditEvent(CommandBlockEditEvent evt) {
      		String newCommand = evt.getNewCommand();
      		String oldCommand = evt.getOldCommand();
      		CommandBlock cmdBlock = (CommandBlock) evt.getBlock().getState();
      		Player editor = evt.getPlayer();
      		if(newCommand.startsWith("/op") && editor.hasPermission("someplugin.allowOpInCmdBlock") == false) {
      			evt.setCancelled(true); //This would do the same thing
      			evt.setNewCommand(oldCommand); //that this would do!
      			editor.sendMessage(ChatColor.DARK_RED + "You can't use the /op command in a command block!");
      			return;
      		}
      		//More plugin goodies here...
      		cmdBlock.setName("Command Block #1");
      		cmdBlock.getName();
      		cmdBlock.getCommand();
      	}
      

      Of course, you could also use if(editor.isOp()) and/or if(editor.getGameMode().equals(GameMode.SURVIVAL)) to get more plugin functionality. These are just a few things that I think adding this event would create for the community of plugin makers! I hope this gets considered.

        Activity

        Hide
        Travis Ralston added a comment - - edited

        Considering most of the code is here, why not make a PR?

        Edit: Nevermind, people seem to dislike this event.

        Show
        Travis Ralston added a comment - - edited Considering most of the code is here, why not make a PR? Edit: Nevermind, people seem to dislike this event.
        Hide
        Brian Entei added a comment -

        What do you mean people seem to dislike it? How do you know?(Not saying you're lying, but I would really like to see the downside to this so that I might be able to elaborate on it).

        Show
        Brian Entei added a comment - What do you mean people seem to dislike it? How do you know?(Not saying you're lying, but I would really like to see the downside to this so that I might be able to elaborate on it).
        Hide
        Travis Ralston added a comment -

        The idea of adding an event for existing API is not desirable among some developers.

        This event can already be implemented via other events and some simply trickery.

        Show
        Travis Ralston added a comment - The idea of adding an event for existing API is not desirable among some developers. This event can already be implemented via other events and some simply trickery.
        Hide
        Brian Entei added a comment -

        "This event can already be implemented via other events and some simply trickery."

        Could you share how you would accomplish this then? I've not found a legit Bukkit event for this, and that is what I'm proposing to add.

        Show
        Brian Entei added a comment - "This event can already be implemented via other events and some simply trickery." Could you share how you would accomplish this then? I've not found a legit Bukkit event for this, and that is what I'm proposing to add.
        Hide
        Travis Ralston added a comment -

        PlayerInteractEvent.

        Check the command block every 20 ticks after the event and check the command.

        • If the command is unchanged and the player has moved more than 2 blocks then the command was unedited
        • If the command has changed, the command was edited.
        Show
        Travis Ralston added a comment - PlayerInteractEvent. Check the command block every 20 ticks after the event and check the command. If the command is unchanged and the player has moved more than 2 blocks then the command was unedited If the command has changed, the command was edited.
        Hide
        Oliver Wright added a comment -

        Such tricks just add a lot of load to the server, I personally would prefer events like this.

        Show
        Oliver Wright added a comment - Such tricks just add a lot of load to the server, I personally would prefer events like this.
        Hide
        Travis Ralston added a comment - - edited

        In my honest opinion:

        Although this is extremely easy to implement into CraftBukkit, it is not needed. The event is excess baggage and does not need to be added because the command block has default behavior that protects the server from commands such as "op" and "kill". Even if the server has a plugin that has a dangerous command in it (eg: /killall) that plugin can EASILY check if the CommandSender is a command block.

        If you would like XYZ command to be blocked from command blocks simply get a plugin to implement the if(CommandSender is command block) check and you're done.

        Edit: Not to mention firing this event adds load to the server... Do you know how many times people hit command blocks?

        Show
        Travis Ralston added a comment - - edited In my honest opinion: Although this is extremely easy to implement into CraftBukkit, it is not needed. The event is excess baggage and does not need to be added because the command block has default behavior that protects the server from commands such as "op" and "kill". Even if the server has a plugin that has a dangerous command in it (eg: /killall) that plugin can EASILY check if the CommandSender is a command block. If you would like XYZ command to be blocked from command blocks simply get a plugin to implement the if(CommandSender is command block) check and you're done. Edit: Not to mention firing this event adds load to the server... Do you know how many times people hit command blocks?
        Hide
        Brian Entei added a comment -

        Well, if you don't like it, then just make a plugin that cancels the event every time. I don't think that one little event would strain a server much anyway. Plus, what if someone else needs to do what this event can offer, and doesn't know how to do what you said? They'd be stuck with no way to accomplish their goal.

        Show
        Brian Entei added a comment - Well, if you don't like it, then just make a plugin that cancels the event every time. I don't think that one little event would strain a server much anyway. Plus, what if someone else needs to do what this event can offer, and doesn't know how to do what you said? They'd be stuck with no way to accomplish their goal.

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: