Skip to content

Commit

Permalink
Working on saving MarioData to NBT
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Dec 19, 2024
1 parent a427a07 commit 9cf1571
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.fqf.mario_qua_mario.characters;

import com.fqf.mario_qua_mario.MarioQuaMarioContent;
import com.fqf.mario_qua_mario.definitions.states.CharacterDefinition;
import com.fqf.mario_qua_mario.mariodata.IMarioAuthoritativeData;
import com.fqf.mario_qua_mario.mariodata.IMarioClientData;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

import static com.fqf.mario_qua_mario.util.StatCategory.*;

public class Luigi extends Mario implements CharacterDefinition {
@Override public @NotNull Identifier getID() {
return MarioQuaMarioContent.makeID("luigi");
}

@Override public Set<StatModifier> getStatModifiers() {
return Set.of(
// Luigi walks and runs faster
new StatModifier(Set.of(FORWARD, WALKING, SPEED), 1.45),
new StatModifier(Set.of(FORWARD, RUNNING, SPEED), 1.2),
new StatModifier(Set.of(FORWARD, P_RUNNING, SPEED), 1.2),
new StatModifier(Set.of(FORWARD, RUNNING, ACCELERATION), 1.2),

// Luigi jumps higher
new StatModifier(Set.of(JUMPING_GRAVITY), 0.94),
new StatModifier(Set.of(JUMP_VELOCITY), 1.05),

// Luigi is slipperier
new StatModifier(Set.of(FRICTION), 0.4),
new StatModifier(Set.of(DRAG), 0.4)
);
}
}
3 changes: 2 additions & 1 deletion content/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
],

"mqm-characters": [
"com.fqf.mario_qua_mario.characters.Mario"
"com.fqf.mario_qua_mario.characters.Mario",
"com.fqf.mario_qua_mario.characters.Luigi"
],

"main": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

