Skip to content

Commit

Permalink
Further work on /mario command and networking
Browse files Browse the repository at this point in the history
  • Loading branch information
floral-qua-floral committed Dec 18, 2024
1 parent be6972d commit 879fa25
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fqf.mario_qua_mario.packets;

import com.fqf.mario_qua_mario.MarioClientHelperManager;
import com.fqf.mario_qua_mario.registries.RegistryManager;
import com.fqf.mario_qua_mario.registries.actions.AbstractParsedAction;
import com.fqf.mario_qua_mario.registries.actions.ParsedActionHelper;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
Expand All @@ -25,6 +26,27 @@ public static void registerClientReceivers() {
ParsedActionHelper.get(payload.newAction())
)
);

// EmpowerRevertS2CPayload Receiver
ClientPlayNetworking.registerGlobalReceiver(MarioDataPackets.EmpowerRevertS2CPayload.ID, (payload, context) ->
getMarioFromID(context, payload.marioID()).mqm$getMarioData().setPowerUp(
RegistryManager.POWER_UPS.get(payload.toPower()), false, payload.seed()
)
);

// AssignPowerUpS2CPayload Receiver
ClientPlayNetworking.registerGlobalReceiver(MarioDataPackets.AssignPowerUpS2CPayload.ID, (payload, context) ->
getMarioFromID(context, payload.marioID()).mqm$getMarioData().setPowerUpTransitionless(
RegistryManager.POWER_UPS.get(payload.newPower())
)
);

// AssignCharacterS2CPayload Receiver
ClientPlayNetworking.registerGlobalReceiver(MarioDataPackets.AssignCharacterS2CPayload.ID, (payload, context) ->
getMarioFromID(context, payload.marioID()).mqm$getMarioData().setCharacter(
RegistryManager.CHARACTERS.get(payload.newCharacter())
)
);
}

