mirror of
https://github.com/Theta-Dev/ConstructionWand.git
synced 2025-09-15 14:17:17 +02:00
74 lines
2.8 KiB
Java
74 lines
2.8 KiB
Java
package thetadev.constructionwand.client;
|
|
|
|
import com.mojang.blaze3d.vertex.PoseStack;
|
|
import com.mojang.blaze3d.vertex.VertexConsumer;
|
|
import net.minecraft.client.renderer.LevelRenderer;
|
|
import net.minecraft.client.renderer.MultiBufferSource;
|
|
import net.minecraft.client.renderer.RenderType;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.world.entity.Entity;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.world.phys.BlockHitResult;
|
|
import net.minecraft.world.phys.HitResult;
|
|
import net.minecraftforge.client.event.DrawSelectionEvent;
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
import thetadev.constructionwand.basics.WandUtil;
|
|
import thetadev.constructionwand.items.wand.ItemWand;
|
|
import thetadev.constructionwand.wand.WandJob;
|
|
|
|
import java.util.Set;
|
|
|
|
public class RenderBlockPreview
|
|
{
|
|
public WandJob wandJob;
|
|
public Set<BlockPos> undoBlocks;
|
|
|
|
@SubscribeEvent
|
|
public void renderBlockHighlight(DrawSelectionEvent.HighlightBlock event) {
|
|
if(event.getTarget().getType() != HitResult.Type.BLOCK) return;
|
|
|
|
BlockHitResult rtr = event.getTarget();
|
|
Entity entity = event.getCamera().getEntity();
|
|
if(!(entity instanceof Player player)) return;
|
|
Set<BlockPos> blocks;
|
|
float colorR = 0, colorG = 0, colorB = 0;
|
|
|
|
ItemStack wand = WandUtil.holdingWand(player);
|
|
if(wand == null) return;
|
|
|
|
if(!(player.isCrouching() && ClientEvents.isOptKeyDown())) {
|
|
if(wandJob == null || !compareRTR(wandJob.rayTraceResult, rtr) || !(wandJob.wand.equals(wand))) {
|
|
wandJob = ItemWand.getWandJob(player, player.level, rtr, wand);
|
|
}
|
|
blocks = wandJob.getBlockPositions();
|
|
}
|
|
else {
|
|
blocks = undoBlocks;
|
|
colorG = 1;
|
|
}
|
|
|
|
if(blocks == null || blocks.isEmpty()) return;
|
|
|
|
PoseStack ms = event.getPoseStack();
|
|
MultiBufferSource buffer = event.getMultiBufferSource();
|
|
VertexConsumer lineBuilder = buffer.getBuffer(RenderType.LINES);
|
|
|
|
double partialTicks = event.getPartialTicks();
|
|
double d0 = player.xOld + (player.getX() - player.xOld) * partialTicks;
|
|
double d1 = player.yOld + player.getEyeHeight() + (player.getY() - player.yOld) * partialTicks;
|
|
double d2 = player.zOld + (player.getZ() - player.zOld) * partialTicks;
|
|
|
|
for(BlockPos block : blocks) {
|
|
AABB aabb = new AABB(block).move(-d0, -d1, -d2);
|
|
LevelRenderer.renderLineBox(ms, lineBuilder, aabb, colorR, colorG, colorB, 0.4F);
|
|
}
|
|
|
|
event.setCanceled(true);
|
|
}
|
|
|
|
private static boolean compareRTR(BlockHitResult rtr1, BlockHitResult rtr2) {
|
|
return rtr1.getBlockPos().equals(rtr2.getBlockPos()) && rtr1.getDirection().equals(rtr2.getDirection());
|
|
}
|
|
}
|