Class O2Houses

java.lang.Object
net.pottercraft.ollivanders2.house.O2Houses

public class O2Houses extends Object
"While you are here, your house will be something like your family within Hogwarts. You will have classes with the rest of your house, sleep in your house dormitory, and spend free time in your house common room."

O2Houses manages the Hogwarts house system including sorting players, tracking house membership, managing house points, and displaying a scoreboard with house standings. Houses are optional and configurable, allowing servers to customize house names and chat colors. The class persists house data to disk and fires events when players are sorted.

House Features:

  • Player sorting into four Hogwarts houses
  • House membership persistence across server restarts
  • House points system with scoreboard display
  • Customizable house names and colors via configuration
  • Team-based player grouping with colored display names
  • OllivandersPlayerSortedEvent fire on successful sorting
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static boolean
    Whether to display sorting messages on broadcast to all players.
    static boolean
    Whether houses are enabled.
  • Constructor Summary

    Constructors
    Constructor
    Description
    O2Houses(@NotNull Ollivanders2 plugin)
    Constructor for the house management system.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    addHousePoints(@NotNull O2HouseType houseType, int points)
    Add points to a specific house.
    void
    addPlayerToHouseTeam(@NotNull org.bukkit.entity.Player player)
    Add a player to their assigned house team with colored display name.
    void
    forceSetHouse(@NotNull org.bukkit.entity.Player player, @NotNull O2HouseType houseType)
    Force sets the players house to a house.
    @NotNull ArrayList<String>
    Get all the house names.
    @Nullable O2HouseType
    getHouse(@NotNull UUID pid)
    Get the house a player is sorted in to.
    @Nullable O2HouseType
    getHouse(@NotNull org.bukkit.entity.Player player)
    Get the house a player is sorted in to.
    @NotNull ArrayList<String>
    getHouseMembers(@NotNull O2HouseType houseType)
    Get a list of the players sorted in to a house.
    @Nullable O2HouseType
    getHouseType(@Nullable String name)
    Get the house type by name.
    boolean
    isSorted(@NotNull UUID pid)
    Determines if a player has been sorted already or not.
    boolean
    isSorted(@NotNull org.bukkit.entity.Player player)
    Determines if a player has been sorted already or not.
    void
    Cleanup when the plugin disables.
    void
    Initialize and enable the house system on plugin startup.
    boolean
    Reset the complete house system to its initial state.
    boolean
    Reset all house points to zero.
    void
    Save all house data to disk as JSON files.
    boolean
    setHousePoints(@NotNull O2HouseType houseType, int points)
    Set the house points for a specific house.
    boolean
    sort(@NotNull org.bukkit.entity.Player player, @NotNull O2HouseType houseType)
    Sort a player into a house.
    boolean
    subtractHousePoints(@NotNull O2HouseType houseType, int points)
    Subtract points from a specific house.
    void
    unsort(@NotNull org.bukkit.entity.Player player)
    Remove a player from their house, making them unsorted.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • O2Houses

      public O2Houses(@NotNull @NotNull Ollivanders2 plugin)
      Constructor for the house management system.

      Initializes the O2Houses manager with a reference to the Ollivanders2 plugin and creates common utility instances for logging and debugging.

      Parameters:
      plugin - the Ollivanders2 plugin instance
  • Method Details

    • onEnable

      public void onEnable()
      Initialize and enable the house system on plugin startup.

      Reads house configuration options, creates the scoreboard infrastructure, initializes house points, loads persisted house data from disk, and displays the house points scoreboard to players. If houses are disabled in the configuration, this method returns early without performing initialization.

    • getHouseType

      @Nullable public @Nullable O2HouseType getHouseType(@Nullable @Nullable String name)
      Get the house type by name.

      Performs case-insensitive matching against all house types. The input name is automatically trimmed of whitespace before comparison. Returns null if no matching house is found.

      Parameters:
      name - the name of the house (case-insensitive, spaces are trimmed)
      Returns:
      the matching house type, or null if no match is found
    • getAllHouseNames

      @NotNull public @NotNull ArrayList<String> getAllHouseNames()
      Get all the house names.

      Returns a fresh list of all house names. The list reflects the current names of each house type, which may have been customized via configuration. This method returns a new list each time it is called.

      Returns:
      a new ArrayList containing all house names in order
    • onDisable

      public void onDisable()
      Cleanup when the plugin disables.

      Called when the Ollivanders2 plugin is being shut down. The O2Houses manager persists all house-related data to disk before the plugin terminates. This ensures that house memberships and point scores are preserved across server restarts.

      Saved Data:

      • Player-to-house mappings: which players are sorted into which houses
      • House point scores: accumulated points for each of the four houses
      • All data is persisted as JSON files via GsonDAO
      See Also:
    • saveHouses

      public void saveHouses()
      Save all house data to disk as JSON files.

      Persists the current player-to-house mappings and house point scores to disk via GsonDAO. This method writes two JSON files: one containing player house assignments and one containing house points. Does nothing if houses are disabled via configuration.

    • sort

      public boolean sort(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull O2HouseType houseType)
      Sort a player into a house.

      Assigns the player to the specified house if they are not already sorted. Adds the player to the house team with house-colored display name, broadcasts a title message to all sorted players (if configured), and fires an OllivandersPlayerSortedEvent. Returns false if the player is already sorted.

      Parameters:
      player - the player to sort
      houseType - the house to sort them into
      Returns:
      true if the player is successfully sorted, false if already sorted
    • unsort

      public void unsort(@NotNull @NotNull org.bukkit.entity.Player player)
      Remove a player from their house, making them unsorted.

      Removes the player from their current house team and the house map. This should only be used on a player reset or when explicitly removing a player from the house system. If the player is not currently sorted, this method does nothing.

      Parameters:
      player - the player to unsort
    • isSorted

      public boolean isSorted(@NotNull @NotNull org.bukkit.entity.Player player)
      Determines if a player has been sorted already or not.
      Parameters:
      player - the player to check
      Returns:
      true if the player has been sorted, false otherwise.
    • isSorted

      public boolean isSorted(@NotNull @NotNull UUID pid)
      Determines if a player has been sorted already or not.
      Parameters:
      pid - the uuid of the player to check
      Returns:
      true if the player has been sorted, false otherwise.
    • forceSetHouse

      public void forceSetHouse(@NotNull @NotNull org.bukkit.entity.Player player, @NotNull @NotNull O2HouseType houseType)
      Force sets the players house to a house.

      This will happen even if a player has been previously sorted. This is a separate function so that sort() is not accidentally used once a player has been sorted.

      Parameters:
      player - the player to sort
      houseType - the house to add them to
    • getHouse

      @Nullable public @Nullable O2HouseType getHouse(@NotNull @NotNull org.bukkit.entity.Player player)
      Get the house a player is sorted in to.
      Parameters:
      player - the player to get the house for
      Returns:
      the House the player is sorted in to, null otherwise.
    • getHouse

      @Nullable public @Nullable O2HouseType getHouse(@NotNull @NotNull UUID pid)
      Get the house a player is sorted in to.
      Parameters:
      pid - the uuid of the player to search for
      Returns:
      the House the player is sorted in to, null otherwise.
    • getHouseMembers

      @NotNull public @NotNull ArrayList<String> getHouseMembers(@NotNull @NotNull O2HouseType houseType)
      Get a list of the players sorted in to a house.
      Parameters:
      houseType - the house to get the members of
      Returns:
      the names of all members of the specified house.
    • setHousePoints

      public boolean setHousePoints(@NotNull @NotNull O2HouseType houseType, int points)
      Set the house points for a specific house.

      Updates the point score for the specified house and refreshes the scoreboard display. This is a thread-safe operation that sets the exact point value (not an increment).

      Parameters:
      houseType - the house to set points for
      points - the new point value to set for this house
      Returns:
      true if the scoreboard was successfully updated, false otherwise
    • resetHousePoints

      public boolean resetHousePoints()
      Reset all house points to zero.

      Sets the point score for each house back to 0 and refreshes the scoreboard display to reflect the changes. This method does not affect player house membership; only the accumulated house points are cleared. The scoreboard is immediately updated to show the reset values to all players.

      Returns:
      true if the scoreboard was successfully updated after resetting points, false otherwise
      See Also:
    • reset

      public boolean reset()
      Reset the complete house system to its initial state.

      Performs a full reset of the house management system by clearing all player-to-house mappings and resetting all house points to zero. This removes all players from their assigned houses and clears the accumulated points for all houses. The scoreboard display is updated immediately to reflect the reset state. Use this when you need a complete wipe of all house data.

      Returns:
      true if the scoreboard was successfully updated after the reset, false otherwise
      See Also:
    • addHousePoints

      public boolean addHousePoints(@NotNull @NotNull O2HouseType houseType, int points)
      Add points to a specific house.

      Increments the point score for the specified house by the given amount. The new total is calculated by adding the points parameter to the house's current score. The scoreboard is automatically updated to display the new point value to all players. This is equivalent to calling setHousePoints() with the sum of the current score and the increment value.

      Parameters:
      houseType - the house to add points to
      points - the amount of points to add (can be positive or negative)
      Returns:
      true if the scoreboard was successfully updated, false otherwise
      See Also:
    • subtractHousePoints

      public boolean subtractHousePoints(@NotNull @NotNull O2HouseType houseType, int points)
      Subtract points from a specific house.

      Decrements the point score for the specified house by the given amount. The new total is calculated by subtracting the points parameter from the house's current score. If the subtraction would result in a negative value, the house points are clamped to zero instead. The scoreboard is automatically updated to display the new point value to all players.

      Parameters:
      houseType - the house to subtract points from
      points - the amount of points to subtract; if this is greater than or equal to the house's total, points are set to 0
      Returns:
      true if the scoreboard was successfully updated, false otherwise
      See Also:
    • addPlayerToHouseTeam

      public void addPlayerToHouseTeam(@NotNull @NotNull org.bukkit.entity.Player player)
      Add a player to their assigned house team with colored display name.

      Convenience method that automatically retrieves the player's assigned house from the house map and adds them to the corresponding team with house-colored display name. This method is called during player sorting and when players rejoin the server. If the player is not in the house map or their house team does not exist, this method returns silently.

      Parameters:
      player - the player to add to their house team
      See Also: