1.16.5 Help with code

Discussion in 'Spigot Plugin Development' started by iTaeyeon, Jul 12, 2021.

  1. Hello. I just wasted around 1 hour just to find a code for give a specific custom items, but without success.
    Thing where I'm stucked is in args[]. I only found one code, but that seems does not work.

    I'm asking you guys would you help me (code for me if possible) a code for command?

    I want to /giveme <player> <item> <amount>

    I already have custom items, so in <item> I want to be like , if "item1" that should give custom item no.1 , if "item2" that should give another item.

    Thanks alot!

    ps: code I used to try, but not work: https://hastebin.com/ecekerugud.java (for some reason on execution, it's just return me "give blablabla" that I definned in "plugin.yml" /giveme Player dirt 1)
     
  2. Code (Text):
                        if(!(org.bukkit.Material.getMaterial(args[1]) == null)){
                         
                            int Amt = Integer.parseInt(args[1]);
                         
                            ItemStack togive = new ItemStack(Material.valueOf(args[1]), Integer.parseInt(args[2]));
                        }
    It's because, your re-use "args 1", it can't be Amt and material
     
    • Like Like x 1
  3. I don't understand
     
  4. I don't know if this block of code is your really complete code but this one lacks method that gives the player the itemstack formed from the command, to do that.

    Code (Java):
    player.getInventory().addItem(itemstack);
    player.updateInventory();
    Also you should check if the amount given is really a number before u parse the string.

    And u reuse args[1] as your amount which is a type not a number so that should be args[2]
     
  5. still didn't succeed, I'm lost
     
  6. I had a fit reading that but I'm guessing you didn't add your command to the plugin.yml
    If you didn't this is how. Just put this at the bottom of the plugin.yml
    Code (YAML):
     commandname:
        description
    : What does this command do?
        usage
    : /<command>
     
  7. Hey bro! You need to either learn java or learn spigot. But you won't learn if you keep doing this, and your coding experience will forever be annoying and horrible. There is a lot wrong with the code you provided, so you should learn to do it for yourself and create your own code! :D

    Also note that updating the inventory is very unnecessary in this case; all you need to do is add the item to the inventory.
     
  8. Better yet... learn both :D

    I learnt Java/Spigot from decompiling code and seeing how it all fits together
     
  9. Damn that's brave I respect XD I've been doing that with NMS and it's tricky sometimes.
    I had no real Java experience when I learned to code so I just took a whole bunch if tutorials, and things started clicking in my brain. OP, as long as you're patient, you'll be able to learn everything for yourself. :)
    Head onto YouTube and find a couple Bukkit / Spigot beginner coding tutorials. Almost everyone started there.
     
    • Agree Agree x 1