public static PlayerEntity getMarioFromID(ClientPlayNetworking.Context context, int marioID) {
Expand Down
165 changes: 103 additions & 62 deletions mod/src/main/java/com/fqf/mario_qua_mario/MarioCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.RandomSeed;

import java.util.Locale;

Expand All @@ -29,66 +31,77 @@ public static void registerMarioCommand() {
dispatcher.register(literal("mario")
.then(literal("set")
.then(literal("enabled")
.then(argument("enabled", BoolArgumentType.bool())
.requires(source -> source.hasPermissionLevel(0))
.executes(context -> setEnabled(context, false))
.then(argument("target", EntityArgumentType.player())
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> setEnabled(context, true))
)
.then(argument("enabled", BoolArgumentType.bool())
.requires(source -> source.hasPermissionLevel(0))
.executes(context -> setEnabled(context, false))
.then(argument("target", EntityArgumentType.player())
.requires(source -> source.hasPermissionLevel(2))
.executes(context -> setEnabled(context, true))
)
)
)
.then(literal("action")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("action", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.ACTIONS_KEY))
.executes(context -> setAction(context, false))
.then(argument("target", EntityArgumentType.player())
.executes(context -> setAction(context, true))
)
.requires(source -> source.hasPermissionLevel(2))
.then(argument("action", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.ACTIONS_KEY))
.executes(context -> setAction(context, false))
.then(argument("mario", EntityArgumentType.player())
.executes(context -> setAction(context, true))
)
)
)
.then(literal("powerUp")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("power", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.POWER_UPS_KEY))
.executes(context -> setPowerUp(context, false))
.then(argument("target", EntityArgumentType.player())
.executes(context -> setPowerUp(context, true))
)
.requires(source -> source.hasPermissionLevel(2))
.then(argument("power-up", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.POWER_UPS_KEY))
.executes(context -> setPowerUp(context, false))
.then(argument("mario", EntityArgumentType.player())
.executes(context -> setPowerUp(context, true))
)
)
)
.then(literal("character")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("character", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.CHARACTERS_KEY))
.executes(context -> setCharacter(context, false))
.then(argument("target", EntityArgumentType.player())
.executes(context -> setCharacter(context, true))
)
.requires(source -> source.hasPermissionLevel(2))
.then(argument("character", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.CHARACTERS_KEY))
.executes(context -> setCharacter(context, false))
.then(argument("mario", EntityArgumentType.player())
.executes(context -> setCharacter(context, true))
)
)
)
)
.then(literal("perform")
.then(literal("stomp")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("stomp", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.STOMP_TYPES_KEY))
.then(argument("goomba", EntityArgumentType.entity())
.executes(context -> executeStomp(context, false))
.then(argument("target", EntityArgumentType.player())
.executes(context -> executeStomp(context, true))
)
)
.requires(source -> source.hasPermissionLevel(2))
.then(argument("stomp", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.STOMP_TYPES_KEY))
.then(argument("goomba", EntityArgumentType.entity())
.executes(context -> executeStomp(context, false))
.then(argument("mario", EntityArgumentType.player())
.executes(context -> executeStomp(context, true))
)
)
)
)
.then(literal("bump")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("position", BlockPosArgumentType.blockPos())
.executes(context -> executeBump(context, false, Direction.UP, 4))
.then(makeBumpDirectionFork(Direction.UP))
.then(makeBumpDirectionFork(Direction.DOWN))
.then(makeBumpDirectionFork(Direction.NORTH))
.then(makeBumpDirectionFork(Direction.SOUTH))
.then(makeBumpDirectionFork(Direction.EAST))
.then(makeBumpDirectionFork(Direction.WEST))
.requires(source -> source.hasPermissionLevel(2))
.then(argument("position", BlockPosArgumentType.blockPos())
.executes(context -> executeBump(context, false, Direction.UP, 4))
.then(makeBumpDirectionFork(Direction.UP))
.then(makeBumpDirectionFork(Direction.DOWN))
.then(makeBumpDirectionFork(Direction.NORTH))
.then(makeBumpDirectionFork(Direction.SOUTH))
.then(makeBumpDirectionFork(Direction.EAST))
.then(makeBumpDirectionFork(Direction.WEST))
)
)
.then(literal("actionTransition")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("from", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.ACTIONS_KEY))
.then(argument("to", RegistryEntryReferenceArgumentType.registryEntry(registryAccess, RegistryManager.ACTIONS_KEY))
.executes(context -> executeActionTransition(context, false))
.then(argument("mario", EntityArgumentType.player())
.executes(context -> executeActionTransition(context, true))
)
)
)
)
)
)
Expand All @@ -100,46 +113,49 @@ private static int sendFeedback(CommandContext<ServerCommandSource> context, Str
return 1;
}

private static ServerPlayerEntity getPlayerFromCmd(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven, String argumentName) throws CommandSyntaxException {
return playerArgumentGiven ? EntityArgumentType.getPlayer(context, argumentName) : context.getSource().getPlayerOrThrow();
}
private static ServerPlayerEntity getPlayerFromCmd(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
return playerArgumentGiven ? EntityArgumentType.getPlayer(context, "target") : context.getSource().getPlayerOrThrow();
return getPlayerFromCmd(context, playerArgumentGiven, "mario");
}

private static int setEnabled(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
// return sendFeedback(environment, MarioDataPackets.setMarioEnabled(
// getPlayerFromCmd(environment, playerArgumentGiven),
// BoolArgumentType.getBool(environment, "enabled")
// ));
getPlayerFromCmd(context, playerArgumentGiven, "target");

return 0;
}

private static int setAction(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
// return sendFeedback(environment, MarioDataPackets.forceSetMarioAction(
// getPlayerFromCmd(environment, playerArgumentGiven),
// RegistryEntryReferenceArgumentType.getRegistryEntry(environment, "action", RegistryManager.ACTIONS_KEY).value()
// ));
ServerPlayerEntity mario = getPlayerFromCmd(context, playerArgumentGiven);
RegistryEntry<AbstractParsedAction> actionEntry =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "action", RegistryManager.ACTIONS_KEY);
mario.mqm$getMarioData().setActionTransitionless(actionEntry.value());
MarioDataPackets.setActionTransitionlessS2C(mario, true, actionEntry.value());
Identifier newActionID =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "action", RegistryManager.ACTIONS_KEY).value().ID;
mario.mqm$getMarioData().assignAction(newActionID);

