Skip to content

Commit

Permalink
Merge pull request #57 from cdqtzrc/fabric/1.21
Browse files Browse the repository at this point in the history
修复两个bug
  • Loading branch information
Gu-ZT authored Jun 24, 2024
2 parents 77ef9ba + 5ed3ce9 commit cbb3e1f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Button {
private final ItemStack onItem;
private final ItemStack offItem;
CompoundTag compoundTag = new CompoundTag();
public static final String GCA_CLEAR = "GcaClear";

private final List<Consumer> turnOnConsumers = new ArrayList<>();

Expand Down Expand Up @@ -69,7 +70,7 @@ public Button(boolean defaultState, Item onItem, Item offItem, int itemCount) {

public Button(boolean defaultState, Item onItem, Item offItem, int itemCount, Component onText, Component offText) {
this.flag = defaultState;
this.compoundTag.putBoolean("GcaClear", true);
this.compoundTag.putBoolean(GCA_CLEAR, true);

ItemStack onItemStack = new ItemStack(onItem, itemCount);
onItemStack.set(DataComponents.CUSTOM_DATA, CustomData.of(compoundTag));
Expand All @@ -84,7 +85,7 @@ public Button(boolean defaultState, Item onItem, Item offItem, int itemCount, Co

public Button(boolean defaultState, @NotNull ItemStack onItem, @NotNull ItemStack offItem) {
this.flag = defaultState;
this.compoundTag.putBoolean("GcaClear", true);
this.compoundTag.putBoolean(GCA_CLEAR, true);

ItemStack onItemStack = onItem.copy();
onItemStack.set(DataComponents.CUSTOM_DATA, CustomData.of(compoundTag.copy()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.dubhe.gugle.carpet.mixin;

import dev.dubhe.gugle.carpet.api.menu.control.Button;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand All @@ -26,10 +27,10 @@ private void doClick(int slotIndex, int button, ClickType clickType, Player play
Slot slot = gca$self.getSlot(slotIndex);
ItemStack itemStack = slot.getItem();
CustomData customData = itemStack.get(DataComponents.CUSTOM_DATA);
if (customData == null || customData.copyTag().get("GcaClear") == null) {
if (customData == null || customData.copyTag().get(Button.GCA_CLEAR) == null) {
return;
}
if (customData.copyTag().getBoolean("GcaClear")) {
if (customData.copyTag().getBoolean(Button.GCA_CLEAR)) {
itemStack.setCount(0);
ci.cancel();
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/dev/dubhe/gugle/carpet/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@

import carpet.patches.EntityPlayerMPFake;
import dev.dubhe.gugle.carpet.GcaSetting;
import dev.dubhe.gugle.carpet.api.menu.control.Button;
import dev.dubhe.gugle.carpet.tools.FakePlayerAutoReplaceTool;
import dev.dubhe.gugle.carpet.tools.FakePlayerAutoReplenishment;
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.component.PatchedDataComponentMap;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.CustomData;
import net.minecraft.world.level.Level;
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;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(ItemStack.class)
abstract class ItemStackMixin {
@Shadow
public abstract Item getItem();

@Shadow
@Final
PatchedDataComponentMap components;

@Inject(method = "use", at = @At("HEAD"))
private void use(Level level, Player player, InteractionHand usedHand, CallbackInfoReturnable<InteractionResultHolder<ItemStack>> cir) {
if (GcaSetting.fakePlayerAutoReplenishment && player instanceof EntityPlayerMPFake fakePlayer) {
Expand All @@ -32,5 +47,14 @@ private void hurtAndBreak(int i, LivingEntity livingEntity, EquipmentSlot equipm
FakePlayerAutoReplaceTool.autoReplaceTool(fakePlayer);
}
}

@Inject(method = "getComponents", at = @At("HEAD"), cancellable = true)
private void getComponents(CallbackInfoReturnable<DataComponentMap> cir) {
CustomData customData = this.components.get(DataComponents.CUSTOM_DATA);
if (customData == null || customData.copyTag().get(Button.GCA_CLEAR) == null) {
return;
}
cir.setReturnValue(this.components);
}
}

26 changes: 21 additions & 5 deletions src/main/java/dev/dubhe/gugle/carpet/mixin/PlayerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.dubhe.gugle.carpet.tools.FakePlayerEnderChestContainer;
import dev.dubhe.gugle.carpet.tools.FakePlayerInventoryContainer;
import dev.dubhe.gugle.carpet.tools.FakePlayerInventoryMenu;
import net.minecraft.client.player.RemotePlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleMenuProvider;
Expand Down Expand Up @@ -40,11 +41,22 @@ private void tick(CallbackInfo ci) {

@WrapOperation(method = "interactOn", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;interact(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;"))
private InteractionResult interactOn(Entity entity, Player player, InteractionHand hand, Operation<InteractionResult> original) {
if (!(entity instanceof EntityPlayerMPFake fakePlayer)) {
return original.call(entity, player, hand);
if (entity instanceof EntityPlayerMPFake fakePlayer) {
// 打开物品栏
return this.openInventory(player, fakePlayer);
} else if (entity instanceof RemotePlayer) {
// 在客户端中,玩家可以与客户端的被交互玩家交互并返回PASS,这时交互玩家手上如果拿着可以使用的物品,则物品会被使用
// 所以如果判断被交互实体是客户端玩家,返回SUCCESS
return InteractionResult.SUCCESS;
}
return original.call(entity, player, hand);
}

@Unique
private InteractionResult openInventory(Player player, EntityPlayerMPFake fakePlayer) {
SimpleMenuProvider provider = null;
if (player.isShiftKeyDown()) {
// 打开末影箱
if (GcaSetting.openFakePlayerEnderChest) {
provider = new SimpleMenuProvider(
(i, inventory, p) -> ChestMenu.sixRows(
Expand All @@ -54,6 +66,7 @@ private InteractionResult interactOn(Entity entity, Player player, InteractionHa
ComponentTranslate.trans("gca.player.ender_chest", fakePlayer.getDisplayName())
);
} else {
// 打开额外功能菜单
provider = new SimpleMenuProvider(
(i, inventory, p) -> ChestMenu.threeRows(
i, inventory,
Expand All @@ -63,6 +76,7 @@ private InteractionResult interactOn(Entity entity, Player player, InteractionHa
);
}
} else if (GcaSetting.openFakePlayerInventory) {
// 打开物品栏
provider = new SimpleMenuProvider(
(i, inventory, p) -> new FakePlayerInventoryMenu(
i, inventory,
Expand All @@ -71,9 +85,11 @@ private InteractionResult interactOn(Entity entity, Player player, InteractionHa
ComponentTranslate.trans("gca.player.inventory", fakePlayer.getDisplayName())
);
}
if (provider != null) {
player.openMenu(provider);

if (provider == null) {
return InteractionResult.PASS;
}
return InteractionResult.SUCCESS;
player.openMenu(provider);
return InteractionResult.CONSUME;
}
}

0 comments on commit cbb3e1f

Please sign in to comment.