From 6c85449b6349956e890ad5a851f981ad9b965f07 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Wed, 9 Aug 2023 22:48:10 -0400 Subject: [PATCH] Rewrite testmod debug renderer to be cooler --- .../embeddedt/modernfix/testmod/TestMod.java | 26 +++++++++++++- .../modernfix/testmod/mixin/ChunkMixin.java | 25 +++++++++++++ .../testmod/mixin/DebugLevelSourceMixin.java | 36 +++++++++++++++++++ .../src/main/resources/fabric.mod.json | 1 + .../src/main/resources/testmod.mixins.json | 13 +++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/ChunkMixin.java create mode 100644 fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/DebugLevelSourceMixin.java create mode 100644 fabric/testmod/src/main/resources/testmod.mixins.json diff --git a/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/TestMod.java b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/TestMod.java index f519edc53..5ad72e50a 100644 --- a/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/TestMod.java +++ b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/TestMod.java @@ -4,16 +4,23 @@ import net.fabricmc.api.ModInitializer; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.ArrayList; +import java.util.List; + public class TestMod implements ModInitializer { public static final String ID = "mfix_testmod"; public static final Logger LOGGER = LogManager.getLogger("ModernFix TestMod"); - public static final int NUM_COLORS = 32; + public static final int NUM_COLORS = 100; public static final int MAX_COLOR = NUM_COLORS - 1; + public static final List WOOL_STATES = new ArrayList<>(); + @Override public void onInitialize() { // Register 1 million blocks & items @@ -26,6 +33,7 @@ public void onInitialize() { for(int b = 0; b < NUM_COLORS; b++) { ResourceLocation name = new ResourceLocation(ID, "wool_" + r + "_" + g + "_" + b); TestBlock block = Registry.register(Registry.BLOCK, name, new TestBlock()); + WOOL_STATES.add(block.defaultBlockState()); Registry.register(Registry.ITEM, name, new TestBlockItem(block)); numRegistered++; if((numRegistered % progressReport) == 0) { @@ -37,4 +45,20 @@ public void onInitialize() { watch.stop(); LOGGER.info("Registered {} registry entries in {}", totalToRegister, watch); } + + private static final BlockState AIR = Blocks.AIR.defaultBlockState(); + + public static BlockState getColorCubeStateFor(int chunkX, int chunkY, int chunkZ) { + BlockState blockState = AIR; + if (chunkX >= 0 && chunkY >= 0 && chunkZ >= 0 && chunkX % 2 == 0 && chunkY % 2 == 0 && chunkZ % 2 == 0) { + chunkX /= 2; + chunkY /= 2; + chunkZ /= 2; + if(chunkX <= TestMod.MAX_COLOR && chunkY <= TestMod.MAX_COLOR && chunkZ <= TestMod.MAX_COLOR) { + blockState = TestMod.WOOL_STATES.get((chunkX * TestMod.NUM_COLORS * TestMod.NUM_COLORS) + (chunkY * TestMod.NUM_COLORS) + chunkZ); + } + } + + return blockState; + } } diff --git a/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/ChunkMixin.java b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/ChunkMixin.java new file mode 100644 index 000000000..1356aece6 --- /dev/null +++ b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/ChunkMixin.java @@ -0,0 +1,25 @@ +package org.embeddedt.modernfix.testmod.mixin; + +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.LevelChunk; +import org.embeddedt.modernfix.testmod.TestMod; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +@Mixin(LevelChunk.class) +public class ChunkMixin { + @Shadow @Final private Level level; + + @Inject(method = "getBlockState", at = @At("HEAD"), cancellable = true) + private void redirectDebugWorld(BlockPos pos, CallbackInfoReturnable cir) { + if(this.level.isDebug()) { + cir.setReturnValue(TestMod.getColorCubeStateFor(pos.getX(), pos.getY(), pos.getZ())); + } + } +} diff --git a/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/DebugLevelSourceMixin.java b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/DebugLevelSourceMixin.java new file mode 100644 index 000000000..40d3eba96 --- /dev/null +++ b/fabric/testmod/src/main/java/org/embeddedt/modernfix/testmod/mixin/DebugLevelSourceMixin.java @@ -0,0 +1,36 @@ +package org.embeddedt.modernfix.testmod.mixin; + +import net.minecraft.core.BlockPos; +import net.minecraft.server.level.WorldGenRegion; +import net.minecraft.world.level.StructureFeatureManager; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.levelgen.DebugLevelSource; +import org.embeddedt.modernfix.testmod.TestMod; +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(DebugLevelSource.class) +public class DebugLevelSourceMixin { + @Inject(method = "applyBiomeDecoration", at = @At("HEAD"), cancellable = true) + private void showColorCube(WorldGenRegion region, StructureFeatureManager structureManager, CallbackInfo ci) { + ci.cancel(); + BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos(); + int i = region.getCenterX(); + int j = region.getCenterZ(); + + for(int k = 0; k < 16; ++k) { + for(int l = 0; l < 16; ++l) { + int m = (i << 4) + k; + int n = (j << 4) + l; + for(int y = 0; y < 255; y++) { + BlockState blockState = TestMod.getColorCubeStateFor(m, y, n); + if (blockState != null) { + region.setBlock(mutableBlockPos.set(m, y, n), blockState, 2); + } + } + } + } + } +} diff --git a/fabric/testmod/src/main/resources/fabric.mod.json b/fabric/testmod/src/main/resources/fabric.mod.json index 3188e5034..6ebb4d54e 100644 --- a/fabric/testmod/src/main/resources/fabric.mod.json +++ b/fabric/testmod/src/main/resources/fabric.mod.json @@ -14,6 +14,7 @@ }, "license": "LGPL-3.0", "environment": "*", + "mixins": [ "testmod.mixins.json" ], "entrypoints": { "main": [ "org.embeddedt.modernfix.testmod.TestMod" diff --git a/fabric/testmod/src/main/resources/testmod.mixins.json b/fabric/testmod/src/main/resources/testmod.mixins.json new file mode 100644 index 000000000..9eeb91ea8 --- /dev/null +++ b/fabric/testmod/src/main/resources/testmod.mixins.json @@ -0,0 +1,13 @@ +{ + "required": true, + "package": "org.embeddedt.modernfix.testmod.mixin", + "compatibilityLevel": "JAVA_8", + "minVersion": "0.8", + "mixins": [ + "ChunkMixin", + "DebugLevelSourceMixin" + ], + "injectors": { + "defaultRequire": 1 + } +} \ No newline at end of file