From 143b480a45c1344033aa3c6232ea0f19cd17acde Mon Sep 17 00:00:00 2001 From: Ruben Taelman Date: Sun, 23 Jun 2024 09:41:33 +0200 Subject: [PATCH] Fix entities sometimes spawning in chest, Closes #163 --- .../org/cyclops/colossalchests/block/ChestWall.java | 12 ++++++++++++ .../cyclops/colossalchests/block/ColossalChest.java | 12 ++++++++++++ .../org/cyclops/colossalchests/block/Interface.java | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/main/java/org/cyclops/colossalchests/block/ChestWall.java b/src/main/java/org/cyclops/colossalchests/block/ChestWall.java index 87927e05..9fa93c65 100644 --- a/src/main/java/org/cyclops/colossalchests/block/ChestWall.java +++ b/src/main/java/org/cyclops/colossalchests/block/ChestWall.java @@ -6,6 +6,7 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.SpawnPlacements; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -24,6 +25,9 @@ import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.LivingSpawnEvent; +import net.minecraftforge.eventbus.api.Event; import org.cyclops.colossalchests.blockentity.BlockEntityColossalChest; import org.cyclops.cyclopscore.block.multi.CubeDetector; import org.cyclops.cyclopscore.helper.MinecraftHelpers; @@ -47,6 +51,14 @@ public ChestWall(Block.Properties properties, ChestMaterial material) { this.registerDefaultState(this.stateDefinition.any() .setValue(ENABLED, false)); + MinecraftForge.EVENT_BUS.addListener(this::onLivingSpawn); + } + + public void onLivingSpawn(LivingSpawnEvent.CheckSpawn event) { + // Only isValidSpawn is insufficient in some cases, so we add this forceful check as well. + if (event.getSpawnReason() != MobSpawnType.CHUNK_GENERATION && event.getEntity().getBlockStateOn().getBlock() == this) { + event.setResult(Event.Result.DENY); + } } @Override diff --git a/src/main/java/org/cyclops/colossalchests/block/ColossalChest.java b/src/main/java/org/cyclops/colossalchests/block/ColossalChest.java index f21675e1..1c891f91 100644 --- a/src/main/java/org/cyclops/colossalchests/block/ColossalChest.java +++ b/src/main/java/org/cyclops/colossalchests/block/ColossalChest.java @@ -18,6 +18,7 @@ import net.minecraft.world.MenuProvider; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.SpawnPlacements; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -41,6 +42,9 @@ import net.minecraft.world.level.material.FluidState; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.Vec3; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.LivingSpawnEvent; +import net.minecraftforge.eventbus.api.Event; import org.cyclops.colossalchests.Advancements; import org.cyclops.colossalchests.RegistryEntries; import org.cyclops.colossalchests.blockentity.BlockEntityColossalChest; @@ -75,6 +79,14 @@ public ColossalChest(Properties properties, ChestMaterial material) { this.registerDefaultState(this.stateDefinition.any() .setValue(ENABLED, false)); + MinecraftForge.EVENT_BUS.addListener(this::onLivingSpawn); + } + + public void onLivingSpawn(LivingSpawnEvent.CheckSpawn event) { + // Only isValidSpawn is insufficient in some cases, so we add this forceful check as well. + if (event.getSpawnReason() != MobSpawnType.CHUNK_GENERATION && event.getEntity().getBlockStateOn().getBlock() == this) { + event.setResult(Event.Result.DENY); + } } @org.jetbrains.annotations.Nullable diff --git a/src/main/java/org/cyclops/colossalchests/block/Interface.java b/src/main/java/org/cyclops/colossalchests/block/Interface.java index 8b1b154a..6ab9dfa9 100644 --- a/src/main/java/org/cyclops/colossalchests/block/Interface.java +++ b/src/main/java/org/cyclops/colossalchests/block/Interface.java @@ -6,6 +6,7 @@ import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.SpawnPlacements; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; @@ -24,6 +25,9 @@ import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.phys.BlockHitResult; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.living.LivingSpawnEvent; +import net.minecraftforge.eventbus.api.Event; import org.cyclops.colossalchests.blockentity.BlockEntityColossalChest; import org.cyclops.colossalchests.blockentity.BlockEntityInterface; import org.cyclops.cyclopscore.block.BlockWithEntity; @@ -50,6 +54,14 @@ public Interface(Block.Properties properties, ChestMaterial material) { this.registerDefaultState(this.stateDefinition.any() .setValue(ENABLED, false)); + MinecraftForge.EVENT_BUS.addListener(this::onLivingSpawn); + } + + public void onLivingSpawn(LivingSpawnEvent.CheckSpawn event) { + // Only isValidSpawn is insufficient in some cases, so we add this forceful check as well. + if (event.getSpawnReason() != MobSpawnType.CHUNK_GENERATION && event.getEntity().getBlockStateOn().getBlock() == this) { + event.setResult(Event.Result.DENY); + } } @Override