Enter a PIN in a GUI?

Discussion in 'Spigot Plugin Development' started by Snowmite, Apr 20, 2017.

  1. How would I get when a player enters 4 numbers into a GUI? There will be buttons that will be 0-9 and they will click the pin they have set... (PSUEDO CODE IS NICE)
     
  2. Listen to the inventory click event, check the inventory is yours, and then you could figure out the number clicked on from event#getSlot + 1. However, it wouldn't look too great cause you'd have just one button on the row below because inventories are 9 based, and you have 10 numbers. You could perhaps modify an anvil GUI and let the users enter their number in the provided text field.
     
  3. Here is what I am thinking, not entirely sure if it'd work.

    Code (Text):

    int click1;
    int click2;
    int click3;
    int click4;

    InventoryClickEvent
    if(click1 == null)
       click1 = e.getSlot() + 1;
       return

    if(click1 != null && click2 == null)
       click2 = e.getSlot() + 1;
       return;

    if(click1 != null && click2 != null && click3 == null)
       click3 = e.getSlot() + 1;
       return;

    if(click1 != null && click2 != null && click3 != null && click4 == null)
       click4 = e.getSlot() + 1;
       return;

    String pin = click1 + "" + click2 + "" + click3 + "" + click4;
     
    Now you can either save that to a file (if thats their first time setting up a pin), or you can check if their entry (pin) is equal to the string stored in the config
     
  4. You can always work it out like a phone, have 9 rows and 6 columns, and the it would be a 3x3 cube of the buttons and one middle bottom for the zero.
     
    • Like Like x 1
  5. I did something like this by comparing the amount and the type of the item. The 0 has a type of stained glass and an amount of 1, the rest have the same type and an amount representing the number.
     
  6. Maybe create an inventory and an hashmap with:
    public Map<Player, Integer> Combinationitem1 = new HashMap<Player, Integer>();
    public Map<Player, Integer> Combinationitem2 = new HashMap<Player, Integer>();
    ect.
    The inventory:
    Inventory inv = Bukkit.createInventory(null, 9 * 1, ChatColor.GOLD + enter your pin);
    than:
    inv.setitem(slot, itemstack);
    Than create a item with "Proceed pin" or something.
    after that let a player open the inventory with:
    player.openinventory(inv);
    Combinationitem1.put(player, 0);
    Combinationitem2.put(player, 0);
    Than with an onclick event check if Combinationitem1 is empty or Combinationitem1.get(player) so you know if a player is in the pin inventory
    than check if he clicked the first item ect with slots, than cancel the event, than set for example Combinationitem1 to + 1.
    if the item name is "Proceed pin", check if the hashmaps has the same value as the combination!

    i hope you can use this!

    Bye,
    Bart
     
  7. Strahan

    Benefactor

    That's kinda painful. The same method can be achieved a bit more cleanly as follows:

    Code (Text):
    List<Integer> pinEntry = new ArrayList<Integer>();

    onInventoryClick(e) {
      if proper inv {
        pinEntry.add(e.getSlot() + 1);
        e.setCancelled(true);
        if (pinEntry.size() == 4) {
          if (pinEntry.stream().map(i -> i.toString()).collect(Collectors.joining("").equals(varWithRightPin)) {
            do stuff;
          } else {
            yell at person;
          }
          pinEntry.clear();
        }
      }
    }
    I say this a lot - if you find yourself doing basically the same blocks of code over and over again in a function, it likely means you aren't doing it right and can optimize.
     
    • Informative Informative x 1