Change saved disguises to not wipe on load

This commit is contained in:
libraryaddict 2024-04-07 23:35:40 +12:00
parent 5d5df0ed7c
commit 4653366231
5 changed files with 31 additions and 65 deletions

View File

@ -806,6 +806,8 @@ public abstract class Disguise {
removeBossBar();
}
DisguiseUtilities.saveDisguises(getEntity());
return true;
}
@ -1035,6 +1037,7 @@ public abstract class Disguise {
}
makeBossBar();
DisguiseUtilities.saveDisguises(getEntity());
return true;
}

View File

@ -811,6 +811,8 @@ public class DisguiseUtilities {
if (disg.getEntity() instanceof Player ? !DisguiseConfig.isSavePlayerDisguises() :
!DisguiseConfig.isSaveEntityDisguises()) {
// Save empty array, clear any saved disguises
saveDisguises(disg.getEntity(), new Disguise[0]);
break;
}
@ -862,11 +864,20 @@ public class DisguiseUtilities {
}
}
public static void saveDisguises(Entity owningEntity) {
saveDisguises(owningEntity, DisguiseAPI.getDisguises(owningEntity));
}
public static void saveDisguises(Entity owningEntity, Disguise[] disguise) {
if (!LibsPremium.isPremium()) {
return;
}
// If the disguises should not be saved
if (!(owningEntity instanceof Player ? DisguiseConfig.isSavePlayerDisguises() : DisguiseConfig.isSaveEntityDisguises())) {
disguise = new Disguise[0];
}
if (!NmsVersion.v1_14.isSupported()) {
saveDisguises(owningEntity.getUniqueId(), disguise);
return;
@ -932,36 +943,23 @@ public class DisguiseUtilities {
}
}
@Deprecated
public static Disguise[] getSavedDisguises(UUID entityUUID) {
return getSavedDisguises(entityUUID, false);
}
public static Disguise[] getSavedDisguises(Entity entity) {
return getSavedDisguises(entity, false);
}
public static Disguise[] getSavedDisguises(Entity entity, boolean remove) {
if (!LibsPremium.isPremium()) {
return new Disguise[0];
}
if (!NmsVersion.v1_14.isSupported()) {
return getSavedDisguises(entity.getUniqueId(), remove);
return getSavedDisguises(entity.getUniqueId());
}
PersistentDataContainer container = entity.getPersistentDataContainer();
String data = container.get(savedDisguisesKey, PersistentDataType.STRING);
if (data == null) {
return getSavedDisguises(entity.getUniqueId(), remove);
return getSavedDisguises(entity.getUniqueId());
}
try {
if (remove) {
container.remove(savedDisguisesKey);
}
Disguise[] disguises = loadDisguises(data);
if (disguises == null) {
@ -970,11 +968,9 @@ public class DisguiseUtilities {
return disguises;
} catch (Throwable e) {
if (!remove) {
container.remove(savedDisguisesKey);
}
container.remove(savedDisguisesKey);
getLogger().severe("Malformed disguise for " + entity.getUniqueId() + "(" + data + ")");
getLogger().severe("Malformed disguise for " + entity.getUniqueId() + "(" + data + "). Data has been removed.");
e.printStackTrace();
}
@ -982,7 +978,7 @@ public class DisguiseUtilities {
}
@Deprecated
public static Disguise[] getSavedDisguises(UUID entityUUID, boolean remove) {
public static Disguise[] getSavedDisguises(UUID entityUUID) {
if (!isSavedDisguise(entityUUID) || !LibsPremium.isPremium()) {
return new Disguise[0];
}
@ -1011,10 +1007,6 @@ public class DisguiseUtilities {
cached = reader.lines().collect(Collectors.joining("\n"));
}
if (remove) {
removeSavedDisguise(entityUUID);
}
Disguise[] disguises;
if (cached.isEmpty()) {

View File

@ -87,7 +87,7 @@ public class DisguiseListener implements Listener {
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity);
if (disguises.length == 0) {
continue;
@ -322,13 +322,7 @@ public class DisguiseListener implements Listener {
}
for (Entity entity : event.getChunk().getEntities()) {
Disguise[] disguises = DisguiseAPI.getDisguises(entity);
if (disguises.length == 0) {
continue;
}
DisguiseUtilities.saveDisguises(entity, disguises);
DisguiseUtilities.saveDisguises(entity);
}
}
@ -347,11 +341,10 @@ public class DisguiseListener implements Listener {
Disguise[] disguises = DisguiseAPI.getDisguises(entity);
if (disguises.length == 0) {
continue;
if (disguises.length > 0) {
disguisesSaved++;
}
disguisesSaved++;
DisguiseUtilities.saveDisguises(entity, disguises);
}
@ -367,7 +360,7 @@ public class DisguiseListener implements Listener {
}
for (Entity entity : event.getChunk().getEntities()) {
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity);
if (disguises.length == 0) {
continue;
@ -389,7 +382,7 @@ public class DisguiseListener implements Listener {
}
for (Entity entity : event.getWorld().getEntities()) {
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity);
if (disguises.length == 0) {
continue;
@ -433,7 +426,7 @@ public class DisguiseListener implements Listener {
}
if (DisguiseConfig.isSavePlayerDisguises()) {
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(p, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(p);
if (disguises.length > 0) {
DisguiseUtilities.resetPluginTimer();
@ -596,17 +589,7 @@ public class DisguiseListener implements Listener {
}
}*/
if (!DisguiseConfig.isSavePlayerDisguises()) {
return;
}
Disguise[] disguises = DisguiseAPI.getDisguises(player);
if (disguises.length == 0) {
return;
}
DisguiseUtilities.saveDisguises(player, disguises);
DisguiseUtilities.saveDisguises(player);
}
@EventHandler

View File

@ -18,7 +18,7 @@ public class DisguiseListener1_18 implements Listener {
}
for (Entity entity : event.getEntities()) {
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity);
if (disguises.length == 0) {
continue;
@ -40,13 +40,7 @@ public class DisguiseListener1_18 implements Listener {
}
for (Entity entity : event.getEntities()) {
Disguise[] disguises = DisguiseAPI.getDisguises(entity);
if (disguises.length == 0) {
continue;
}
DisguiseUtilities.saveDisguises(entity, disguises);
DisguiseUtilities.saveDisguises(entity);
}
}
}

View File

@ -33,7 +33,7 @@ public class PaperDisguiseListener implements Listener {
Entity entity = event.getEntity();
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity, true);
Disguise[] disguises = DisguiseUtilities.getSavedDisguises(entity);
if (disguises.length == 0) {
return;
@ -55,13 +55,7 @@ public class PaperDisguiseListener implements Listener {
Entity entity = event.getEntity();
Disguise[] disguises = DisguiseAPI.getDisguises(entity);
if (disguises.length == 0) {
return;
}
DisguiseUtilities.saveDisguises(entity, disguises);
DisguiseUtilities.saveDisguises(entity);
}
private boolean isWeirdBuild() {