Class O2Houses
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
FieldsModifier and TypeFieldDescriptionstatic booleanWhether to display sorting messages on broadcast to all players.static booleanWhether houses are enabled. -
Constructor Summary
ConstructorsConstructorDescriptionO2Houses(@NotNull Ollivanders2 plugin) Constructor for the house management system. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddHousePoints(@NotNull O2HouseType houseType, int points) Add points to a specific house.voidaddPlayerToHouseTeam(@NotNull org.bukkit.entity.Player player) Add a player to their assigned house team with colored display name.voidforceSetHouse(@NotNull org.bukkit.entity.Player player, @NotNull O2HouseType houseType) Force sets the players house to a house.Get all the house names.@Nullable O2HouseTypeGet the house a player is sorted in to.@Nullable O2HouseTypegetHouse(@NotNull org.bukkit.entity.Player player) Get the house a player is sorted in to.getHouseMembers(@NotNull O2HouseType houseType) Get a list of the players sorted in to a house.@Nullable O2HouseTypegetHouseType(@Nullable String name) Get the house type by name.booleanDetermines if a player has been sorted already or not.booleanisSorted(@NotNull org.bukkit.entity.Player player) Determines if a player has been sorted already or not.voidCleanup when the plugin disables.voidonEnable()Initialize and enable the house system on plugin startup.booleanreset()Reset the complete house system to its initial state.booleanReset all house points to zero.voidSave all house data to disk as JSON files.booleansetHousePoints(@NotNull O2HouseType houseType, int points) Set the house points for a specific house.booleansort(@NotNull org.bukkit.entity.Player player, @NotNull O2HouseType houseType) Sort a player into a house.booleansubtractHousePoints(@NotNull O2HouseType houseType, int points) Subtract points from a specific house.voidunsort(@NotNull org.bukkit.entity.Player player) Remove a player from their house, making them unsorted.
-
Field Details
-
useHouses
public static boolean useHousesWhether houses are enabled.- See Also:
-
displayMessageOnSort
public static boolean displayMessageOnSortWhether to display sorting messages on broadcast to all players.- See Also:
-
-
Constructor Details
-
O2Houses
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
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
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 sorthouseType- 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
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 sorthouseType- the house to add them to
-
getHouse
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
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
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
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 forpoints- 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
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 topoints- the amount of points to add (can be positive or negative)- Returns:
- true if the scoreboard was successfully updated, false otherwise
- See Also:
-
subtractHousePoints
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 frompoints- 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:
-