diff --git a/src/main/java/fi/dy/masa/tweakeroo/config/FeatureToggle.java b/src/main/java/fi/dy/masa/tweakeroo/config/FeatureToggle.java index 3723761c6..64b4e5ddb 100644 --- a/src/main/java/fi/dy/masa/tweakeroo/config/FeatureToggle.java +++ b/src/main/java/fi/dy/masa/tweakeroo/config/FeatureToggle.java @@ -22,6 +22,7 @@ public enum FeatureToggle implements IHotkeyTogglable, IConfigNotifiable 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."), diff --git a/src/main/java/fi/dy/masa/tweakeroo/mixin/MixinClientPlayerEntity.java b/src/main/java/fi/dy/masa/tweakeroo/mixin/MixinClientPlayerEntity.java index 7bdfa8a70..faebb4167 100644 --- a/src/main/java/fi/dy/masa/tweakeroo/mixin/MixinClientPlayerEntity.java +++ b/src/main/java/fi/dy/masa/tweakeroo/mixin/MixinClientPlayerEntity.java @@ -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; @@ -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 { @@ -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) { @@ -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) { diff --git a/src/main/java/fi/dy/masa/tweakeroo/util/InventoryUtils.java b/src/main/java/fi/dy/masa/tweakeroo/util/InventoryUtils.java index 0ca435336..8ce0e4358 100644 --- a/src/main/java/fi/dy/masa/tweakeroo/util/InventoryUtils.java +++ b/src/main/java/fi/dy/masa/tweakeroo/util/InventoryUtils.java @@ -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) {