generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 61
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
11 changed files
with
146 additions
and
8 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
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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/plusls/ommc/feature/autoSwitchElytra/AutoSwitchElytraUtil.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,62 @@ | ||
package com.plusls.ommc.feature.autoSwitchElytra; | ||
|
||
import com.plusls.ommc.ModInfo; | ||
import com.plusls.ommc.feature.sortInventory.SortInventoryUtil; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.client.network.ClientPlayerEntity; | ||
import net.minecraft.entity.effect.StatusEffects; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.screen.ScreenHandler; | ||
|
||
import java.util.ArrayList; | ||
import java.util.function.Predicate; | ||
|
||
public class AutoSwitchElytraUtil { | ||
public static final int CHEST_SLOT_IDX = 6; | ||
|
||
public static boolean myCheckFallFlying(PlayerEntity player) { | ||
if (!player.isOnGround() && !player.isFallFlying() && !player.isTouchingWater() && !player.hasStatusEffect(StatusEffects.LEVITATION)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static void autoSwitch(int sourceSlot, MinecraftClient client, ClientPlayerEntity clientPlayerEntity, Predicate<ItemStack> check) { | ||
if (client.interactionManager == null) { | ||
return; | ||
} | ||
ScreenHandler screenHandler = clientPlayerEntity.currentScreenHandler; | ||
ArrayList<Integer> clickQueue = new ArrayList<>(); | ||
ItemStack cursorStack = screenHandler.getCursorStack().copy(); | ||
ArrayList<ItemStack> itemStacks = new ArrayList<>(); | ||
int playerInventoryStartIdx = SortInventoryUtil.getPlayerInventoryStartIdx(screenHandler); | ||
for (int i = 0; i < screenHandler.slots.size(); ++i) { | ||
itemStacks.add(screenHandler.slots.get(i).getStack().copy()); | ||
} | ||
if (!cursorStack.isEmpty()) { | ||
// 把鼠标的物品放到玩家仓库中 | ||
clickQueue.addAll(SortInventoryUtil.addItemStack(itemStacks, cursorStack, playerInventoryStartIdx, screenHandler.slots.size())); | ||
} | ||
if (!cursorStack.isEmpty()) { | ||
// 放不下了就扔出去 | ||
clickQueue.add(ScreenHandler.EMPTY_SPACE_SLOT_INDEX); | ||
} | ||
int idxToSwitch = -1; | ||
for (int i = 0; i < itemStacks.size(); ++i) { | ||
if (check.test(itemStacks.get(i))) { | ||
idxToSwitch = i; | ||
break; | ||
} | ||
} | ||
boolean sourceSlotEmpty = itemStacks.get(sourceSlot).isEmpty(); | ||
if (idxToSwitch != -1) { | ||
clickQueue.add(idxToSwitch); | ||
clickQueue.add(sourceSlot); | ||
if (!sourceSlotEmpty) { | ||
clickQueue.add(idxToSwitch); | ||
} | ||
SortInventoryUtil.doClick(clientPlayerEntity, screenHandler.syncId, client.interactionManager, clickQueue); | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
src/main/java/com/plusls/ommc/mixin/feature/autoSwitchElytra/MixinClientPlayerEntity.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,53 @@ | ||
package com.plusls.ommc.mixin.feature.autoSwitchElytra; | ||
|
||
import com.mojang.authlib.GameProfile; | ||
import com.plusls.ommc.feature.autoSwitchElytra.AutoSwitchElytraUtil; | ||
import net.minecraft.client.MinecraftClient; | ||
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.item.ItemStack; | ||
import net.minecraft.item.Items; | ||
import net.minecraft.util.registry.Registry; | ||
import org.spongepowered.asm.mixin.Final; | ||
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; | ||
|
||
@Mixin(ClientPlayerEntity.class) | ||
public abstract class MixinClientPlayerEntity extends AbstractClientPlayerEntity { | ||
@Shadow | ||
@Final | ||
protected MinecraftClient client; | ||
|
||
boolean prevFallFlying = false; | ||
|
||
public MixinClientPlayerEntity(ClientWorld world, GameProfile profile) { | ||
super(world, profile); | ||
} | ||
|
||
@Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getEquippedStack(Lnet/minecraft/entity/EquipmentSlot;)Lnet/minecraft/item/ItemStack;", ordinal = 0)) | ||
private void autoSwitchElytra(CallbackInfo ci) { | ||
ItemStack chestItemStack = this.getEquippedStack(EquipmentSlot.CHEST); | ||
if (chestItemStack.isOf(Items.ELYTRA) || !AutoSwitchElytraUtil.myCheckFallFlying(this)) { | ||
return; | ||
} | ||
AutoSwitchElytraUtil.autoSwitch(AutoSwitchElytraUtil.CHEST_SLOT_IDX, this.client, (ClientPlayerEntity) (Object) this, itemStack -> itemStack.isOf(Items.ELYTRA)); | ||
} | ||
|
||
@Inject(method = "tickMovement", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isFallFlying()Z", ordinal = 0)) | ||
private void autoSwitchChest(CallbackInfo ci) { | ||
ItemStack chestItemStack = this.getEquippedStack(EquipmentSlot.CHEST); | ||
if (!chestItemStack.isOf(Items.ELYTRA) || !prevFallFlying || this.isFallFlying()) { | ||
prevFallFlying = this.isFallFlying(); | ||
return; | ||
} | ||
prevFallFlying = this.isFallFlying(); | ||
AutoSwitchElytraUtil.autoSwitch(AutoSwitchElytraUtil.CHEST_SLOT_IDX, this.client, (ClientPlayerEntity) (Object) this, itemStack -> Registry.ITEM.getId(itemStack.getItem()).toString().contains("_chestplate")); | ||
|
||
} | ||
|
||
} |
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
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