From df8ea44299f5241a94b8ecf750e6b5bb3bc65930 Mon Sep 17 00:00:00 2001 From: IzzelAliz Date: Sun, 24 Mar 2024 19:03:30 +0800 Subject: [PATCH] Fix mixin conflict with architectury api (#1282) --- .../core/world/spawner/BaseSpawnerBridge.java | 2 +- .../mixin/core/world/spawner/BaseSpawnerMixin.java | 6 +++--- .../mod/server/entity/EntityClassLookup.java | 11 ++++------- .../core/world/level/BaseSpawnerMixin_Forge.java | 14 +++++++++++--- .../world/level/BaseSpawnerMixin_NeoForge.java | 12 ++++++++++-- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/bridge/core/world/spawner/BaseSpawnerBridge.java b/arclight-common/src/main/java/io/izzel/arclight/common/bridge/core/world/spawner/BaseSpawnerBridge.java index c9ecbf0b5..c397280fd 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/bridge/core/world/spawner/BaseSpawnerBridge.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/bridge/core/world/spawner/BaseSpawnerBridge.java @@ -11,7 +11,7 @@ public interface BaseSpawnerBridge { - boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData); + boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData, boolean result); void bridge$forge$finalizeSpawnerSpawn(Mob mob, ServerLevelAccessor level, DifficultyInstance difficulty, @Nullable SpawnGroupData spawnData, @Nullable CompoundTag spawnTag); } diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/spawner/BaseSpawnerMixin.java b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/spawner/BaseSpawnerMixin.java index 167573ad0..e3fa81874 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/spawner/BaseSpawnerMixin.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mixin/core/world/spawner/BaseSpawnerMixin.java @@ -121,7 +121,7 @@ public void serverTick(ServerLevel level, BlockPos pos) { entity.moveTo(entity.getX(), entity.getY(), entity.getZ(), level.random.nextFloat() * 360.0F, 0.0F); if (entity instanceof Mob mob) { - if (this.bridge$forge$checkSpawnRules(mob, level, MobSpawnType.SPAWNER, spawnData)) { + if (this.bridge$forge$checkSpawnRules(mob, level, MobSpawnType.SPAWNER, spawnData, spawnData.getCustomSpawnRules().isEmpty() && !mob.checkSpawnRules(level, MobSpawnType.SPAWNER) || !mob.checkSpawnObstruction(level))) { continue; } @@ -169,8 +169,8 @@ public void serverTick(ServerLevel level, BlockPos pos) { } @Override - public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData) { - return spawnData.getCustomSpawnRules().isEmpty() && !mob.checkSpawnRules(level, MobSpawnType.SPAWNER) || !mob.checkSpawnObstruction(level); + public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData, boolean result) { + return result; } @Override diff --git a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/entity/EntityClassLookup.java b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/entity/EntityClassLookup.java index 2a1de4aa7..35cb31211 100644 --- a/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/entity/EntityClassLookup.java +++ b/arclight-common/src/main/java/io/izzel/arclight/common/mod/server/entity/EntityClassLookup.java @@ -1,6 +1,5 @@ package io.izzel.arclight.common.mod.server.entity; -import io.izzel.arclight.api.Unsafe; import io.izzel.arclight.common.bridge.bukkit.EntityTypeBridge; import io.izzel.arclight.common.bridge.core.entity.projectile.DamagingProjectileEntityBridge; import io.izzel.arclight.common.mod.server.ArclightServer; @@ -66,6 +65,7 @@ import net.minecraft.world.entity.animal.horse.TraderLlama; import net.minecraft.world.entity.animal.horse.ZombieHorse; import net.minecraft.world.entity.animal.sniffer.Sniffer; +import net.minecraft.world.entity.boss.EnderDragonPart; import net.minecraft.world.entity.boss.enderdragon.EndCrystal; import net.minecraft.world.entity.boss.enderdragon.EnderDragon; import net.minecraft.world.entity.boss.wither.WitherBoss; @@ -219,14 +219,10 @@ private static Function } public static void init() { - Unsafe.ensureClassInitialized(CraftEntityTypes.class); - Map> entityTypeData = Unsafe.getStatic(CraftEntityTypes.class, "ENTITY_TYPE_DATA"); - var allEntityClasses = new HashSet>(); - for (var entry : entityTypeData.entrySet()) { - var bukkitType = entry.getKey(); + for (var bukkitType : org.bukkit.entity.EntityType.values()) { Class entityClass = bukkitType.getEntityClass(); - if (!allEntityClasses.contains(entityClass)) { + if (entityClass != null && !allEntityClasses.contains(entityClass)) { var next = new LinkedList>(); next.add(entityClass); while (!next.isEmpty()) { @@ -367,6 +363,7 @@ private static Class forName(String name) { add(AbstractGolem.class, new EntityClass<>(org.bukkit.entity.Golem.class, org.bukkit.craftbukkit.v.entity.CraftGolem.class, org.bukkit.craftbukkit.v.entity.CraftGolem::new)); add(Player.class, new EntityClass<>(org.bukkit.entity.HumanEntity.class, org.bukkit.craftbukkit.v.entity.CraftHumanEntity.class, org.bukkit.craftbukkit.v.entity.CraftHumanEntity::new)); add(AbstractFish.class, new EntityClass<>(org.bukkit.entity.Fish.class, org.bukkit.craftbukkit.v.entity.CraftFish.class, org.bukkit.craftbukkit.v.entity.CraftFish::new)); + add(EnderDragonPart.class, new EntityClass<>(org.bukkit.entity.EnderDragonPart.class, org.bukkit.craftbukkit.v.entity.CraftEnderDragonPart.class, org.bukkit.craftbukkit.v.entity.CraftEnderDragonPart::new)); // vanilla mob types add(ElderGuardian.class, new EntityClass<>(org.bukkit.entity.ElderGuardian.class, org.bukkit.craftbukkit.v.entity.CraftElderGuardian.class, org.bukkit.craftbukkit.v.entity.CraftElderGuardian::new)); diff --git a/arclight-forge/src/main/java/io/izzel/arclight/forge/mixin/core/world/level/BaseSpawnerMixin_Forge.java b/arclight-forge/src/main/java/io/izzel/arclight/forge/mixin/core/world/level/BaseSpawnerMixin_Forge.java index 671e93760..53af98658 100644 --- a/arclight-forge/src/main/java/io/izzel/arclight/forge/mixin/core/world/level/BaseSpawnerMixin_Forge.java +++ b/arclight-forge/src/main/java/io/izzel/arclight/forge/mixin/core/world/level/BaseSpawnerMixin_Forge.java @@ -9,7 +9,9 @@ import net.minecraft.world.level.BaseSpawner; import net.minecraft.world.level.ServerLevelAccessor; import net.minecraft.world.level.SpawnData; -import net.minecraftforge.event.ForgeEventFactory; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.MobSpawnEvent; +import net.minecraftforge.eventbus.api.Event; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; @@ -17,8 +19,14 @@ public class BaseSpawnerMixin_Forge implements BaseSpawnerBridge { @Override - public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData) { - return !ForgeEventFactory.checkSpawnPositionSpawner(mob, level, MobSpawnType.SPAWNER, spawnData, (BaseSpawner) (Object) this); + public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData, boolean original) { + var spawnEvent = new MobSpawnEvent.PositionCheck(mob, level, spawnType, null); + MinecraftForge.EVENT_BUS.post(spawnEvent); + var result = spawnEvent.getResult(); + if (result == Event.Result.DEFAULT) { + return original; + } + return result == Event.Result.ALLOW; } @Override diff --git a/arclight-neoforge/src/main/java/io/izzel/arclight/neoforge/mixin/core/world/level/BaseSpawnerMixin_NeoForge.java b/arclight-neoforge/src/main/java/io/izzel/arclight/neoforge/mixin/core/world/level/BaseSpawnerMixin_NeoForge.java index 9887ae1c0..95c5dec0a 100644 --- a/arclight-neoforge/src/main/java/io/izzel/arclight/neoforge/mixin/core/world/level/BaseSpawnerMixin_NeoForge.java +++ b/arclight-neoforge/src/main/java/io/izzel/arclight/neoforge/mixin/core/world/level/BaseSpawnerMixin_NeoForge.java @@ -9,7 +9,10 @@ import net.minecraft.world.level.BaseSpawner; import net.minecraft.world.level.ServerLevelAccessor; import net.minecraft.world.level.SpawnData; +import net.neoforged.bus.api.Event; +import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.EventHooks; +import net.neoforged.neoforge.event.entity.living.MobSpawnEvent; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; @@ -17,8 +20,13 @@ public abstract class BaseSpawnerMixin_NeoForge implements BaseSpawnerBridge { @Override - public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData) { - return !EventHooks.checkSpawnPositionSpawner(mob, level, MobSpawnType.SPAWNER, spawnData, (BaseSpawner) (Object) this); + public boolean bridge$forge$checkSpawnRules(Mob mob, ServerLevelAccessor level, MobSpawnType spawnType, SpawnData spawnData, boolean original) { + var event = new MobSpawnEvent.PositionCheck(mob, level, spawnType, null); + NeoForge.EVENT_BUS.post(event); + if (event.getResult() == Event.Result.DEFAULT) { + return original; + } + return event.getResult() == Event.Result.ALLOW; } @Override