Solved Sorting list of regions

Discussion in 'Plugin Development' started by torpkev, Dec 10, 2018.

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

    torpkev

    I've set up some regions using my code, each region can have flags that control what can be done there, and some regions can be inside others.

    So for example, I have a region called City, it has an order_by of 1 and a flag that says PREVENT_BREAK = true and PREVENT_PLACE = true. Inside the City region is a sub-region called Zone with an order_by = 2. It has a flag that says PREVENT_BREAK = true, PREVENT_ENDERPEARL = true, and PREVENT_BURN = true.

    I then have another sub-region that is inside the Zone region (so a sub-sub-region I guess) called AllowBurn with an order_by of 5 which has PREVENT_BURN = false. So I can use that region inside my larger region for areas where I'd allow things to burn.

    I have a class for each region along the lines of:

    Region Class:
    - Name
    - Order_By
    - List<Flags>

    and a Flags class like:

    Flags Class:
    - Name
    - Is_Active

    I have a list of my regions where the player is, so if they're inside the AllowBurn, my list might be City, Zone and AllowBurn

    I'd like to sort the list by order_by and then apply the flags in turn - so in my example above, I'd want to pull the flags for City, then Zone and then AllowBurn - but with PREVENT_BURN flag = false even though the zone has it as true (so they HAVE to go in order).

    So using my example:

    City - PREVENT_PLACE = true
    City - PREVENT_BREAK = true
    Zone - PREVENT_BREAK = true
    Zone - PREVENT_ENDERPEARL = true
    Zone - PREVENT_BURN = true
    AllowBurn - PREVENT_BURN = false

    If the player is standing in the AllowBurn region (which is inside the Zone region which is inside the City region), the flags should be:

    PREVENT_PLACE = true
    PREVENT_BREAK = true
    PREVENT_ENDERPEARL = true
    PREVENT_BURN = false

    So - Assuming I already have the List<Region> - What would be the best way to sort the list by Region.Order_By?

    Thanks
     
  2. @torpkev

    I suggest you let the class Region implement Comparable<Region> and use yourRegionList.sort() to sort the list. In the compareTo method of Region, you should return 1, 0 or -1 depending on what region has the highest orderBy.
     
  3. Offline

    torpkev

    Thanks - that is the approach I ended up going with.
     
Thread Status:
Not open for further replies.

Share This Page