From c4269543814cff0ca408c53633e3c3a8df1c9a21 Mon Sep 17 00:00:00 2001 From: Floral <49110090+floral-qua-floral@users.noreply.github.com> Date: Mon, 21 Oct 2024 00:21:43 -0400 Subject: [PATCH] Fixed respawning issues. I HATE RESPAWN!!!! --- .../com/floralquafloral/MarioQuaMario.java | 19 ++++++- .../mariodata/MarioDataManager.java | 51 +++++++++++++++++-- .../mariodata/MarioDataPackets.java | 2 +- .../mariodata/MarioPlayerData.java | 50 ++++++++++-------- .../mariodata/client/MarioClientData.java | 15 ++++-- .../floralquafloral/mixin/EntityMixin.java | 10 ---- .../mixin/PlayerEntityMixin.java | 9 ++++ .../stomp/basestomptypes/JumpStomp.java | 2 + 8 files changed, 117 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/floralquafloral/MarioQuaMario.java b/src/main/java/com/floralquafloral/MarioQuaMario.java index 08c2d10..a050a67 100644 --- a/src/main/java/com/floralquafloral/MarioQuaMario.java +++ b/src/main/java/com/floralquafloral/MarioQuaMario.java @@ -8,6 +8,7 @@ import me.shedaniel.autoconfig.serializer.GsonConfigSerializer; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.gamerule.v1.GameRuleFactory; import net.fabricmc.fabric.api.gamerule.v1.GameRuleRegistry; @@ -60,7 +61,6 @@ public class MarioQuaMario implements ModInitializer { @Override public void onInitialize() { LOGGER.info("MarioQuaMario.java loaded on environment type " + FabricLoader.getInstance().getEnvironmentType()); - MarioDataManager.wipePlayerData(); MarioDataManager.registerEventListeners(); RegistryManager.register(); @@ -68,5 +68,22 @@ public void onInitialize() { MarioPackets.registerCommon(); MarioCommand.registerMarioCommand(); + +// ServerLivingEntityEvents.ALLOW_DEATH.register((livingEntity, damageSource, damageAmount) -> { +// if(livingEntity instanceof ServerPlayerEntity player) { +// MarioData data = MarioDataManager.getMarioData(player); +// if(data.isEnabled()) { +// // Revert if possible +// boolean canRevert = player.isOnGround(); +// if(canRevert) { +// player.playSound(RegistryManager.POWER_UP_SFX); +// data.setYVel(0.5); +// player.setHealth(20.0F); +// return false; +// } +// } +// } +// return true; +// }); } } \ No newline at end of file diff --git a/src/main/java/com/floralquafloral/mariodata/MarioDataManager.java b/src/main/java/com/floralquafloral/mariodata/MarioDataManager.java index 7419b49..be66371 100644 --- a/src/main/java/com/floralquafloral/mariodata/MarioDataManager.java +++ b/src/main/java/com/floralquafloral/mariodata/MarioDataManager.java @@ -4,9 +4,12 @@ import com.floralquafloral.MarioQuaMario; import com.floralquafloral.mariodata.client.MarioClientData; import com.floralquafloral.mixin.PlayerEntityMixin; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.fabricmc.fabric.api.networking.v1.EntityTrackingEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; @@ -17,31 +20,67 @@ import java.util.HashMap; import java.util.Map; +import java.util.Set; public class MarioDataManager { private static final Map SERVER_PLAYERS_DATA = new HashMap<>(); private static final Map CLIENT_PLAYERS_DATA = new HashMap<>(); public static void registerEventListeners() { + ServerLifecycleEvents.SERVER_STARTING.register((server) -> wipePlayerData()); + + ServerPlayerEvents.AFTER_RESPAWN.register((oldPlayer, newPlayer, alive) -> { + MarioQuaMario.LOGGER.info("Player respawned!" + + "\nOld: " + oldPlayer + + "\nNew: " + newPlayer + + "\nAlive: " + alive + ); + + MarioData data = getMarioData(oldPlayer); + ((MarioPlayerData) data).setMario(newPlayer); + SERVER_PLAYERS_DATA.put(newPlayer, data); + MarioDataPackets.sendAllData(newPlayer, newPlayer); + }); + + + ServerTickEvents.START_SERVER_TICK.register((server) -> { - for(Map.Entry entry : SERVER_PLAYERS_DATA.entrySet()) { + PlayerEntity removeMe = null; + Set> entrySet = SERVER_PLAYERS_DATA.entrySet(); + for(Map.Entry entry : entrySet) { ServerPlayerEntity player = (ServerPlayerEntity) entry.getKey(); if(player.isDisconnected()) { - SERVER_PLAYERS_DATA.remove(player); + MarioQuaMario.LOGGER.info("Removing player: {}", player); + removeMe = player; continue; } ((MarioPlayerData) entry.getValue()).tick(); } + + // This approach means we can only remove one player per tick. I don't care!!!!!!!!!!! Bite me! + if(removeMe != null) { + SERVER_PLAYERS_DATA.remove(removeMe); + } }); ClientTickEvents.START_CLIENT_TICK.register((client) -> { for(Map.Entry entry : CLIENT_PLAYERS_DATA.entrySet()) { - ((MarioPlayerData) entry.getValue()).tick(); + if(!entry.getKey().isRemoved()) ((MarioPlayerData) entry.getValue()).tick(); } }); + ClientEntityEvents.ENTITY_LOAD.register((entity, world) -> { + if(entity instanceof ClientPlayerEntity clientPlayer) { + MarioClientData data = (MarioClientData) getMarioData(clientPlayer); + CLIENT_PLAYERS_DATA.remove(clientPlayer); + CLIENT_PLAYERS_DATA.remove(data.getMario()); + data.setMario(clientPlayer); + CLIENT_PLAYERS_DATA.put(clientPlayer, data); + } + }); + ClientPlayConnectionEvents.DISCONNECT.register((handler, client) -> wipePlayerData()); // ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> { // @@ -61,8 +100,12 @@ public static MarioData getMarioData(PlayerEntity mario) { MarioData playerData = RELEVANT_MAP.get(mario); if(playerData == null) { - if(mario.isMainPlayer() && mario instanceof ClientPlayerEntity marioClient) + if(mario.isMainPlayer() && mario instanceof ClientPlayerEntity marioClient) { playerData = new MarioClientData(marioClient); +// playerData = MarioClientData.getInstance(); +// if(playerData == null) playerData = new MarioClientData(marioClient); +// else ((MarioClientData) playerData).setMario(mario); + } else// if(isClient) playerData = new MarioPlayerData(mario); // else diff --git a/src/main/java/com/floralquafloral/mariodata/MarioDataPackets.java b/src/main/java/com/floralquafloral/mariodata/MarioDataPackets.java index 8b8e0a5..4d016ef 100644 --- a/src/main/java/com/floralquafloral/mariodata/MarioDataPackets.java +++ b/src/main/java/com/floralquafloral/mariodata/MarioDataPackets.java @@ -55,7 +55,7 @@ public static void registerClient() { SetCharacterS2CPayload.registerReceiver(); } - private static void sendAllData(ServerPlayerEntity toWho, PlayerEntity aboutWho) { + public static void sendAllData(ServerPlayerEntity toWho, PlayerEntity aboutWho) { MarioData data = getMarioData(aboutWho); // am I supposed to send one packet with all this data or is this fine???? ServerPlayNetworking.send(toWho, new SetEnabledS2CPayload(aboutWho, data.isEnabled())); diff --git a/src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java b/src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java index 4b16b81..9b62cd1 100644 --- a/src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java +++ b/src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java @@ -16,28 +16,38 @@ public class MarioPlayerData implements MarioData { private ParsedPowerUp powerUp; private ParsedCharacter character; - private final PlayerEntity MARIO; + private PlayerEntity mario; @Override public PlayerEntity getMario() { - return this.MARIO; + return this.mario; } + public void setMario(PlayerEntity mario) { this.mario = mario; } public MarioPlayerData(PlayerEntity mario) { - this.MARIO = mario; - this.setEnabled(true); + this.mario = mario; + this.enabled = true; this.action = RegistryManager.ACTIONS.get(Identifier.of("qua_mario:basic")); this.powerUp = RegistryManager.POWER_UPS.get(Identifier.of("qua_mario:super")); this.character = RegistryManager.CHARACTERS.get(Identifier.of("qua_mario:mario")); - MarioQuaMario.LOGGER.info("Initialized a MarioData: {}", this); + MarioQuaMario.LOGGER.info("Initialized a MarioData: {}, for {}", this, mario); + } + public MarioPlayerData(PlayerEntity mario, MarioData oldData) { + this.mario = mario; + this.enabled = oldData.isEnabled(); + this.action = oldData.getAction(); + this.powerUp = oldData.getPowerUp(); + this.character = oldData.getCharacter(); + + MarioQuaMario.LOGGER.info("Initialized a MarioData from old data: {}, for {}, from {}", this, mario, oldData); } @Override public boolean useMarioPhysics() { return( isEnabled() - && !MARIO.getAbilities().flying - && !MARIO.isFallFlying() - && !MARIO.hasVehicle() - && !MARIO.isClimbing() + && !mario.getAbilities().flying + && !mario.isFallFlying() + && !mario.hasVehicle() + && !mario.isClimbing() ); } @@ -55,12 +65,12 @@ private MarioVelocities ensure() { this.isGenerated = true; // Calculate forward and sideways vector components - double yawRad = Math.toRadians(MARIO.getYaw()); + double yawRad = Math.toRadians(mario.getYaw()); this.negativeSineYaw = -Math.sin(yawRad); this.cosineYaw = Math.cos(yawRad); // Calculate current forwards and sideways velocity - Vec3d currentVel = MARIO.getVelocity(); + Vec3d currentVel = mario.getVelocity(); this.forward = currentVel.x * negativeSineYaw + currentVel.z * cosineYaw; this.strafe = currentVel.x * cosineYaw + currentVel.z * -negativeSineYaw; this.vertical = currentVel.y; @@ -87,7 +97,7 @@ private void apply() { } @Override public double getYVel() { if(this.VELOCITIES.isGenerated) return this.VELOCITIES.vertical; - else return this.MARIO.getVelocity().y; + else return this.mario.getVelocity().y; } @Override public void setForwardVel(double forward) { VELOCITIES.ensureDirty().forward = forward; @@ -98,8 +108,8 @@ private void apply() { @Override public void setYVel(double vertical) { if(this.VELOCITIES.isGenerated) this.VELOCITIES.ensureDirty().vertical = vertical; else { - Vec3d oldVel = this.MARIO.getVelocity(); - this.MARIO.setVelocity(oldVel.x, vertical, oldVel.z); + Vec3d oldVel = this.mario.getVelocity(); + this.mario.setVelocity(oldVel.x, vertical, oldVel.z); } } @Override public void applyModifiedVelocity() { @@ -142,11 +152,11 @@ public void tick() { this.setActionTransitionless(action); } @Override public void setActionTransitionless(ParsedAction action) { - if(!this.MARIO.getWorld().isClient) { + if(!this.mario.getWorld().isClient) { if(this.action.ANIMATION != null) - CPMIntegration.commonAPI.playAnimation(PlayerEntity.class, this.MARIO, this.action.ANIMATION, 0); + CPMIntegration.commonAPI.playAnimation(PlayerEntity.class, this.mario, this.action.ANIMATION, 0); if(action.ANIMATION != null) - CPMIntegration.commonAPI.playAnimation(PlayerEntity.class, this.MARIO, action.ANIMATION, 1); + CPMIntegration.commonAPI.playAnimation(PlayerEntity.class, this.mario, action.ANIMATION, 1); } this.action = action; } @@ -157,15 +167,15 @@ public void tick() { MarioQuaMario.LOGGER.info("Set Power-up to {}", powerUp.ID); this.powerUp.losePower(this); powerUp.acquirePower(this); - this.MARIO.setHealth(this.MARIO.getMaxHealth()); + this.mario.setHealth(this.mario.getMaxHealth()); this.powerUp = powerUp; - this.MARIO.calculateDimensions(); + this.mario.calculateDimensions(); } @Override public ParsedCharacter getCharacter() { return character; } @Override public void setCharacter(ParsedCharacter character) { this.character = character; - this.MARIO.calculateDimensions(); + this.mario.calculateDimensions(); } } diff --git a/src/main/java/com/floralquafloral/mariodata/client/MarioClientData.java b/src/main/java/com/floralquafloral/mariodata/client/MarioClientData.java index e0c0225..fe00e6c 100644 --- a/src/main/java/com/floralquafloral/mariodata/client/MarioClientData.java +++ b/src/main/java/com/floralquafloral/mariodata/client/MarioClientData.java @@ -7,6 +7,7 @@ import com.floralquafloral.util.CPMIntegration; import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.entity.MovementType; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import org.joml.Vector2d; @@ -17,14 +18,18 @@ public static MarioClientData getInstance() { return instance; } - private final ClientPlayerEntity MARIO_CLIENT; + private ClientPlayerEntity marioClient; @Override public ClientPlayerEntity getMario() { - return MARIO_CLIENT; + return marioClient; + } + @Override public void setMario(PlayerEntity mario) { + this.marioClient = (ClientPlayerEntity) mario; + super.setMario(mario); } public MarioClientData(ClientPlayerEntity mario) { super(mario); - this.MARIO_CLIENT = mario; + this.marioClient = mario; MarioClientData.instance = this; } @@ -54,11 +59,11 @@ public boolean travel(Vec3d movementInput) { getAction().attemptTransitions(this, TransitionPhase.POST_TICK); applyModifiedVelocity(); - MARIO_CLIENT.move(MovementType.PLAYER, MARIO_CLIENT.getVelocity()); + marioClient.move(MovementType.PLAYER, marioClient.getVelocity()); if(getAction().attemptTransitions(this, TransitionPhase.POST_MOVE)) applyModifiedVelocity(); - MARIO_CLIENT.updateLimbs(false); + marioClient.updateLimbs(false); return true; } diff --git a/src/main/java/com/floralquafloral/mixin/EntityMixin.java b/src/main/java/com/floralquafloral/mixin/EntityMixin.java index 5d8dbde..8f1e6e9 100644 --- a/src/main/java/com/floralquafloral/mixin/EntityMixin.java +++ b/src/main/java/com/floralquafloral/mixin/EntityMixin.java @@ -39,16 +39,6 @@ private void preventSettingSneakPose(EntityPose pose, CallbackInfo ci) { } } - @Inject(method = "getPose", at = @At("TAIL"), cancellable = true) - private void preventGettingSneakPose(CallbackInfoReturnable cir) { - if((Entity) (Object) this instanceof PlayerEntity player && cir.getReturnValue() == EntityPose.CROUCHING) { - if(MarioDataManager.getMarioData(player).getSneakProhibited()) { - player.setPose(EntityPose.STANDING); - cir.setReturnValue(EntityPose.STANDING); - } - } - } - @Inject(method = "playStepSounds", at = @At("HEAD"), cancellable = true) private void preventStepSounds(BlockPos pos, BlockState state, CallbackInfo ci) { if(((Entity) (Object) this) instanceof PlayerEntity player) { diff --git a/src/main/java/com/floralquafloral/mixin/PlayerEntityMixin.java b/src/main/java/com/floralquafloral/mixin/PlayerEntityMixin.java index 95afdca..c896a68 100644 --- a/src/main/java/com/floralquafloral/mixin/PlayerEntityMixin.java +++ b/src/main/java/com/floralquafloral/mixin/PlayerEntityMixin.java @@ -1,10 +1,12 @@ package com.floralquafloral.mixin; +import com.floralquafloral.MarioQuaMario; import com.floralquafloral.mariodata.MarioData; import com.floralquafloral.mariodata.client.MarioClientData; import com.floralquafloral.mariodata.MarioDataManager; import com.floralquafloral.registries.states.character.ParsedCharacter; import com.floralquafloral.registries.states.powerup.ParsedPowerUp; +import net.minecraft.entity.Entity; import net.minecraft.entity.EntityDimensions; import net.minecraft.entity.EntityPose; import net.minecraft.entity.player.PlayerEntity; @@ -21,6 +23,13 @@ public abstract class PlayerEntityMixin { @Inject(method = "travel", at = @At("HEAD"), cancellable = true) private void travelHook(Vec3d movementInput, CallbackInfo ci) { +// PlayerEntity player = (PlayerEntity) (Object) this; +// if(player.getWorld().isClient) MarioQuaMario.LOGGER.info("Travel mixin:" +// + "\nPlayer: " + player +// + "\nData: " + MarioDataManager.getMarioData(player) +// + "\nDataM: " + MarioDataManager.getMarioData(player).getMario() +// + "\nPhys: " + MarioDataManager.getMarioData(player).useMarioPhysics() +// ); if(MarioDataManager.getMarioData(this) instanceof MarioClientData marioClientData && marioClientData.useMarioPhysics() && marioClientData.travel(movementInput)) ci.cancel(); diff --git a/src/main/java/com/floralquafloral/registries/stomp/basestomptypes/JumpStomp.java b/src/main/java/com/floralquafloral/registries/stomp/basestomptypes/JumpStomp.java index 5f8526a..e66fca3 100644 --- a/src/main/java/com/floralquafloral/registries/stomp/basestomptypes/JumpStomp.java +++ b/src/main/java/com/floralquafloral/registries/stomp/basestomptypes/JumpStomp.java @@ -9,6 +9,7 @@ import net.minecraft.entity.MovementType; import net.minecraft.item.ItemStack; import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.sound.SoundEvents; import net.minecraft.util.Identifier; import net.minecraft.util.math.Vec3d; import org.jetbrains.annotations.NotNull; @@ -61,6 +62,7 @@ public void executeServer(MarioPlayerData data, Entity target, boolean harmless, @Override public void executeClient(MarioPlayerData data, boolean isSelf, Entity target, boolean harmless, long seed) { executeCommon(data, target); + target.playSound(SoundEvents.BLOCK_BEACON_POWER_SELECT, 1.0F, 1.0F); } private void executeCommon(MarioPlayerData data, Entity target) {