Skip to content

Commit

Permalink
Safe access to the offer list (#118 #120)
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Jul 30, 2024
1 parent 92b6034 commit d696798
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.plusls.MasaGadget.api.fake;

import net.minecraft.world.item.trading.MerchantOffers;

public interface AbstractVillagerAccessor {
MerchantOffers masa_gadget$safeGetOffers();
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class VillageTradeEnchantedBookInfo {

List<Component> 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/masa_gadget_mod.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"compatibilityLevel": "JAVA_8",
"plugin": "top.hendrixshen.magiclib.impl.mixin.MagicMixinPlugin",
"client": [
"MixinAbstractVillager",
"MixinConnection",
"accessor.AccessorAbstractMinecartContainer",
"accessor.AccessorAbstractVillager",
Expand Down Expand Up @@ -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"
Expand Down

This file was deleted.

0 comments on commit d696798

Please sign in to comment.