diff --git a/docs/changelog_v3.3.x.md b/docs/changelog_v3.3.x.md index 4d71c68b6..7676dc7e3 100644 --- a/docs/changelog_v3.3.x.md +++ b/docs/changelog_v3.3.x.md @@ -17,6 +17,11 @@ These change logs represent the work that has been going on within prison. # 3.3.0-alpha.18 2024-05-21 +* **Bug Fix: When a new player was joining prison, and there were placeholders being used in the rank commands for either the default ladder, or the first rank, the resolution of the placeholders was triggering a new player on-join processing within prison.** +This happed because the new RankPlayer object was not being added to the PlayerManager before ranking the player... now the player's object will be there when the rank commands are processed. +Honestly have no idea why this has not been an issue in the past.... + + * **Docs... added curseForge.com to the list of locations where prison can be downloaded from.** diff --git a/prison-core/src/main/java/tech/mcprison/prison/ranks/data/RankPlayer.java b/prison-core/src/main/java/tech/mcprison/prison/ranks/data/RankPlayer.java index 2171cda20..230e24cee 100644 --- a/prison-core/src/main/java/tech/mcprison/prison/ranks/data/RankPlayer.java +++ b/prison-core/src/main/java/tech/mcprison/prison/ranks/data/RankPlayer.java @@ -322,6 +322,8 @@ public boolean checkName( String playerName ) { RankPlayerName rpn = new RankPlayerName( playerName, System.currentTimeMillis() ); getNames().add( rpn ); + dirty = true; + added = true; } } @@ -2023,6 +2025,15 @@ private String applySecondaryPlaceholdersCheck( String placeholder, String value return results; } + /** + * Does nothing like the function name says. + * + * This is just a placeholder so the compiler won't generate + * a variable not used warning. + */ + public void doNothing() { + } + // public long getRankScoreCooldown() { // return rankScoreCooldown; // } diff --git a/prison-ranks/src/main/java/tech/mcprison/prison/ranks/data/RankPlayerFactory.java b/prison-ranks/src/main/java/tech/mcprison/prison/ranks/data/RankPlayerFactory.java index 366cd4f06..542c3e799 100644 --- a/prison-ranks/src/main/java/tech/mcprison/prison/ranks/data/RankPlayerFactory.java +++ b/prison-ranks/src/main/java/tech/mcprison/prison/ranks/data/RankPlayerFactory.java @@ -109,15 +109,19 @@ public static Document toDocument( RankPlayer rankPlayer ) { * since it will skip processing for them. *

* - *

Note, this will not save the player's new rank. The save function must be - * managed and called outside of this. + *

Note, this will save the player's new rank. *

