diff --git a/changelog.md b/changelog.md index f57191832c..6245b63b6c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,7 @@ # 7.5.18 * Updated contributor book +* Fixed ReaperSpawner eating all your CPU # 7.5.17 diff --git a/common/src/main/java/net/mca/mixin/MixinFlintAndSteelItem.java b/common/src/main/java/net/mca/mixin/MixinFlintAndSteelItem.java new file mode 100644 index 0000000000..ea5a1dbd50 --- /dev/null +++ b/common/src/main/java/net/mca/mixin/MixinFlintAndSteelItem.java @@ -0,0 +1,23 @@ +package net.mca.mixin; + +import net.mca.server.world.data.VillageManager; +import net.minecraft.item.FlintAndSteelItem; +import net.minecraft.item.ItemUsageContext; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.util.ActionResult; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(FlintAndSteelItem.class) +public class MixinFlintAndSteelItem { + @Inject(method = "useOnBlock(Lnet/minecraft/item/ItemUsageContext;)Lnet/minecraft/util/ActionResult;", at = @At("RETURN")) + private void mca$onUseOnBlock(ItemUsageContext context, CallbackInfoReturnable cir) { + if (cir.getReturnValue().isAccepted() && context.getWorld() instanceof ServerWorld serverWorld) { + serverWorld.getServer().execute(() -> + VillageManager.get(serverWorld).getReaperSpawner().trySpawnReaper(serverWorld, context.getBlockPos()) + ); + } + } +} diff --git a/common/src/main/java/net/mca/mixin/MixinServerWorld.java b/common/src/main/java/net/mca/mixin/MixinServerWorld.java index c66916a5d4..596324bd6c 100644 --- a/common/src/main/java/net/mca/mixin/MixinServerWorld.java +++ b/common/src/main/java/net/mca/mixin/MixinServerWorld.java @@ -1,11 +1,8 @@ package net.mca.mixin; import net.mca.server.SpawnQueue; -import net.mca.server.world.data.VillageManager; -import net.minecraft.block.BlockState; import net.minecraft.entity.Entity; import net.minecraft.server.world.ServerWorld; -import net.minecraft.util.math.BlockPos; import net.minecraft.world.StructureWorldAccess; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; @@ -29,17 +26,6 @@ private void onAddEntity(Entity entity, CallbackInfoReturnable info) { info.setReturnValue(false); } } - @Inject(method = "onBlockChanged(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;Lnet/minecraft/block/BlockState;)V", - at = @At("HEAD") - ) - public void onOnBlockChanged(BlockPos pos, BlockState oldBlock, BlockState newBlock, CallbackInfo info) { - if (oldBlock.getBlock() != newBlock.getBlock()) { - final ServerWorld self = (ServerWorld)(Object)this; - self.getServer().execute(() -> - VillageManager.get(self).getReaperSpawner().trySpawnReaper(self, newBlock, pos) - ); - } - } } @Mixin(ProtoChunk.class) diff --git a/common/src/main/java/net/mca/server/ReaperSpawner.java b/common/src/main/java/net/mca/server/ReaperSpawner.java index 72fb56f2c2..9a37eb8be4 100644 --- a/common/src/main/java/net/mca/server/ReaperSpawner.java +++ b/common/src/main/java/net/mca/server/ReaperSpawner.java @@ -9,7 +9,6 @@ import net.mca.server.world.data.VillageManager; import net.mca.util.WorldUtils; import net.minecraft.block.Block; -import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.EntityType; import net.minecraft.entity.SpawnReason; @@ -17,15 +16,12 @@ import net.minecraft.nbt.NbtElement; import net.minecraft.nbt.NbtHelper; import net.minecraft.registry.tag.BlockTags; -import net.minecraft.server.world.ChunkTicketType; -import net.minecraft.server.world.ServerChunkManager; import net.minecraft.server.world.ServerWorld; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.ChunkPos; import net.minecraft.util.math.Direction; -import net.minecraft.util.math.Vec3i; import net.minecraft.world.World; import java.util.*; @@ -33,8 +29,6 @@ import java.util.stream.Stream; public class ReaperSpawner { - public static final ChunkTicketType REAPER = ChunkTicketType.create("mca:reaper", Vec3i::compareTo, 100); - private static final Direction[] HORIZONTALS = new Direction[]{ Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST }; @@ -62,25 +56,19 @@ private void warn(World world, BlockPos pos, String phrase) { .ifPresent(p -> p.sendMessage(Text.translatable(phrase).formatted(Formatting.RED), true)); } - public void trySpawnReaper(ServerWorld world, BlockState state, BlockPos pos) { - if (!state.isIn(BlockTags.FIRE)) { - return; - } + public void trySpawnReaper(ServerWorld world, BlockPos pos) { if (!Config.getInstance().allowGrimReaper) { return; } - ServerChunkManager chunkManager = world.getChunkManager(); ChunkPos chunkPos = new ChunkPos(pos); - chunkManager.addTicket(REAPER, chunkPos, 16, pos); // Make sure the neighboring chunks are loaded if (!WorldUtils.isAreaLoaded(world, chunkPos, 1)) { - chunkManager.removeTicket(REAPER, chunkPos, 16, pos); return; } - if (world.getBlockState(pos.down()).getBlock() != Blocks.EMERALD_BLOCK) { + if (world.getBlockState(pos).getBlock() != Blocks.EMERALD_BLOCK) { return; } @@ -100,17 +88,15 @@ public void trySpawnReaper(ServerWorld world, BlockState state, BlockPos pos) { return; } - start(new SummonPosition(pos, totems)); + start(new SummonPosition(pos.up(), totems)); EntityType.LIGHTNING_BOLT.spawn(world, pos, SpawnReason.TRIGGERED); - world.setBlockState(pos.down(), Blocks.SOUL_SOIL.getDefaultState(), Block.NOTIFY_NEIGHBORS | Block.NOTIFY_LISTENERS); - world.setBlockState(pos, BlocksMCA.INFERNAL_FLAME.get().getDefaultState(), Block.NOTIFY_NEIGHBORS | Block.NOTIFY_LISTENERS); + world.setBlockState(pos, Blocks.SOUL_SOIL.getDefaultState(), Block.NOTIFY_NEIGHBORS | Block.NOTIFY_LISTENERS); + world.setBlockState(pos.up(), BlocksMCA.INFERNAL_FLAME.get().getDefaultState(), Block.NOTIFY_NEIGHBORS | Block.NOTIFY_LISTENERS); totems.forEach(totem -> world.setBlockState(totem, BlocksMCA.INFERNAL_FLAME.get().getDefaultState(), Block.NOTIFY_LISTENERS | Block.FORCE_STATE) ); - - chunkManager.removeTicket(REAPER, chunkPos, 16, pos); } private void start(SummonPosition pos) { @@ -144,7 +130,7 @@ private boolean isNightTime(World world) { } private Set getTotemsFires(World world, BlockPos pos) { - int groundY = pos.getY() - 2; + int groundY = pos.getY() - 1; int leftSkyHeight = world.getTopY() - groundY; int minPillarHeight = Math.min(Config.getInstance().minPillarHeight, leftSkyHeight); BlockPos.Mutable target = new BlockPos.Mutable(); diff --git a/common/src/main/resources/mca.mixin.json b/common/src/main/resources/mca.mixin.json index 33454829c3..cd4a03d23e 100644 --- a/common/src/main/resources/mca.mixin.json +++ b/common/src/main/resources/mca.mixin.json @@ -5,10 +5,10 @@ "compatibilityLevel": "JAVA_17", "mixins": [ "MixinAbstractFurnaceBlockEntity", "MixinActivity", "MixinCriteria", "MixinDefaultParticleType", "MixinEntityType", - "MixinGoatEntity", "MixinHorseBaseEntity", "MixinMemoryModuleType", "MixinMilkBucketItem", "MixinPlayerEntity", - "MixinPlayerInventory", "MixinProtoChunk", "MixinSensorType", "MixinServerPlayNetworkHandler", "MixinServerWorld", - "MixinTranslatableText", "MixinVillagerEntity", "MixinVillagerEntityInvoker", "MixinVillagerProfession", - "MixinZombieEntity", "MixinZombieVillagerEntity" + "MixinFlintAndSteelItem", "MixinGoatEntity", "MixinHorseBaseEntity", "MixinMemoryModuleType", "MixinMilkBucketItem", + "MixinPlayerEntity", "MixinPlayerInventory", "MixinProtoChunk", "MixinSensorType", "MixinServerPlayNetworkHandler", + "MixinServerWorld", "MixinTranslatableText", "MixinVillagerEntity", "MixinVillagerEntityInvoker", + "MixinVillagerProfession", "MixinZombieEntity", "MixinZombieVillagerEntity" ], "client": [ "client.MixinArmorFeatureRenderer", "client.MixinGameRenderer", "client.MixinHeldItemFeatureRenderer",