Skip to content

Commit

Permalink
More stuff working
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadiboo committed Apr 11, 2024
1 parent e92ceb4 commit 535d0ff
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 214 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ dependencies {
// // Apply Mixin AP
// annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'

// // From https://github.com/MinecraftForge/MinecraftForge/blob/0579572148079a6bc483ff42404878962fc48702/build.gradle#L55
// def NIGHTCONFIG_VERSION = '3.6.0'
// // See https://github.com/MinecraftForge/MinecraftForge/blob/0579572148079a6bc483ff42404878962fc48702/build.gradle#L280-L281
// library "com.electronwill.night-config:core:${NIGHTCONFIG_VERSION}"
// library "com.electronwill.night-config:toml:${NIGHTCONFIG_VERSION}"
// For the config color picker (we include it in our production jar with ShadowJar)
library 'org.beryx:awt-color-factory:1.0.2'
// For @Nullable
Expand Down
28 changes: 1 addition & 27 deletions src/main/java/io/github/cadiboo/nocubes/NoCubes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,17 @@
import io.github.cadiboo.nocubes.config.NoCubesConfig;
import io.github.cadiboo.nocubes.smoothable.SmoothableHandler;
import io.github.cadiboo.nocubes.util.ModUtil;
import net.fabricmc.api.ModInitializer;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;

/**
* @author Cadiboo
*/
public final class NoCubes implements ModInitializer {
public final class NoCubes {

public static final String MOD_ID = "nocubes";
public static final SmoothableHandler smoothableHandler = SmoothableHandler.create();

@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.

// TODO: REMOVE
NoCubesConfig.Common.debugEnabled = true;
NoCubesConfig.Client.render = true;
NoCubesConfig.Server.mesher = NoCubesConfig.Server.MesherType.SurfaceNets.instance;
NoCubesConfig.Server.collisionsEnabled = true;
NoCubesConfig.Server.tempMobCollisionsDisabled = true;
NoCubesConfig.Server.extendFluidsRange = 3;
NoCubesConfig.Smoothables.recomputeInMemoryLookup(java.util.Collections.emptyList(), java.util.Collections.emptyList(), true);
}

// public NoCubes() {
// var modBus = FMLJavaModLoadingContext.get().getModEventBus();
// NoCubesConfig.register(ModLoadingContext.get(), modBus);
// if (FMLEnvironment.dist.isClient())
// modBus.addListener((RegisterKeyMappingsEvent event) -> KeyMappings.register(event, MinecraftForge.EVENT_BUS));
// NoCubesNetwork.register();
// modBus.addListener((FMLClientSetupEvent event) -> OverlayRenderers.register(MinecraftForge.EVENT_BUS));
// }

// region API
/**
* For other mods.
Expand Down

This file was deleted.

49 changes: 23 additions & 26 deletions src/main/java/io/github/cadiboo/nocubes/client/KeyMappings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
//import net.minecraftforge.client.event.RegisterKeyMappingsEvent;
//import net.minecraftforge.event.TickEvent;
//import net.minecraftforge.eventbus.api.IEventBus;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.glfw.GLFW;

import java.util.function.Consumer;

import static io.github.cadiboo.nocubes.client.RenderHelper.reloadAllChunks;

/**
Expand All @@ -40,30 +39,28 @@ public final class KeyMappings {
public static final String TOGGLE_SMOOTHABLE_BLOCK_TYPE = "toggleSmoothable";
public static final String TOGGLE_SMOOTHABLE_BLOCK_STATE = "toggleSmoothableBlockState";

// public static void register(RegisterKeyMappingsEvent registerEvent, IEventBus forgeBus) {
// LOG.debug("Registering keybindings");
// var keybindings = Lists.newArrayList(
// makeKeybinding(registerEvent, TOGGLE_VISUALS, InputConstants.UNKNOWN.getValue(), KeyMappings::toggleVisuals),
// makeKeybinding(registerEvent, TOGGLE_SMOOTHABLE_BLOCK_TYPE, GLFW.GLFW_KEY_N, () -> toggleLookedAtSmoothable(true)),
// makeKeybinding(registerEvent, TOGGLE_SMOOTHABLE_BLOCK_STATE, InputConstants.UNKNOWN.getValue(), () -> toggleLookedAtSmoothable(false))
// );
// forgeBus.addListener((TickEvent.ClientTickEvent tickEvent) -> {
// if (tickEvent.phase != TickEvent.Phase.END)
// return;
// for (var keybinding : keybindings)
// if (keybinding.getKey().consumeClick()) {
// LOG.debug("Keybinding {} pressed", keybinding.getKey().getName());
// keybinding.getValue().run();
// }
// });
// }
public static void register(Consumer<KeyMapping> registerKey, Consumer<Runnable> registerClientTickHandler) {
LOG.debug("Registering keybindings");
var keybindings = Lists.newArrayList(
makeKeybinding(registerKey, TOGGLE_VISUALS, InputConstants.UNKNOWN.getValue(), KeyMappings::toggleVisuals),
makeKeybinding(registerKey, TOGGLE_SMOOTHABLE_BLOCK_TYPE, GLFW.GLFW_KEY_N, () -> toggleLookedAtSmoothable(true)),
makeKeybinding(registerKey, TOGGLE_SMOOTHABLE_BLOCK_STATE, InputConstants.UNKNOWN.getValue(), () -> toggleLookedAtSmoothable(false))
);
registerClientTickHandler.accept(() -> {
for (var keybinding : keybindings)
if (keybinding.getKey().consumeClick()) {
LOG.debug("Keybinding {} pressed", keybinding.getKey().getName());
keybinding.getValue().run();
}
});
}

// private static Pair<KeyMapping, Runnable> makeKeybinding(RegisterKeyMappingsEvent event, String name, int key, Runnable action) {
// LOG.debug("Registering keybinding {}", name);
// var mapping = new KeyMapping(qualifyName(name), key, NoCubes.MOD_ID + ".keycategory");
// event.register(mapping);
// return Pair.of(mapping, action);
// }
private static Pair<KeyMapping, Runnable> makeKeybinding(Consumer<KeyMapping> registerKey, String name, int key, Runnable action) {
LOG.debug("Registering keybinding {}", name);
var mapping = new KeyMapping(qualifyName(name), key, NoCubes.MOD_ID + ".keycategory");
registerKey.accept(mapping);
return Pair.of(mapping, action);
}

private static String qualifyName(String name) {
return NoCubes.MOD_ID + ".key." + name;
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/io/github/cadiboo/nocubes/client/RenderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,29 @@ public static void drawLinePosColorFromTo(BlockPos startOffset, Vec start, Block
);
}

public static void drawFacePosColor(Face face, Vec3 camera, BlockPos pos, ColorParser.Color color, VertexConsumer buffer, PoseStack matrix) {
public static void drawFacePosColor(
Face face, Vec3 camera,
BlockPos pos, ColorParser.Color color,
VertexConsumer buffer, PoseStack matrix
) {
drawFacePosColor(
face, camera.x, camera.y, camera.z,
pos, color, buffer, matrix
);
}

public static void drawFacePosColor(
Face face, double cameraX, double cameraY, double cameraZ,
BlockPos pos, ColorParser.Color color,
VertexConsumer buffer, PoseStack matrix
) {
var v0 = face.v0;
var v1 = face.v1;
var v2 = face.v2;
var v3 = face.v3;
var x = pos.getX() - camera.x;
var y = pos.getY() - camera.y;
var z = pos.getZ() - camera.z;
var x = pos.getX() - cameraX;
var y = pos.getY() - cameraY;
var z = pos.getZ() - cameraZ;

var v0x = (float) (x + v0.x);
var v1x = (float) (x + v1.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
//import net.minecraftforge.client.event.RenderHighlightEvent;
//import net.minecraftforge.client.event.RenderLevelStageEvent;
//import net.minecraftforge.eventbus.api.IEventBus;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;

Expand All @@ -43,80 +42,66 @@
*/
public final class OverlayRenderers {

// public static void register(IEventBus events) {
// events.addListener(OverlayRenderers::renderBlockHighlight);
// var meshProfiler = new RollingProfiler(600);
// var debugOverlays = Lists.<Pair<String, DebugOverlay>>newArrayList(
// Pair.of("drawOutlineAroundNearbySmoothableBlocks", OverlayRenderers::drawOutlineAroundNearbySmoothableBlocks),
// Pair.of("drawOutlineAroundNearbySmoothableBlocks", OverlayRenderers::drawOutlineAroundNearbySmoothableBlocks),
// Pair.of("maybeRenderMeshAndRecordPerformance", (camera, matrix, lines) -> maybeRenderMeshAndRecordPerformance(camera, matrix, lines, meshProfiler)),
// Pair.of("drawNearbyMeshCollisions", OverlayRenderers::drawNearbyMeshCollisions),
// Pair.of("drawNearbyCollisions", OverlayRenderers::drawNearbyCollisions),
// Pair.of("drawNearbyDensities", OverlayRenderers::drawNearbyDensities)
// );
// events.addListener((RenderLevelStageEvent event) -> renderDebugOverlays(event, debugOverlays));
// }
public static void register(Consumer<Consumer<PoseStack>> registerPerFrameHandler) {
var meshProfiler = new RollingProfiler(600);
var debugOverlays = Lists.<Pair<String, DebugOverlay>>newArrayList(
Pair.of("drawOutlineAroundNearbySmoothableBlocks", OverlayRenderers::drawOutlineAroundNearbySmoothableBlocks),
Pair.of("drawOutlineAroundNearbySmoothableBlocks", OverlayRenderers::drawOutlineAroundNearbySmoothableBlocks),
Pair.of("maybeRenderMeshAndRecordPerformance", (camera, matrix, lines) -> maybeRenderMeshAndRecordPerformance(camera, matrix, lines, meshProfiler)),
Pair.of("drawNearbyMeshCollisions", OverlayRenderers::drawNearbyMeshCollisions),
Pair.of("drawNearbyCollisions", OverlayRenderers::drawNearbyCollisions),
Pair.of("drawNearbyDensities", OverlayRenderers::drawNearbyDensities)
);
registerPerFrameHandler.accept(matrix -> renderDebugOverlays(matrix, debugOverlays));
}

public static boolean renderNoCubesBlockHighlight(
PoseStack matrix, VertexConsumer buffer,
double cameraX, double cameraY, double cameraZ,
BlockGetter world, BlockPos lookingAtPos, BlockState state
) {
if (!NoCubesConfig.Client.render)
return false;
if (!NoCubesConfig.Client.renderSelectionBox)
return false;
if (!NoCubes.smoothableHandler.isSmoothable(state))
return false;
var mesher = NoCubesConfig.Server.mesher;
var stateSolidity = MeshRenderer.isSolidRender(state);
try (var area = new Area(world, lookingAtPos, ModUtil.VEC_ONE, mesher)) {
var color = NoCubesConfig.Client.selectionBoxColor;
Predicate<BlockState> isSmoothable = NoCubes.smoothableHandler::isSmoothable;
mesher.generateGeometry(area, s -> isSmoothable.test(s) && MeshRenderer.isSolidRender(s) == stateSolidity, (pos, face) -> {
drawFacePosColor(face, cameraX, cameraY, cameraZ, area.start, color, buffer, matrix);
return true;
});
}
return true;
}

// public static void renderBlockHighlight(RenderHighlightEvent event) {
// if (!NoCubesConfig.Client.render)
// return;
// if (!NoCubesConfig.Client.renderSelectionBox)
// return;
// var world = Minecraft.getInstance().level;
// if (world == null)
// return;
// var targetHitResult = event.getTarget();
// if (!(targetHitResult instanceof BlockHitResult target))
// return;
// var lookingAtPos = target.getBlockPos();
// var state = world.getBlockState(lookingAtPos);
// if (!NoCubes.smoothableHandler.isSmoothable(state))
// return;
//
// event.setCanceled(true);
//
// var camera = event.getCamera().getPosition();
// var matrix = event.getPoseStack();
// var buffer = event.getMultiBufferSource().getBuffer(RenderType.lines());
// var mesher = NoCubesConfig.Server.mesher;
// var stateSolidity = MeshRenderer.isSolidRender(state);
// try (var area = new Area(world, lookingAtPos, ModUtil.VEC_ONE, mesher)) {
// var color = NoCubesConfig.Client.selectionBoxColor;
// Predicate<BlockState> isSmoothable = NoCubes.smoothableHandler::isSmoothable;
// mesher.generateGeometry(area, s -> isSmoothable.test(s) && MeshRenderer.isSolidRender(s) == stateSolidity, (pos, face) -> {
// drawFacePosColor(face, camera, area.start, color, buffer, matrix);
// return true;
// });
// }
// }
//
// public static void renderDebugOverlays(RenderLevelStageEvent event, List<Pair<String, DebugOverlay>> overlays) {
// if (!NoCubesConfig.Common.debugEnabled)
// return;
//
// if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_CUTOUT_BLOCKS)
// return;
//
// var minecraft = Minecraft.getInstance();
// var camera = minecraft.gameRenderer.getMainCamera();
//
//// drawBlockDestructionProgressForDebug(minecraft, camera);
// var bufferSource = minecraft.renderBuffers().bufferSource();
// Supplier<VertexConsumer> linesSupplier = () -> bufferSource.getBuffer(RenderType.lines());
// overlays.forEach(overlay -> {
// var profiler = minecraft.getProfiler();
// profiler.push(overlay.getLeft());
// try {
// overlay.getRight().render(camera, event.getPoseStack(), linesSupplier);
// } finally {
// profiler.pop();
// }
// });
//
// // Hack to finish buffer because RenderWorldLastEvent seems to fire after vanilla normally finishes them
// bufferSource.endBatch(RenderType.lines());
// }
static void renderDebugOverlays(PoseStack matrix, List<Pair<String, DebugOverlay>> overlays) {
if (!NoCubesConfig.Common.debugEnabled)
return;

var minecraft = Minecraft.getInstance();
var camera = minecraft.gameRenderer.getMainCamera();

// drawBlockDestructionProgressForDebug(minecraft, camera);
var bufferSource = minecraft.renderBuffers().bufferSource();
Supplier<VertexConsumer> linesSupplier = () -> bufferSource.getBuffer(RenderType.lines());
overlays.forEach(overlay -> {
var profiler = minecraft.getProfiler();
profiler.push(overlay.getLeft());
try {
overlay.getRight().render(camera, matrix, linesSupplier);
} finally {
profiler.pop();
}
});

// Hack to finish buffer because RenderWorldLastEvent seems to fire after vanilla normally finishes them
bufferSource.endBatch(RenderType.lines());
}

interface DebugOverlay {
void render(Camera camera, PoseStack matrix, Supplier<VertexConsumer> linesSupplier);
Expand Down
Loading

0 comments on commit 535d0ff

Please sign in to comment.