Skip to content

Commit

Permalink
Tweak input handling, fix when menu is open
Browse files Browse the repository at this point in the history
  • Loading branch information
legobmw99 committed Jan 7, 2025
1 parent 56af0bc commit 0abf2e4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 43 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Allomancy 5.2.0

This mod adds the basics of [Allomancy](http://coppermind.net/wiki/Allomancy) from Brandon Sanderson's book series *Mistborn*.

This mod is currently updated for `Minecraft 1.21.4` and `NeoForge 21.4.47`
This mod is currently updated for `Minecraft 1.21.4` and `NeoForge 21.4.50`

Please verify and report any issues!

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ minecraft_version=1.21.4
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.21.4,1.22)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=21.4.47-beta
neo_version=21.4.50-beta
# The Neo version range can use any version of Neo as bounds
neo_version_range=[21.4.47-beta,)
neo_version_range=[21.4.50-beta,)
# The loader version range can only use the major version of FML as bounds
loader_version_range=[4,)
# read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import com.legobmw99.allomancy.modules.powers.client.gui.MetalSelectScreen;
import com.legobmw99.allomancy.modules.powers.client.network.PowerRequests;
import com.legobmw99.allomancy.modules.powers.data.AllomancerAttachment;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.client.player.ClientInput;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Input;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.ProjectileUtil;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -68,42 +72,6 @@ public static HitResult getMouseOverExtended(float dist) {

}

private static float calculateImpulse(boolean input, boolean otherInput) {
if (input == otherInput) {
return 0.0F;
} else {
return input ? 1.0F : -1.0F;
}
}

public static void fakeMovement(ClientInput input) {
// TODO not working
// // See similar code in https://github.com/gigaherz/ToolBelt/blob/master/src/main/java/dev/gigaherz/toolbelt/client/ToolBeltClient.java#L186
// Options options = Minecraft.getInstance().options;
// LocalPlayer player = Minecraft.getInstance().player;
// var window = Minecraft.getInstance().getWindow().getWindow();
//
// input.tick();
// // from KeyboardInput#tick
// input.keyPresses = new Input(options.keyUp.isDown(), options.keyDown.isDown(), options.keyLeft
// .isDown(),
// options.keyRight.isDown(), options.keyJump.isDown(), options
// .keyShift.isDown(),
// options.keySprint.isDown());
// input.forwardImpulse = calculateImpulse(input.keyPresses.forward(), input.keyPresses.backward());
// input.leftImpulse = calculateImpulse(input.keyPresses.left(), input.keyPresses.right());

// // from LocalPlayer#aiStep
// if (!player.isSprinting() && (!(player.isInWater() || player.isInFluidType(
// (fluidType, height) -> player.canSwimInFluidType(fluidType))) ||
// (player.isUnderWater() || player.canStartSwimming())) &&
// input.forwardImpulse >= 0.8 && !player.isUsingItem() &&
// (player.getFoodData().getFoodLevel() > 6.0F || player.mayFly()) &&
// !player.hasEffect(MobEffects.BLINDNESS) &&
// InputConstants.isKeyDown(window, options.keySprint.getKey().getValue())) {
// player.setSprinting(true);
// }
}

public static void registerKeyBinding(final RegisterKeyMappingsEvent evt) {
burn = new KeyMapping("key.burn", GLFW.GLFW_KEY_V, "key.categories.allomancy");
Expand Down Expand Up @@ -131,18 +99,18 @@ public static void acceptAllomancyKeybinds() {
return;
}

if (hud.isDown()) {
if (isKeyDown(hud)) {
PowersConfig.enable_overlay.set(!PowersConfig.enable_overlay.get());
return;
}
var data = player.getData(AllomancerAttachment.ALLOMANCY_DATA);

for (int i = 0; i < powers.length; i++) {
if (powers[i].isDown()) {
if (isKeyDown(powers[i])) {
PowerRequests.toggleBurn(Metal.getMetal(i), data);
}
}
if (burn.isDown()) {
if (isKeyDown(burn)) {
switch (data.getPowerCount()) {
case 0:
break;
Expand All @@ -155,4 +123,62 @@ public static void acceptAllomancyKeybinds() {
}
}
}


private static float calculateImpulse(boolean input, boolean otherInput) {
if (input == otherInput) {
return 0.0F;
} else {
return input ? 1.0F : -1.0F;
}
}

// See similar code in https://github.com/gigaherz/ToolBelt/blob/master/src/main/java/dev/gigaherz/toolbelt/client/ToolBeltClient.java#L186
private static boolean isKeyDown0(KeyMapping keybind) {
if (keybind.isUnbound()) {
return false;
}

return switch (keybind.getKey().getType()) {
case KEYSYM -> InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(),
keybind.getKey().getValue());
case MOUSE -> GLFW.glfwGetMouseButton(Minecraft.getInstance().getWindow().getWindow(),
keybind.getKey().getValue()) == GLFW.GLFW_PRESS;
default -> false;
};
}


private static boolean isKeyDown(KeyMapping keybind) {
return isKeyDown0(keybind) && keybind.getKeyConflictContext().isActive() &&
keybind.getKeyModifier().isActive(keybind.getKeyConflictContext());
}


public static void fakeMovement(ClientInput input) {
// basically KeyboardInput#tick()
Options settings = Minecraft.getInstance().options;
input.keyPresses =
new Input(isKeyDown0(settings.keyUp), isKeyDown0(settings.keyDown), isKeyDown0(settings.keyLeft),
isKeyDown0(settings.keyRight), isKeyDown0(settings.keyJump), isKeyDown0(settings.keyShift),
isKeyDown0(settings.keySprint));
input.forwardImpulse = calculateImpulse(input.keyPresses.forward(), input.keyPresses.backward());
input.leftImpulse = calculateImpulse(input.keyPresses.left(), input.keyPresses.right());

// See #LocalPlayer.aiStep()
var player = Minecraft.getInstance().player;
if (player.isMovingSlowly()) {
input.leftImpulse = (float) (input.leftImpulse * 0.3D);
input.forwardImpulse = (float) (input.forwardImpulse * 0.3D);
}

if (!player.isSprinting() && (!(player.isInWater() || player.isInFluidType(
(fluidType, height) -> player.canSwimInFluidType(fluidType))) ||
(player.isUnderWater() || player.canStartSwimming())) &&
input.forwardImpulse >= 0.8 && !player.isUsingItem() &&
(player.getFoodData().getFoodLevel() > 6.0F || player.mayFly()) &&
!player.hasEffect(MobEffects.BLINDNESS) && isKeyDown0(settings.keySprint)) {
player.setSprinting(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void tryPushPullEntity(EntityPushPullPayload payload, IPayloadCont
Physical.lurch(payload.force(), player, target.blockPosition());
} else if (target instanceof ItemEntity || target instanceof FallingBlockEntity ||
target instanceof ArmorStand ||
(target instanceof AbstractMinecart && !target.isVehicle())) {
(target instanceof AbstractMinecart && target.getControllingPassenger() != player)) {
Physical.lurch(payload.force() / 2.0, target, player.blockPosition());

// Split the difference
Expand Down

0 comments on commit 0abf2e4

Please sign in to comment.