|
|
|
@ -5,7 +5,7 @@ import me.william278.husksync.PlayerData;
|
|
|
|
|
import me.william278.husksync.Settings;
|
|
|
|
|
import me.william278.husksync.api.events.SyncCompleteEvent;
|
|
|
|
|
import me.william278.husksync.api.events.SyncEvent;
|
|
|
|
|
import me.william278.husksync.bukkit.data.PlayerSerializer;
|
|
|
|
|
import me.william278.husksync.bukkit.data.DataSerializer;
|
|
|
|
|
import me.william278.husksync.redis.RedisMessage;
|
|
|
|
|
import org.bukkit.*;
|
|
|
|
|
import org.bukkit.advancement.Advancement;
|
|
|
|
@ -37,8 +37,8 @@ public class PlayerSetter {
|
|
|
|
|
*/
|
|
|
|
|
private static String getNewSerializedPlayerData(Player player) throws IOException {
|
|
|
|
|
return RedisMessage.serialize(new PlayerData(player.getUniqueId(),
|
|
|
|
|
PlayerSerializer.serializeInventory(player.getInventory().getContents()),
|
|
|
|
|
PlayerSerializer.serializeInventory(player.getEnderChest().getContents()),
|
|
|
|
|
DataSerializer.serializeInventory(player.getInventory().getContents()),
|
|
|
|
|
DataSerializer.serializeInventory(player.getEnderChest().getContents()),
|
|
|
|
|
player.getHealth(),
|
|
|
|
|
Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_MAX_HEALTH)).getBaseValue(),
|
|
|
|
|
player.getHealthScale(),
|
|
|
|
@ -46,15 +46,15 @@ public class PlayerSetter {
|
|
|
|
|
player.getSaturation(),
|
|
|
|
|
player.getExhaustion(),
|
|
|
|
|
player.getInventory().getHeldItemSlot(),
|
|
|
|
|
PlayerSerializer.serializePotionEffects(getPlayerPotionEffects(player)),
|
|
|
|
|
DataSerializer.serializePotionEffects(getPlayerPotionEffects(player)),
|
|
|
|
|
player.getTotalExperience(),
|
|
|
|
|
player.getLevel(),
|
|
|
|
|
player.getExp(),
|
|
|
|
|
player.getGameMode().toString(),
|
|
|
|
|
PlayerSerializer.getSerializedStatisticData(player),
|
|
|
|
|
DataSerializer.getSerializedStatisticData(player),
|
|
|
|
|
player.isFlying(),
|
|
|
|
|
PlayerSerializer.getSerializedAdvancements(player),
|
|
|
|
|
PlayerSerializer.getSerializedLocation(player)));
|
|
|
|
|
DataSerializer.getSerializedAdvancements(player),
|
|
|
|
|
DataSerializer.getSerializedLocation(player)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -139,14 +139,14 @@ public class PlayerSetter {
|
|
|
|
|
// Set the player's data from the PlayerData
|
|
|
|
|
try {
|
|
|
|
|
if (Settings.syncAdvancements) {
|
|
|
|
|
setPlayerAdvancements(player, PlayerSerializer.deserializeAdvancementData(data.getSerializedAdvancements()), data);
|
|
|
|
|
setPlayerAdvancements(player, DataSerializer.deserializeAdvancementData(data.getSerializedAdvancements()), data);
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncInventories) {
|
|
|
|
|
setPlayerInventory(player, PlayerSerializer.deserializeInventory(data.getSerializedInventory()));
|
|
|
|
|
setPlayerInventory(player, DataSerializer.deserializeInventory(data.getSerializedInventory()));
|
|
|
|
|
player.getInventory().setHeldItemSlot(data.getSelectedSlot());
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncEnderChests) {
|
|
|
|
|
setPlayerEnderChest(player, PlayerSerializer.deserializeInventory(data.getSerializedEnderChest()));
|
|
|
|
|
setPlayerEnderChest(player, DataSerializer.deserializeInventory(data.getSerializedEnderChest()));
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncHealth) {
|
|
|
|
|
player.setHealthScale(data.getHealthScale() <= 0 ? data.getHealthScale() : 20D);
|
|
|
|
@ -163,17 +163,17 @@ public class PlayerSetter {
|
|
|
|
|
setPlayerExperience(player, data);
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncPotionEffects) {
|
|
|
|
|
setPlayerPotionEffects(player, PlayerSerializer.deserializePotionEffects(data.getSerializedEffectData()));
|
|
|
|
|
setPlayerPotionEffects(player, DataSerializer.deserializePotionEffects(data.getSerializedEffectData()));
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncStatistics) {
|
|
|
|
|
setPlayerStatistics(player, PlayerSerializer.deserializeStatisticData(data.getSerializedStatistics()));
|
|
|
|
|
setPlayerStatistics(player, DataSerializer.deserializeStatisticData(data.getSerializedStatistics()));
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncGameMode) {
|
|
|
|
|
player.setGameMode(GameMode.valueOf(data.getGameMode()));
|
|
|
|
|
}
|
|
|
|
|
if (Settings.syncLocation) {
|
|
|
|
|
player.setFlying(player.getAllowFlight() && data.isFlying());
|
|
|
|
|
setPlayerLocation(player, PlayerSerializer.deserializePlayerLocationData(data.getSerializedLocation()));
|
|
|
|
|
setPlayerLocation(player, DataSerializer.deserializePlayerLocationData(data.getSerializedLocation()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle the SyncCompleteEvent
|
|
|
|
@ -240,9 +240,9 @@ public class PlayerSetter {
|
|
|
|
|
* Update a player's advancements and progress to match the advancementData
|
|
|
|
|
*
|
|
|
|
|
* @param player The player to set the advancements of
|
|
|
|
|
* @param advancementData The ArrayList of {@link PlayerSerializer.AdvancementRecord}s to set
|
|
|
|
|
* @param advancementData The ArrayList of {@link DataSerializer.AdvancementRecord}s to set
|
|
|
|
|
*/
|
|
|
|
|
private static void setPlayerAdvancements(Player player, ArrayList<PlayerSerializer.AdvancementRecord> advancementData, PlayerData data) {
|
|
|
|
|
private static void setPlayerAdvancements(Player player, ArrayList<DataSerializer.AdvancementRecord> advancementData, PlayerData data) {
|
|
|
|
|
// Temporarily disable advancement announcing if needed
|
|
|
|
|
boolean announceAdvancementUpdate = false;
|
|
|
|
|
if (Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) {
|
|
|
|
@ -260,7 +260,7 @@ public class PlayerSetter {
|
|
|
|
|
boolean correctExperienceCheck = false; // Determines whether the experience might have changed warranting an update
|
|
|
|
|
Advancement advancement = serverAdvancements.next();
|
|
|
|
|
AdvancementProgress playerProgress = player.getAdvancementProgress(advancement);
|
|
|
|
|
for (PlayerSerializer.AdvancementRecord record : advancementData) {
|
|
|
|
|
for (DataSerializer.AdvancementRecord record : advancementData) {
|
|
|
|
|
// If the advancement is one on the data
|
|
|
|
|
if (record.advancementKey().equals(advancement.getKey().getNamespace() + ":" + advancement.getKey().getKey())) {
|
|
|
|
|
|
|
|
|
@ -303,9 +303,9 @@ public class PlayerSetter {
|
|
|
|
|
* Set a player's statistics (in the Statistic menu)
|
|
|
|
|
*
|
|
|
|
|
* @param player The player to set the statistics of
|
|
|
|
|
* @param statisticData The {@link PlayerSerializer.StatisticData} to set
|
|
|
|
|
* @param statisticData The {@link DataSerializer.StatisticData} to set
|
|
|
|
|
*/
|
|
|
|
|
private static void setPlayerStatistics(Player player, PlayerSerializer.StatisticData statisticData) {
|
|
|
|
|
private static void setPlayerStatistics(Player player, DataSerializer.StatisticData statisticData) {
|
|
|
|
|
// Set untyped statistics
|
|
|
|
|
for (Statistic statistic : statisticData.untypedStatisticValues().keySet()) {
|
|
|
|
|
player.setStatistic(statistic, statisticData.untypedStatisticValues().get(statistic));
|
|
|
|
@ -346,12 +346,12 @@ public class PlayerSetter {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a player's location from {@link PlayerSerializer.PlayerLocation} data
|
|
|
|
|
* Set a player's location from {@link DataSerializer.PlayerLocation} data
|
|
|
|
|
*
|
|
|
|
|
* @param player The {@link Player} to teleport
|
|
|
|
|
* @param location The {@link PlayerSerializer.PlayerLocation}
|
|
|
|
|
* @param location The {@link DataSerializer.PlayerLocation}
|
|
|
|
|
*/
|
|
|
|
|
private static void setPlayerLocation(Player player, PlayerSerializer.PlayerLocation location) {
|
|
|
|
|
private static void setPlayerLocation(Player player, DataSerializer.PlayerLocation location) {
|
|
|
|
|
// Don't teleport if the location is invalid
|
|
|
|
|
if (location == null) {
|
|
|
|
|
return;
|
|
|
|
|