Skip to content

Commit

Permalink
Bug Fix: When a new player was joining prison, and there were placeho…
Browse files Browse the repository at this point in the history
…lders 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....
  • Loading branch information
rbluer committed May 22, 2024
1 parent 2cb9bcd commit 3110bbb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 20 deletions.
5 changes: 5 additions & 0 deletions docs/changelog_v3.3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ public boolean checkName( String playerName ) {
RankPlayerName rpn = new RankPlayerName( playerName, System.currentTimeMillis() );
getNames().add( rpn );

dirty = true;

added = true;
}
}
Expand Down Expand Up @@ -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;
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,19 @@ public static Document toDocument( RankPlayer rankPlayer ) {
* since it will skip processing for them.
* </p>
*
* <p>Note, this will not save the player's new rank. The save function must be
* managed and called outside of this.
* <p>Note, this will save the player's new rank.
* </p>
*/
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<Rank> firstRank = defaultLadder.getLowestRank();

Expand All @@ -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 ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );

}
}
Expand Down Expand Up @@ -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:
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 );
}

}


Expand Down

0 comments on commit 3110bbb

Please sign in to comment.