Skip to content

Commit bffb291

Browse files
committed
Improve player name mapper loading and add tests
1 parent 8095efc commit bffb291

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

src/main/java/org/mvplugins/multiverse/inventories/profile/FlatFileProfileDataSource.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,9 @@ private CompletableFuture<Void> modifyGlobalProfile(GlobalProfile globalProfile,
315315
@Override
316316
public CompletableFuture<Void> updateGlobalProfile(GlobalProfile globalProfile) {
317317
File globalFile = profileFilesLocator.getGlobalFile(globalProfile.getPlayerUUID().toString());
318+
boolean didPlayerNameChange = playerNamesMapper.setPlayerName(globalProfile.getPlayerUUID(), globalProfile.getLastKnownName());
318319
return asyncFileIO.queueFileAction(globalFile, () -> processGlobalProfileWrite(globalProfile))
319-
.thenCompose(ignore -> playerNamesMapper.setPlayerName(globalProfile.getPlayerUUID(), globalProfile.getLastKnownName())
320+
.thenCompose(ignore -> didPlayerNameChange
320321
? asyncFileIO.queueFileAction(playerNamesMapper.getFile(), playerNamesMapper::savePlayerNames)
321322
: CompletableFuture.completedFuture(null));
322323
}

src/main/java/org/mvplugins/multiverse/inventories/profile/PlayerNamesMapper.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public final class PlayerNamesMapper {
2727

2828
private static final String FILENAME = "playernames.json";
2929

30-
private final MultiverseInventories inventories;
3130
private final Provider<ProfileDataSource> profileDataSourceProvider;
3231
private final Provider<ProfileCacheManager> profileCacheManagerProvider;
3332

@@ -43,7 +42,6 @@ public final class PlayerNamesMapper {
4342
@NotNull Provider<ProfileDataSource> profileDataSourceProvider,
4443
@NotNull Provider<ProfileCacheManager> profileCacheManagerProvider
4544
) {
46-
this.inventories = inventories;
4745
this.profileDataSourceProvider = profileDataSourceProvider;
4846
this.profileCacheManagerProvider = profileCacheManagerProvider;
4947

@@ -92,30 +90,45 @@ private void buildPlayerNamesMap() {
9290
.thenAccept(globalProfile -> setPlayerName(uuid, globalProfile.getLastKnownName())))
9391
.toArray(CompletableFuture[]::new);
9492
CompletableFuture.allOf(futures).thenRun(this::savePlayerNames).join();
93+
profileCacheManagerProvider.get().clearAllGlobalProfileCaches();
9594
Logging.info("Generated player names map.");
9695
}
9796

9897
boolean setPlayerName(UUID uuid, String name) {
99-
Logging.finer("Setting player name mapping for %s to %s", uuid, name);
98+
if (playerNamesJson == null) {
99+
throw new IllegalStateException("Player names mapper has not been loaded yet.");
100+
}
100101
if (Strings.isNullOrEmpty(name)) {
101102
return false;
102103
}
103104
if (getKey(name).filter(g -> g.getPlayerUUID().equals(uuid)).isDefined()) {
104105
return false;
105106
}
107+
108+
Logging.finer("Setting player name mapping for %s to %s", uuid, name);
106109
GlobalProfileKey globalProfileKey = GlobalProfileKey.create(uuid, name);
107-
playerNamesJson.put(uuid.toString(), name);
110+
111+
// Handle remove of old playername
112+
Object oldName = playerNamesJson.put(uuid.toString(), name);
113+
playerNamesMap.remove(String.valueOf(oldName));
114+
108115
playerNamesMap.put(name, globalProfileKey);
109116
playerUUIDMap.put(uuid, globalProfileKey);
110117
return true;
111118
}
112119

113120
void savePlayerNames() {
121+
if (playerNamesJson == null) {
122+
throw new IllegalStateException("Player names mapper has not been loaded yet.");
123+
}
124+
125+
Logging.finer("Saving player names map...");
114126
try (FileWriter fileWriter = new FileWriter(playerNamesFile)) {
115127
fileWriter.write(JSONValue.toJSONString(playerNamesJson));
116128
} catch (Exception e) {
117129
e.printStackTrace();
118130
}
131+
Logging.finer("Saving player names map... Done!");
119132
}
120133

121134
File getFile() {

src/test/java/org/mvplugins/multiverse/inventories/profile/PlayerNameChangeTest.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager
1111
import org.mvplugins.multiverse.inventories.profile.key.GlobalProfileKey
1212
import org.mvplugins.multiverse.inventories.util.FutureNow
1313
import java.nio.file.Path
14-
import kotlin.test.BeforeTest
15-
import kotlin.test.Test
16-
import kotlin.test.assertEquals
17-
import kotlin.test.assertFalse
18-
import kotlin.test.assertNotEquals
19-
import kotlin.test.assertTrue
14+
import kotlin.test.*
2015

2116
class PlayerNameChangeTest : TestWithMockBukkit() {
2217

2318
private lateinit var profileDataSource: ProfileDataSource
19+
private lateinit var playerNamesMapper: PlayerNamesMapper
2420
private lateinit var player: PlayerMock
2521

2622
@BeforeTest
2723
fun setUp() {
2824
profileDataSource = serviceLocator.getService(ProfileDataSource::class.java).takeIf { it != null } ?: run {
2925
throw IllegalStateException("ProfileDataSource is not available as a service") }
26+
playerNamesMapper = serviceLocator.getService(PlayerNamesMapper::class.java).takeIf { it != null } ?: run {
27+
throw IllegalStateException("PlayerNamesMapper is not available as a service") }
3028

3129
val worldManager = serviceLocator.getActiveService(WorldManager::class.java).takeIf { it != null } ?: run {
3230
throw IllegalStateException("WorldManager is not available as a service") }
@@ -40,6 +38,7 @@ class PlayerNameChangeTest : TestWithMockBukkit() {
4038
assertTrue(worldGroupManager.load().isSuccess)
4139

4240
player = server.addPlayer("Benji_0224")
41+
assertEquals(GlobalProfileKey.create(player.uniqueId, "Benji_0224"), playerNamesMapper.getKey("Benji_0224").orNull)
4342
}
4443

4544
@Test
@@ -81,5 +80,9 @@ class PlayerNameChangeTest : TestWithMockBukkit() {
8180

8281
// check player profile
8382
assertEquals("benthecat10", FutureNow.get(profileDataSource.getGlobalProfile(GlobalProfileKey.create(player)))?.lastKnownName)
83+
84+
// check name mapper updated
85+
assertEquals(GlobalProfileKey.create(player.uniqueId, "benthecat10"), playerNamesMapper.getKey("benthecat10").orNull)
86+
assertNull(playerNamesMapper.getKey("Benji_0224").orNull)
8487
}
8588
}

0 commit comments

Comments
 (0)