1.15.2 Help make my code more efficient please! (if possible)

Discussion in 'Spigot Plugin Development' started by VivianMusic, Mar 2, 2020.

  1. Hello all, I make a lot of threads on this forum but it's because I want to try finding the most efficient/clean way to do everything. I know it's a lot to ask, but I have a class of code that looks pretty messy, but I don't think it can be improved upon as far as looking clean, all of the code is necessary (I believe) prove me wrong?: https://github.com/Squallzz/Nationss/blob/master/src/com/squallz/nations/managers/GUIManager.java

    Thanks for any help you might be able to give
     
  2. A start would be to use some kind of utility class to make ItemStacks, that'd replace the 9-12 lines of code per ItemStack to like 1-2, and looks prettier
     
    #2 loidsemus, Mar 2, 2020
    Last edited: Mar 2, 2020
  3. That's kinda what I'm already doing, but the problem is that so many different factors change what the itemstacks need to say and I don't know the best way to go about that
     
  4. For instance
    Code (Text):
    public ItemStack createNationMemberHead(UUID uuid){
            OfflinePlayer player = Bukkit.getServer().getOfflinePlayer(uuid);
            NPlayer nplayer = mainClass.nPlayerManager.getNPlayerByUUID(uuid);

            ItemStack head = new ItemStack(Material.PLAYER_HEAD);
            SkullMeta headMeta = (SkullMeta) head.getItemMeta();
            headMeta.setOwningPlayer(Bukkit.getServer().getOfflinePlayer(uuid));
            headMeta.setDisplayName(ChatColor.YELLOW + player.getName());
            ArrayList<String> headLore = new ArrayList<>();
            headLore.add(ChatColor.GRAY + "Rank: " + ChatColor.YELLOW + nplayer.getNationRole().toString().substring(0, 1).toUpperCase() + nplayer.getNationRole().toString().substring(1).toLowerCase());
            headLore.add("");
            headLore.add(ChatColor.GRAY + "Click to manage");
            headMeta.setLore(headLore);
            head.setItemMeta(headMeta);

            return head;
        }
     
  5. Like loidsemus mentioned, a large majority of your code appears to be making lists of item lore. You could add some utiility method that helps you put lore together more easily (or better yet, they could be read from a config file in case you want to make any future changes).

    It might also be worthwhile to have some IconManager class or something of that nature that provides these items for you, especially given their dynamic lore (I typed most of this before there were any responses, but I assume loidsemus was trying to suggest the same thing).
     
  6. Hmmm...config file with dynamic lore I'll have to check it out. Thanks
     
    • Like Like x 1