Skip to content

Commit

Permalink
feat: auto switch elytra when falling (#4)
Browse files Browse the repository at this point in the history
* feat: tweakAutoSwitchElytra

* fix: reset autoSwitchElytraChestplate on falling
  • Loading branch information
zly2006 authored Jun 22, 2024
1 parent 99ae9da commit 7e4759b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum FeatureToggle implements IHotkeyTogglable, IConfigNotifiable<IConfig
TWEAK_AFTER_CLICKER ("tweakAfterClicker", false, "", KeybindSettings.INGAME_BOTH, "Enables a \"after clicker\" tweak, which does automatic right\nclicks on the just-placed block.\nUseful for example for Repeaters (setting the delay).\nTo quickly adjust the value, scroll while\nholding down the tweak toggle keybind."),
TWEAK_AIM_LOCK ("tweakAimLock", false, "", "Enables an aim lock, locking the yaw and pitch rotations\nto the current values.\nThis is separate from the snap aim lock,\nwhich locks them to the snapped value.\nThis allows locking them \"freely\" to the current value."),
TWEAK_ANGEL_BLOCK ("tweakAngelBlock", false, "", "Enables an \"Angel Block\" tweak, which allows\nplacing blocks in mid-air in Creative mode"),
TWEAK_AUTO_SWITCH_ELYTRA ("tweakAutoSwitchElytra", false, "", "Automatically switches to the Elytra when falling, and back to\nthe previous item when landing"),
TWEAK_BLOCK_REACH_OVERRIDE ("tweakBlockReachOverride", false, true, "", "Overrides the block reach distance with\nthe one set in Generic -> blockReachDistance"),
TWEAK_BLOCK_TYPE_BREAK_RESTRICTION("tweakBlockTypeBreakRestriction", false, "", "Restricts which blocks you are able to break (manually).\nSee the corresponding 'blockBreakRestriction*' configs in the Lists category."),
TWEAK_BREAKING_GRID ("tweakBreakingGrid", false, "", KeybindSettings.INGAME_BOTH, "When enabled, you can only break blocks in\na grid pattern, with a configurable interval.\nTo quickly adjust the interval, scroll while\nholding down the tweak toggle keybind."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package fi.dy.masa.tweakeroo.mixin;

import com.mojang.authlib.GameProfile;
import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.util.CameraEntity;
import fi.dy.masa.tweakeroo.util.CameraUtils;
import fi.dy.masa.tweakeroo.util.DummyMovementInput;
import fi.dy.masa.tweakeroo.util.InventoryUtils;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.input.Input;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Hand;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -11,20 +27,6 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.input.Input;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.util.Hand;

import fi.dy.masa.tweakeroo.config.Configs;
import fi.dy.masa.tweakeroo.config.FeatureToggle;
import fi.dy.masa.tweakeroo.util.CameraEntity;
import fi.dy.masa.tweakeroo.util.CameraUtils;
import fi.dy.masa.tweakeroo.util.DummyMovementInput;

@Mixin(ClientPlayerEntity.class)
public abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity
{
Expand All @@ -36,6 +38,7 @@ public abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity
private final DummyMovementInput dummyMovementInput = new DummyMovementInput(null);
private Input realInput;
private float realNauseaIntensity;
private ItemStack autoSwitchElytraChestplate = ItemStack.EMPTY;

private MixinClientPlayerEntity(ClientWorld world, GameProfile profile)
{
Expand Down Expand Up @@ -117,6 +120,45 @@ private void disableDoubleTapSprint(CallbackInfo ci)
}
}

@Inject(method = "tickMovement", at = @At(value = "INVOKE", shift = At.Shift.BEFORE, target = "Lnet/minecraft/client/network/ClientPlayerEntity;getEquippedStack(Lnet/minecraft/entity/EquipmentSlot;)Lnet/minecraft/item/ItemStack;"))
private void onFallFlyingCheckChestSlot(CallbackInfo ci)
{
if (FeatureToggle.TWEAK_AUTO_SWITCH_ELYTRA.getBooleanValue())
{
// auto switch if it is not elytra or is totally broken.
if (getEquippedStack(EquipmentSlot.CHEST).isOf(Items.ELYTRA) == false
|| getEquippedStack(EquipmentSlot.CHEST).getDamage() > getEquippedStack(EquipmentSlot.CHEST).getMaxDamage() - 10)
{
autoSwitchElytraChestplate = getEquippedStack(EquipmentSlot.CHEST).copy();
InventoryUtils.swapElytraWithChestPlate(this);
}
}
else {
// reset auto switch item if the feature is disabled.
autoSwitchElytraChestplate = ItemStack.EMPTY;
}
}

@Inject(method = "tickMovement", at = @At("RETURN"))
private void onMovementEnd(CallbackInfo ci) {
if (FeatureToggle.TWEAK_AUTO_SWITCH_ELYTRA.getBooleanValue())
{
if (!autoSwitchElytraChestplate.isEmpty() && !isFallFlying() && getEquippedStack(EquipmentSlot.CHEST).isOf(Items.ELYTRA))
{
if (playerScreenHandler.getCursorStack().isEmpty())
{
int targetSlot = InventoryUtils.findSlotWithItem(playerScreenHandler, autoSwitchElytraChestplate, true, false);

if (targetSlot >= 0)
{
InventoryUtils.swapItemToEquipmentSlot(this, EquipmentSlot.CHEST, targetSlot);
autoSwitchElytraChestplate = ItemStack.EMPTY;
}
}
}
}
}

@Inject(method = "tick", at = @At("HEAD"))
private void disableMovementInputsPre(CallbackInfo ci)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ else if (hand == Hand.OFF_HAND)
}
}

private static void swapItemToEquipmentSlot(PlayerEntity player, EquipmentSlot type, int sourceSlotNumber)
public static void swapItemToEquipmentSlot(PlayerEntity player, EquipmentSlot type, int sourceSlotNumber)
{
if (sourceSlotNumber != -1 && player.currentScreenHandler == player.playerScreenHandler)
{
Expand Down

0 comments on commit 7e4759b

Please sign in to comment.