fix: shared object instance causes default profile sharing

This commit is contained in:
aro 2022-11-08 20:53:23 +01:00
parent b4d9c48fd7
commit 0bcbcbaa19
3 changed files with 15 additions and 4 deletions

View file

@ -15,12 +15,12 @@ public class AppearanceManager {
private AppearanceManager(UUID uuid) { private AppearanceManager(UUID uuid) {
this.player = Bukkit.getPlayer(uuid); this.player = Bukkit.getPlayer(uuid);
this.profile = dataStore.getData(uuid).orElse(NickoProfile.EMPTY_PROFILE); this.profile = dataStore.getData(uuid).orElse(NickoProfile.EMPTY_PROFILE.clone());
} }
private AppearanceManager(String name) { private AppearanceManager(String name) {
this.player = null; this.player = null;
this.profile = dataStore.getOfflineData(name).orElse(NickoProfile.EMPTY_PROFILE); this.profile = dataStore.getOfflineData(name).orElse(NickoProfile.EMPTY_PROFILE.clone());
} }
public static AppearanceManager get(Player player) { public static AppearanceManager get(Player player) {

View file

@ -1,6 +1,6 @@
package net.artelnatif.nicko.disguise; package net.artelnatif.nicko.disguise;
public class NickoProfile { public class NickoProfile implements Cloneable {
public static final NickoProfile EMPTY_PROFILE = new NickoProfile(null, null); public static final NickoProfile EMPTY_PROFILE = new NickoProfile(null, null);
private String name; private String name;
@ -39,4 +39,15 @@ public class NickoProfile {
", empty='" + isEmpty() + '\'' + ", empty='" + isEmpty() + '\'' +
'}'; '}';
} }
@Override
public NickoProfile clone() {
Object o;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
return (NickoProfile) o;
}
} }

View file

@ -52,7 +52,7 @@ public class PlayerDataStore {
retrievedProfile.ifPresent(profile -> profiles.put(uuid, profile)); retrievedProfile.ifPresent(profile -> profiles.put(uuid, profile));
return retrievedProfile; return retrievedProfile;
} else { } else {
final NickoProfile newProfile = NickoProfile.EMPTY_PROFILE; final NickoProfile newProfile = NickoProfile.EMPTY_PROFILE.clone();
profiles.put(uuid, newProfile); profiles.put(uuid, newProfile);
return Optional.of(newProfile); return Optional.of(newProfile);
} }