From 0f1a82214a947d1dce268df088d2bcd9c23c581e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E9=9B=A8=E8=90=BD?= Date: Sun, 15 Sep 2024 16:08:20 +0800 Subject: [PATCH] Fix world generating exceptions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋雨落 --- .../util/BlockStateListPopulatorMixin.java | 30 ++++++++++++++ .../util/DelegatedGeneratorAccessMixin.java | 40 +++++++++++++++++++ .../util/DummyGeneratorAccessMixin.java | 25 ++++++++++++ .../entity/npc/AbstractVillagerMixin.java | 4 +- .../resources/mixins.arclight.bukkit.json | 5 ++- 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/BlockStateListPopulatorMixin.java create mode 100644 arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DelegatedGeneratorAccessMixin.java create mode 100644 arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DummyGeneratorAccessMixin.java diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/BlockStateListPopulatorMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/BlockStateListPopulatorMixin.java new file mode 100644 index 000000000..125fee7fd --- /dev/null +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/BlockStateListPopulatorMixin.java @@ -0,0 +1,30 @@ +package io.izzel.arclight.common.mixin.bukkit.util; + +import io.izzel.arclight.common.bridge.core.world.IWorldWriterBridge; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.LevelAccessor; +import org.bukkit.craftbukkit.v.util.BlockStateListPopulator; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(BlockStateListPopulator.class) +public class BlockStateListPopulatorMixin implements IWorldWriterBridge { + @Shadow @Final private LevelAccessor world; + + @Override + public boolean bridge$addEntity(Entity entity, CreatureSpawnEvent.SpawnReason reason) { + return ((IWorldWriterBridge) world).bridge$addEntity(entity, reason); + } + + @Override + public void bridge$pushAddEntityReason(CreatureSpawnEvent.SpawnReason reason) { + ((IWorldWriterBridge) world).bridge$pushAddEntityReason(reason); + } + + @Override + public CreatureSpawnEvent.SpawnReason bridge$getAddEntityReason() { + return ((IWorldWriterBridge) world).bridge$getAddEntityReason(); + } +} diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DelegatedGeneratorAccessMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DelegatedGeneratorAccessMixin.java new file mode 100644 index 000000000..4aa1e5c96 --- /dev/null +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DelegatedGeneratorAccessMixin.java @@ -0,0 +1,40 @@ +package io.izzel.arclight.common.mixin.bukkit.util; + +import io.izzel.arclight.common.bridge.core.world.IWorldWriterBridge; +import io.izzel.arclight.common.bridge.core.world.WorldBridge; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.level.WorldGenLevel; +import org.bukkit.craftbukkit.v.util.DelegatedGeneratorAccess; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +@Mixin(DelegatedGeneratorAccess.class) +public abstract class DelegatedGeneratorAccessMixin implements IWorldWriterBridge { + @Shadow public abstract WorldGenLevel getHandle(); + + @Override + public boolean bridge$addEntity(Entity entity, CreatureSpawnEvent.SpawnReason reason) { + if (getHandle() != (Object) this) { + return ((IWorldWriterBridge) getHandle()).bridge$addEntity(entity, reason); + } else { + this.bridge$pushAddEntityReason(reason); + return getHandle().addFreshEntity(entity); + } + } + + @Override + public void bridge$pushAddEntityReason(CreatureSpawnEvent.SpawnReason reason) { + if (getHandle() != (Object) this) { + ((IWorldWriterBridge) getHandle()).bridge$pushAddEntityReason(reason); + } + } + + @Override + public CreatureSpawnEvent.SpawnReason bridge$getAddEntityReason() { + if (getHandle() != (Object) this) { + return ((IWorldWriterBridge) getHandle()).bridge$getAddEntityReason(); + } + return null; + } +} diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DummyGeneratorAccessMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DummyGeneratorAccessMixin.java new file mode 100644 index 000000000..049e8b4d0 --- /dev/null +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/bukkit/util/DummyGeneratorAccessMixin.java @@ -0,0 +1,25 @@ +package io.izzel.arclight.common.mixin.bukkit.util; + +import io.izzel.arclight.common.bridge.core.world.IWorldWriterBridge; +import net.minecraft.world.entity.Entity; +import org.bukkit.craftbukkit.v.util.DummyGeneratorAccess; +import org.bukkit.event.entity.CreatureSpawnEvent; +import org.spongepowered.asm.mixin.Mixin; + +@Mixin(DummyGeneratorAccess.class) +public class DummyGeneratorAccessMixin implements IWorldWriterBridge { + @Override + public boolean bridge$addEntity(Entity entity, CreatureSpawnEvent.SpawnReason reason) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void bridge$pushAddEntityReason(CreatureSpawnEvent.SpawnReason reason) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public CreatureSpawnEvent.SpawnReason bridge$getAddEntityReason() { + throw new UnsupportedOperationException("Not supported yet."); + } +} diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/entity/npc/AbstractVillagerMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/entity/npc/AbstractVillagerMixin.java index 7f3d4c93f..615ea5d2d 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/entity/npc/AbstractVillagerMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/entity/npc/AbstractVillagerMixin.java @@ -10,6 +10,8 @@ import net.minecraft.world.item.trading.MerchantOffers; import net.minecraft.world.level.Level; import org.bukkit.Bukkit; +import org.bukkit.craftbukkit.v.CraftServer; +import org.bukkit.craftbukkit.v.entity.CraftAbstractVillager; import org.bukkit.craftbukkit.v.inventory.CraftMerchant; import org.bukkit.craftbukkit.v.inventory.CraftMerchantRecipe; import org.bukkit.entity.AbstractVillager; @@ -37,7 +39,7 @@ public abstract class AbstractVillagerMixin extends PathfinderMobMixin implement @Override public CraftMerchant bridge$getCraftMerchant() { - return (craftMerchant == null) ? craftMerchant = new CraftMerchant((net.minecraft.world.entity.npc.AbstractVillager) (Object) this) : craftMerchant; + return (craftMerchant == null) ? craftMerchant = new CraftAbstractVillager(((CraftServer) Bukkit.getServer()), (net.minecraft.world.entity.npc.AbstractVillager) (Object) this) : craftMerchant; } @Redirect(method = "addOffersFromItemListings", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/trading/MerchantOffers;add(Ljava/lang/Object;)Z")) diff --git a/arclight-common/src/main/resources/mixins.arclight.bukkit.json b/arclight-common/src/main/resources/mixins.arclight.bukkit.json index de14e80a7..74326b422 100644 --- a/arclight-common/src/main/resources/mixins.arclight.bukkit.json +++ b/arclight-common/src/main/resources/mixins.arclight.bukkit.json @@ -43,6 +43,9 @@ "PotionTypeMixin", "Registry_SimpleRegistryMixin", "StandardMessengerMixin", - "WatchdogThreadMixin" + "WatchdogThreadMixin", + "util.BlockStateListPopulatorMixin", + "util.DelegatedGeneratorAccessMixin", + "util.DummyGeneratorAccessMixin" ] } \ No newline at end of file