-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
common/src/main/java/sh/okx/civmodern/common/macro/ToggleSneakMacro.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package sh.okx.civmodern.common.macro; | ||
|
||
import com.google.common.eventbus.Subscribe; | ||
import com.mojang.blaze3d.platform.InputConstants; | ||
import java.util.Objects; | ||
import net.minecraft.client.KeyMapping; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.OptionInstance; | ||
import net.minecraft.client.Options; | ||
import net.minecraft.client.player.LocalPlayer; | ||
import org.jetbrains.annotations.NotNull; | ||
import sh.okx.civmodern.common.events.ClientTickEvent; | ||
import sh.okx.civmodern.common.mixins.KeyMappingAccessor; | ||
|
||
public class ToggleSneakMacro { | ||
private final KeyMapping macroBinding; | ||
private final KeyMapping sneakBinding; | ||
private final KeyMappingAccessor sneakBindingAccessor; | ||
private final OptionInstance<Boolean> toggleSneak; | ||
|
||
public ToggleSneakMacro( | ||
final @NotNull KeyMapping macroBinding | ||
) { | ||
this.macroBinding = Objects.requireNonNull(macroBinding); | ||
final Options options = Minecraft.getInstance().options; | ||
this.sneakBinding = options.keyShift; | ||
this.sneakBindingAccessor = (KeyMappingAccessor) this.sneakBinding; | ||
this.toggleSneak = options.toggleCrouch(); | ||
} | ||
|
||
@Subscribe | ||
private void onTick( | ||
final @NotNull ClientTickEvent event | ||
) { | ||
final LocalPlayer player = Minecraft.getInstance().player; | ||
if (player == null) { | ||
return; | ||
} | ||
|
||
final boolean isSneaking = this.sneakBinding.isDown(); | ||
|
||
while (this.macroBinding.consumeClick()) { | ||
this.toggleSneak.set(false); | ||
if (isSneaking) { | ||
this.sneakBinding.setDown(false); | ||
continue; | ||
} | ||
this.sneakBinding.setDown(true); | ||
this.toggleSneak.set(true); | ||
} | ||
|
||
while (this.sneakBinding.consumeClick()) { | ||
this.toggleSneak.set(false); | ||
} | ||
|
||
if ( | ||
player.isInWater() | ||
|| player.isSwimming() | ||
|| player.isSprinting() | ||
|| player.isFallFlying() // Elytra gliding | ||
|| player.getAbilities().flying // Creative flying | ||
) { | ||
this.toggleSneak.set(false); | ||
this.sneakBinding.setDown(InputConstants.isKeyDown( | ||
Minecraft.getInstance().getWindow().getWindow(), | ||
this.sneakBindingAccessor.getKey().getValue() | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters