2024-11-24 03:06:00 +00:00
/ *
*
* FactionManager
*
* Class is responsible for keeping track of factions chosen by the players in the game and saves to the configuration
* file for persistent data storage . Class handles checks as well for eligibility purposes ( making sure players can join , etc )
*
* /
package jesse.keeblarcraft.FactionMgr ;
import java.util.List ;
import jesse.keeblarcraft.ConfigMgr.ConfigManager ;
import jesse.keeblarcraft.Utils.ChatUtil ;
import jesse.keeblarcraft.Utils.ChatUtil.CONSOLE_COLOR ;
import jesse.keeblarcraft.Utils.CustomExceptions.FILE_WRITE_EXCEPTION ;
import net.minecraft.server.network.ServerPlayerEntity ;
import net.minecraft.text.Text ;
public class FactionManager {
2024-11-24 20:06:34 +00:00
private static String FACTION_CFG_FILE = " config/keeblarcraft/factions/factions.json " ;
2024-11-24 03:06:00 +00:00
ConfigManager config = new ConfigManager ( ) ;
private static FactionManager static_inst ;
public static FactionManager GetInstance ( ) {
if ( static_inst = = null ) {
static_inst = new FactionManager ( ) ;
}
return static_inst ;
}
private class FactionConfigClassWrapper {
FactionConfig factions = new FactionConfig ( ) ;
}
FactionConfigClassWrapper factionConfig ; // = new FactionConfigClassWrapper();
// Constructor
public FactionManager ( ) {
// Read in config at start of object
System . out . println ( " FACTIONMANAGER CONSTRUCTOR CALLED " ) ;
Boolean existingFile = false ;
factionConfig = new FactionConfigClassWrapper ( ) ;
Boolean tmpchck = factionConfig = = null ;
System . out . println ( " Is factionConfig null still? " + ( tmpchck ? " YES " : " NO " ) ) ;
try {
factionConfig = config . GetJsonObjectFromFile ( FACTION_CFG_FILE , FactionConfigClassWrapper . class ) ;
tmpchck = factionConfig = = null ;
System . out . println ( " Is factionconfig null after trying to load stuff? " + ( tmpchck ? " YES " : " NO " ) ) ;
existingFile = true ;
} catch ( Exception e ) {
// Do nothing
}
// Create the file if it didn't exist before
if ( ! existingFile )
{
try {
2024-11-24 20:06:34 +00:00
config . CreateDirectory ( FACTION_CFG_FILE ) ;
2024-11-24 03:06:00 +00:00
FlashConfig ( ) ;
} catch ( Exception e ) {
System . out . println ( ChatUtil . ColoredString ( " Could not write to file " , CONSOLE_COLOR . RED ) ) ;
}
}
if ( factionConfig = = null ) {
// the only way for this to be possible is if the read-in was bad. flash config file then try again
factionConfig = new FactionConfigClassWrapper ( ) ;
//TODO: Add safe-guard in here to check if default faction dir exists and move it to OLD/CORRUPTED (so data is not nuked from orbit)
factionConfig . factions = new FactionConfig ( ) ;
FlashConfig ( ) ;
}
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn LeaveFaction
///
/// @param[in] player is the player on the server
///
/// @brief Leave a faction
///
/// @return True if player can leave their faction, false if not
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public Boolean LeaveFaction ( ServerPlayerEntity player ) {
Boolean success = false ;
String playerFac = factionConfig . factions . FindFactionOfPlayer ( player . getUuidAsString ( ) ) ;
if ( playerFac ! = " " ) {
success = factionConfig . factions . LeaveFaction ( playerFac , player . getUuidAsString ( ) , player . getDisplayName ( ) . toString ( ) ) ;
player . sendMessage ( Text . of ( " [Factions]: You left your faction! " ) ) ;
} else {
player . sendMessage ( Text . of ( " [Factions]: You are not in a faction! " ) ) ;
}
return success ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn CreateFaction
///
/// @param[in] factionName is the faction to be created
///
/// @param[in] player is the player on the server
///
/// @brief Create a new faction
///
/// @return True if successfully created, false if not
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public Boolean CreateFaction ( String factionName , ServerPlayerEntity creator ) {
Boolean success = false ;
String facOfPlayer = factionConfig . factions . FindFactionOfPlayer ( creator . getUuidAsString ( ) ) ;
if ( facOfPlayer = = " " ) {
success = factionConfig . factions . CreateFaction ( factionName , creator . getUuidAsString ( ) , creator . getDisplayName ( ) . toString ( ) ) ;
if ( ! success ) {
creator . sendMessage ( Text . of ( " [Factions]: Could not create faction - faction already exists. " ) ) ;
} else {
creator . sendMessage ( Text . of ( " [Factions]: Successfully created faction! " ) ) ;
FlashConfig ( ) ;
}
} else {
creator . sendMessage ( Text . of ( " [Factions]: You are already in a faction! You cannot create one. " ) ) ;
}
return success ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn DeleteFaction
///
/// @param[in] factionName is the faction to be created
///
/// @param[in] caller is the player on the server
///
/// @brief Delete a faction
///
/// @return True if successfully deleted, false if not
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public Boolean DeleteFaction ( String factionName , ServerPlayerEntity caller ) {
Boolean success = factionConfig . factions . DeleteFaction ( factionName , caller . getUuidAsString ( ) ) ;
if ( ! success ) {
caller . sendMessage ( Text . of ( " [Factions]: Could not delete faction. You must be owner & faction must exist. " ) ) ;
} else {
caller . sendMessage ( Text . of ( " [Factions]: Successfully deleted faction. " ) ) ;
FlashConfig ( ) ;
}
return success ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn ListOfFactions
///
/// @brief Get a list of all factions on the server
///
/// @return Faction list. Empty list if no factions
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public List < String > ListOfFactions ( ) {
System . out . println ( " Callthrough of listoffactions " ) ;
return factionConfig . factions . ListOfAllFactions ( ) ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn GetFactionOfPlayer
///
/// @param[in] playerUuid is the uuid of the player
///
/// @brief Gets the faction of a player on the server
///
/// @return String with faction name. Empty string if no faction tie
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public String GetFactionOfPlayer ( String playerUuid ) {
return factionConfig . factions . FindFactionOfPlayer ( playerUuid ) ;
}
public Boolean PromotePlayer ( ServerPlayerEntity caller , String promoteeUuid , String promoteeDisplayName ) {
Boolean success = factionConfig . factions . PromotePlayer ( GetFactionOfPlayer ( caller . getUuidAsString ( ) ) , caller . getUuidAsString ( ) , promoteeUuid , promoteeDisplayName ) ;
if ( ! success ) {
caller . sendMessage ( Text . of ( " [Factions]: Could not promote player - you need to be a higher rank than them and they cannot be promoted to your level! " ) ) ;
} else {
caller . sendMessage ( Text . of ( " [Factions]: Successfully promoted player! " ) ) ;
}
return success ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn DemotePlayer
///
/// @param[in] caller is the player who is demoting another player
///
/// @param[in] demoteeUuid is the player's uuid who is being demoted
///
/// @param[in] demoteeDisplayName is the players display name who is being demoted
///
/// @brief Demote a player from a faction
///
/// @return True if player is demoted. False if not
/////////////////////////////////////////////////////////////////////////////
public Boolean DemotePlayer ( ServerPlayerEntity caller , String demoteeUuid , String demoteeDisplayName ) {
Boolean success = factionConfig . factions . DemotePlayer ( GetFactionOfPlayer ( caller . getUuidAsString ( ) ) , caller . getUuidAsString ( ) , demoteeUuid , demoteeDisplayName ) ;
2024-11-24 03:06:00 +00:00
if ( ! success ) {
caller . sendMessage ( Text . of ( " [Factions]: Could not demote player - you need to be a higher rank than them to demote them! " ) ) ;
} else {
caller . sendMessage ( Text . of ( " [Factions]: Successfully demoted player! " ) ) ;
}
return success ;
}
2024-12-06 07:04:09 +00:00
/////////////////////////////////////////////////////////////////////////////
/// @fn FlashConfig
///
/// @brief Update the faction configuration file on disk
/////////////////////////////////////////////////////////////////////////////
2024-11-24 03:06:00 +00:00
public void FlashConfig ( ) {
try {
config . WriteToJsonFile ( FACTION_CFG_FILE , factionConfig ) ;
} catch ( FILE_WRITE_EXCEPTION e ) {
System . out . println ( " config writing of faction file failed. oh well! " ) ;
}
}
}