mirror of
https://github.com/WiIIiam278/HuskSync.git
synced 2025-12-27 10:39:11 +00:00
Support deserializing old package stuff
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
package me.william278.husksync.bukkit.data;
|
||||||
|
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.Statistic;
|
||||||
|
import org.bukkit.World;
|
||||||
|
import org.bukkit.entity.EntityType;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds legacy data store methods for data storage
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@SuppressWarnings("DeprecatedIsStillUsed")
|
||||||
|
public class DataSerializer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A record used to store data for advancement synchronisation
|
||||||
|
*
|
||||||
|
* @deprecated Old format - Use {@link AdvancementRecordDate} instead
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@SuppressWarnings("DeprecatedIsStillUsed")
|
||||||
|
// Suppress deprecation warnings here (still used for backwards compatibility)
|
||||||
|
public record AdvancementRecord(String advancementKey,
|
||||||
|
ArrayList<String> awardedAdvancementCriteria) implements Serializable {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A record used to store data for a player's statistics
|
||||||
|
*/
|
||||||
|
public record StatisticData(HashMap<Statistic, Integer> untypedStatisticValues,
|
||||||
|
HashMap<Statistic, HashMap<Material, Integer>> blockStatisticValues,
|
||||||
|
HashMap<Statistic, HashMap<Material, Integer>> itemStatisticValues,
|
||||||
|
HashMap<Statistic, HashMap<EntityType, Integer>> entityStatisticValues) implements Serializable {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A record used to store data for native advancement synchronisation, tracking advancement date progress
|
||||||
|
*/
|
||||||
|
public record AdvancementRecordDate(String key, Map<String, Date> criteriaMap) implements Serializable {
|
||||||
|
public AdvancementRecordDate(String key, List<String> criteriaList) {
|
||||||
|
this(key, new HashMap<>() {{
|
||||||
|
criteriaList.forEach(s -> put(s, Date.from(Instant.EPOCH)));
|
||||||
|
}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A record used to store data for a player's location
|
||||||
|
*/
|
||||||
|
public record PlayerLocation(double x, double y, double z, float yaw, float pitch,
|
||||||
|
String worldName, World.Environment environment) implements Serializable {
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,8 +15,6 @@ import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
|
|||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.time.Instant;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -194,12 +192,12 @@ public class DataSerializer {
|
|||||||
return serializedPotionEffect != null ? new PotionEffect((Map<String, Object>) serializedPotionEffect) : null;
|
return serializedPotionEffect != null ? new PotionEffect((Map<String, Object>) serializedPotionEffect) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static DataSerializer.PlayerLocation deserializePlayerLocationData(String serializedLocationData) throws IOException {
|
public static me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation deserializePlayerLocationData(String serializedLocationData) throws IOException {
|
||||||
if (serializedLocationData.isEmpty()) {
|
if (serializedLocationData.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return (DataSerializer.PlayerLocation) RedisMessage.deserialize(serializedLocationData);
|
return (me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation) RedisMessage.deserialize(serializedLocationData);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new IOException("Unable to decode class type.", e);
|
throw new IOException("Unable to decode class type.", e);
|
||||||
}
|
}
|
||||||
@@ -207,19 +205,19 @@ public class DataSerializer {
|
|||||||
|
|
||||||
public static String getSerializedLocation(Player player) throws IOException {
|
public static String getSerializedLocation(Player player) throws IOException {
|
||||||
final Location playerLocation = player.getLocation();
|
final Location playerLocation = player.getLocation();
|
||||||
return RedisMessage.serialize(new DataSerializer.PlayerLocation(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ(),
|
return RedisMessage.serialize(new me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ(),
|
||||||
playerLocation.getYaw(), playerLocation.getPitch(), player.getWorld().getName(), player.getWorld().getEnvironment()));
|
playerLocation.getYaw(), playerLocation.getPitch(), player.getWorld().getName(), player.getWorld().getEnvironment()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserializes a player's advancement data as serialized with {@link #getSerializedAdvancements(Player)} into {@link AdvancementRecordDate} data.
|
* Deserializes a player's advancement data as serialized with {@link #getSerializedAdvancements(Player)} into {@link me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate} data.
|
||||||
*
|
*
|
||||||
* @param serializedAdvancementData The serialized advancement data {@link String}
|
* @param serializedAdvancementData The serialized advancement data {@link String}
|
||||||
* @return The deserialized {@link AdvancementRecordDate} for the player
|
* @return The deserialized {@link me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate} for the player
|
||||||
* @throws IOException If the deserialization fails
|
* @throws IOException If the deserialization fails
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked") // Ignore the unchecked cast here
|
@SuppressWarnings("unchecked") // Ignore the unchecked cast here
|
||||||
public static List<DataSerializer.AdvancementRecordDate> deserializeAdvancementData(String serializedAdvancementData) throws IOException {
|
public static List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate> deserializeAdvancementData(String serializedAdvancementData) throws IOException {
|
||||||
if (serializedAdvancementData.isEmpty()) {
|
if (serializedAdvancementData.isEmpty()) {
|
||||||
return new ArrayList<>();
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
@@ -227,15 +225,15 @@ public class DataSerializer {
|
|||||||
List<?> deserialize = (List<?>) RedisMessage.deserialize(serializedAdvancementData);
|
List<?> deserialize = (List<?>) RedisMessage.deserialize(serializedAdvancementData);
|
||||||
|
|
||||||
// Migrate old AdvancementRecord into date format
|
// Migrate old AdvancementRecord into date format
|
||||||
if (!deserialize.isEmpty() && deserialize.get(0) instanceof AdvancementRecord) {
|
if (!deserialize.isEmpty() && deserialize.get(0) instanceof me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecord) {
|
||||||
deserialize = ((List<AdvancementRecord>) deserialize).stream()
|
deserialize = ((List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecord>) deserialize).stream()
|
||||||
.map(o -> new AdvancementRecordDate(
|
.map(o -> new me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate(
|
||||||
o.advancementKey,
|
o.advancementKey(),
|
||||||
o.awardedAdvancementCriteria
|
o.awardedAdvancementCriteria()
|
||||||
)).toList();
|
)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
return (List<AdvancementRecordDate>) deserialize;
|
return (List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate>) deserialize;
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new IOException("Unable to decode class type.", e);
|
throw new IOException("Unable to decode class type.", e);
|
||||||
}
|
}
|
||||||
@@ -250,7 +248,7 @@ public class DataSerializer {
|
|||||||
*/
|
*/
|
||||||
public static String getSerializedAdvancements(Player player) throws IOException {
|
public static String getSerializedAdvancements(Player player) throws IOException {
|
||||||
Iterator<Advancement> serverAdvancements = Bukkit.getServer().advancementIterator();
|
Iterator<Advancement> serverAdvancements = Bukkit.getServer().advancementIterator();
|
||||||
ArrayList<DataSerializer.AdvancementRecordDate> advancementData = new ArrayList<>();
|
ArrayList<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate> advancementData = new ArrayList<>();
|
||||||
|
|
||||||
while (serverAdvancements.hasNext()) {
|
while (serverAdvancements.hasNext()) {
|
||||||
final AdvancementProgress progress = player.getAdvancementProgress(serverAdvancements.next());
|
final AdvancementProgress progress = player.getAdvancementProgress(serverAdvancements.next());
|
||||||
@@ -259,25 +257,25 @@ public class DataSerializer {
|
|||||||
final Map<String, Date> awardedCriteria = new HashMap<>();
|
final Map<String, Date> awardedCriteria = new HashMap<>();
|
||||||
progress.getAwardedCriteria().forEach(s -> awardedCriteria.put(s, progress.getDateAwarded(s)));
|
progress.getAwardedCriteria().forEach(s -> awardedCriteria.put(s, progress.getDateAwarded(s)));
|
||||||
|
|
||||||
advancementData.add(new DataSerializer.AdvancementRecordDate(advancementKey.getNamespace() + ":" + advancementKey.getKey(), awardedCriteria));
|
advancementData.add(new me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate(advancementKey.getNamespace() + ":" + advancementKey.getKey(), awardedCriteria));
|
||||||
}
|
}
|
||||||
|
|
||||||
return RedisMessage.serialize(advancementData);
|
return RedisMessage.serialize(advancementData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deserializes a player's statistic data as serialized with {@link #getSerializedStatisticData(Player)} into {@link StatisticData}.
|
* Deserializes a player's statistic data as serialized with {@link #getSerializedStatisticData(Player)} into {@link me.william278.husksync.bukkit.data.DataSerializer.StatisticData}.
|
||||||
*
|
*
|
||||||
* @param serializedStatisticData The serialized statistic data {@link String}
|
* @param serializedStatisticData The serialized statistic data {@link String}
|
||||||
* @return The deserialized {@link StatisticData} for the player
|
* @return The deserialized {@link me.william278.husksync.bukkit.data.DataSerializer.StatisticData} for the player
|
||||||
* @throws IOException If the deserialization fails
|
* @throws IOException If the deserialization fails
|
||||||
*/
|
*/
|
||||||
public static DataSerializer.StatisticData deserializeStatisticData(String serializedStatisticData) throws IOException {
|
public static me.william278.husksync.bukkit.data.DataSerializer.StatisticData deserializeStatisticData(String serializedStatisticData) throws IOException {
|
||||||
if (serializedStatisticData.isEmpty()) {
|
if (serializedStatisticData.isEmpty()) {
|
||||||
return new DataSerializer.StatisticData(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
|
return new me.william278.husksync.bukkit.data.DataSerializer.StatisticData(new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
return (DataSerializer.StatisticData) RedisMessage.deserialize(serializedStatisticData);
|
return (me.william278.husksync.bukkit.data.DataSerializer.StatisticData) RedisMessage.deserialize(serializedStatisticData);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (ClassNotFoundException e) {
|
||||||
throw new IOException("Unable to decode class type.", e);
|
throw new IOException("Unable to decode class type.", e);
|
||||||
}
|
}
|
||||||
@@ -322,46 +320,8 @@ public class DataSerializer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DataSerializer.StatisticData statisticData = new DataSerializer.StatisticData(untypedStatisticValues, blockStatisticValues, itemStatisticValues, entityStatisticValues);
|
me.william278.husksync.bukkit.data.DataSerializer.StatisticData statisticData = new me.william278.husksync.bukkit.data.DataSerializer.StatisticData(untypedStatisticValues, blockStatisticValues, itemStatisticValues, entityStatisticValues);
|
||||||
return RedisMessage.serialize(statisticData);
|
return RedisMessage.serialize(statisticData);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* A record used to store data for a player's location
|
|
||||||
*/
|
|
||||||
public record PlayerLocation(double x, double y, double z, float yaw, float pitch,
|
|
||||||
String worldName, World.Environment environment) implements Serializable {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A record used to store data for advancement synchronisation
|
|
||||||
*
|
|
||||||
* @deprecated Old format - Use {@link AdvancementRecordDate} instead
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
@SuppressWarnings("DeprecatedIsStillUsed") // Suppress deprecation warnings here (still used for backwards compatibility)
|
|
||||||
public record AdvancementRecord(String advancementKey,
|
|
||||||
ArrayList<String> awardedAdvancementCriteria) implements Serializable {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A record used to store data for native advancement synchronisation, tracking advancement date progress
|
|
||||||
*/
|
|
||||||
public record AdvancementRecordDate(String key, Map<String, Date> criteriaMap) implements Serializable {
|
|
||||||
AdvancementRecordDate(String key, List<String> criteriaList) {
|
|
||||||
this(key, new HashMap<>() {{
|
|
||||||
criteriaList.forEach(s -> put(s, Date.from(Instant.EPOCH)));
|
|
||||||
}});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A record used to store data for a player's statistics
|
|
||||||
*/
|
|
||||||
public record StatisticData(HashMap<Statistic, Integer> untypedStatisticValues,
|
|
||||||
HashMap<Statistic, HashMap<Material, Integer>> blockStatisticValues,
|
|
||||||
HashMap<Statistic, HashMap<Material, Integer>> itemStatisticValues,
|
|
||||||
HashMap<Statistic, HashMap<EntityType, Integer>> entityStatisticValues) implements Serializable {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,7 +159,7 @@ public class PlayerSetter {
|
|||||||
// Set the player's data from the PlayerData
|
// Set the player's data from the PlayerData
|
||||||
try {
|
try {
|
||||||
if (Settings.syncAdvancements) {
|
if (Settings.syncAdvancements) {
|
||||||
List<DataSerializer.AdvancementRecordDate> advancementRecords
|
List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate> advancementRecords
|
||||||
= DataSerializer.deserializeAdvancementData(data.getSerializedAdvancements());
|
= DataSerializer.deserializeAdvancementData(data.getSerializedAdvancements());
|
||||||
|
|
||||||
if (Settings.useNativeImplementation) {
|
if (Settings.useNativeImplementation) {
|
||||||
@@ -277,7 +277,7 @@ public class PlayerSetter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void nativeSyncPlayerAdvancements(final Player player, final List<DataSerializer.AdvancementRecordDate> advancementRecords) {
|
private static void nativeSyncPlayerAdvancements(final Player player, final List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate> advancementRecords) {
|
||||||
final Object playerAdvancements = AdvancementUtils.getPlayerAdvancements(player);
|
final Object playerAdvancements = AdvancementUtils.getPlayerAdvancements(player);
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
@@ -316,9 +316,9 @@ public class PlayerSetter {
|
|||||||
* Update a player's advancements and progress to match the advancementData
|
* Update a player's advancements and progress to match the advancementData
|
||||||
*
|
*
|
||||||
* @param player The player to set the advancements of
|
* @param player The player to set the advancements of
|
||||||
* @param advancementData The ArrayList of {@link DataSerializer.AdvancementRecordDate}s to set
|
* @param advancementData The ArrayList of {@link me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate}s to set
|
||||||
*/
|
*/
|
||||||
private static void setPlayerAdvancements(Player player, List<DataSerializer.AdvancementRecordDate> advancementData, PlayerData data) {
|
private static void setPlayerAdvancements(Player player, List<me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate> advancementData, PlayerData data) {
|
||||||
// Temporarily disable advancement announcing if needed
|
// Temporarily disable advancement announcing if needed
|
||||||
boolean announceAdvancementUpdate = false;
|
boolean announceAdvancementUpdate = false;
|
||||||
if (Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) {
|
if (Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) {
|
||||||
@@ -336,7 +336,7 @@ public class PlayerSetter {
|
|||||||
boolean correctExperienceCheck = false; // Determines whether the experience might have changed warranting an update
|
boolean correctExperienceCheck = false; // Determines whether the experience might have changed warranting an update
|
||||||
Advancement advancement = serverAdvancements.next();
|
Advancement advancement = serverAdvancements.next();
|
||||||
AdvancementProgress playerProgress = player.getAdvancementProgress(advancement);
|
AdvancementProgress playerProgress = player.getAdvancementProgress(advancement);
|
||||||
for (DataSerializer.AdvancementRecordDate record : advancementData) {
|
for (me.william278.husksync.bukkit.data.DataSerializer.AdvancementRecordDate record : advancementData) {
|
||||||
// If the advancement is one on the data
|
// If the advancement is one on the data
|
||||||
if (record.key().equals(advancement.getKey().getNamespace() + ":" + advancement.getKey().getKey())) {
|
if (record.key().equals(advancement.getKey().getNamespace() + ":" + advancement.getKey().getKey())) {
|
||||||
|
|
||||||
@@ -379,9 +379,9 @@ public class PlayerSetter {
|
|||||||
* Set a player's statistics (in the Statistic menu)
|
* Set a player's statistics (in the Statistic menu)
|
||||||
*
|
*
|
||||||
* @param player The player to set the statistics of
|
* @param player The player to set the statistics of
|
||||||
* @param statisticData The {@link DataSerializer.StatisticData} to set
|
* @param statisticData The {@link me.william278.husksync.bukkit.data.DataSerializer.StatisticData} to set
|
||||||
*/
|
*/
|
||||||
private static void setPlayerStatistics(Player player, DataSerializer.StatisticData statisticData) {
|
private static void setPlayerStatistics(Player player, me.william278.husksync.bukkit.data.DataSerializer.StatisticData statisticData) {
|
||||||
// Set untyped statistics
|
// Set untyped statistics
|
||||||
for (Statistic statistic : statisticData.untypedStatisticValues().keySet()) {
|
for (Statistic statistic : statisticData.untypedStatisticValues().keySet()) {
|
||||||
player.setStatistic(statistic, statisticData.untypedStatisticValues().get(statistic));
|
player.setStatistic(statistic, statisticData.untypedStatisticValues().get(statistic));
|
||||||
@@ -422,12 +422,12 @@ public class PlayerSetter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set a player's location from {@link DataSerializer.PlayerLocation} data
|
* Set a player's location from {@link me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation} data
|
||||||
*
|
*
|
||||||
* @param player The {@link Player} to teleport
|
* @param player The {@link Player} to teleport
|
||||||
* @param location The {@link DataSerializer.PlayerLocation}
|
* @param location The {@link me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation}
|
||||||
*/
|
*/
|
||||||
private static void setPlayerLocation(Player player, DataSerializer.PlayerLocation location) {
|
private static void setPlayerLocation(Player player, me.william278.husksync.bukkit.data.DataSerializer.PlayerLocation location) {
|
||||||
// Don't teleport if the location is invalid
|
// Don't teleport if the location is invalid
|
||||||
if (location == null) {
|
if (location == null) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user