Uploaded image for project: '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.

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated: