Skip to content

Commit

Permalink
fixed reaper cpu burner
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke100000 committed May 17, 2024
1 parent 1961caa commit 6a6c322
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 7.5.18

* Updated contributor book
* Fixed ReaperSpawner eating all your CPU

# 7.5.17

Expand Down
23 changes: 23 additions & 0 deletions common/src/main/java/net/mca/mixin/MixinFlintAndSteelItem.java
Original file line number Diff line number Diff line change
@@ -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<ActionResult> cir) {
if (cir.getReturnValue().isAccepted() && context.getWorld() instanceof ServerWorld serverWorld) {
serverWorld.getServer().execute(() ->
VillageManager.get(serverWorld).getReaperSpawner().trySpawnReaper(serverWorld, context.getBlockPos())
);
}
}
}
14 changes: 0 additions & 14 deletions common/src/main/java/net/mca/mixin/MixinServerWorld.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -29,17 +26,6 @@ private void onAddEntity(Entity entity, CallbackInfoReturnable<Boolean> 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)
Expand Down
26 changes: 6 additions & 20 deletions common/src/main/java/net/mca/server/ReaperSpawner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,26 @@
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;
import net.minecraft.nbt.NbtCompound;
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.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReaperSpawner {
public static final ChunkTicketType<BlockPos> REAPER = ChunkTicketType.create("mca:reaper", Vec3i::compareTo, 100);

private static final Direction[] HORIZONTALS = new Direction[]{
Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST
};
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down Expand Up @@ -144,7 +130,7 @@ private boolean isNightTime(World world) {
}

private Set<BlockPos> 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();
Expand Down
8 changes: 4 additions & 4 deletions common/src/main/resources/mca.mixin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6a6c322

Please sign in to comment.