1.12.2 How to create a spigot plugin that prioritizes higher priority WorldGuard regions

Discussion in 'Spigot Plugin Development' started by auvio, Jun 13, 2021.

  1. So, I am trying to create a plugin that shows what location / WorldGuard region you are in on the scoreboard. I am not sure how do deal with overlapping regions. I have set them higher priority in WorldGuard, but my code that I have written to do this isn't working. The priority of the region I want to show up in that area is 100, the priority of the one that overlaps in a different area is 1, and the priority of the region that is supposed to be under them all is 0. Here is the code that I have written so far.


    Code (Java):
    for (ProtectedRegion rg : WGBukkit.getRegionManager(player.getWorld()).getApplicableRegions(player.getLocation())) {
                            if (rg.getPriority() == (int)0 && (rg.getId().equalsIgnoreCase("hub")))
                            {
                                board.set("     Location: §8Spawn", Integer.valueOf(3));
                            } else if (rg.getPriority() == (int)0 && (rg.getId().equalsIgnoreCase("forest")))
                            {
                                board.set("     Location: §2Forest", Integer.valueOf(3));
                            } else if (rg.getPriority() == (int)1 && (rg.getId().equalsIgnoreCase("redacted")))
                            {
                                board.set("     Location: §3Redacted", Integer.valueOf(3));
                            } else if (rg.getPriority() == (int)10 && (rg.getId().equalsIgnoreCase("mystical_bees")))
                            {
                                board.set("     Location: §eMystical Bees", Integer.valueOf(3));
                            } else if (rg.getPriority() == (int)0 && (rg.getId().equalsIgnoreCase("fir_forest")))
                            {
                                board.set("     Location: §2Fir Forest", Integer.valueOf(3));
                            } else if (rg.getPriority() == (int)100  && (rg.getId().equalsIgnoreCase("mountainiouspass")))
                            {
                                board.set("     Location: §8Mountanious Pass", Integer.valueOf(3));
                                System.out.println("mountaniouspass");
                            }
                        }
     
  2. well that's hell bad way of writing code i guess...
    advice: optimize your code style first.
     
    • Agree Agree x 2
  3. My idea for better optimization of this would be to store a region as a variable as the 'highest priority' region. Then in your loop, you can check if the current 'highest priority' region has a lower priority than the next iteration and if it does to update the variable.

    That way, despite what the values might be, you don't have to mess with casting, or all of these else if statements. It's much cleaner and a logical way to get the highest priority region in any instance.

    Also you should check out switch statements as they'd help you out a lot if you have other instances of using loads of else if statements dealing with the same variable. They're much cleaner than this method anyway.