Skip to content

Commit

Permalink
MarioTravelData and common-side travelHook implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Nov 9, 2024
1 parent fc127c1 commit 1931797
Show file tree
Hide file tree
Showing 49 changed files with 941 additions and 685 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/floralquafloral/MarioCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.floralquafloral.mariodata.MarioDataManager;
import com.floralquafloral.mariodata.MarioDataPackets;
import com.floralquafloral.mariodata.MarioPlayerData;
import com.floralquafloral.mariodata.moveable.MarioServerData;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.stomp.ParsedStomp;
import com.floralquafloral.registries.stomp.StompHandler;
Expand Down Expand Up @@ -123,7 +124,7 @@ private static int executeStomp(CommandContext<ServerCommandSource> context, boo
stomper.teleport((ServerWorld) target.getWorld(), target.getX(), target.getY() + target.getHeight(), target.getZ(), target.getPitch(), target.getYaw());
long seed = RandomSeed.getSeed();
StompHandler.networkStomp(stomper, target, stompType, false, seed);
stompType.executeServer((MarioPlayerData) MarioDataManager.getMarioData(stomper), target, false, seed);
stompType.executeServer((MarioServerData) MarioDataManager.getMarioData(stomper), target, false, seed);

return sendFeedback(context, "Made " + stomper.getName().getString() + " perform a stomp of type " + stompType.ID + " on " + target.getName().getString());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/floralquafloral/MarioPackets.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static void registerCommon() {
SyncUseCharacterStatsS2CPayload.register();
MarioDataPackets.registerCommon();
StompHandler.registerPackets();
VoiceLine.registerPackets();
// VoiceLine.registerPackets();
JumpSoundPlayer.registerPackets();

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
Expand All @@ -35,7 +35,7 @@ public static void registerClient() {
SyncUseCharacterStatsS2CPayload.registerReceiver();
MarioDataPackets.registerClient();
StompHandler.registerPacketsClient();
VoiceLine.registerPacketsClient();
// VoiceLine.registerPacketsClient();
JumpSoundPlayer.registerPacketsClient();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/floralquafloral/MarioQuaMario.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void onInitialize() {
MarioDataManager.registerEventListeners();

RegistryManager.register();
MarioSFX.staticInitialize();


MarioPackets.registerCommon();

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/floralquafloral/VoiceLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public enum VoiceLine {
FIREBALL,
GET_STAR;

private static final VoiceLine[] VOICE_LINE_VALUES = VoiceLine.values();
private static final Map<PlayerEntity, PositionedSoundInstance> PLAYER_VOICE_LINES = new HashMap<>();
private final Map<ParsedCharacter, SoundEvent> SOUND_EVENTS;

Expand Down
127 changes: 127 additions & 0 deletions src/main/java/com/floralquafloral/mariodata/MarioClientSideData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.floralquafloral.mariodata;

import com.floralquafloral.MarioQuaMario;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.util.JumpSoundPlayer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundManager;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.random.Random;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

@SuppressWarnings("UnusedReturnValue")
public interface MarioClientSideData extends MarioData {
SoundManager SOUND_MANAGER = MinecraftClient.getInstance().getSoundManager();

default PositionedSoundInstance playSoundEvent(
SoundEvent event, SoundCategory category,
double x, double y, double z,
float pitch, float volume, long seed
) {
PositionedSoundInstance sound = new PositionedSoundInstance(
event, category,
volume, pitch,
Random.create(seed),
x, y, z
);
SOUND_MANAGER.play(sound);
return sound;
}

default PositionedSoundInstance playSoundEvent(SoundEvent event, long seed) {
PlayerEntity mario = this.getMario();
return playSoundEvent(
event, SoundCategory.PLAYERS,
mario.getX(), mario.getY(), mario.getZ(),
1.0F, 1.0F, seed
);
}

default PositionedSoundInstance playSoundEvent(SoundEvent event, float pitch, float volume, long seed) {
PlayerEntity mario = this.getMario();
return playSoundEvent(
event, SoundCategory.PLAYERS,
mario.getX(), mario.getY(), mario.getZ(),
pitch, volume, seed
);
}

default PositionedSoundInstance playSoundEvent(SoundEvent event, Entity entity, SoundCategory category, long seed) {
return playSoundEvent(
event, category,
entity.getX(), entity.getY(), entity.getZ(),
1.0F, 1.0F, seed
);
}

default PositionedSoundInstance voice(VoiceLine line, long seed) {
PlayerEntity mario = this.getMario();

SOUND_MANAGER.stop(VoiceLine.MARIO_VOICE_LINES.get(this));

PositionedSoundInstance newSoundInstance = this.playSoundEvent(
line.SOUND_EVENTS.get(this.getCharacter()), SoundCategory.VOICE,
mario.getX(), mario.getY(), mario.getZ(),
1.0F, this.getPowerUp().VOICE_PITCH,
seed
);
VoiceLine.MARIO_VOICE_LINES.put(this, newSoundInstance);

return newSoundInstance;
}

default void playJumpSound(long seed) {
JumpSoundPlayer.playJumpSfx(this, seed);
}

enum VoiceLine {
SELECT,
DUCK,

DOUBLE_JUMP,
TRIPLE_JUMP,
GYMNAST_SALUTE,

DUCK_JUMP,
LONG_JUMP,
BACKFLIP,
SIDEFLIP,
WALL_JUMP,

REVERT,
BURNT,

FIREBALL,
GET_STAR;

private static final Map<MarioClientSideData, PositionedSoundInstance> MARIO_VOICE_LINES = new HashMap<>();
public static void staticInitialize() {

}

private final Map<ParsedCharacter, SoundEvent> SOUND_EVENTS;
VoiceLine() {
SOUND_EVENTS = new HashMap<>();

for(ParsedCharacter character : RegistryManager.CHARACTERS) {
Identifier id = Identifier.of(character.ID.getNamespace(), "voice." + character.ID.getPath() + "." + this.name().toLowerCase(Locale.ROOT));
MarioQuaMario.LOGGER.info("Automatically registering VoiceLine sound event {}...", id);
SoundEvent event = SoundEvent.of(id);
Registry.register(Registries.SOUND_EVENT, id, event);
SOUND_EVENTS.put(character, event);
}
}
}
}
18 changes: 1 addition & 17 deletions src/main/java/com/floralquafloral/mariodata/MarioData.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,12 @@

public interface MarioData {
PlayerEntity getMario();
boolean isClient();
boolean useMarioPhysics();

void setForwardVel(double forward);
void setStrafeVel(double strafe);
default void setForwardStrafeVel(double forward, double strafe) {
this.setForwardVel(forward);
this.setStrafeVel(strafe);
}
void setYVel(double vertical);
double getForwardVel();
double getStrafeVel();
double getYVel();
void applyModifiedVelocity();

boolean isEnabled();
void setEnabled(boolean enabled);
ParsedAction getAction();
boolean getSneakProhibited();
void setAction(ParsedAction action, long seed);
void setActionTransitionless(ParsedAction action);
ParsedPowerUp getPowerUp();
void setPowerUp(ParsedPowerUp powerUp);
ParsedCharacter getCharacter();
void setCharacter(ParsedCharacter character);
}
55 changes: 25 additions & 30 deletions src/main/java/com/floralquafloral/mariodata/MarioDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import com.floralquafloral.MarioPackets;
import com.floralquafloral.MarioQuaMario;
import com.floralquafloral.mariodata.client.MarioClientData;
import com.floralquafloral.mixin.PlayerEntityMixin;
import com.floralquafloral.mariodata.moveable.MarioMainClientData;
import com.floralquafloral.mariodata.moveable.MarioServerData;
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;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -23,8 +21,8 @@
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<>();
private static final Map<PlayerEntity, MarioPlayerData> SERVER_PLAYERS_DATA = new HashMap<>();
private static final Map<PlayerEntity, MarioPlayerData> CLIENT_PLAYERS_DATA = new HashMap<>();
public static boolean useCharacterStats = true;

public static void registerEventListeners() {
Expand All @@ -40,8 +38,8 @@ public static void registerEventListeners() {
+ "\nAlive: " + alive
);

MarioData data = getMarioData(oldPlayer);
((MarioPlayerData) data).setMario(newPlayer);
MarioPlayerData data = getMarioData(oldPlayer);
data.setMario(newPlayer);
SERVER_PLAYERS_DATA.put(newPlayer, data);
MarioDataPackets.sendAllData(newPlayer, newPlayer);
});
Expand All @@ -50,16 +48,16 @@ public static void registerEventListeners() {

ServerTickEvents.START_SERVER_TICK.register((server) -> {
PlayerEntity removeMe = null;
Set<Map.Entry<PlayerEntity, MarioData>> entrySet = SERVER_PLAYERS_DATA.entrySet();
for(Map.Entry<PlayerEntity, MarioData> entry : entrySet) {
Set<Map.Entry<PlayerEntity, MarioPlayerData>> entrySet = SERVER_PLAYERS_DATA.entrySet();
for(Map.Entry<PlayerEntity, MarioPlayerData> entry : entrySet) {
ServerPlayerEntity player = (ServerPlayerEntity) entry.getKey();
if(player.isDisconnected()) {
MarioQuaMario.LOGGER.info("Removing player: {}", player);
removeMe = player;
continue;
}

((MarioPlayerData) entry.getValue()).tick();
entry.getValue().tick();
}

// This approach means we can only remove one player per tick. I don't care!!!!!!!!!!! Bite me!
Expand All @@ -69,14 +67,14 @@ public static void registerEventListeners() {
});

ClientTickEvents.START_CLIENT_TICK.register((client) -> {
for(Map.Entry<PlayerEntity, MarioData> entry : CLIENT_PLAYERS_DATA.entrySet()) {
if(!entry.getKey().isRemoved()) ((MarioPlayerData) entry.getValue()).tick();
for(Map.Entry<PlayerEntity, MarioPlayerData> entry : CLIENT_PLAYERS_DATA.entrySet()) {
if(!entry.getKey().isRemoved()) entry.getValue().tick();
}
});

ClientEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if(entity instanceof ClientPlayerEntity clientPlayer) {
MarioClientData data = MarioClientData.getInstance();
MarioMainClientData data = MarioMainClientData.getInstance();
if(data == null) return;
CLIENT_PLAYERS_DATA.remove(clientPlayer);
CLIENT_PLAYERS_DATA.remove(data.getMario());
Expand All @@ -99,34 +97,31 @@ public static void wipePlayerData() {
CLIENT_PLAYERS_DATA.clear();
}

public static MarioData getMarioData(PlayerEntity mario) {
public static MarioPlayerData getMarioData(PlayerEntity mario) {
boolean isClient = mario.getWorld().isClient;
final Map<PlayerEntity, MarioData> RELEVANT_MAP = isClient ? CLIENT_PLAYERS_DATA : SERVER_PLAYERS_DATA;
MarioData playerData = RELEVANT_MAP.get(mario);
final Map<PlayerEntity, MarioPlayerData> RELEVANT_MAP = isClient ? CLIENT_PLAYERS_DATA : SERVER_PLAYERS_DATA;
MarioPlayerData playerData = RELEVANT_MAP.get(mario);

if(playerData == null) {
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
// playerData = new MarioServerPlayerData((ServerPlayerEntity) mario);
if(mario.isMainPlayer() && mario instanceof ClientPlayerEntity marioClient)
playerData = new MarioMainClientData(marioClient);
else if(isClient)
playerData = new MarioOtherClientData(mario);
else
playerData = new MarioServerData((ServerPlayerEntity) mario);

RELEVANT_MAP.put(mario, playerData);
}

return playerData;
}
public static MarioData getMarioData(ServerPlayNetworking.Context context, int playerID) {
public static MarioPlayerData getMarioData(ServerPlayNetworking.Context context, int playerID) {
return getMarioData(MarioPackets.getPlayerFromInt(context, playerID));
}
public static MarioData getMarioData(ClientPlayNetworking.Context context, int playerID) {
public static MarioPlayerData getMarioData(ClientPlayNetworking.Context context, int playerID) {
return getMarioData(MarioPackets.getPlayerFromInt(context, playerID));
}
public static MarioData getMarioData(Object object) {
public static MarioPlayerData getMarioData(Object object) {
return getMarioData((PlayerEntity) object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public static String setMarioEnabled(ServerPlayerEntity player, boolean enabled)
}

public static void setMarioAction(ServerPlayerEntity mario, ParsedAction action, long seed) {
MarioData data = getMarioData(mario);
boolean foundTransition = data.getAction().transitionTo((MarioPlayerData) data, action, seed);
MarioPlayerData data = getMarioData(mario);
boolean foundTransition = data.getAction().transitionTo(data, action, seed);
if(foundTransition || !mario.getWorld().getGameRules().getBoolean(MarioQuaMario.REJECT_INVALID_ACTION_TRANSITIONS)) {
data.setActionTransitionless(action);
MarioPackets.sendPacketToTrackersExclusive(mario, new SetActionS2CPayload(mario, action, false, seed));
Expand Down Expand Up @@ -142,7 +142,7 @@ public SetActionS2CPayload(PlayerEntity mario, ParsedAction newAction, boolean t
}
public static void registerReceiver() {
ClientPlayNetworking.registerGlobalReceiver(ID, (payload, context) -> {
MarioData data = getMarioData(context, payload.mario);
MarioPlayerData data = getMarioData(context, payload.mario);
ParsedAction action = RegistryManager.ACTIONS.get(payload.newAction);
if(payload.transitionless)
data.setActionTransitionless(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.floralquafloral.mariodata;

import net.minecraft.entity.player.PlayerEntity;

public class MarioOtherClientData extends MarioPlayerData implements MarioClientSideData {
public MarioOtherClientData(PlayerEntity mario) {
super(mario);
}

@Override
public void tick() {
this.getAction().clientTick(this, false);
this.getPowerUp().clientTick(this, false);
this.getCharacter().clientTick(this, false);
}
}
Loading

0 comments on commit 1931797

Please sign in to comment.