Skip to content

Commit

Permalink
Improve Random Tick Catching
Browse files Browse the repository at this point in the history
  • Loading branch information
Bawnorton committed Jun 2, 2023
1 parent 55dc184 commit 8abb7a7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.19.4+build.2
loader_version=0.14.19

# Mod Properties
mod_version=0.2.0
mod_version=0.2.1
maven_group=com.bawnorton.nerina
archives_base_name=neruina

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.block.BlockState;
import net.minecraft.block.BlockWithEntity;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -63,12 +64,17 @@ public static void renderRenderers(MatrixStack matrixStack, Camera camera) {
}

private static boolean isValidBlock(BlockPos pos) {
BlockEntity blockEntity = null;
BlockState blockState = null;
BlockEntity blockEntity = null;
boolean hasBlockEntity = false;
if (client.world != null) {
blockEntity = client.world.getBlockEntity(pos);
blockState = client.world.getBlockState(pos);
if(blockState.getBlock() instanceof BlockWithEntity) {
blockEntity = client.world.getBlockEntity(pos);
hasBlockEntity = true;
}
}
return blockEntity != null && blockState != Blocks.AIR.getDefaultState();
if(hasBlockEntity && blockEntity != null) return true;
return blockState != null && blockState.getBlock() != Blocks.AIR;
}
}
16 changes: 9 additions & 7 deletions src/main/java/com/bawnorton/neruina/Neruina.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,7 +20,7 @@ public class Neruina implements ModInitializer {
private static final List<BlockEntity> ERRORED_BLOCK_ENTITIES = new ArrayList<>();
private static final List<Entity> ERRORED_ENTITIES = new ArrayList<>();
private static final List<ItemStack> ERRORED_ITEM_STACKS = new ArrayList<>();
private static final List<BlockState> ERRORED_BLOCK_STATES = new ArrayList<>();
private static final List<ImmutablePair<BlockPos, BlockState>> ERRORED_BLOCK_STATES = new ArrayList<>();

@Override
public void onInitialize() {
Expand Down Expand Up @@ -57,15 +59,15 @@ public static void addErrored(ItemStack item) {
ERRORED_ITEM_STACKS.add(item);
}

public static boolean isErrored(BlockState blockState) {
return ERRORED_BLOCK_STATES.contains(blockState);
public static boolean isErrored(BlockPos pos, BlockState state) {
return ERRORED_BLOCK_STATES.contains(new ImmutablePair<>(pos, state));
}

public static void addErrored(BlockState blockState) {
ERRORED_BLOCK_STATES.add(blockState);
public static void addErrored(BlockPos pos, BlockState state) {
ERRORED_BLOCK_STATES.add(new ImmutablePair<>(pos, state));
}

public static void removeErrored(BlockState blockState) {
ERRORED_BLOCK_STATES.remove(blockState);
public static void removeErrored(BlockPos pos, BlockState state) {
ERRORED_BLOCK_STATES.remove(new ImmutablePair<>(pos, state));
}
}
13 changes: 11 additions & 2 deletions src/main/java/com/bawnorton/neruina/mixin/ServerWorldMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
import net.minecraft.util.math.random.Random;
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.CallbackInfo;

@Mixin(ServerWorld.class)
public abstract class ServerWorldMixin {
@Inject(method = "onBlockChanged", at = @At("HEAD"))
private void removeErrored(BlockPos pos, BlockState oldBlock, BlockState newBlock, CallbackInfo ci) {
if (Neruina.isErrored(pos, oldBlock)) {
Neruina.removeErrored(pos, oldBlock);
}
}

@WrapOperation(method = "tickChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/BlockState;randomTick(Lnet/minecraft/server/world/ServerWorld;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/math/random/Random;)V"))
private void catchTickingBlockState(BlockState instance, ServerWorld world, BlockPos pos, Random random, Operation<Void> original) {
try {
if (Neruina.isErrored(instance)) {
if (Neruina.isErrored(pos, instance)) {
return;
}
original.call(instance, world, pos, random);
} catch (RuntimeException e) {
String message = String.format("§b[Neruina]: §cCaught Ticking BlockState from random tick [%s] at position [x=%s, y=%s, z=%s]. Please resolve.", instance.getBlock().getName().getString(), pos.getX(), pos.getY(), pos.getZ());
Neruina.LOGGER.warn("Server: " + message, e);
Neruina.addErrored(instance);
Neruina.addErrored(pos, instance);
PlayerManager playerManager = world.getServer().getPlayerManager();
playerManager.getPlayerList().forEach(player -> Networking.sendBadBlockPacket(player, pos));
ConditionalRunnable.create(() -> playerManager.broadcast(Text.of(message), false), () -> playerManager.getCurrentPlayerCount() > 0);
Expand Down

0 comments on commit 8abb7a7

Please sign in to comment.