Array Help

Discussion in 'Plugin Development' started by TheCakeMaster, Oct 5, 2014.

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

    TheCakeMaster

    So I'm making a plugin that displays a bunch of gadgets for my server, and I'm trying to make a menu where players can unlock items and stuff.
    The problem I'm having is the player do something the first time (unlocking the item) and then doing something different every time after that (using the unlocked item). I've tried checking if the player is in an array, and if not then add them to the array, but this doesn't seem to be working.
    Here is the bit of my code:
    Code:
    @EventHandler
    public void click(InventoryClickEvent event) {
    Player player = (Player) event.getWhoClicked();
    ItemStack clicked = event.getCurrentItem();
    Inventory inv = event.getInventory();
    String player1 = player.getName();
     
    if (inv.getName().equals(mainGadgetMenu.getName())) {
    if (clicked.getType() == Material.HAY_BLOCK) {
    player.sendMessage("Item has worked.");
    ArrayList<String> playerArray = new ArrayList<String>();
     
    if (playerArray.contains(player1)) {
    player.sendMessage("player1 has been found in the array.");
    }
    else {
    player.sendMessage("player1 has not been found in the array!");
    playerArray.add(player1);
    }
     
    event.setCancelled(true);
    player.closeInventory();
    }
    }
    }
    }
    Any help would be much appreciated :)
    EDIT: I just did the "Item has worked" to make sure the general clicking of the item did something.
     
  2. Offline

    Zettelkasten

    You want your ArrayList to be declared outside of the click() method so it will still persist even if the event is completed.
    Also, for your purposes, a (Hash)Set would be better as it does not have an order and does not allow a player to be in it twice. Also, it is performing faster when using contains(...).
     
  3. Offline

    Minesuchtiiii

    Code:java
    1. if (playerArray.contains(player1)) {
    2. player.sendMessage("player1 has been found in the array.");
    3. }


    you shouldn't put the string player1 in the message, cause there will ever stand "player1" and not his name..

    do it like this:

    Code:java
    1. if (playerArray.contains(player1)) {
    2. player.sendMessage(player1 + " has been found in the array.");
    3. }
     
  4. Offline

    TheCakeMaster

    That was only a test to check if anything had been found in the array, I am just using that until I've fixed it.

    Thanks heaps!
    Trying it now.

    EDIT: Works!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  5. Offline

    Minesuchtiiii

    Kk didn't know this just wanted to help.
    You're welcome!
     
Thread Status:
Not open for further replies.

Share This Page