From d6967985b96f46ef95bd43c24b7180b873e2a6cc Mon Sep 17 00:00:00 2001 From: Hendrix-Shen Date: Tue, 30 Jul 2024 19:21:18 +0800 Subject: [PATCH] Safe access to the offer list (#118 #120) Signed-off-by: Hendrix-Shen --- .../api/fake/AbstractVillagerAccessor.java | 7 ++++ .../VillageTradeEnchantedBookInfo.java | 3 +- .../VillagerNextRestockTimeInfo.java | 3 +- .../mixin/MixinAbstractVillager.java | 39 +++++++++++++++++++ .../MixinAbstractVillager.java | 8 ---- .../MixinRenderUtils.java | 7 ++-- .../resources/masa_gadget_mod.mixins.json | 2 +- .../MixinAbstractVillager.java | 24 ------------ 8 files changed, 55 insertions(+), 38 deletions(-) create mode 100644 src/main/java/com/plusls/MasaGadget/api/fake/AbstractVillagerAccessor.java create mode 100644 src/main/java/com/plusls/MasaGadget/mixin/MixinAbstractVillager.java delete mode 100644 src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java delete mode 100644 versions/1.21.0-fabric/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java diff --git a/src/main/java/com/plusls/MasaGadget/api/fake/AbstractVillagerAccessor.java b/src/main/java/com/plusls/MasaGadget/api/fake/AbstractVillagerAccessor.java new file mode 100644 index 0000000..a49cfa7 --- /dev/null +++ b/src/main/java/com/plusls/MasaGadget/api/fake/AbstractVillagerAccessor.java @@ -0,0 +1,7 @@ +package com.plusls.MasaGadget.api.fake; + +import net.minecraft.world.item.trading.MerchantOffers; + +public interface AbstractVillagerAccessor { + MerchantOffers masa_gadget$safeGetOffers(); +} diff --git a/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillageTradeEnchantedBookInfo.java b/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillageTradeEnchantedBookInfo.java index 6f20e1b..de5e56f 100644 --- a/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillageTradeEnchantedBookInfo.java +++ b/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillageTradeEnchantedBookInfo.java @@ -1,6 +1,7 @@ package com.plusls.MasaGadget.impl.feature.entityInfo; import com.google.common.collect.Lists; +import com.plusls.MasaGadget.api.fake.AbstractVillagerAccessor; import com.plusls.MasaGadget.util.PcaSyncProtocol; import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; @@ -43,7 +44,7 @@ public class VillageTradeEnchantedBookInfo { List ret = Lists.newArrayList(); - for (MerchantOffer tradeOffer : villager.getOffers()) { + for (MerchantOffer tradeOffer : ((AbstractVillagerAccessor) (villager)).masa_gadget$safeGetOffers()) { ItemStack sellItem = tradeOffer.getResult(); ItemStackCompat sellItemCompat = ItemStackCompat.of(sellItem); diff --git a/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillagerNextRestockTimeInfo.java b/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillagerNextRestockTimeInfo.java index 4213bec..c519a24 100644 --- a/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillagerNextRestockTimeInfo.java +++ b/src/main/java/com/plusls/MasaGadget/impl/feature/entityInfo/VillagerNextRestockTimeInfo.java @@ -1,5 +1,6 @@ package com.plusls.MasaGadget.impl.feature.entityInfo; +import com.plusls.MasaGadget.api.fake.AbstractVillagerAccessor; import com.plusls.MasaGadget.mixin.accessor.AccessorVillager; import com.plusls.MasaGadget.util.PcaSyncProtocol; import net.minecraft.ChatFormatting; @@ -61,7 +62,7 @@ public class VillagerNextRestockTimeInfo { // 因为刁民的需要补货的函数,会检查当前货物是否被消耗,从使用的角度只需要关心当前货物是否用完 private static boolean needsRestock(@NotNull Villager villagerEntity) { if (villagerEntity.getVillagerData().getProfession() != VillagerProfession.NONE) { - for (MerchantOffer offer : villagerEntity.getOffers()) { + for (MerchantOffer offer : ((AbstractVillagerAccessor) (villagerEntity)).masa_gadget$safeGetOffers()) { if (offer.isOutOfStock()) { return true; } diff --git a/src/main/java/com/plusls/MasaGadget/mixin/MixinAbstractVillager.java b/src/main/java/com/plusls/MasaGadget/mixin/MixinAbstractVillager.java new file mode 100644 index 0000000..4f6d2d0 --- /dev/null +++ b/src/main/java/com/plusls/MasaGadget/mixin/MixinAbstractVillager.java @@ -0,0 +1,39 @@ +package com.plusls.MasaGadget.mixin; + +import com.plusls.MasaGadget.api.fake.AbstractVillagerAccessor; +import net.minecraft.world.entity.npc.AbstractVillager; +import net.minecraft.world.item.trading.MerchantOffers; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +//#if MC > 12006 +//$$ import org.jetbrains.annotations.Nullable; +//#endif + +@Mixin(AbstractVillager.class) +public abstract class MixinAbstractVillager implements AbstractVillagerAccessor { + //#if MC > 12006 + //$$ @Shadow + //$$ @Nullable + //$$ protected MerchantOffers offers; + //$$ @Shadow + //$$ protected abstract void updateTrades(); + //#else + @Shadow + public abstract MerchantOffers getOffers(); + //#endif + + @Override + public MerchantOffers masa_gadget$safeGetOffers() { + //#if MC > 12006 + //$$ if (this.offers == null) { + //$$ this.offers = new MerchantOffers(); + //$$ this.updateTrades(); + //$$ } + //$$ + //$$ return this.offers; + //#else + return this.getOffers(); + //#endif + } +} diff --git a/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java b/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java deleted file mode 100644 index 560a8cd..0000000 --- a/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.plusls.MasaGadget.mixin.mod_tweak.tweakeroo.inventoryPreviewSupportTradeOfferList; - -import org.spongepowered.asm.mixin.Mixin; -import top.hendrixshen.magiclib.api.preprocess.DummyClass; - -@Mixin(DummyClass.class) -public class MixinAbstractVillager { -} diff --git a/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinRenderUtils.java b/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinRenderUtils.java index 318840d..e132fb8 100644 --- a/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinRenderUtils.java +++ b/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinRenderUtils.java @@ -1,5 +1,6 @@ package com.plusls.MasaGadget.mixin.mod_tweak.tweakeroo.inventoryPreviewSupportTradeOfferList; +import com.plusls.MasaGadget.api.fake.AbstractVillagerAccessor; import com.plusls.MasaGadget.game.Configs; import com.plusls.MasaGadget.impl.generic.HitResultHandler; import com.plusls.MasaGadget.util.ModId; @@ -74,12 +75,12 @@ private static Container renderTradeOfferList(Container inv) { SimpleContainer simpleInventory = new SimpleContainer(MixinRenderUtils.masa_gadget$maxTradeOfferSize); - for (MerchantOffer tradeOffer : abstractVillager.getOffers()) { - for (int i = 0; i < simpleInventory.getContainerSize(); ++i) { + for (MerchantOffer tradeOffer : ((AbstractVillagerAccessor) (abstractVillager)).masa_gadget$safeGetOffers()) { + for (int i = 0; i < simpleInventory.getContainerSize(); i++) { ItemStack itemStack = simpleInventory.getItem(i); if (itemStack.isEmpty()) { - simpleInventory.setItem(i, tradeOffer.getResult().copy()); + simpleInventory.setItem(i, tradeOffer.getResult()); break; } } diff --git a/src/main/resources/masa_gadget_mod.mixins.json b/src/main/resources/masa_gadget_mod.mixins.json index 1a93152..d000b42 100644 --- a/src/main/resources/masa_gadget_mod.mixins.json +++ b/src/main/resources/masa_gadget_mod.mixins.json @@ -5,6 +5,7 @@ "compatibilityLevel": "JAVA_8", "plugin": "top.hendrixshen.magiclib.impl.mixin.MagicMixinPlugin", "client": [ + "MixinAbstractVillager", "MixinConnection", "accessor.AccessorAbstractMinecartContainer", "accessor.AccessorAbstractVillager", @@ -50,7 +51,6 @@ "mod_tweak.tweakeroo.inventoryPreviewSupportSelect.MixinInGameHud", "mod_tweak.tweakeroo.inventoryPreviewSupportSelect.MixinInventoryOverlay", "mod_tweak.tweakeroo.inventoryPreviewSupportShulkerBoxItemEntity.MixinRenderUtils", - "mod_tweak.tweakeroo.inventoryPreviewSupportTradeOfferList.MixinAbstractVillager", "mod_tweak.tweakeroo.inventoryPreviewSupportTradeOfferList.MixinRenderUtils", "mod_tweak.tweakeroo.inventoryPreviewSyncDataClientOnly.MixinClientPacketListener", "mod_tweak.tweakeroo.inventoryPreviewUseCache.MixinMixinRenderUtils" diff --git a/versions/1.21.0-fabric/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java b/versions/1.21.0-fabric/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java deleted file mode 100644 index a72545a..0000000 --- a/versions/1.21.0-fabric/src/main/java/com/plusls/MasaGadget/mixin/mod_tweak/tweakeroo/inventoryPreviewSupportTradeOfferList/MixinAbstractVillager.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.plusls.MasaGadget.mixin.mod_tweak.tweakeroo.inventoryPreviewSupportTradeOfferList; - -import com.llamalad7.mixinextras.injector.wrapoperation.Operation; -import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; -import com.plusls.MasaGadget.game.Configs; -import net.minecraft.world.entity.npc.AbstractVillager; -import net.minecraft.world.level.Level; -import org.jetbrains.annotations.NotNull; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; - -@Mixin(AbstractVillager.class) -public class MixinAbstractVillager { - @WrapOperation( - method = "getOffers", - at = @At( - value = "FIELD", - target = "Lnet/minecraft/world/level/Level;isClientSide:Z" - ) - ) - private boolean forgiveInvoke(Level instance, @NotNull Operation original) { - return original.call(instance) && !Configs.inventoryPreviewSupportTradeOfferList.getBooleanValue(); - } -}