Development Assistance Problems with setting metadata and using it

Discussion in 'Plugin Help/Development/Requests' started by Riib11, Nov 7, 2014.

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


    I'm trying to have a command that sets the "command" metadata of a block to a certain command (string). Then, when a player right clicks it, the server runs the command stored inside the "command" metadata. For some reason, I think the metadata isn't being set though

    onCommand (setting the command) code:
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (sender instanceof Player) {
                // Is a player
                Player player = (Player) sender;
                // Is BlockCommands command
                if (cmd.getName().equalsIgnoreCase("blockcommands")
                        || cmd.getName().equalsIgnoreCase("bc")) {
                         // CREATE
                   if (args.length == 2) {
                        // Which BlockCommands command
                        if (args[0].equals("create")) {
                            // Passed <command> argument
                            String command = args[1];
                            // Block to set the command of
                            Block targetblock = player.getLocation().add(1, 0, 0)
                            // Set metadata "command" to the passed command
                                    new FixedMetadataValue(this, command));
                            // Player feedback
                                    + "[BlockCommands] "
                                    + ChatColor.DARK_GREEN
                                    + "Command Set: "
                                    + ChatColor.GREEN
                                    + targetblock.getMetadata("command").get(0)
                        return true;
                return true;
            // Not a player
            sender.sendMessage(ChatColor.GREEN + "[BlockCommands] " + ChatColor.RED
                    + "You must be a player to use this command");
            return false;

    onPlayerInteract (running the command) code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Block block = event.getClickedBlock();
                Player player = event.getPlayer();
                // If it is a BlockCommands set block
                if (block.hasMetadata("command")) {
                    // See what the metadata is
                    player.sendMessage(ChatColor.GREEN + "[BlockCommands] "
                            + ChatColor.GOLD
                            + block.getMetadata("command").get(0).asString());
                    // Run the attached command           
    Thanks in advance for any help offered!
Thread Status:
Not open for further replies.

Share This Page