javaminecraftspigotminecraft-commands

How to Pass Material Argument from player in Minecraft


I want to setHelmet of an armor stand and i want to pass argument[1] as "example Material.TNT"

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    if(sender instanceof Player){
        Player player = (Player) sender;
        if(args.length == 0) {
            ArmorStand armorStand = (ArmorStand) player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
        }
        if(args.length == 1){
            ArmorStand armorStand = (ArmorStand) player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
            armorStand.setHelmet(new ItemStack(Material.player));
       }
    }

    return true;

I am expecting to get Material."material" and set it as a helmet when player send command result command : /armorstand Material.TNT


Solution

  • I hope my answer is not too late, but it is written unsolved.

    You should get the material through

    try {
        armorStand.setHelmet(new ItemStack(Material.valueOf(args[0].toUpperCase())));
    } catch (IllegalArgumentException e) {
        // handle your error
    }
    

    if the first argument is for example "TNT" (/armorstand TNT)

    Otherwise, if you insist to use "Material.TNT" you will have to remove it with

    args[0].replace("Material.", "")
    

    Don't forget to only use args[0] when you are sure at least one argument has been given (which you have already done with your second condition)