fix backpack didn't sync
This commit is contained in:
parent
733f37cbb3
commit
90742aafa0
|
|
@ -179,7 +179,7 @@ public class VanillaSync {
|
|||
long last_update = rs2.getLong("last_update");
|
||||
boolean enable = rs2.getBoolean("enable");
|
||||
if (enable && System.currentTimeMillis() < last_update + 300000.0) {
|
||||
event.getConnection().disconnect(Component.translatable("playersync.already_online"));
|
||||
event.getConnection().disconnect(Component.translatableWithFallback("playersync.already_online","You can't join more than one synchronization server at the same time."));
|
||||
qr2.connection().close();
|
||||
return;
|
||||
}
|
||||
|
|
@ -190,7 +190,7 @@ public class VanillaSync {
|
|||
}
|
||||
} catch (Exception e) {
|
||||
PlayerSync.LOGGER.error("SqlException detected!", e);
|
||||
event.getConnection().disconnect(Component.translatable("playersync.sqlexception"));
|
||||
event.getConnection().disconnect(Component.translatableWithFallback("playersync.sqlexception","SqlException detected!Connection lost,please contact with your admin."));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -198,7 +198,7 @@ public class VanillaSync {
|
|||
public static Set<String> deadPlayerWhileLogging = ConcurrentHashMap.newKeySet();
|
||||
public static Set<String> syncNotCompletedPlayer = ConcurrentHashMap.newKeySet();
|
||||
|
||||
public static void doPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) throws SQLException, CommandSyntaxException, IOException {
|
||||
public static void doPlayerJoin(PlayerEvent.PlayerLoggedInEvent event) {
|
||||
ServerPlayer joinedPlayer = (ServerPlayer) event.getEntity();
|
||||
String player_uuid = joinedPlayer.getUUID().toString();
|
||||
if (joinedPlayer.isDeadOrDying()) {
|
||||
|
|
@ -243,14 +243,11 @@ public class VanillaSync {
|
|||
PlayerSync.LOGGER.info("Starting synchronization for player " + player_uuid);
|
||||
|
||||
// First query: check basic player data
|
||||
syncNotCompletedPlayer.add(player_uuid);
|
||||
JDBCsetUp.QueryResult qr1 = JDBCsetUp.executeQuery("SELECT online, last_server FROM player_data WHERE uuid='" + player_uuid + "'");
|
||||
ResultSet rs1 = qr1.resultSet();
|
||||
ServerPlayer serverPlayer = (ServerPlayer) event.getEntity();
|
||||
|
||||
// Mod support
|
||||
ModsSupport modsSupport = new ModsSupport();
|
||||
modsSupport.onPlayerJoin(serverPlayer);
|
||||
|
||||
if (!rs1.next()) {
|
||||
store(event.getEntity(), true);
|
||||
JDBCsetUp.executeUpdate("UPDATE server_info SET last_update=" + System.currentTimeMillis() + " WHERE id=" + JdbcConfig.SERVER_ID.get());
|
||||
|
|
@ -261,29 +258,11 @@ public class VanillaSync {
|
|||
syncNotCompletedPlayer.remove(player_uuid);
|
||||
return;
|
||||
}
|
||||
boolean online = rs1.getBoolean("online");
|
||||
int lastServer = rs1.getInt("last_server");
|
||||
|
||||
// Second query: retrieve full player data
|
||||
JDBCsetUp.QueryResult qr2 = JDBCsetUp.executeQuery("SELECT * FROM player_data WHERE uuid='" + player_uuid + "'");
|
||||
ResultSet rs2 = qr2.resultSet();
|
||||
|
||||
// Check if player is already online on another server
|
||||
if (online && lastServer != JdbcConfig.SERVER_ID.get()) {
|
||||
JDBCsetUp.QueryResult qr3 = JDBCsetUp.executeQuery("SELECT last_update,enable FROM server_info WHERE id='" + lastServer + "'");
|
||||
ResultSet rs3 = qr3.resultSet();
|
||||
if (rs3.next()) {
|
||||
long last_update = rs3.getLong("last_update");
|
||||
boolean enable = rs3.getBoolean("enable");
|
||||
if (enable && System.currentTimeMillis() < last_update + 300000.0) {
|
||||
event.getEntity().removeTag("player_synced");
|
||||
serverPlayer.connection.disconnect(Component.translatableWithFallback("playersync.already_online", "You can't join more than one synchronization server at the same time."));
|
||||
return;
|
||||
}
|
||||
JDBCsetUp.executeUpdate("UPDATE server_info SET enable= '0' WHERE id=" + lastServer);
|
||||
}
|
||||
rs3.close();
|
||||
}
|
||||
JDBCsetUp.executeUpdate("UPDATE server_info SET last_update=" + System.currentTimeMillis() + " WHERE id=" + JdbcConfig.SERVER_ID.get());
|
||||
JDBCsetUp.executeUpdate("UPDATE player_data SET online= '1',last_server=" + JdbcConfig.SERVER_ID.get() + " WHERE uuid='" + player_uuid + "'");
|
||||
|
||||
|
|
@ -350,6 +329,10 @@ public class VanillaSync {
|
|||
rs1.close();
|
||||
qr1.close();
|
||||
|
||||
// Mod support
|
||||
ModsSupport modsSupport = new ModsSupport();
|
||||
modsSupport.onPlayerJoin(serverPlayer);
|
||||
|
||||
PlayerSync.LOGGER.info("Sync data for player {} completed.", player_uuid);
|
||||
syncNotCompletedPlayer.remove(player_uuid);
|
||||
} catch (Exception e) {
|
||||
|
|
@ -391,7 +374,7 @@ public class VanillaSync {
|
|||
String nbtString = deserializeString(serializedNbt);
|
||||
CompoundTag compoundTag = NbtUtils.snbtToStructure(nbtString);
|
||||
|
||||
if (compoundTag == null || compoundTag.isEmpty() || !compoundTag.contains("id", Tag.TAG_STRING)) {
|
||||
if (compoundTag.isEmpty() || !compoundTag.contains("id", Tag.TAG_STRING)) {
|
||||
return ItemStack.EMPTY; // Invalid or empty tag
|
||||
}
|
||||
|
||||
|
|
@ -673,7 +656,7 @@ public class VanillaSync {
|
|||
PlayerSync.LOGGER.warn("Advancements file for " + player_uuid + " does not exist (yet).");
|
||||
}
|
||||
|
||||
if (advancements != null && advancements.exists()) {
|
||||
if (advancements.exists()) {
|
||||
PlayerSync.LOGGER.debug("Storing advancements for " + player_uuid + " from " + advancements.toPath());
|
||||
advancementBytes = Files.readAllBytes(advancements.toPath());
|
||||
} else {
|
||||
|
|
@ -838,4 +821,4 @@ public class VanillaSync {
|
|||
CuriosCache.tryStoreCuriosToCache(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -31,6 +31,50 @@ public class ModsSupport {
|
|||
* The saved data is stored as a flat map with composite keys ("slotType:index").
|
||||
*/
|
||||
public void onPlayerJoin(net.minecraft.world.entity.player.Player player) throws SQLException {
|
||||
doRestoreCurios(player);
|
||||
doRestoreBackpacks(player);
|
||||
}
|
||||
|
||||
private static void doRestoreBackpacks(Player player) {
|
||||
if (ModList.get().isLoaded("sophisticatedbackpacks")) {
|
||||
// --- Begin Backpack Data Restore ---
|
||||
PlayerSync.LOGGER.info("Restoring backpack data for player " + player.getUUID());
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.util.PlayerInventoryProvider.get().runOnBackpacks(player, (ItemStack backpackItem, String handler, String identifier, int slot) -> {
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.IBackpackWrapper backpackWrapper = net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.BackpackWrapper
|
||||
.fromStack(backpackItem);
|
||||
|
||||
// Retrieve the contents UUID from the backpack's NBT using NBTHelper
|
||||
Optional<UUID> uuidOpt = backpackWrapper.getContentsUuid();
|
||||
if (uuidOpt.isPresent()) {
|
||||
UUID contentsUuid = uuidOpt.get();
|
||||
try {
|
||||
JDBCsetUp.QueryResult qrBackpack = JDBCsetUp.executeQuery("SELECT backpack_nbt FROM backpack_data WHERE uuid='" + contentsUuid + "'");
|
||||
ResultSet rsBackpack = qrBackpack.resultSet();
|
||||
if (rsBackpack.next()) {
|
||||
String serialized = rsBackpack.getString("backpack_nbt");
|
||||
String nbtString = VanillaSync.deserializeString(serialized);
|
||||
CompoundTag backpackNbt = NbtUtils.snbtToStructure(nbtString);
|
||||
// Update BackpackStorage with the retrieved NBT
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage.get().setBackpackContents(contentsUuid, backpackNbt);
|
||||
PlayerSync.LOGGER.info("Restored backpack data for UUID " + contentsUuid);
|
||||
}
|
||||
rsBackpack.close();
|
||||
qrBackpack.connection().close();
|
||||
} catch (SQLException e) {
|
||||
PlayerSync.LOGGER.error("Error restoring backpack data for UUID " + contentsUuid, e);
|
||||
} catch (CommandSyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
PlayerSync.LOGGER.warn("Backpack item in slot " + slot + " has no contentsUuid during restore");
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// --- End Backpack Data Restore ---
|
||||
}
|
||||
}
|
||||
|
||||
private void doRestoreCurios(Player player) throws SQLException {
|
||||
if (ModList.get().isLoaded("curios")) {
|
||||
// Obtain the handler from the API.
|
||||
Optional<ICuriosItemHandler> handlerOpt = CuriosApi.getCuriosInventory(player);
|
||||
|
|
@ -94,43 +138,6 @@ public class ModsSupport {
|
|||
StoreCurios(player, true);
|
||||
}
|
||||
}
|
||||
if(ModList.get().isLoaded("sophisticatedbackpacks")){
|
||||
// --- Begin Backpack Data Restore ---
|
||||
PlayerSync.LOGGER.info("Restoring backpack data for player " + player.getUUID());
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.util.PlayerInventoryProvider.get().runOnBackpacks(player, (ItemStack backpackItem, String handler, String identifier, int slot) -> {
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.IBackpackWrapper backpackWrapper = net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.BackpackWrapper
|
||||
.fromStack(backpackItem);
|
||||
|
||||
// Retrieve the contents UUID from the backpack's NBT using NBTHelper
|
||||
Optional<UUID> uuidOpt = backpackWrapper.getContentsUuid();
|
||||
if (uuidOpt.isPresent()) {
|
||||
UUID contentsUuid = uuidOpt.get();
|
||||
try {
|
||||
JDBCsetUp.QueryResult qrBackpack = JDBCsetUp.executeQuery("SELECT backpack_nbt FROM backpack_data WHERE uuid='" + contentsUuid + "'");
|
||||
ResultSet rsBackpack = qrBackpack.resultSet();
|
||||
if (rsBackpack.next()) {
|
||||
String serialized = rsBackpack.getString("backpack_nbt");
|
||||
String nbtString = VanillaSync.deserializeString(serialized);
|
||||
CompoundTag backpackNbt = NbtUtils.snbtToStructure(nbtString);
|
||||
// Update BackpackStorage with the retrieved NBT
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage.get().setBackpackContents(contentsUuid, backpackNbt);
|
||||
net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage.get().setDirty();
|
||||
PlayerSync.LOGGER.info("Restored backpack data for UUID " + contentsUuid);
|
||||
}
|
||||
rsBackpack.close();
|
||||
qrBackpack.connection().close();
|
||||
} catch (SQLException e) {
|
||||
PlayerSync.LOGGER.error("Error restoring backpack data for UUID " + contentsUuid, e);
|
||||
} catch (CommandSyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
PlayerSync.LOGGER.warn("Backpack item in slot " + slot + " has no contentsUuid during restore");
|
||||
}
|
||||
return false;
|
||||
});
|
||||
// --- End Backpack Data Restore ---
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user