Fixed Sync Issue after Respawning #850 #845 #843

This commit is contained in:
Tschipp 2025-12-25 14:19:05 +01:00
parent 4ca8a885da
commit 550940b4ab
6 changed files with 15 additions and 10 deletions

3
.gitignore vendored
View File

@ -22,3 +22,6 @@ build
eclipse eclipse
run run
runs runs
# vscode
.vscode/*

View File

@ -108,6 +108,10 @@ public class CarryOnCommon
CarryOnData carry = CarryOnDataManager.getCarryData(player); CarryOnData carry = CarryOnDataManager.getCarryData(player);
if(carry.isCarrying()) if(carry.isCarrying())
{ {
// Dumb Fix to sync Carry Data after a respawn with KeepInventory, because we can't sync in the first tick.
if(player.tickCount == 1)
CarryOnDataManager.setCarryData(player, carry);
if(carry.getActiveScript().isPresent()) if(carry.getActiveScript().isPresent())
{ {
String cmd = carry.getActiveScript().get().scriptEffects().commandLoop(); String cmd = carry.getActiveScript().get().scriptEffects().commandLoop();

View File

@ -22,6 +22,7 @@ package tschipp.carryon.common.carry;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.server.TickTask;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundEvents;
@ -288,7 +289,7 @@ public class PlacementHandler
public static void placeCarriedOnDeath(ServerPlayer oldPlayer, ServerPlayer newPlayer, boolean died) public static void placeCarriedOnDeath(ServerPlayer oldPlayer, ServerPlayer newPlayer, boolean died)
{ {
CarryOnData carry = CarryOnDataManager.getCarryData(oldPlayer); CarryOnData carry = CarryOnDataManager.getCarryData(oldPlayer);
if (((ServerLevel) oldPlayer.level()).getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY) || !died) { if (oldPlayer.level().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY) || !died) {
if (!carry.isCarrying(CarryType.PLAYER)) { if (!carry.isCarrying(CarryType.PLAYER)) {
CarryOnDataManager.setCarryData(newPlayer, carry); CarryOnDataManager.setCarryData(newPlayer, carry);
newPlayer.getInventory().setSelectedSlot(oldPlayer.getInventory().getSelectedSlot()); newPlayer.getInventory().setSelectedSlot(oldPlayer.getInventory().getSelectedSlot());

View File

@ -43,7 +43,7 @@ public class CarryOnFabricMod implements ModInitializer {
// the isAlive check avoids us syncing attachment data about dead players. Which causes a disconnect // the isAlive check avoids us syncing attachment data about dead players. Which causes a disconnect
// player.tickCount > 0 avoids us syncing attachment data about players the instant they spawn. // player.tickCount > 0 avoids us syncing attachment data about players the instant they spawn.
// Which also causes a disconnect as the player entity may not be synced yet. // Which also causes a disconnect as the player entity may not be synced yet.
return p.connection != null && player.isAlive() && player.tickCount > 0; return p.connection != null && player.isAlive() && !p.isRemoved() && player.tickCount > 0;
}) })

View File

@ -22,6 +22,7 @@ package tschipp.carryon.events;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.CommonLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.CommonLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
@ -133,13 +134,9 @@ public class CommonEvents {
CarryOnCommon.onCarryTick(player); CarryOnCommon.onCarryTick(player);
}); });
ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource)->{ ServerPlayerEvents.COPY_FROM.register(((oldPlayer, newPlayer, alive) -> {
if (!(entity instanceof ServerPlayer)) return; PlacementHandler.placeCarriedOnDeath(oldPlayer, newPlayer, !alive);
var player = (ServerPlayer) entity; }));
var carry = CarryOnDataManager.getCarryData(player);
if (carry.isCarrying(CarryType.PLAYER) || !player.level().getGameRules().getBoolean(GameRules.RULE_KEEPINVENTORY)) PlacementHandler.placeCarried(player);
});
PlayerBlockBreakEvents.BEFORE.register(((world, player, pos, state, blockEntity) -> { PlayerBlockBreakEvents.BEFORE.register(((world, player, pos, state, blockEntity) -> {
if(!CarryOnCommon.onTryBreakBlock(player)) if(!CarryOnCommon.onTryBreakBlock(player))

View File

@ -24,7 +24,7 @@ public class CarryOnDataSyncHandler implements AttachmentSyncHandler<CarryOnData
// the isAlive check avoids us syncing attachment data about dead players. Which causes a disconnect // the isAlive check avoids us syncing attachment data about dead players. Which causes a disconnect
// player.tickCount <= 0 avoids us syncing attachment data about players the instant they spawn. // player.tickCount <= 0 avoids us syncing attachment data about players the instant they spawn.
// Which also causes a disconnect as the player entity may not be synced yet. // Which also causes a disconnect as the player entity may not be synced yet.
if (to.connection == null || !player.isAlive() || player.tickCount <= 0) if (to.connection == null || !player.isAlive() || player.tickCount <= 0 || player.isRemoved())
return false; return false;
return AttachmentSyncHandler.super.sendToPlayer(holder, to); return AttachmentSyncHandler.super.sendToPlayer(holder, to);
} }