Skip to content

Commit

Permalink
Preparing to separate API from implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Nov 20, 2024
1 parent 17ef4d5 commit 570401d
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 155 deletions.
5 changes: 3 additions & 2 deletions mod/src/main/java/com/floralquafloral/MarioQuaMario.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.mariodata.MarioDataManager;
import com.floralquafloral.mariodata.MarioDataPackets;
import com.floralquafloral.mariodata.MarioPlayerData;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.util.ModConfig;
import com.floralquafloral.util.MarioSFX;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void onInitialize() {
// Mario can't be damaged by a mob that he's high enough to stomp on
ServerLivingEntityEvents.ALLOW_DAMAGE.register((livingEntity, damageSource, amount) -> {
if(livingEntity instanceof PlayerEntity player && damageSource.getSource() instanceof LivingEntity sourceEntity && sourceEntity.equals(damageSource.getAttacker())) {
MarioData data = MarioDataManager.getMarioData(player);
MarioPlayerData data = MarioDataManager.getMarioData(player);
if(data.isEnabled() && livingEntity.getY() >= sourceEntity.getY() + sourceEntity.getHeight() && data.getAction().STOMP != null) {
LOGGER.info("Prevented Mario from taking damage against {} due to stomp eligibility.", sourceEntity);
return false;
Expand All @@ -95,7 +96,7 @@ public void onInitialize() {

ServerLivingEntityEvents.ALLOW_DEATH.register((livingEntity, damageSource, amount) -> {
if(livingEntity instanceof ServerPlayerEntity player) {
MarioData data = MarioDataManager.getMarioData(player);
MarioPlayerData data = MarioDataManager.getMarioData(player);
if(data.isEnabled()) {
// Revert if possible
Identifier revertTargetID = data.getPowerUp().REVERT_TARGET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static Set<BlockPos> bumpBlocksClient(
int strength, Direction direction
) {
boolean playBumpSound = false;
int modifier = marioClientData.getPowerUp().BUMP_STRENGTH_MODIFIER + marioClientData.getCharacter().BUMP_STRENGTH_MODIFIER;
int modifier = marioClientData.getBumpStrengthModifier();

Set<BlockPos> blocksBumped = new HashSet<>();
Set<BlockSoundGroup> soundGroups = new HashSet<>();
Expand Down Expand Up @@ -212,7 +212,7 @@ public static void bumpBlocksServer(
int strength, Direction direction, boolean networkToBumper
) {
boolean canRepeatBump = false;
int modifier = marioServerData.getPowerUp().BUMP_STRENGTH_MODIFIER + marioServerData.getCharacter().BUMP_STRENGTH_MODIFIER;
int modifier = marioServerData.getBumpStrengthModifier();
Set<BlockPos> bumpedPositions = new HashSet<>();

for(BlockPos pos : positions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.floralquafloral.MarioQuaMario;
import com.floralquafloral.registries.RegistryManager;
import com.floralquafloral.registries.states.action.ParsedAction;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.entity.Entity;
import net.minecraft.registry.Registries;
Expand All @@ -19,6 +21,7 @@
* An instance of MarioData on the client-side. Mostly used for playing sound effects, especially voice sounds.
* This isn't necessarily for the main client!
*/
@SuppressWarnings("UnusedReturnValue")
public interface MarioClientSideData extends MarioData {
PositionedSoundInstance playSoundEvent(
SoundEvent event, SoundCategory category,
Expand All @@ -33,6 +36,7 @@ PositionedSoundInstance playSoundEvent(
void playJumpSound(long seed);

PositionedSoundInstance voice(VoiceLine line, long seed);
float getVoicePitch();

enum VoiceLine {
SELECT,
Expand All @@ -52,25 +56,6 @@ enum VoiceLine {
BURNT,

FIREBALL,
GET_STAR;

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);
}
}
public SoundEvent getSoundEvent(ParsedCharacter character) {
return SOUND_EVENTS.get(character);
}
GET_STAR
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
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.util.Identifier;
import net.minecraft.util.math.random.Random;

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

/**
Expand Down Expand Up @@ -75,9 +83,9 @@ default PositionedSoundInstance voice(MarioClientSideData.VoiceLine line, long s
SOUND_MANAGER.stop(MARIO_VOICE_LINES.get(this));

PositionedSoundInstance newSoundInstance = this.playSoundEvent(
line.getSoundEvent(this.getCharacter()), SoundCategory.VOICE,
VOICE_SOUND_EVENTS.get(line).get(this.getCharacterID()), SoundCategory.VOICE,
mario.getX(), mario.getY(), mario.getZ(),
this.getPowerUp().VOICE_PITCH, 1.0F,
this.getVoicePitch(), 1.0F,
seed
);
MARIO_VOICE_LINES.put(this, newSoundInstance);
Expand All @@ -90,4 +98,21 @@ default void playJumpSound(long seed) {
JumpSoundPlayer.playJumpSfx(this, seed);
}

EnumMap<VoiceLine, Map<Identifier, SoundEvent>> VOICE_SOUND_EVENTS = new EnumMap<>(VoiceLine.class);
class VoiceSoundEventInitializer {
public static void initialize() {
for(MarioClientSideData.VoiceLine voiceLine : MarioClientSideData.VoiceLine.values()) {
Map<Identifier, SoundEvent> soundEvents = new HashMap<>();
VOICE_SOUND_EVENTS.put(voiceLine, soundEvents);

for(ParsedCharacter character : RegistryManager.CHARACTERS) {
Identifier id = Identifier.of(character.ID.getNamespace(), "voice." + character.ID.getPath() + "." + voiceLine.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);
soundEvents.put(character.ID, event);
}
}
}
}
}
20 changes: 13 additions & 7 deletions mod/src/main/java/com/floralquafloral/mariodata/MarioData.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.floralquafloral.mariodata;

import com.floralquafloral.registries.states.action.ParsedAction;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import com.floralquafloral.stats.CharaStat;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;

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

double getStat(CharaStat stat);
double getStatMultiplier(CharaStat stat);
int getBumpStrengthModifier();
boolean isSneakProhibited();



boolean isEnabled();
ParsedAction getAction();
boolean getSneakProhibited();
ParsedPowerUp getPowerUp();
ParsedCharacter getCharacter();
Identifier getActionID();
Identifier getPowerUpID();
Identifier getCharacterID();

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void registerClient() {
}

public static void sendAllData(ServerPlayerEntity toWho, PlayerEntity aboutWho) {
MarioData data = getMarioData(aboutWho);
MarioPlayerData 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()));
ServerPlayNetworking.send(toWho, new SetActionS2CPayload(aboutWho, data.getAction(), true, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import com.floralquafloral.stats.CharaStat;
import com.floralquafloral.stats.StatCategory;
import com.floralquafloral.util.MarioSFX;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -66,13 +67,54 @@ public MarioPlayerData(PlayerEntity mario) {
@Override public boolean useMarioPhysics() {
return(
isEnabled()
&& !mario.getAbilities().flying
&& !mario.isFallFlying()
&& !this.mario.getAbilities().flying
&& !this.mario.isFallFlying()
// && !mario.hasVehicle()
// && !mario.isClimbing()
);
}

private static final double MOVEMENT_SPEED_MULTIPLIER = 1.0 / 0.10000000149011612;
private static final double MOVEMENT_SPEED_MULTIPLIER_SPRINTING = 1.0 / 0.13000000312924387;

@Override
public double getStat(CharaStat stat) {
double modifiedBase = stat.BASE * this.getStatMultiplier(stat);

double attributeFactor = 1.0;
double attributeAddend = 0.0;

if(stat.CATEGORIES.contains(StatCategory.SPEED) && (
stat.CATEGORIES.contains(StatCategory.WALKING)
|| stat.CATEGORIES.contains(StatCategory.RUNNING)
|| stat.CATEGORIES.contains(StatCategory.P_RUNNING)
|| stat.CATEGORIES.contains(StatCategory.DUCKING)
)) {
attributeFactor = this.mario.getAttributes().getValue(EntityAttributes.GENERIC_MOVEMENT_SPEED)
* (this.mario.isSprinting() ? MOVEMENT_SPEED_MULTIPLIER_SPRINTING : MOVEMENT_SPEED_MULTIPLIER);
}

if(stat.CATEGORIES.contains(StatCategory.JUMP_VELOCITY)) {
attributeAddend = this.mario.getJumpBoostVelocityModifier();
}

return attributeFactor * modifiedBase + attributeAddend;
}

@Override
public double getStatMultiplier(CharaStat stat) {
return this.powerUp.getStatMultiplier(stat) * this.character.getStatMultiplier(stat);
}

@Override
public int getBumpStrengthModifier() {
return this.powerUp.BUMP_STRENGTH_MODIFIER + this.character.BUMP_STRENGTH_MODIFIER;
}

public float getVoicePitch() {
return this.powerUp.VOICE_PITCH;
}

public boolean attemptDismount = false;

public abstract void tick();
Expand All @@ -90,10 +132,13 @@ public void setEnabledInternal(boolean enabled) {
attackSpeedAttributeInstance.removeModifier(ATTACK_SLOWDOWN_ID);
attackSpeedAttributeInstance.addPersistentModifier(ATTACK_SLOWDOWN);
}
@Override public ParsedAction getAction() {
public ParsedAction getAction() {
return action;
}
@Override public boolean getSneakProhibited() {
@Override public Identifier getActionID() {
return action.ID;
}
@Override public boolean isSneakProhibited() {
return useMarioPhysics() && getAction().SNEAK_LEGALITY.prohibitSneak();
}

Expand Down Expand Up @@ -199,24 +244,28 @@ public void setActionTransitionless(ParsedAction action) {
this.mario.setPose(this.mario.getPose());

}
@Override public ParsedPowerUp getPowerUp() {
public ParsedPowerUp getPowerUp() {
return powerUp;
}
@Override public Identifier getPowerUpID() {
return powerUp.ID;
}
public void setPowerUp(ParsedPowerUp powerUp) {
MarioQuaMario.LOGGER.info("Set Power-up to {}", powerUp.ID);
this.powerUp.losePower(this);
powerUp.acquirePower(this);
this.mario.setHealth(this.mario.getMaxHealth());
this.powerUp = powerUp;
this.mario.calculateDimensions();
CharaStat.invalidateCache();
}
@Override public ParsedCharacter getCharacter() {
public ParsedCharacter getCharacter() {
return character;
}
@Override public Identifier getCharacterID() {
return character.ID;
}
public void setCharacter(ParsedCharacter character) {
this.character = character;
this.mario.calculateDimensions();
CharaStat.invalidateCache();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.floralquafloral.mariodata.moveable;

import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.registries.states.action.ParsedAction;
import com.floralquafloral.registries.states.character.ParsedCharacter;
import com.floralquafloral.registries.states.powerup.ParsedPowerUp;
import org.jetbrains.annotations.NotNull;

public interface MarioTravelData extends MarioData {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.floralquafloral.mixin;

import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.mariodata.MarioDataManager;
import net.minecraft.client.network.ClientPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -18,7 +17,7 @@ private void preventSlowDown(CallbackInfoReturnable<Boolean> cir) {

@Inject(method = "isInSneakingPose", at = @At("HEAD"), cancellable = true)
private void preventSneakPose(CallbackInfoReturnable<Boolean> cir) {
if(MarioDataManager.getMarioData(this).getSneakProhibited())
if(MarioDataManager.getMarioData(this).isSneakProhibited())
cir.setReturnValue(false);
}
}
9 changes: 4 additions & 5 deletions mod/src/main/java/com/floralquafloral/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.floralquafloral.mixin;

import com.floralquafloral.MarioQuaMario;
import com.floralquafloral.mariodata.MarioData;
import com.floralquafloral.mariodata.MarioDataManager;
import com.floralquafloral.mariodata.MarioPlayerData;
import com.floralquafloral.mariodata.moveable.MarioServerData;
Expand Down Expand Up @@ -40,7 +39,7 @@ private void setSwimming(boolean swimming, CallbackInfo ci) {
@Inject(at = @At("HEAD"), method = "isInSneakingPose", cancellable = true)
private void isInSneakingPose(CallbackInfoReturnable<Boolean> cir) {
if((Entity) (Object) this instanceof PlayerEntity player) {
if(MarioDataManager.getMarioData(player).getSneakProhibited())
if(MarioDataManager.getMarioData(player).isSneakProhibited())
cir.setReturnValue(false);
}
}
Expand All @@ -49,7 +48,7 @@ private void isInSneakingPose(CallbackInfoReturnable<Boolean> cir) {
private void preventSettingSneakPose(EntityPose pose, CallbackInfo ci) {
if((Entity) (Object) this instanceof PlayerEntity player && pose == EntityPose.CROUCHING) {
// MarioQuaMario.LOGGER.info("setPose called on player! Pose == {}", pose);
if(MarioDataManager.getMarioData(player).getSneakProhibited()) {
if(MarioDataManager.getMarioData(player).isSneakProhibited()) {

if(player.getPose() == EntityPose.CROUCHING)
player.setPose(EntityPose.STANDING);
Expand All @@ -61,7 +60,7 @@ private void preventSettingSneakPose(EntityPose pose, CallbackInfo ci) {
@Inject(method = "playStepSounds", at = @At("HEAD"), cancellable = true)
private void preventStepSounds(BlockPos pos, BlockState state, CallbackInfo ci) {
if(((Entity) (Object) this) instanceof PlayerEntity player) {
MarioData data = MarioDataManager.getMarioData(player);
MarioPlayerData data = MarioDataManager.getMarioData(player);
if(!data.getAction().SLIDING_STATUS.doFootsteps())
ci.cancel();
}
Expand All @@ -73,7 +72,7 @@ private void preventStepSounds(BlockPos pos, BlockState state, CallbackInfo ci)
@Inject(method = "move", at = @At("HEAD"), cancellable = true)
private void executeStompsOnServer(MovementType movementType, Vec3d movement, CallbackInfo ci) {
if((Entity) (Object) this instanceof ServerPlayerEntity mario && shouldStompHook) {
MarioData data = MarioDataManager.getMarioData(mario);
MarioPlayerData data = MarioDataManager.getMarioData(mario);
if(data.useMarioPhysics()) {
ParsedAction action = data.getAction();
if(action.STOMP != null) {
Expand Down
Loading

0 comments on commit 570401d

Please sign in to comment.