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

    Riib11

    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:
    Code:
    @Override
        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)
                                    .getBlock();
                            // Set metadata "command" to the passed command
                            targetblock.setMetadata("command",
                                    new FixedMetadataValue(this, command));
                            // Player feedback
                            player.sendMessage(ChatColor.GREEN
                                    + "[BlockCommands] "
                                    + ChatColor.DARK_GREEN
                                    + "Command Set: "
                                    + ChatColor.GREEN
                                    + targetblock.getMetadata("command").get(0)
                                            .asString());
                        }
                        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:
    Code:
    @EventHandler
        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           
                    player.getServer().dispatchCommand(
                            plugin.getServer().getConsoleSender(),
                            block.getMetadata("command").get(0).asString());
     
                }
            }
        }
    Thanks in advance for any help offered!
     
Thread Status:
Not open for further replies.

Share This Page