44import com .dumptruckman .minecraft .util .Logging ;
55import com .google .common .cache .Cache ;
66import com .google .common .cache .CacheBuilder ;
7+ import com .google .common .collect .Sets ;
78import net .minidev .json .parser .JSONParser ;
89import net .minidev .json .parser .ParseException ;
910import org .bukkit .configuration .InvalidConfigurationException ;
@@ -143,11 +144,26 @@ private File getFolder(ContainerType type, String folderName) {
143144 * @return The data file for a player.
144145 * @throws IOException if there was a problem creating the file.
145146 */
146- File getPlayerFile (ContainerType type , String dataName , String playerName ) throws IOException {
147+ private File getPlayerFile (ContainerType type , String dataName , String playerName ) throws IOException {
148+ return getPlayerFile (type , dataName , playerName , true );
149+ }
150+
151+ /**
152+ * Retrieves the data file for a player based on a given world/group name, creating it if necessary.
153+ *
154+ * @param type Indicates whether data is for group or world.
155+ * @param dataName The name of the group or world.
156+ * @param playerName The name of the player.
157+ * @return The data file for a player.
158+ * @throws IOException if there was a problem creating the file.
159+ */
160+ private File getPlayerFile (ContainerType type , String dataName , String playerName , boolean createNew ) throws IOException {
147161 File jsonPlayerFile = new File (this .getFolder (type , dataName ), playerName + JSON );
148162 if (!jsonPlayerFile .exists ()) {
149163 try {
150- jsonPlayerFile .createNewFile ();
164+ if (createNew ) {
165+ jsonPlayerFile .createNewFile ();
166+ }
151167 } catch (IOException e ) {
152168 throw new IOException ("Could not create necessary player data file: " + jsonPlayerFile .getPath ()
153169 + ". Data for " + playerName + " in " + type .name ().toLowerCase () + " " + dataName
@@ -547,7 +563,9 @@ public void setLoadOnLogin(final String playerName, final boolean loadOnLogin) {
547563 }
548564
549565 @ Override
550- public void migratePlayerData (String oldName , String newName , UUID uuid , boolean removeOldData ) throws IOException {
566+ public void migratePlayerData (String oldName , String newName , UUID uuid ) throws IOException {
567+ clearPlayerCache (uuid );
568+
551569 File [] worldFolders = worldFolder .listFiles (File ::isDirectory );
552570 if (worldFolders == null ) {
553571 throw new IOException ("Could not enumerate world folders" );
@@ -557,36 +575,41 @@ public void migratePlayerData(String oldName, String newName, UUID uuid, boolean
557575 throw new IOException ("Could not enumerate group folders" );
558576 }
559577
560- for (File worldFolder : worldFolders ) {
561- ProfileKey key = ProfileKey .createProfileKey (ContainerType .WORLD , worldFolder .getName (),
562- ProfileTypes .ADVENTURE , uuid , oldName );
563- updatePlayerData (getPlayerData (key ));
564- updatePlayerData (getPlayerData (ProfileKey .createProfileKey (key , ProfileTypes .CREATIVE )));
565- updatePlayerData (getPlayerData (ProfileKey .createProfileKey (key , ProfileTypes .SURVIVAL )));
566- }
567-
568- for (File groupFolder : groupFolders ) {
569- ProfileKey key = ProfileKey .createProfileKey (ContainerType .GROUP , groupFolder .getName (),
570- ProfileTypes .ADVENTURE , uuid , oldName );
571- updatePlayerData (getPlayerData (key ));
572- updatePlayerData (getPlayerData (ProfileKey .createProfileKey (key , ProfileTypes .CREATIVE )));
573- updatePlayerData (getPlayerData (ProfileKey .createProfileKey (key , ProfileTypes .SURVIVAL )));
574- }
578+ migrateForContainerType (worldFolders , ContainerType .WORLD , oldName , newName );
579+ migrateForContainerType (groupFolders , ContainerType .GROUP , oldName , newName );
580+ }
575581
576- if (removeOldData ) {
577- for (File worldFolder : worldFolders ) {
578- removePlayerData (ContainerType .WORLD , worldFolder .getName (), ProfileTypes .ADVENTURE , oldName );
579- removePlayerData (ContainerType .WORLD , worldFolder .getName (), ProfileTypes .CREATIVE , oldName );
580- removePlayerData (ContainerType .WORLD , worldFolder .getName (), ProfileTypes .SURVIVAL , oldName );
582+ private void migrateForContainerType (File [] folders , ContainerType containerType , String oldName , String newName ) throws IOException {
583+ for (File folder : folders ) {
584+ File oldNameFile = getPlayerFile (containerType , folder .getName (), oldName , false );
585+ File newNameFile = getPlayerFile (containerType , folder .getName (), newName , false );
586+ if (!oldNameFile .exists ()) {
587+ Logging .fine ("No old data for player %s in %s %s to migrate." ,
588+ oldName , containerType .name (), folder .getName ());
589+ continue ;
590+ }
591+ if (newNameFile .exists ()) {
592+ Logging .warning ("Data already exists for player %s in %s %s. Not migrating." ,
593+ newName , containerType .name (), folder .getName ());
594+ continue ;
581595 }
582- for ( File groupFolder : groupFolders ) {
583- removePlayerData ( ContainerType . GROUP , groupFolder . getName (), ProfileTypes . ADVENTURE , oldName );
584- removePlayerData ( ContainerType . GROUP , groupFolder . getName (), ProfileTypes . CREATIVE , oldName );
585- removePlayerData ( ContainerType . GROUP , groupFolder . getName (), ProfileTypes . SURVIVAL , oldName ) ;
596+ if (! oldNameFile . renameTo ( newNameFile ) ) {
597+ Logging . warning ( "Could not rename old data file for player %s in %s %s to %s." ,
598+ oldName , containerType . name (), folder . getName (), newName );
599+ continue ;
586600 }
601+ Logging .fine ("Migrated data for player %s in %s %s to %s." ,
602+ oldName , containerType .name (), folder .getName (), newName );
587603 }
588604 }
589605
606+ void clearPlayerCache (UUID playerUUID ) {
607+ profileCache .invalidateAll (Sets .filter (
608+ profileCache .asMap ().keySet (),
609+ key -> key .getPlayerUUID ().equals (playerUUID )
610+ ));
611+ }
612+
590613 @ Override
591614 public void clearProfileCache (ProfileKey key ) {
592615 profileCache .invalidate (key );
0 commit comments