Skip to content

Commit

Permalink
晶洞高亮
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoupraw committed Apr 12, 2024
1 parent e20a5d0 commit 6eca9d5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 28 deletions.
8 changes: 2 additions & 6 deletions common/src/main/java/dev/dubhe/anvilcraft/item/GeodeItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import dev.dubhe.anvilcraft.util.BlockHighlightUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentUtils;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.*;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
Expand Down Expand Up @@ -42,7 +38,7 @@ public GeodeItem(Properties properties) {
for (int z = -radius; z <= radius; z += interval) {
int height = level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z);
for (int y = level.getMinBuildHeight(); y <= height; y += interval) {
BlockPos offsetPos = pos.offset(x, y, z);
BlockPos offsetPos = new BlockPos(pos.getX() + x, y, pos.getZ() + z);
BlockState state = level.getBlockState(offsetPos);
if (!state.is(BlockTags.CRYSTAL_SOUND_BLOCKS)) continue;
MutableComponent component = ComponentUtils.wrapInSquareBrackets(Component.translatable(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,91 @@
package dev.dubhe.anvilcraft.util;

import lombok.Getter;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormat;
import dev.dubhe.anvilcraft.AnvilCraft;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.Camera;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderStateShard;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3i;
import org.joml.Vector3ic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.OptionalDouble;

public class BlockHighlightUtil {
@Environment(EnvType.CLIENT)
public static final RenderType NO_DEPTH = RenderType.create(AnvilCraft.MOD_ID + "_no_depth", DefaultVertexFormat.POSITION_COLOR_NORMAL, VertexFormat.Mode.LINES, 256, true, true, RenderType.CompositeState.builder()

Check warning on line 27 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 217). Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:27:0: warning: Line is longer than 120 characters (found 217). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
.setShaderState(RenderStateShard.RENDERTYPE_LINES_SHADER)

Check warning on line 28 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:28:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.setWriteMaskState(RenderStateShard.COLOR_WRITE)

Check warning on line 29 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:29:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.setCullState(RenderStateShard.NO_CULL)

Check warning on line 30 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:30:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.setDepthTestState(RenderStateShard.NO_DEPTH_TEST)

Check warning on line 31 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:31:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.setLayeringState(RenderStateShard.VIEW_OFFSET_Z_LAYERING)

Check warning on line 32 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:32:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.setLineState(new RenderStateShard.LineStateShard(OptionalDouble.of(2)))

Check warning on line 33 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:33:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
.createCompositeState(true));

Check warning on line 34 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 '.' has incorrect indentation level 6, expected level should be 8. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:34:7: warning: '.' has incorrect indentation level 6, expected level should be 8. (com.puppycrawl.tools.checkstyle.checks.indentation.IndentationCheck)
public static final Map<Vector3ic, Long> SUBCHUNKS = new HashMap<>();
private static Level level = null;
@Getter
private static final Map<BlockPos, Integer> highlight = new ConcurrentHashMap<>();


/**
* 高亮方块
*
* @param level 维度
* @param pos 位置
*/
public static void highlightBlock(Level level, BlockPos pos) {
if (BlockHighlightUtil.level == level) BlockHighlightUtil.highlight.put(pos, 400);
else {
BlockHighlightUtil.level = level;
BlockHighlightUtil.highlight.clear();
BlockHighlightUtil.highlight.put(pos, 400);
if (BlockHighlightUtil.getLevel() != level) {
BlockHighlightUtil.setLevel(level);
SUBCHUNKS.clear();
}
// TODO 高亮方块
SUBCHUNKS.put(new Vector3i(Math.floorDiv(pos.getX(), 16), Math.floorDiv(pos.getY(), 16), Math.floorDiv(pos.getZ(), 16)), level.getGameTime());

Check warning on line 49 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Line is longer than 120 characters (found 150). Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:49:0: warning: Line is longer than 120 characters (found 150). (com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck)
}

/**
* 获取被高亮的方块列表
*
* @param level 维度
* @return 被高亮的方块列表
*/
public static Map<BlockPos, Integer> getHighlight(Level level) {
if (BlockHighlightUtil.level != level) BlockHighlightUtil.highlight.clear();
return BlockHighlightUtil.highlight;
@Environment(EnvType.CLIENT)

Check warning on line 51 in common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java

View workflow job for this annotation

GitHub Actions / checkstyle

[Checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /home/runner/work/AnvilCraftMod/AnvilCraftMod/common/src/main/java/dev/dubhe/anvilcraft/util/BlockHighlightUtil.java:51:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)
public static void render(Level level, MultiBufferSource consumers, PoseStack poseStack, Camera camera) {
VertexConsumer consumer = consumers.getBuffer(BlockHighlightUtil.NO_DEPTH);
Vec3 cameraPos = camera.getPosition();
int color = 0xFF8932B8;
poseStack.pushPose();
poseStack.translate(-cameraPos.x, -cameraPos.y, -cameraPos.z);
for (var iterator = BlockHighlightUtil.SUBCHUNKS.entrySet().iterator(); iterator.hasNext(); ) {
var entry = iterator.next();
Vector3ic subchunk = entry.getKey();
Long moment = entry.getValue();
if (level.getGameTime() > moment + 60 * 20) {
iterator.remove();
continue;
}
Vector3ic pos1 = subchunk.mul(16, new Vector3i());
Vector3ic pos2 = pos1.add(16, 16, 16, new Vector3i());
LevelRenderer.renderLineBox(
poseStack,
consumer,
pos1.x(),
pos1.y(),
pos1.z(),
pos2.x(),
pos2.y(),
pos2.z(),
(color >> 16 & 0xFF) / 255f,
(color >> 8 & 0xFF) / 255f,
(color & 0xFF) / 255f,
(color >> 24) / 255f
);
}
poseStack.popPose();
}
public static Level getLevel() {
return level;
}
public static void setLevel(Level level) {
BlockHighlightUtil.level = level;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dev.dubhe.anvilcraft.client.fabric;

import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.util.BlockHighlightUtil;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;

public class AnvilCraftClient implements ClientModInitializer {
Expand All @@ -11,5 +14,11 @@ public void onInitializeClient() {
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CORRUPTED_BEACON.get(), RenderType.translucent());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.RESIN_BLOCK.get(), RenderType.translucent());
BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.AMBER_BLOCK.get(), RenderType.translucent());
WorldRenderEvents.AFTER_ENTITIES.register(context -> {
if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return;
MultiBufferSource consumers = context.consumers();
if (consumers == null) return;
BlockHighlightUtil.render(context.world(), consumers, context.matrixStack(), context.camera());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import dev.dubhe.anvilcraft.AnvilCraft;
import dev.dubhe.anvilcraft.config.AnvilCraftConfig;
import dev.dubhe.anvilcraft.init.ModCommands;
import dev.dubhe.anvilcraft.util.BlockHighlightUtil;
import me.shedaniel.autoconfig.AutoConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraftforge.client.ConfigScreenHandler;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.fml.ModLoadingContext;
Expand All @@ -19,7 +23,14 @@ public class AnvilCraftForge {
public AnvilCraftForge() {
AnvilCraft.init();
MinecraftForge.EVENT_BUS.addListener(AnvilCraftForge::registerCommand);

MinecraftForge.EVENT_BUS.addListener((RenderLevelStageEvent event) -> {
if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_ENTITIES) return;
if (BlockHighlightUtil.SUBCHUNKS.isEmpty()) return;
ClientLevel level = Minecraft.getInstance().level;
if (level == null) return;
BlockHighlightUtil.render(level, Minecraft.getInstance().renderBuffers().bufferSource(), event.getPoseStack(), event.getCamera());
});

ModLoadingContext.get().registerExtensionPoint(
ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory(
Expand Down

0 comments on commit 6eca9d5

Please sign in to comment.