Solved String to Material?

Discussion in 'Plugin Development' started by mccrafter1212, Mar 24, 2016.

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

    mccrafter1212

    I've been trying REALLY hard to find a solution to this. I've currently got the following code:
    Code:
    if (cmd.getName().equalsIgnoreCase("i") && sender instanceof Player) {
               
                Player p = (Player) sender;
               
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Usage: /i <Material.NAME> <amount>");
                } else {
                    if (args[0].contains("Material.") == true) {
                        String type = args[0];
                        p.getInventory().addItem(new ItemStack(type, 1));
                    }
                }
               
                p.sendMessage(ChatColor.GOLD + "You have been given 1 diamond!");
               
            }
    I've spent about 2 hours working on this... I've researched everywhere and even looked into the Essentials plugin source code! It's too complicated for me and I understand nothing, even though I was able to find the class for the command. Where you see
    Code:
    p.getInventory().addItem(new ItemStack(type, 1));
    is where I keep getting my error... Apparently you can't use a variable in place of Material.MATERIALNAME. So I've decided to come here and make a post. The command should work like this (It's an example): /i Material.DIAMOND 64 - This would give you a stack of diamonds.. I'm taking the whole argument 0 and turning it into the material type. I can't make the variable a Material type though or else I get an error saying to change it to String. I don't know how to convert it.. Can I get some help?
     
  2. Maybe something like
    Code:
    Material material = Material.matchMaterial(name);
    p.getInventory().addItem(new ItemStack(material, 1));
    But if you have problems with something this easy, I suggest you check out a tutorial about Java before starting to write plugins. Or at least check out the Java Docs next time.

    To @ComputerTurtle 's solution, item ids are deprecated as you noticed so you shouldnt use them and he wanted to know how to parse a material name.
     
    Last edited: Mar 24, 2016
  3. Offline

    ComputerTurtle

    I'll give it a shot.
    I added an int for the args (for the material type)
    even doe, it is depricated ;/
    Code:
    //haven't tested it, but I added an int for args :)   
    public boolean onCommand(CommandSender sender, Command command, String cmd, String[] args, int args2) {
            if (command.getName().equalsIgnoreCase("i") && sender instanceof Player) {
                  
                Player p = (Player) sender;
              
                if (args.length == 0) {
                    p.sendMessage(ChatColor.RED + "Usage: /i <Material.NAME> <amount>");
                } else {
                    if (args[0].contains("Material.") == true) {
    //I know it's depricated :(
                        @SuppressWarnings("deprecation")
                        ItemStack type = new ItemStack(args2);
                        p.getInventory().addItem(type);
                        }
                    }
                }
            return false;         
            }
     
  4. Offline

    Zombie_Striker

    @ComputerTurtle
    Thanks captain spoonfeeder! Here are some notes about your code:
    • You do not need "==true", as it is redundant, since line 10 already returns a boolean.
    • You cannot just add arguments to the parameter! "args2" would just break the onCommand.
    • Even if it could work, you are using an integer to get a material type. That would mean you would be using Type IDs, which both mojang and bukkit/spigot no longer wants people to use.
    • Your always returning false, which means that you will always receive the usage of the command. You need to return true if the command worked the way it should.
    You managed to hit all 6 reasons from this thread which explains why we don't spoonfeed. Congrats!
     
Thread Status:
Not open for further replies.

Share This Page