Command to change the block that the blocktool sets the right clicked block to.

Discussion in 'Programming' started by Glenn, Feb 19, 2020.

  1. Hello,

    I am creating a plugin that changes a right clicked block to another block when they are holding a specific item. They are able to get that item by using /blockchanging givetool. I want to create a new command that allows the player to change the material that is being set when they right click the block. Here is my current code:


    Code (Text):
    package commands;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class BlocktoolCommand implements CommandExecutor {

        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            ItemStack bt = new ItemStack(Material.BLAZE_ROD);
            ItemMeta btMeta = bt.getItemMeta();
            btMeta.setDisplayName(ChatColor.GREEN + "Block Tool");
         
            List<String> lore = new ArrayList<>();
            lore.add(ChatColor.GRAY + "The real wand!");
         
            btMeta.setLore(lore);
            bt.setItemMeta(btMeta);
         
            Player player = (Player) sender;
            if (sender instanceof Player) {
                player.getInventory().addItem(bt);
            } else if (!(sender instanceof Player)) {
                System.out.println("You may not run this command from console.");
                return true;
            }
         
            return false;
        }

    }

    Code (Text):
    package listeners;

    import java.util.HashSet;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.event.player.PlayerInteractEvent;

    import commands.ChangeBlockCommand;

    import commands.ChangeBlockCommand;

    import commands.ChangeBlockCommand;

    public class InventoryListener implements Listener {
     
        ItemStack bt = new ItemStack(Material.BLAZE_ROD);
        ItemMeta btMeta = bt.getItemMeta();
        HashSet<Material> m = new HashSet<>();
     
        @EventHandler
        public void InvEvent(PlayerInteractEvent e) {
         
            Player p = (Player) e.getPlayer();
         
            if (p.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "Block Tool")) {
                e.getClickedBlock().setType(ChangeBlockCommand);
            }
        }
    }
    Code (Text):
    package blockchanging;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.java.JavaPlugin;

    import commands.BlocktoolCommand;
    import commands.ChangeBlockCommand;
    import listeners.InventoryListener;

    public class main extends JavaPlugin {
     
        @Override
        public void onEnable() {
            getCommand("blockchanging givetool").setExecutor(new BlocktoolCommand());
         
            Bukkit.getPluginManager().registerEvents(new InventoryListener(), this);
         
            getCommand("blockchanging changeblock").setExecutor(new ChangeBlockCommand());
        }
    }
    THIS IS THE SECTION I NEED HELP WITH:
    Code (Text):
    package commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class ChangeBlockCommand implements CommandExecutor {
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
            if (!(sender instanceof Player)) {
                System.out.println("You may not run this command from console.");
                return true;
            }
         
            Player p = (Player) sender;
         
            if (cmd.getName().equalsIgnoreCase("blockchanging changeblock")) {
             
            }
         
            return false;
        }
     
    }
     
    Basically, I just need the above code to set a variable (I believe) to a material type, so when the command is ran (blockchanging changeblock diamondblock) it will set that variable to DIAMOND_BLOCK and it will then change the right clicked block to diamond blocks, and I can then change it to emerald block and it will do the same.
     
  2. There's so many errors in your code.
    Use java convetion for naming your package. First rule of programming.

    Do not case the sender to a Player before checking instanceof.
    If you use the command from the console you'll get an error.
    Inverse these two lines.

    Why are you using else if ????? If the sender isn't a player, you don't have to check if (!(sender instanceof Player))...
    There's only two options with your if, this isn't optimized.

    Sorry, but no. A command is only one word. No space. What did you put into your plugin yaml? You need to check for the args (givetool) in the command method itself.

    This class is only called when the cmd.getName equals "blockchanging changeblock" as defined in your JavaPlugin class... this isn't totally useless.

    Please just tell us exactly what you're trying to achieve with this command cause this isn't clear at all.