From d7c1f5297a7b5e21dfc911c199b0f9d950fff279 Mon Sep 17 00:00:00 2001 From: Ryan Fox Date: Sat, 29 Jan 2022 19:06:54 -0800 Subject: [PATCH] Bring back /fly command The persistentFlight option just kicks in flight whenever a player joins the server mid-air. Servers with this will have the /fly command enabled anyway, so it doesn't hurt. --- .../party/_2a03/mc/command/FlyCommand.java | 14 ++++---- .../party/_2a03/mc/mixin/MixinGameMode.java | 32 ------------------- .../_2a03/mc/mixin/MixinPlayerManager.java | 32 +++++++++++++++++++ .../mc/mixin/MixinServerPlayerEntity.java | 9 ------ .../minecraft-tweaks-2a03.mixins.json | 2 +- 5 files changed, 41 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/party/_2a03/mc/mixin/MixinGameMode.java create mode 100644 src/main/java/party/_2a03/mc/mixin/MixinPlayerManager.java diff --git a/src/main/java/party/_2a03/mc/command/FlyCommand.java b/src/main/java/party/_2a03/mc/command/FlyCommand.java index 5293165..6aca014 100644 --- a/src/main/java/party/_2a03/mc/command/FlyCommand.java +++ b/src/main/java/party/_2a03/mc/command/FlyCommand.java @@ -1,6 +1,7 @@ package party._2a03.mc.command; import com.mojang.brigadier.CommandDispatcher; +import net.minecraft.entity.player.PlayerAbilities; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.network.ServerPlayerEntity; @@ -11,21 +12,22 @@ public class FlyCommand { public static void register(CommandDispatcher dispatcher) { dispatcher.register(CommandManager.literal("fly").executes(ctx -> { ServerCommandSource source = ctx.getSource(); - //if (Config.getBool("disableFlyCommand")) { + if (Config.getBool("disableFlyCommand")) { source.sendFeedback(new LiteralText("The /fly command is disabled"), true); return 0; - /*} + } ServerPlayerEntity sender = source.getPlayer(); - boolean flight = sender.abilities.allowFlying; - sender.abilities.allowFlying = !flight; + PlayerAbilities abilities = sender.getAbilities(); + boolean flight = abilities.allowFlying; + abilities.allowFlying = !flight; if (!flight) source.sendFeedback(new LiteralText("I wanna fly like an eagle... to the sea!"), true); else { source.sendFeedback(new LiteralText("Flight disabled"), true); - sender.abilities.flying = false; + abilities.flying = false; } sender.sendAbilitiesUpdate(); - return 1;*/ + return 1; })); } } diff --git a/src/main/java/party/_2a03/mc/mixin/MixinGameMode.java b/src/main/java/party/_2a03/mc/mixin/MixinGameMode.java deleted file mode 100644 index d966c46..0000000 --- a/src/main/java/party/_2a03/mc/mixin/MixinGameMode.java +++ /dev/null @@ -1,32 +0,0 @@ -package party._2a03.mc.mixin; - -import net.minecraft.entity.player.PlayerAbilities; -import net.minecraft.world.GameMode; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import party._2a03.mc.util.Config; - -@Mixin(GameMode.class) -public class MixinGameMode { - private boolean flying; - - @Inject(method = "setAbilities", at = @At("HEAD")) - public void checkFlight(PlayerAbilities abilities, CallbackInfo ci) { - this.flying = abilities.flying; - } - - @Inject(method = "setAbilities", at = @At("TAIL")) - public void restoreFlight(PlayerAbilities abilities, CallbackInfo ci) { - if (!Config.getBool("persistentFlight")) - return; - if (abilities.flying) - return; - if (this.flying) { - abilities.allowFlying = true; - abilities.flying = true; - } - } -} diff --git a/src/main/java/party/_2a03/mc/mixin/MixinPlayerManager.java b/src/main/java/party/_2a03/mc/mixin/MixinPlayerManager.java new file mode 100644 index 0000000..521102f --- /dev/null +++ b/src/main/java/party/_2a03/mc/mixin/MixinPlayerManager.java @@ -0,0 +1,32 @@ +package party._2a03.mc.mixin; + +import net.minecraft.entity.player.PlayerAbilities; +import net.minecraft.server.PlayerManager; +import net.minecraft.network.ClientConnection; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.world.GameMode; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import party._2a03.mc.util.Config; + +@Mixin(PlayerManager.class) +public abstract class MixinPlayerManager { + @Inject(method = "onPlayerConnect", at = @At(value = "INVOKE", + target = "Lnet/minecraft/server/network/ServerPlayerEntity;getAbilities()Lnet/minecraft/entity/player/PlayerAbilities;")) + public void persistFlight(ClientConnection connection, ServerPlayerEntity player, CallbackInfo ci) { + if (!Config.getBool("persistentFlight")) + return; + if (player.isOnGround()) + return; + GameMode gameMode = player.interactionManager.getGameMode(); + if (gameMode == GameMode.CREATIVE || gameMode == GameMode.SPECTATOR) + return; + PlayerAbilities abilities = player.getAbilities(); + abilities.allowFlying = true; + abilities.flying = true; + } +} + diff --git a/src/main/java/party/_2a03/mc/mixin/MixinServerPlayerEntity.java b/src/main/java/party/_2a03/mc/mixin/MixinServerPlayerEntity.java index d6f5e81..78e2af9 100644 --- a/src/main/java/party/_2a03/mc/mixin/MixinServerPlayerEntity.java +++ b/src/main/java/party/_2a03/mc/mixin/MixinServerPlayerEntity.java @@ -3,7 +3,6 @@ package party._2a03.mc.mixin; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.registry.RegistryKey; -import net.minecraft.world.GameMode; import net.minecraft.world.World; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -30,12 +29,4 @@ public abstract class MixinServerPlayerEntity extends PlayerEntity { ci.cancel(); } } - - /*@Inject(method = "setGameMode", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerPlayerEntity;sendAbilitiesUpdate()V")) - public void disableFlightIfSafe(GameMode gameMode, CallbackInfo ci) { - if (gameMode != GameMode.CREATIVE && gameMode != GameMode.SPECTATOR) { - this.abilities.allowFlying = false; - this.abilities.flying = false; - } - }*/ } diff --git a/src/main/resources/minecraft-tweaks-2a03.mixins.json b/src/main/resources/minecraft-tweaks-2a03.mixins.json index e63dc04..47f6ee9 100644 --- a/src/main/resources/minecraft-tweaks-2a03.mixins.json +++ b/src/main/resources/minecraft-tweaks-2a03.mixins.json @@ -3,8 +3,8 @@ "package": "party._2a03.mc.mixin", "compatibilityLevel": "JAVA_17", "server": [ - "MixinGameMode", "MixinGameModeCommand", + "MixinPlayerManager", "MixinRespawnAnchorBlock", "MixinServerPlayerEntity", "MixinTntBlock",