Arraylist stays empty after putting String inside.

Discussion in 'Plugin Development' started by PlayWolfYT, Jul 16, 2019.

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

    PlayWolfYT

    Hello, I am making a PermissionsSystem based on "PermissionsEx". I want to make a Command ("/rang <Player> <Rank>") to set a Players rank, but I dont want other admins to give away ranks. But I dont want to just have a Permission for the command, instead I want to have a Password.

    Now here's the Problem: Validating the Password should work (it's encrypted), but I tried making a System that takes a Validation-ID (just a random generated string that Points to a command in a hashmap).

    Here's some of my Code I used:

    Code:
    if(commandSender.hasPermission("pperms.rang")) {
    
                if(args.length == 1) {
                    PermissionUser target = PermissionsEx.getUser(args[0]);
                    commandSender.sendMessage(PPerms.prefix + "The Player " + target.getName() + " has the Rank §a" + target.getRankLadderGroup("Plerax").getName());
    
                } else if(args.length == 2) {
                    PermissionUser target = PermissionsEx.getUser(args[0]);
                    if(args[1].equalsIgnoreCase("Admin")) {
                        String uid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 15);
                        new CommandVerifier(uid, target.getName() + " ADMIN LIFETIME");
                        commandSender.sendMessage(PPerms.prefix + "Nutze /validate " + uid + " <Passwort>");
                    }
    

    So, a CommandVerifier gets created, thats this class:
    Code:
    private HashMap < String, String > validationCodes = new HashMap < > ();
    public CommandVerifier(String validationCode, String command) {
      validationCodes.put(validationCode, command);
      Bukkit.broadcastMessage("put " + validationCode + " for " + command);
    }
    
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String label, String[] args) {
      if (commandSender.hasPermission("pperms.validate")) {
          if (args.length == 0) {
              Bukkit.broadcastMessage("Found those codes:");
              validationCodes.keySet().forEach(Bukkit::broadcastMessage);
          }
      
    if (args.length != 2) {
          commandSender.sendMessage(PPerms.prefix + "Please use §c/validate <Code> <Password>");
          return true;
      }
    
    if (validationCodes.containsKey(args[0])) {
          byte[] enteredHashed;
          try {
              MessageDigest digest = MessageDigest.getInstance("SHA-256");
              enteredHashed = digest.digest(args[1].getBytes(StandardCharsets.UTF_8));
              if (enteredHashed == pw) {
                  Bukkit.broadcastMessage("Right Password");
              } else {
                  Bukkit.broadcastMessage("Wrong Password");
              }
          } catch (NoSuchAlgorithmException e) {
                  e.printStackTrace();
          }
      } else {
          Bukkit.broadcastMessage(args[0] + " is not in!");
          commandSender.sendMessage(PPerms.prefix + "Wrong validationcode!");
      }
          return true;
    }
      return false;
    }
    
    
    
    The Array somehow (results of debug) IS EMPTY after it SHOULD have been put in.

    Please help, Thanks!
    - PlayWolfYT
     
  2. Offline

    timtower Administrator Administrator Moderator

    @PlayWolfYT Welcome to the world of scope.
    You are making a new instance of the class, the instance has its own list.
     
  3. Offline

    PlayWolfYT

    Thanks! Now i see.

    I'm so dumb.
     
Thread Status:
Not open for further replies.

Share This Page