[1.11.2] Getting Command Arguments within InventoryClickEvent

Discussion in 'Spigot Plugin Development' started by creepinson, Apr 22, 2017.

  1. Hey there. I am having trouble figuring the following out:

    I am making a ban menu similar to mineplex's, and I am starting to make one item in the menu for banning a player for 1 hour. I made it so that they enter the following command with the argument #2 being the player to ban, and the argument #3 being the reason: /acommando ban <player> <reason>
    I want it so it opens my menu which I can do, but then I want it so when they click that hour ban item in the menu, the player automatically runs /tempban but with the time being 1 hour, ALSO with the player and reason from the command they entered previously. I basically want it so that they enter /acommando ban <player> <reason>
    and then it brings up a menu in which they click the current only option, 1 hour ban, and it runs tempban with the time of an hour and the commands arguments from the /acommando ban command. I cannot figure this out, since InventoryClickEvent is a different method from the boolean method onCommand for my command.
    Thanks,
    Creepinson
     
  2. Store some data somewhere and associate it to the player, perhaps use a hashmap.

    If you're more advanced, you may want to wrap the inventory completely in some sort of gui instance and store your data in there, possibly just as a click callback.
     
    • Agree Agree x 1
  3. I am a bit new to hashmaps and hashsets, I looked it up and found this (NOTE: I WILL CHANGE "quest" OF COURSE):
    HashSet<String> quest = new HashSet<String>();
    quest.add(playerName);
    boolean b = quest.contains(playerName);
    quest.remove(playerName);

    But I don't know how to store the data of the command args, would I do the same but instead of HashSet<String> I do HashSet<String, String>, the first string being the playername and the second being the command argument?
    EDIT: don't need boolean b thing though
     
  4. These are questions that have been answered hundreds of times.

    Also, be careful to remove the data once someone has selected an option, or even use metadata as it won't clog up space if they log out

    As for storing different data, I'd personally use Player and a custom class instance to store it. Look into that, it's a core part of OOP. Again, make sure to clear on selection or as a last resort on logout, or you'll get memory leaks galore.
     
  5. NOTE: WON'T SEE THIS TILL THE NEXT DAY :)
    ok I've been messing around trying to get it to work and I think I got a NPE when my test server tries to enable my plugin? Here is where in the error it says that:
    Caused by: java.lang.NullPointerException
    at me.creepz.menu.MenuBan.<clinit>(MenuBan.java:36) ..........
    I don't know what I am doing wrong lol
    Here is my code:

    package me.creepz.menu;

    import java.util.ArrayList;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;


    public class MenuBan {



    public static Inventory bans;

    public MenuBan(){

    bans = Bukkit.getServer().createInventory(null, 18, "&4&lBan Player");


    }
    public static void createDisplay(Material material, Inventory inv, int Slot, String name, String lore) {

    }
    static {
    ItemStack BAN_1HOUR = new ItemStack(Material.BARRIER);
    ItemMeta meta = BAN_1HOUR.getItemMeta();
    meta.setDisplayName("1 Hour Ban");

    BAN_1HOUR.setItemMeta(meta);

    bans.setItem(0, BAN_1HOUR);


    }



    }
     
  6. bans is null, you need to create an instance of it first - bukkit.createInventory
     
  7. Why is everything static? It hurts my soul.
     
    • Agree Agree x 1
  8. ???

    public MenuBan(){


    bans = Bukkit.getServer().createInventory(null, 18, "&4&lBan Player");


    }

    And then in my plugins class:
    menuBan = new MenuBan();
     
  9. and private MenuBan menuBan;
     
  10. I did that already
     
  11. maldahleh

    Wiki Team

    What did you do already? Also the code you posted is very messy and why is everything static? Your answer was already given to you in the first reply, you want to use a HashMap to store the data associated with the player.
     
  12. "bans is null, you need to create an instance of it first - bukkit.createInventory"
    ??? I did that look above
     
  13. maybe I did it wrong? I don't know what I did wrong there