1.16.5 MySQL add player group

Discussion in 'Spigot Plugin Development' started by Mirlan228, Jun 20, 2021.

  1. Hi everyove!
    I want to make it so that the player's groups were listed in order, separated by commas, but now the player's group is replaced by another group that I set (My bad english sorry)

    Code (Text):
      public static void addGroup(Player p, String gr) {
          List<String> list = new ArrayList<>();
            byte b;
            int i;
            String[] arrayOfString;
            for (i = (arrayOfString = gr.split(", ")).length, b = 0; b < i; ) {
            String string = arrayOfString;
            list.add(string.trim());
            b++;
          }
            Update("UPDATE `golds` SET `group`='" + list.toString() + "' WHERE `uuid`='" + p.getUniqueId().toString() + "'");
       
      }
     
  2. SteelPhoenix

    Moderator

    Your code doesn't even compile, and for the love of all that's holy use prepared statements to avoid SQL injection. Secondly, there's absolutely no reason to store a UUID in a string.

    Also, why did you make your loop this weird and why are you using a byte?
     
    • Agree Agree x 1
  3. Well can you please help me with this problem? I tried to make the loop not weird
     
  4. do you mean there is no point in storing the player's UUID? nickname enough?
     
  5. No. Just store the UUID in your table. (Remove this "toString()")
     
  6. There are quite a few problems with your code. Take a few hours or so to read over some sections of https://www.spigotmc.org/wiki/crash-course-to-java-part-0/ (or the resources included in it such as from Oracle), refactor your code like the prior responses said, and come back if you have any more issues.
     
  7. Strahan

    Benefactor

    Also, as a minor aside to the major issues, I see you are splitting on ", ". If you are making a schema that relies on delimiting, don't include extraneous spaces. That's sloppy. Data in the delimited variable should be tight, with nothing unnecessary applied.

    Personally, I despise string delimiting and avoid it all costs. Why not use a proper collection in the first place?