From 4ed088caee8f365e1307e404a4820d52dafe8d29 Mon Sep 17 00:00:00 2001 From: Patrick Hallbick Date: Thu, 15 Aug 2024 01:20:04 -0400 Subject: [PATCH] Fix #142 --- .../events/CoordinateCasterWorldRenderer.java | 19 ++++++++++++------- .../item/casters/CoordinateCasterCrystal.java | 8 ++++---- .../ScriptorEventsExpectPlatformImpl.java | 4 ++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/common/src/main/java/com/ssblur/scriptor/events/CoordinateCasterWorldRenderer.java b/common/src/main/java/com/ssblur/scriptor/events/CoordinateCasterWorldRenderer.java index 707d1cae..30f0426d 100644 --- a/common/src/main/java/com/ssblur/scriptor/events/CoordinateCasterWorldRenderer.java +++ b/common/src/main/java/com/ssblur/scriptor/events/CoordinateCasterWorldRenderer.java @@ -10,15 +10,16 @@ import net.minecraft.core.Direction; import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; +import org.jetbrains.annotations.Nullable; public class CoordinateCasterWorldRenderer { static final int BOX_COLOR = 0xffff5064; - public static void render(PoseStack matrix) { + public static void render(@Nullable PoseStack matrix) { var player = Minecraft.getInstance().player; - ItemStack item; - if (player != null) { + + if (player != null && matrix != null) { if (player.getItemInHand(InteractionHand.MAIN_HAND).getItem() == ScriptorItems.COORDINATE_CASTING_CRYSTAL.get()) render(matrix, player.getItemInHand(InteractionHand.MAIN_HAND)); if (player.getItemInHand(InteractionHand.OFF_HAND).getItem() == ScriptorItems.COORDINATE_CASTING_CRYSTAL.get()) @@ -27,9 +28,9 @@ public static void render(PoseStack matrix) { } public static void render(PoseStack matrix, ItemStack item) { - if(matrix == null) return; var tesselator = Tesselator.getInstance(); BufferBuilder builder; + MeshData mesh; var camera = Minecraft.getInstance().gameRenderer.getMainCamera(); var last = matrix.last(); var pose = last.pose(); @@ -39,7 +40,6 @@ public static void render(PoseStack matrix, ItemStack item) { RenderSystem.enableBlend(); RenderSystem.setShader(GameRenderer::getPositionColorShader); RenderSystem.disableCull(); - for(var pair: CoordinateCasterCrystal.getCoordinates(item)) { BlockPos pos = pair.getLeft(); Direction direction = pair.getRight(); @@ -58,6 +58,7 @@ public static void render(PoseStack matrix, ItemStack item) { case WEST -> x += 1; case EAST -> xo -= 1; } + if(direction.getAxis() == Direction.Axis.Z) { builder = tesselator.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); @@ -86,7 +87,7 @@ public static void render(PoseStack matrix, ItemStack item) { z ).setColor(BOX_COLOR); - builder.build(); + mesh = builder.build(); } else { builder = tesselator.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR); @@ -115,7 +116,11 @@ public static void render(PoseStack matrix, ItemStack item) { z ).setColor(BOX_COLOR); - builder.build(); + mesh = builder.build(); + } + + if(mesh != null) { + BufferUploader.drawWithShader(mesh); } } diff --git a/common/src/main/java/com/ssblur/scriptor/item/casters/CoordinateCasterCrystal.java b/common/src/main/java/com/ssblur/scriptor/item/casters/CoordinateCasterCrystal.java index d9ee3e1e..23971692 100644 --- a/common/src/main/java/com/ssblur/scriptor/item/casters/CoordinateCasterCrystal.java +++ b/common/src/main/java/com/ssblur/scriptor/item/casters/CoordinateCasterCrystal.java @@ -5,7 +5,6 @@ import com.ssblur.scriptor.helpers.ComponentHelper; import com.ssblur.scriptor.helpers.targetable.Targetable; import net.minecraft.ChatFormatting; -import net.minecraft.client.Minecraft; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.network.chat.Component; @@ -65,8 +64,6 @@ public List getTargetables(ItemStack itemStack, Level level) { @Override public void appendHoverText(ItemStack itemStack, TooltipContext level, List list, TooltipFlag tooltipFlag) { super.appendHoverText(itemStack, level, list, tooltipFlag); - var font = Minecraft.getInstance().font; - var coordinates = getCoordinates(itemStack); for(var pair: coordinates) { var coordinate = pair.getLeft(); @@ -98,8 +95,11 @@ public static void addCoordinate(ItemStack itemStack, BlockPos pos, Direction di var list = itemStack.get(ScriptorDataComponents.COORDINATES); if(list == null) list = new ArrayList<>(); - if(list.size() < 4) + if(list.size() < 4) { + list = new ArrayList<>(list); list.add(List.of(pos.getX(), pos.getY(), pos.getZ(), direction.ordinal())); + } + itemStack.set(ScriptorDataComponents.COORDINATES, list); } public static List getCoordinates(ItemStack itemStack) { diff --git a/fabric/src/main/java/com/ssblur/scriptor/events/fabric/ScriptorEventsExpectPlatformImpl.java b/fabric/src/main/java/com/ssblur/scriptor/events/fabric/ScriptorEventsExpectPlatformImpl.java index 4f8f8b28..8cd4788d 100644 --- a/fabric/src/main/java/com/ssblur/scriptor/events/fabric/ScriptorEventsExpectPlatformImpl.java +++ b/fabric/src/main/java/com/ssblur/scriptor/events/fabric/ScriptorEventsExpectPlatformImpl.java @@ -6,9 +6,9 @@ public class ScriptorEventsExpectPlatformImpl extends ScriptorExpectPlatform { public static void registerClientEvents() { - WorldRenderEvents.BEFORE_ENTITIES.register(context -> { - Thread.yield(); + WorldRenderEvents.AFTER_ENTITIES.register(context -> { CoordinateCasterWorldRenderer.render(context.matrixStack()); + Thread.yield(); }); } }