Skip to content

Commit

Permalink
Rewrite testmod debug renderer to be cooler
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Aug 10, 2023
1 parent 365eb80 commit 6c85449
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockState> WOOL_STATES = new ArrayList<>();

@Override
public void onInitialize() {
// Register 1 million blocks & items
Expand All @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<BlockState> cir) {
if(this.level.isDebug()) {
cir.setReturnValue(TestMod.getColorCubeStateFor(pos.getX(), pos.getY(), pos.getZ()));
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
}
}
1 change: 1 addition & 0 deletions fabric/testmod/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"license": "LGPL-3.0",
"environment": "*",
"mixins": [ "testmod.mixins.json" ],
"entrypoints": {
"main": [
"org.embeddedt.modernfix.testmod.TestMod"
Expand Down
13 changes: 13 additions & 0 deletions fabric/testmod/src/main/resources/testmod.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"required": true,
"package": "org.embeddedt.modernfix.testmod.mixin",
"compatibilityLevel": "JAVA_8",
"minVersion": "0.8",
"mixins": [
"ChunkMixin",
"DebugLevelSourceMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 6c85449

Please sign in to comment.