Skip to content

Commit

Permalink
Fixed respawning issues. I HATE RESPAWN!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Oct 21, 2024
1 parent d4dd30a commit c426954
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 41 deletions.
19 changes: 18 additions & 1 deletion src/main/java/com/floralquafloral/MarioQuaMario.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,13 +61,29 @@ 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();

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;
// });
}
}
51 changes: 47 additions & 4 deletions src/main/java/com/floralquafloral/mariodata/MarioDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,31 +20,67 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MarioDataManager {
private static final Map<PlayerEntity, MarioData> SERVER_PLAYERS_DATA = new HashMap<>();
private static final Map<PlayerEntity, MarioData> 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<PlayerEntity, MarioData> entry : SERVER_PLAYERS_DATA.entrySet()) {
PlayerEntity removeMe = null;
Set<Map.Entry<PlayerEntity, MarioData>> entrySet = SERVER_PLAYERS_DATA.entrySet();
for(Map.Entry<PlayerEntity, MarioData> 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<PlayerEntity, MarioData> 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) -> {
//
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
50 changes: 30 additions & 20 deletions src/main/java/com/floralquafloral/mariodata/MarioPlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/com/floralquafloral/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ private void preventSettingSneakPose(EntityPose pose, CallbackInfo ci) {
}
}

@Inject(method = "getPose", at = @At("TAIL"), cancellable = true)
private void preventGettingSneakPose(CallbackInfoReturnable<EntityPose> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c426954

Please sign in to comment.