*/ public void firstJoin( RankPlayer rankPlayer) { RankLadder defaultLadder = PrisonRanks.getInstance().getDefaultLadder(); - if ( !rankPlayer.getLadderRanks().containsKey( defaultLadder ) ) { + if ( defaultLadder == null ) { + + Output.get().logError( "RankPlayerFactory.firstJoin: No default ladder!!" ); + + } + else if ( !rankPlayer.getLadderRanks().containsKey( defaultLadder ) ) { Optional firstRank = defaultLadder.getLowestRank(); @@ -131,6 +135,9 @@ public void firstJoin( RankPlayer rankPlayer) { rankPlayer.setDirty( true ); + // Saves the new player's rank: + PrisonRanks.getInstance().getPlayerManager().savePlayer(rankPlayer); + // rankPlayer.addRank( defaultRank ); Prison.get().getEventBus().post(new FirstJoinEvent( rankPlayer )); diff --git a/prison-ranks/src/main/java/tech/mcprison/prison/ranks/managers/PlayerManager.java b/prison-ranks/src/main/java/tech/mcprison/prison/ranks/managers/PlayerManager.java index 01fc9253e..ae6c2301d 100644 --- a/prison-ranks/src/main/java/tech/mcprison/prison/ranks/managers/PlayerManager.java +++ b/prison-ranks/src/main/java/tech/mcprison/prison/ranks/managers/PlayerManager.java @@ -19,7 +19,9 @@ import java.io.IOException; import java.text.DecimalFormat; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -232,7 +234,8 @@ public void checkPlayerDefaultRank( RankPlayer rPlayer ) { RankPlayerFactory rankPlayerFactory = new RankPlayerFactory(); rankPlayerFactory.firstJoin( rPlayer ); - PrisonRanks.getInstance().getPlayerManager().savePlayer( rPlayer ); + // It now saves the new player changes within firstJoin() + //PrisonRanks.getInstance().getPlayerManager().savePlayer( rPlayer ); } } @@ -293,12 +296,14 @@ public RankPlayer getPlayer(UUID uid, String playerName) { if ( results == null ) { + debugLogPlayerInfo( "getPlayer(): UUID check:", playerName, false ); + for ( RankPlayer rankPlayer : players ) { if ( uid != null && rankPlayer.getUUID().equals(uid) || - !playerName.isEmpty() && - rankPlayer.getDisplayName() != null && - rankPlayer.getDisplayName().equalsIgnoreCase( playerName ) ) { + !playerName.isEmpty() && + rankPlayer.getName() != null && + rankPlayer.getName().equalsIgnoreCase( playerName ) ) { // This checks to see if they have a new name, if so, then adds it to the history: // But the UID must match: @@ -320,21 +325,25 @@ public RankPlayer getPlayer(UUID uid, String playerName) { // player.checkName( playerName )))).findFirst(); if ( results == null && playerName != null && !"console".equalsIgnoreCase( playerName ) ) { + + debugLogPlayerInfo( "getPlayer(): addPlayer:", playerName, false ); + results = addPlayer(uid, playerName); - if ( results != null ) { - - results.setDirty( true ); - } + // addPlayer() will save the player: +// if ( results != null ) { +// +// results.setDirty( true ); +// } // dirty = results != null; } - // Save if dirty (changed or new): - if ( results != null && results.isDirty() ) { - savePlayer( results ); - - } +// // Save if dirty (changed or new): +// if ( results != null && results.isDirty() ) { +// savePlayer( results ); +// +// } return results; } @@ -392,15 +401,28 @@ protected RankPlayer addPlayerSyncTask( UUID uid, String playerName ) { // We need to create a new player data file. newPlayer = new RankPlayer( uid, playerName ); - newPlayer.checkName( playerName ); +// newPlayer.checkName( playerName ); newPlayer.setDirty( true ); - rankPlayerFactory.firstJoin( newPlayer ); + // WARNING: Must save the newPlayer object to the playerManager collections + // before calling firstJoin(): players.add(newPlayer); getPlayersByName().put( playerName, newPlayer ); + + + debugLogPlayerInfo( "addPlayerSyncTask: firstJoin:", playerName, false ); - savePlayer(newPlayer); + rankPlayerFactory.firstJoin( newPlayer ); + + + boolean joined = newPlayer.getPlayerRankDefault() != null; + String msg = joined ? "joined" : "failed"; + debugLogPlayerInfo( "addPlayerSyncTask: " + msg, playerName, false ); + + + // the new player is now saved in firstJoin()( + //savePlayer(newPlayer); // try { @@ -430,10 +452,34 @@ public void onPlayerJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); + debugLogPlayerInfo( "onPlayerJoin:", player.getName(), true ); + // Player is auto added if they do not exist when calling getPlayer so don't try to // add them a second time. - getPlayer(player.getUUID(), player.getName()); + RankPlayer rPlayer = getPlayer(player.getUUID(), player.getName()); + rPlayer.doNothing(); + } + + public void debugLogPlayerInfo( String eventName, String playerName, boolean date ) { + + if ( Output.get().isDebug() ) { + + boolean newPlayer = !getPlayersByName().containsKey( playerName ); + + SimpleDateFormat sdFmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + String msg = String.format( + "&6%s: &c%s &6%s &d%s", + eventName, + playerName, + date ? sdFmt.format(new Date()) : "", + newPlayer ? "[New Player]" : "" + ); + + Output.get().logInfo( msg ); + } + }