return sendFeedback(context, "Changed " + mario.getName().getString() + "'s action to " + actionEntry.value().ID + ".");
return sendFeedback(context, "Changed " + mario.getName().getString() + "'s action to " + newActionID + ".");
}

private static int setPowerUp(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
// return sendFeedback(environment, MarioDataPackets.setMarioPowerUp(
// getPlayerFromCmd(environment, playerArgumentGiven),
// RegistryEntryReferenceArgumentType.getRegistryEntry(environment, "power", RegistryManager.POWER_UPS_KEY).value()
// ));
return 0;
ServerPlayerEntity mario = getPlayerFromCmd(context, playerArgumentGiven);
Identifier newPowerUpID =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "power-up", RegistryManager.POWER_UPS_KEY).value().ID;
mario.mqm$getMarioData().assignPowerUp(newPowerUpID);

return sendFeedback(context, "Changed " + mario.getName().getString() + "'s power-up to " + newPowerUpID + ".");
}

private static int setCharacter(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
// return sendFeedback(environment, MarioDataPackets.setMarioCharacter(
// getPlayerFromCmd(environment, playerArgumentGiven),
// RegistryEntryReferenceArgumentType.getRegistryEntry(environment, "character", RegistryManager.CHARACTERS_KEY).value()
// ));
return 0;
ServerPlayerEntity mario = getPlayerFromCmd(context, playerArgumentGiven);
Identifier newCharacterID =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "power-up", RegistryManager.CHARACTERS_KEY).value().ID;
mario.mqm$getMarioData().assignCharacter(newCharacterID);


return sendFeedback(context, mario.getName().getString() + " will now play as " + newCharacterID + ".");
}

private static int executeStomp(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
Expand Down Expand Up @@ -178,4 +194,29 @@ private static LiteralArgumentBuilder<ServerCommandSource> makeBumpDirectionFork
)
);
}

private static int executeActionTransition(CommandContext<ServerCommandSource> context, boolean playerArgumentGiven) throws CommandSyntaxException {
AbstractParsedAction fromAction =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "from", RegistryManager.ACTIONS_KEY).value();
AbstractParsedAction toAction =
RegistryEntryReferenceArgumentType.getRegistryEntry(context, "to", RegistryManager.ACTIONS_KEY).value();

long seed = RandomSeed.getSeed();

ServerPlayerEntity mario = getPlayerFromCmd(context, playerArgumentGiven);
boolean successful = mario.mqm$getMarioData().setAction(fromAction, toAction, seed, false);

if(successful) MarioDataPackets.transitionToActionS2C(
mario,
true,
fromAction,
toAction,
seed
);

