[factions-banking] Flight tweaks to be MWAH before I go to bed
Some checks are pending
build / build (21) (push) Waiting to run

This commit is contained in:
Jkibbels 2025-01-18 04:38:34 -05:00
parent d274810ca4
commit 6d14d47e0b
6 changed files with 102 additions and 10 deletions

View File

@ -3,11 +3,13 @@ package jesse.keeblarcraft.AttributeMgr.AttributeNodes.FactionNodes;
import java.util.HashMap;
import java.util.List;
import org.apache.logging.log4j.core.jmx.Server;
import jesse.keeblarcraft.AttributeMgr.AttributeNodes.AbstractNode;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerCommandFlightCallback;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerEnteredBaseCallback;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerExitedBaseCallback;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerInBaseCallback;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerAbilities;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.Text;
@ -16,6 +18,8 @@ import net.minecraft.util.ActionResult;
public class FactionFlight extends AbstractNode {
private final int flightSpeed = 1;
private ServerPlayerEntity player;
private float SPEED_SCALAR = 40.0f; // The value to scale this correctly to CREATIVE flight is '20.0f' however faction flight is slower than creative intentionally
private Boolean canFly = false;
@Override
public String GetNodeTitle() {
@ -34,6 +38,40 @@ public class FactionFlight extends AbstractNode {
return ret;
}
public Boolean ToggleFlight(ServerPlayerEntity player) {
// Only bother with flight if the player can fly
Boolean isFlying = false;
if (canFly) {
PlayerAbilities abilities = player.getAbilities();
// Flight toggle off
if (abilities.flying) {
// Disable flight
abilities.flying = false;
abilities.allowFlying = false;
abilities.setFlySpeed(0);
} else {
// Flight toggle on
isFlying = true;
abilities.allowFlying = true;
abilities.setFlySpeed((float) (flightSpeed / SPEED_SCALAR)); // Dividing by 20f yields max clamp value of 0.5 since MC does 0.0-> 1.0 flight. 0.1 flight is too fast!
}
}
player.sendAbilitiesUpdate();
return isFlying;
}
public void ResetPlayer(ServerPlayerEntity player) {
PlayerAbilities abilities = player.getAbilities();
if (abilities.flying) {
// Disable flight
abilities.flying = false;
abilities.allowFlying = false;
abilities.setFlySpeed(0);
}
player.sendAbilitiesUpdate();
}
@Override
public void RegisterCallbacks(ServerPlayerEntity playerObj, ServerWorld worldObj) {
// PlayerInBaseCallback.EVENT.register((player, world) -> {
@ -43,11 +81,32 @@ public class FactionFlight extends AbstractNode {
PlayerEnteredBaseCallback.EVENT.register((player, world) -> {
player.sendMessage(Text.of("Faction flight enabled"));
canFly = true;
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
ToggleFlight(serverPlayer);
return ActionResult.SUCCESS;
});
PlayerCommandFlightCallback.EVENT.register((player, world) -> {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
Boolean isFlying = ToggleFlight(serverPlayer);
if(isFlying) {
serverPlayer.sendMessage(Text.of("Flight turned on"));
} else if (!isFlying && canFly) {
// Infers toggled off
serverPlayer.sendMessage(Text.of("Flight turned off"));
} else {
// Means player is not in fly-zone
serverPlayer.sendMessage(Text.of("You can only fly within the bounds of your faction base!"));
}
return ActionResult.SUCCESS;
});
PlayerExitedBaseCallback.EVENT.register((player, world) -> {
player.sendMessage(Text.of("Faction flight disabled"));
canFly = false;
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
ResetPlayer(serverPlayer);
return ActionResult.SUCCESS;
});
}

View File

@ -17,7 +17,6 @@ import java.util.HashMap;
import jesse.keeblarcraft.Keeblarcraft;
import jesse.keeblarcraft.AttributeMgr.AttributeNodes.AbstractNode;
import jesse.keeblarcraft.ConfigMgr.ConfigManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;

View File

@ -7,12 +7,14 @@ import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import jesse.keeblarcraft.FactionMgr.FactionManager;
import jesse.keeblarcraft.FactionMgr.Callbacks.PlayerCommandFlightCallback;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.command.argument.EntityArgumentType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
public class FactionCommands {
@ -34,6 +36,8 @@ public class FactionCommands {
.executes(context -> GetFactionInformation(context.getSource().getPlayer()))
.build();
var invite = CommandManager.literal("invite").build();
var fly = CommandManager.literal("fly")
.executes(context -> ForwardFlightCallback(context)).build();
// The below nodes are duplicates but are necessary to make the execute path jump correctly
var createFactionName = CommandManager.argument("faction_name", StringArgumentType.greedyString())
@ -85,6 +89,7 @@ public class FactionCommands {
factionNode.addChild(kick);
factionNode.addChild(info);
factionNode.addChild(invite);
factionNode.addChild(fly);
promote.addChild(promoteName);
demote.addChild(demoteName);
@ -118,6 +123,13 @@ public class FactionCommands {
});
}
private int ForwardFlightCallback(CommandContext<ServerCommandSource> context) {
if (context.getSource().isExecutedByPlayer()) {
ActionResult result = PlayerCommandFlightCallback.EVENT.invoker().interact(context.getSource().getPlayer(), context.getSource().getWorld());
}
return 0;
}
public int SetFactionPower(ServerPlayerEntity caller, String faction, Integer amount) {
FactionManager.GetInstance().SetFactionPower(caller, faction, amount);
return 0;

View File

@ -10,8 +10,6 @@
package jesse.keeblarcraft.Commands;
import java.util.function.Supplier;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
@ -27,8 +25,6 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
public class ShortcutCommands {
float DEFAULT_FLIGHT_SPEED = 0.05f; // Minecraft operates on a 0.0 -> 1.0 scale; with each .01 making a difference in speed. 0.05 is creative flight speed
float SPEED_SCALAR = 20.0f; // For clamping speed down to half of its max output (So 0.5 will be max speed on a system that goes up in 0.05'ths)
public void RegisterShortcutCommands()
@ -130,21 +126,24 @@ public class ShortcutCommands {
ServerPlayerEntity player = context.getSource().getPlayer();
PlayerAbilities abilities = player.getAbilities();
if (abilities.flying && value == null) {
// Disable flight
if (abilities.flying && value == null) {
abilities.flying = false;
abilities.allowFlying = false;
abilities.setFlySpeed(0);
player.sendMessage(Text.of("Disabled flight"));
player.sendAbilitiesUpdate();
} else if (!abilities.flying && value == null) {
value = 1;
}
// Enable flight
if (value != null && value >= 1 && value <= 10) {
abilities.allowFlying = true;
abilities.setFlySpeed((float) (value / SPEED_SCALAR)); // Dividing by 20f yields max clamp value of 0.5 since MC does 0.0-> 1.0 flight. 0.1 flight is too fast!
player.sendAbilitiesUpdate();
player.sendMessage(Text.of("Flight speed set to " + value));
} else {
} else if (value != null) {
player.sendMessage(Text.literal("Only values from 1-10 are accepted"));
}
}

View File

@ -0,0 +1,24 @@
package jesse.keeblarcraft.FactionMgr.Callbacks;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;
public interface PlayerCommandFlightCallback {
Event<PlayerCommandFlightCallback> EVENT = EventFactory.createArrayBacked(PlayerCommandFlightCallback.class,
(listeners) -> (player, world) -> {
for (PlayerCommandFlightCallback listener : listeners) {
ActionResult result = listener.interact(player, world);
if (result != ActionResult.PASS) {
return result;
}
}
return ActionResult.PASS;
});
ActionResult interact(PlayerEntity player, World world);
}

View File

@ -3,7 +3,6 @@ package jesse.keeblarcraft.FactionMgr.Callbacks;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.entity.player.PlayerEntity;
// import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;