public class MarioMainClientData extends MarioMoveableData implements IMarioClientDataImpl {
private ClientPlayerEntity mario;
public MarioMainClientData(ClientPlayerEntity mario) {
public MarioMainClientData() {
super();
this.mario = mario;
}
@Override public ClientPlayerEntity getMario() {
return mario;
}
@Override public void setMario(PlayerEntity mario) {
this.mario = (ClientPlayerEntity) mario;
super.setMario(mario);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

public class MarioOtherClientData extends MarioPlayerData implements IMarioClientDataImpl {
private OtherClientPlayerEntity mario;
public MarioOtherClientData(OtherClientPlayerEntity mario) {
public MarioOtherClientData() {
super();
this.mario = mario;
}
@Override public OtherClientPlayerEntity getMario() {
return this.mario;
}
@Override public void setMario(PlayerEntity mario) {
this.mario = (OtherClientPlayerEntity) mario;
super.setMario(mario);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

@Mixin(ClientPlayerEntity.class)
public class ClientPlayerEntityMarioDataMixin implements MarioMainClientDataHolder {
@Unique private MarioMainClientData marioData;
@Unique private MarioMainClientData marioData = new MarioMainClientData();

@Inject(method = "<init>", at = @At("RETURN"))
private void constructorHook(MinecraftClient client, ClientWorld world, ClientPlayNetworkHandler networkHandler, StatHandler stats, ClientRecipeBook recipeBook, boolean lastSneaking, boolean lastSprinting, CallbackInfo ci) {
mqm$setMarioData(new MarioMainClientData((ClientPlayerEntity) (Object) this));
this.mqm$setMarioData(this.marioData);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

@Mixin(OtherClientPlayerEntity.class)
public class OtherClientPlayerEntityMarioDataMixin implements MarioOtherClientDataHolder {
@Unique private MarioOtherClientData marioData;
@Unique private MarioOtherClientData marioData = new MarioOtherClientData();

@Inject(method = "<init>", at = @At("RETURN"))
private void constructorHook(ClientWorld clientWorld, GameProfile gameProfile, CallbackInfo ci) {
this.marioData = new MarioOtherClientData((OtherClientPlayerEntity) (Object) this);
this.mqm$setMarioData(this.marioData);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_21",
"client": [
"ClientPlayerEntityMarioDataMixin",
"ExampleClientMixin",
"InGameHudMixin",
"OtherClientPlayerEntityMarioDataMixin"
],
Expand Down
7 changes: 7 additions & 0 deletions mod/src/main/java/com/fqf/mario_qua_mario/MarioQuaMario.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.fqf.mario_qua_mario;

import com.fqf.mario_qua_mario.util.MarioEventListeners;
import com.fqf.mario_qua_mario.packets.MarioPackets;
import com.fqf.mario_qua_mario.registries.RegistryManager;
import com.fqf.mario_qua_mario.util.MarioGamerules;
import net.fabricmc.api.ModInitializer;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
Expand All @@ -11,6 +13,8 @@ public class MarioQuaMario implements ModInitializer {
public static final String MOD_ID = "mario_qua_mario";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

public static final String MOD_DATA_KEY = MOD_ID + ".data";

@Override
public void onInitialize() {
LOGGER.info("Mario qua Mario initializing...");
Expand All @@ -20,6 +24,9 @@ public void onInitialize() {
MarioCommand.registerMarioCommand();

MarioPackets.register();

MarioGamerules.register();
MarioEventListeners.register();
}

public static Identifier makeID(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import org.joml.Vector2d;

public abstract class MarioMoveableData extends MarioPlayerData implements IMarioTravelData {
public MarioMoveableData() {
super();
}

private final MarioVelocities VELOCITIES = new MarioVelocities();
private class MarioVelocities {
private double forward;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@
import java.util.Objects;
import java.util.Set;

/**
* The most advanced form of MarioData that can be applied for all players.
*/
public abstract class MarioPlayerData implements IMarioData {
protected MarioPlayerData() {
this.enabled = false;
this.setEnabledInternal(true);
MarioQuaMario.LOGGER.info("Created new MarioData: {}", this);

this.enabled = true;

this.character = Objects.requireNonNull(RegistryManager.CHARACTERS.get(MarioQuaMario.makeID("mario")),
"Mario isn't registered; can't initialize player!");
Expand Down Expand Up @@ -123,7 +121,13 @@ public void refreshPowerSet() {
return this.POWERS.contains(power);
}

public abstract void setMario(PlayerEntity mario);
public void setMario(PlayerEntity mario) {
MarioQuaMario.LOGGER.info("Assigning player to MarioData: {} to {}", mario.getName().getString(), this);
this.setEnabledInternal(this.isEnabled());
this.setActionTransitionless(this.action);
this.setPowerUpTransitionless(this.powerUp);
this.setCharacter(this.character);
}

public void tick() {
long worldTime = this.getMario().getWorld().getTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@

public class MarioServerPlayerData extends MarioMoveableData implements IMarioAuthoritativeData {
private ServerPlayerEntity mario;
public MarioServerPlayerData(ServerPlayerEntity mario) {
public MarioServerPlayerData() {
super();
this.mario = mario;
}
@Override public ServerPlayerEntity getMario() {
return this.mario;
}
@Override public void setMario(PlayerEntity mario) {
this.mario = (ServerPlayerEntity) mario;
super.setMario(mario);
}

@Override public void setEnabled(boolean enable) {

Expand Down Expand Up @@ -98,10 +101,6 @@ public MarioServerPlayerData(ServerPlayerEntity mario) {
this.assignCharacter(Identifier.of(characterID));
}

@Override public void setMario(PlayerEntity mario) {
this.mario = (ServerPlayerEntity) mario;
}

@Override
public boolean isClient() {
return false;
Expand Down
15 changes: 0 additions & 15 deletions mod/src/main/java/com/fqf/mario_qua_mario/mixin/ExampleMixin.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.fqf.mario_qua_mario.mixin;

import com.fqf.mario_qua_mario.MarioQuaMario;
import com.fqf.mario_qua_mario.mariodata.MarioMoveableData;
import com.fqf.mario_qua_mario.mariodata.MarioPlayerData;
import com.fqf.mario_qua_mario.mariodata.injections.MarioDataHolder;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
Expand Down Expand Up @@ -48,4 +51,23 @@ protected void playStepSounds(BlockPos pos, BlockState state) {
default -> super.playStepSounds(pos, state);
};
}

@Override
public void writeCustomDataToNbt(NbtCompound nbt) {
super.writeCustomDataToNbt(nbt);

NbtCompound persistentData = new NbtCompound();
MarioPlayerData data = mqm$getMarioData();

persistentData.putBoolean("Enabled", data.isEnabled());
persistentData.putString("PowerUp", data.getPowerUpID().toString());
persistentData.putString("Character", data.getCharacterID().toString());

MarioQuaMario.LOGGER.info("Wrote player NBT:\nEnabled: {}\nPower-up: {}\nCharacter: {}",
persistentData.getBoolean("Enabled"),
persistentData.getString("PowerUp"),
persistentData.getString("Character"));

nbt.put(MarioQuaMario.MOD_DATA_KEY, persistentData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.fqf.mario_qua_mario.mixin;

import com.fqf.mario_qua_mario.MarioQuaMario;
import com.fqf.mario_qua_mario.mariodata.MarioPlayerData;
import com.fqf.mario_qua_mario.mariodata.MarioServerPlayerData;
import com.fqf.mario_qua_mario.mariodata.injections.MarioServerDataHolder;
import com.fqf.mario_qua_mario.registries.ParsedMarioThing;
import com.fqf.mario_qua_mario.registries.RegistryManager;
import com.fqf.mario_qua_mario.registries.actions.AbstractParsedAction;
import com.fqf.mario_qua_mario.registries.power_granting.ParsedCharacter;
import com.fqf.mario_qua_mario.registries.power_granting.ParsedPowerUp;
import com.mojang.authlib.GameProfile;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.registry.Registry;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import java.util.Objects;

@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin extends PlayerEntity implements MarioServerDataHolder {
@Shadow public ServerPlayNetworkHandler networkHandler;

public ServerPlayerEntityMixin(World world, BlockPos pos, float yaw, GameProfile gameProfile) {
super(world, pos, yaw, gameProfile);
}

@Override
public void readCustomDataFromNbt(NbtCompound nbt) {
super.readCustomDataFromNbt(nbt);

if(nbt.contains(MarioQuaMario.MOD_DATA_KEY, NbtElement.COMPOUND_TYPE)) {
NbtCompound persistentData = nbt.getCompound(MarioQuaMario.MOD_DATA_KEY);

MarioQuaMario.LOGGER.info("Reading player NBT:\nEnabled: {}\nPower-up: {}\nCharacter: {}",
persistentData.getBoolean("Enabled"),
persistentData.getString("PowerUp"),
persistentData.getString("Character"));

MarioServerPlayerData data = mqm$getMarioData();
if(networkHandler == null) {
data.setEnabledInternal(persistentData.getBoolean("Enabled"));
data.setPowerUpTransitionless(getPowerUp(nbt));
data.setCharacter(getCharacter(nbt));
}
else {
data.setEnabled(persistentData.getBoolean("Enabled"));
data.assignPowerUp(getPowerUp(nbt).ID);
data.assignCharacter(getCharacter(nbt).ID);
}
}
}

@Unique
private static ParsedCharacter getCharacter(NbtCompound nbt) {
return getDataFromNbt(nbt.getString("Characters"), MarioQuaMario.makeID("mario"), RegistryManager.CHARACTERS);
}
@Unique
private static ParsedPowerUp getPowerUp(NbtCompound nbt) {
return getDataFromNbt(nbt.getString("PowerUp"), getCharacter(nbt).INITIAL_POWER_UP.ID, RegistryManager.POWER_UPS);
}

@Unique
private static @NotNull <T extends ParsedMarioThing> T getDataFromNbt(String ID, Identifier defaultID, Registry<T> registry) {
@Nullable T attempted = registry.get(Identifier.of(ID));
return attempted == null ? Objects.requireNonNull(registry.get(defaultID)) : attempted;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fqf.mario_qua_mario.mixin;

import com.fqf.mario_qua_mario.MarioQuaMario;
import com.fqf.mario_qua_mario.mariodata.MarioPlayerData;
import com.fqf.mario_qua_mario.mariodata.MarioServerPlayerData;
import com.fqf.mario_qua_mario.mariodata.injections.MarioServerDataHolder;
Expand All @@ -17,11 +18,11 @@

@Mixin(ServerPlayerEntity.class)
public class ServerPlayerMarioDataMixin implements MarioServerDataHolder {
@Unique private MarioServerPlayerData marioServerData;
@Unique private MarioServerPlayerData marioServerData = new MarioServerPlayerData();

@Inject(method = "<init>", at = @At("RETURN"))
private void constructorHook(MinecraftServer server, ServerWorld world, GameProfile profile, SyncedClientOptions clientOptions, CallbackInfo ci) {
this.mqm$setMarioData(new MarioServerPlayerData((ServerPlayerEntity) (Object) this));
this.mqm$setMarioData(this.marioServerData);
}

@Override public @NotNull MarioServerPlayerData mqm$getMarioData() {
Expand Down
Loading

0 comments on commit 9cf1571

Please sign in to comment.