return sendFeedback(context, successful ?
"Successfully made " + mario.getName().getString() + " execute transition \"" + fromAction.ID + "->" + toAction.ID + "\"."
: "No transition exists from " + fromAction.ID + " to " + toAction.ID + "! :("
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ public AbstractParsedAction getAction() {
}

public boolean setAction(@Nullable AbstractParsedAction fromAction, AbstractParsedAction toAction, long seed, boolean forced) {
if(fromAction == null) fromAction = this.getAction();
else if(!forced) {
if(!this.getAction().equals(fromAction) && !forced) {
// Check if we were recently in fromAction. If not, return false.
if(this.RECENT_ACTIONS.stream().noneMatch(pair -> pair.getLeft().equals(fromAction))) {
MarioQuaMario.LOGGER.info("Rejected action transition because we weren't in {} recently!", fromAction);
return false;
}
}
boolean transitionedNaturally = ParsedActionHelper.attemptTransitionTo(this, fromAction, toAction, seed);
if(!transitionedNaturally && forced) this.setActionTransitionless(toAction);
boolean transitionedNaturally = ParsedActionHelper.attemptTransitionTo(this, fromAction == null ? this.getAction() : fromAction, toAction, seed);
if(transitionedNaturally && this instanceof MarioMoveableData moveableData) moveableData.applyModifiedVelocity();
else if(forced) this.setActionTransitionless(toAction);
return transitionedNaturally || forced;
}
public void setActionTransitionless(AbstractParsedAction action) {
Expand All @@ -83,10 +87,10 @@ public ParsedPowerUp getPowerUp() {
return this.getPowerUp().ID;
}

public void setPowerUp(ParsedPowerUp newPowerUp, boolean isReversion) {
this.setPowerUpInternal(newPowerUp);
public void setPowerUp(ParsedPowerUp newPowerUp, boolean isReversion, long seed) {
this.setPowerUpTransitionless(newPowerUp);
}
public void setPowerUpInternal(ParsedPowerUp newPowerUp) {
public void setPowerUpTransitionless(ParsedPowerUp newPowerUp) {
this.powerUp = newPowerUp;
refreshPlayerModel();
refreshPowerSet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fqf.mario_qua_mario.registries.actions.AbstractParsedAction;
import com.fqf.mario_qua_mario.registries.actions.ParsedActionHelper;
import com.fqf.mario_qua_mario.registries.actions.TransitionPhase;
import com.fqf.mario_qua_mario.registries.power_granting.ParsedPowerUp;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
Expand Down Expand Up @@ -32,7 +33,7 @@ public MarioServerPlayerData(ServerPlayerEntity mario) {
AbstractParsedAction toAction = Objects.requireNonNull(RegistryManager.ACTIONS.get(actionID),
"Target action doesn't exist!");
if(this.setAction(this.getAction(), toAction, seed, false)) {
MarioDataPackets.setActionS2C(this.getMario(), true, this.getAction(), toAction, seed);
MarioDataPackets.transitionToActionS2C(this.getMario(), true, this.getAction(), toAction, seed);
}
return false;
}
Expand All @@ -44,28 +45,42 @@ public MarioServerPlayerData(ServerPlayerEntity mario) {
AbstractParsedAction newAction = Objects.requireNonNull(RegistryManager.ACTIONS.get(actionID),
"Target action doesn't exist!");
this.setActionTransitionless(newAction);
MarioDataPackets.setActionTransitionlessS2C(this.getMario(), true, newAction);
MarioDataPackets.assignActionS2C(this.getMario(), true, newAction);
}
@Override public void assignAction(String actionID) {
this.assignAction(Identifier.of(actionID));
}

@Override public void empowerTo(Identifier powerUpID) {
ParsedPowerUp newPowerUp = Objects.requireNonNull(RegistryManager.POWER_UPS.get(powerUpID),
"Target power-up doesn't exist!");

long seed = RandomSeed.getSeed();
this.setPowerUp(newPowerUp, false, seed);
MarioDataPackets.empowerRevertS2C(this.getMario(), newPowerUp, false, seed);
}
@Override public void empowerTo(String powerUpID) {
this.empowerTo(Identifier.of(powerUpID));
}

@Override public void revertTo(Identifier powerUpID) {
ParsedPowerUp newPowerUp = Objects.requireNonNull(RegistryManager.POWER_UPS.get(powerUpID),
"Target power-up doesn't exist!");

long seed = RandomSeed.getSeed();
this.setPowerUp(newPowerUp, true, seed);
MarioDataPackets.empowerRevertS2C(this.getMario(), newPowerUp, true, seed);
}
@Override public void revertTo(String powerUpID) {
this.revertTo(Identifier.of(powerUpID));
}

@Override public void assignPowerUp(Identifier powerUpID) {
ParsedPowerUp newPowerUp = Objects.requireNonNull(RegistryManager.POWER_UPS.get(powerUpID),
"Target power-up doesn't exist!");

this.setPowerUpTransitionless(newPowerUp);
MarioDataPackets.assignPowerUpS2C(this.getMario(), newPowerUp);
}
@Override public void assignPowerUp(String powerUpID) {
this.assignPowerUp(Identifier.of(powerUpID));
Expand Down
Loading

0 comments on commit 879fa25

Please sign in to comment.