diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-07-22 14:43:00 +0800 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2023-08-18 18:05:10 +0800 |
commit | fc65ff5b469fb384d2df422a5a6d8437012a819b (patch) | |
tree | 0b967fa17e1f791b9efc9c630d54546fcc14a615 /src/main/java/me/xmrvizzy/skyblocker/utils | |
parent | 6069d3cf7d2e96ca7ef1975a3dd04e2121a6e3c9 (diff) | |
download | Skyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.tar.gz Skyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.tar.bz2 Skyblocker-fc65ff5b469fb384d2df422a5a6d8437012a819b.zip |
Refactor utils package
Diffstat (limited to 'src/main/java/me/xmrvizzy/skyblocker/utils')
21 files changed, 301 insertions, 729 deletions
diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Boxes.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Boxes.java index 445bcbe5..17874c9c 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Boxes.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/Boxes.java @@ -1,12 +1,10 @@ package me.xmrvizzy.skyblocker.utils; - import net.minecraft.util.math.Box; import net.minecraft.util.math.Direction.Axis; import net.minecraft.util.math.Vec3d; public class Boxes { - /** Returns the vector of the min pos of this box. **/ public static Vec3d getMinVec(Box box) { return new Vec3d(box.minX, box.minY, box.minZ); @@ -17,6 +15,11 @@ public class Boxes { return new Vec3d(box.maxX, box.maxY, box.maxZ); } + /** Returns the vector of the side lengths of this box. **/ + public static Vec3d getLengthVec(Box box) { + return new Vec3d(box.getXLength(), box.getYLength(), box.getZLength()); + } + /** Offsets this box so that minX, minY and minZ are all zero. **/ public static Box moveToZero(Box box) { return box.offset(getMinVec(box).negate()); diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/ItemUtils.java b/src/main/java/me/xmrvizzy/skyblocker/utils/ItemUtils.java index a4d1ee7b..4f74ce3a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/ItemUtils.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/ItemUtils.java @@ -11,17 +11,13 @@ import java.util.List; import java.util.regex.Pattern; public class ItemUtils { + private final static Pattern WHITESPACES = Pattern.compile("^\\s*$"); public static List<Text> getTooltip(ItemStack item) { MinecraftClient client = MinecraftClient.getInstance(); - if (client.player != null && item != null) - // return item.getTooltip(client.player, TooltipContext.Default.NORMAL); - return item.getTooltip(client.player, TooltipContext.Default.BASIC); - return Collections.emptyList(); + return client.player == null || item == null ? Collections.emptyList() : item.getTooltip(client.player, TooltipContext.Default.BASIC); } - private final static Pattern WHITESPACES = Pattern.compile("^\\s*$"); - public static List<String> getTooltipStrings(ItemStack item) { List<Text> lines = getTooltip(item); List<String> list = new ArrayList<>(); diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtils.java b/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtils.java deleted file mode 100644 index d20cfefe..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtils.java +++ /dev/null @@ -1,99 +0,0 @@ -package me.xmrvizzy.skyblocker.utils; - -import com.mojang.blaze3d.systems.RenderSystem; - -import me.xmrvizzy.skyblocker.utils.color.QuadColor; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.render.BufferBuilder; -import net.minecraft.client.render.Camera; -import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.render.Tessellator; -import net.minecraft.client.render.VertexFormat; -import net.minecraft.client.render.VertexFormats; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.entity.Entity; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Box; -import net.minecraft.util.math.Direction; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Vec3d; -// import net.minecraft.util.math.Vec3f; -import net.minecraft.util.math.RotationAxis; - -public class RenderUtils { - - // -------------------- Outline Boxes -------------------- - - public static void drawBoxOutline(BlockPos blockPos, QuadColor color, float lineWidth, Direction... excludeDirs) { - drawBoxOutline(new Box(blockPos), color, lineWidth, excludeDirs); - } - - public static void drawBoxOutline(Box box, QuadColor color, float lineWidth, Direction... excludeDirs) { - if (!FrustumUtils.isBoxVisible(box)) { - return; - } - - setup(); - - MatrixStack matrices = matrixFrom(box.minX, box.minY, box.minZ); - - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - - // Outline - RenderSystem.enableDepthTest(); - RenderSystem.disableCull(); - RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram); - RenderSystem.lineWidth(lineWidth); - - buffer.begin(VertexFormat.DrawMode.LINES, VertexFormats.LINES); - Vertexer.vertexBoxLines(matrices, buffer, Boxes.moveToZero(box), color, excludeDirs); - tessellator.draw(); - - RenderSystem.enableCull(); - RenderSystem.disableDepthTest(); - - cleanup(); - } - - // -------------------- Utils -------------------- - - public static MatrixStack matrixFrom(double x, double y, double z) { - MatrixStack matrices = new MatrixStack(); - - Camera camera = MinecraftClient.getInstance().gameRenderer.getCamera(); - // matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); - matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); - // matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(camera.getYaw() + 180.0F)); - matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(camera.getYaw() + 180.0F)); - - matrices.translate(x - camera.getPos().x, y - camera.getPos().y, z - camera.getPos().z); - - return matrices; - } - - public static Vec3d getInterpolationOffset(Entity e) { - if (MinecraftClient.getInstance().isPaused()) { - return Vec3d.ZERO; - } - - double tickDelta = MinecraftClient.getInstance().getTickDelta(); - return new Vec3d( - e.getX() - MathHelper.lerp(tickDelta, e.lastRenderX, e.getX()), - e.getY() - MathHelper.lerp(tickDelta, e.lastRenderY, e.getY()), - e.getZ() - MathHelper.lerp(tickDelta, e.lastRenderZ, e.getZ())); - } - - public static boolean pointExistsInArea(int x, int y, int x1, int y1, int x2, int y2) { - return x >= x1 && x <= x2 && y >= y1 && y <= y2; - } - - public static void setup() { - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - } - - public static void cleanup() { - RenderSystem.disableBlend(); - } -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtilsLiving.java b/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtilsLiving.java deleted file mode 100644 index 79ec18e9..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderUtilsLiving.java +++ /dev/null @@ -1,151 +0,0 @@ -package me.xmrvizzy.skyblocker.utils; - -/* - * This file is part of the MacHack distribution (https://github.com/BleachDrinker420/bleachhack-1.14/). - * Copyright (c) 2019 Bleach. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - */ - - -import com.mojang.blaze3d.systems.RenderSystem; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.font.TextRenderer; -import net.minecraft.client.render.*; -import net.minecraft.client.render.model.json.ModelTransformationMode; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.item.ItemStack; -import net.minecraft.text.Text; -import net.minecraft.util.math.RotationAxis; -import org.apache.commons.lang3.reflect.FieldUtils; -import org.joml.Vector3f; - -import java.lang.reflect.Field; - -public class RenderUtilsLiving { - - private static final MinecraftClient mc = MinecraftClient.getInstance(); - private static Field shaderLightField; - - /** Draws text in the world. **/ - public static void drawText(Text text, double x, double y, double z, double scale, boolean shadow) { - drawText(text, x, y, z, 0, 0, scale, shadow); - } - - /** Draws text in the world. **/ - public static void drawText(Text text, double x, double y, double z, double offX, double offY, double scale, boolean fill) { - MatrixStack matrices = matrixFrom(x, y, z); - - Camera camera = mc.gameRenderer.getCamera(); - // matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-camera.getYaw())); - matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(-camera.getYaw())); - // matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); - matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); - - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - - matrices.translate(offX, offY, 0); - matrices.scale(-0.025f * (float) scale, -0.025f * (float) scale, 1); - - int halfWidth = mc.textRenderer.getWidth(text) / 2; - - VertexConsumerProvider.Immediate immediate = VertexConsumerProvider.immediate(Tessellator.getInstance().getBuffer()); - - if (fill) { - int opacity = (int) (MinecraftClient.getInstance().options.getTextBackgroundOpacity(0.25F) * 255.0F) << 24; - mc.textRenderer.draw(text, -halfWidth, 0f, 553648127, false, matrices.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.NORMAL, opacity, 0xf000f0); - immediate.draw(); - } else { - matrices.push(); - matrices.translate(1, 1, 0); - mc.textRenderer.draw(text.copyContentOnly(), -halfWidth, 0f, 0x202020, false, matrices.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.NORMAL, 0, 0xf000f0); - immediate.draw(); - matrices.pop(); - } - - mc.textRenderer.draw(text, -halfWidth, 0f, -1, false, matrices.peek().getPositionMatrix(), immediate, TextRenderer.TextLayerType.NORMAL, 0, 0xf000f0); - immediate.draw(); - - RenderSystem.disableBlend(); - } - - /** Draws a 2D gui items somewhere in the world. **/ - public static void drawGuiItem(double x, double y, double z, double offX, double offY, double scale, ItemStack item) { - if (item.isEmpty()) { - return; - } - - MatrixStack matrices = matrixFrom(x, y, z); - - Camera camera = mc.gameRenderer.getCamera(); - // matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-camera.getYaw())); - matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(-camera.getYaw())); - // matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); - matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); - - matrices.translate(offX, offY, 0); - matrices.scale((float) scale, (float) scale, 0.001f); - - // matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(180f)); - matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(180f)); - - //mc.getBufferBuilders().getEntityVertexConsumers().draw(); - - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - - // Vec3f[] currentLight = getCurrentLight(); - Vector3f[] currentLight = getCurrentLight(); - DiffuseLighting.disableGuiDepthLighting(); - - mc.getBufferBuilders().getEntityVertexConsumers().draw(); - - mc.getItemRenderer().renderItem(item, ModelTransformationMode.GUI, 0xF000F0, - OverlayTexture.DEFAULT_UV, matrices, mc.getBufferBuilders().getEntityVertexConsumers(), mc.world, 0); - - mc.getBufferBuilders().getEntityVertexConsumers().draw(); - - RenderSystem.setShaderLights(currentLight[0], currentLight[1]); - RenderSystem.disableBlend(); - } - - public static MatrixStack matrixFrom(double x, double y, double z) { - MatrixStack matrices = new MatrixStack(); - - Camera camera = mc.gameRenderer.getCamera(); - // matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(camera.getPitch())); - matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(camera.getPitch())); - // matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(camera.getYaw() + 180.0F)); - matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(camera.getYaw() + 180.0F)); - - matrices.translate(x - camera.getPos().x, y - camera.getPos().y, z - camera.getPos().z); - - return matrices; - } - - // public static Vec3f[] getCurrentLight() { - public static Vector3f[] getCurrentLight() { - if (shaderLightField == null) { - shaderLightField = FieldUtils.getField(RenderSystem.class, "shaderLightDirections", true); - } - - try { - // return (Vec3f[]) shaderLightField.get(null); - return (Vector3f[]) shaderLightField.get(null); - } catch (Exception e) { - throw new RuntimeException(e); - } - } -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Vertexer.java b/src/main/java/me/xmrvizzy/skyblocker/utils/Vertexer.java deleted file mode 100644 index cf6d90b6..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Vertexer.java +++ /dev/null @@ -1,164 +0,0 @@ -package me.xmrvizzy.skyblocker.utils; - -import me.xmrvizzy.skyblocker.utils.color.LineColor; -import me.xmrvizzy.skyblocker.utils.color.QuadColor; -import net.minecraft.client.render.VertexConsumer; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.util.math.Box; -import net.minecraft.util.math.Direction; -import net.minecraft.util.math.MathHelper; -// import net.minecraft.util.math.Matrix3f; -// import net.minecraft.util.math.Matrix4f; -// import net.minecraft.util.math.Vec3f; -import org.joml.Matrix3f; -import org.joml.Matrix4f; -import org.joml.Vector3f; -import org.apache.commons.lang3.ArrayUtils; - -public class Vertexer { - - public static final int CULL_BACK = 0; - public static final int CULL_FRONT = 1; - public static final int CULL_NONE = 2; - - public static void vertexBoxQuads(MatrixStack matrices, VertexConsumer vertexConsumer, Box box, QuadColor quadColor, Direction... excludeDirs) { - float x1 = (float) box.minX; - float y1 = (float) box.minY; - float z1 = (float) box.minZ; - float x2 = (float) box.maxX; - float y2 = (float) box.maxY; - float z2 = (float) box.maxZ; - - int cullMode = excludeDirs.length == 0 ? CULL_BACK : CULL_NONE; - - if (!ArrayUtils.contains(excludeDirs, Direction.DOWN)) { - vertexQuad(matrices, vertexConsumer, x1, y1, z1, x2, y1, z1, x2, y1, z2, x1, y1, z2, cullMode, quadColor); - } - - if (!ArrayUtils.contains(excludeDirs, Direction.WEST)) { - vertexQuad(matrices, vertexConsumer, x1, y1, z2, x1, y2, z2, x1, y2, z1, x1, y1, z1, cullMode, quadColor); - } - - if (!ArrayUtils.contains(excludeDirs, Direction.EAST)) { - vertexQuad(matrices, vertexConsumer, x2, y1, z1, x2, y2, z1, x2, y2, z2, x2, y1, z2, cullMode, quadColor); - } - - if (!ArrayUtils.contains(excludeDirs, Direction.NORTH)) { - vertexQuad(matrices, vertexConsumer, x1, y1, z1, x1, y2, z1, x2, y2, z1, x2, y1, z1, cullMode, quadColor); - } - - if (!ArrayUtils.contains(excludeDirs, Direction.SOUTH)) { - vertexQuad(matrices, vertexConsumer, x2, y1, z2, x2, y2, z2, x1, y2, z2, x1, y1, z2, cullMode, quadColor); - } - - if (!ArrayUtils.contains(excludeDirs, Direction.UP)) { - vertexQuad(matrices, vertexConsumer, x1, y2, z2, x2, y2, z2, x2, y2, z1, x1, y2, z1, cullMode, quadColor); - } - } - - public static void vertexQuad(MatrixStack matrices, VertexConsumer vertexConsumer, float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3, float x4, float y4, float z4, int cullMode, QuadColor quadColor) { - int[] color = quadColor.getAllColors(); - - if (cullMode != CULL_FRONT) { - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x1, y1, z1).color(color[0], color[1], color[2], color[3]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x2, y2, z2).color(color[4], color[5], color[6], color[7]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x3, y3, z3).color(color[8], color[9], color[10], color[11]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x4, y4, z4).color(color[12], color[13], color[14], color[15]).next(); - } - - if (cullMode != CULL_BACK) { - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x4, y4, z4).color(color[0], color[1], color[2], color[3]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x3, y3, z3).color(color[4], color[5], color[6], color[7]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x2, y2, z2).color(color[8], color[9], color[10], color[11]).next(); - vertexConsumer.vertex(matrices.peek().getPositionMatrix(), x1, y1, z1).color(color[12], color[13], color[14], color[15]).next(); - } - } - - public static void vertexBoxLines(MatrixStack matrices, VertexConsumer vertexConsumer, Box box, QuadColor quadColor, Direction... excludeDirs) { - float x1 = (float) box.minX; - float y1 = (float) box.minY; - float z1 = (float) box.minZ; - float x2 = (float) box.maxX; - float y2 = (float) box.maxY; - float z2 = (float) box.maxZ; - - boolean exDown = ArrayUtils.contains(excludeDirs, Direction.DOWN); - boolean exWest = ArrayUtils.contains(excludeDirs, Direction.WEST); - boolean exEast = ArrayUtils.contains(excludeDirs, Direction.EAST); - boolean exNorth = ArrayUtils.contains(excludeDirs, Direction.NORTH); - boolean exSouth = ArrayUtils.contains(excludeDirs, Direction.SOUTH); - boolean exUp = ArrayUtils.contains(excludeDirs, Direction.UP); - - int[] color = quadColor.getAllColors(); - - if (!exDown) { - vertexLine(matrices, vertexConsumer, x1, y1, z1, x2, y1, z1, LineColor.single(color[0], color[1], color[2], color[3])); - vertexLine(matrices, vertexConsumer, x2, y1, z1, x2, y1, z2, LineColor.single(color[4], color[5], color[6], color[7])); - vertexLine(matrices, vertexConsumer, x2, y1, z2, x1, y1, z2, LineColor.single(color[8], color[9], color[10], color[11])); - vertexLine(matrices, vertexConsumer, x1, y1, z2, x1, y1, z1, LineColor.single(color[12], color[13], color[14], color[15])); - } - - if (!exWest) { - if (exDown) vertexLine(matrices, vertexConsumer, x1, y1, z1, x1, y1, z2, LineColor.single(color[0], color[1], color[2], color[3])); - vertexLine(matrices, vertexConsumer, x1, y1, z2, x1, y2, z2, LineColor.single(color[4], color[5], color[6], color[7])); - vertexLine(matrices, vertexConsumer, x1, y1, z1, x1, y2, z1, LineColor.single(color[8], color[9], color[10], color[11])); - if (exUp) vertexLine(matrices, vertexConsumer, x1, y2, z1, x1, y2, z2, LineColor.single(color[12], color[13], color[14], color[15])); - } - - if (!exEast) { - if (exDown) vertexLine(matrices, vertexConsumer, x2, y1, z1, x2, y1, z2, LineColor.single(color[0], color[1], color[2], color[3])); - vertexLine(matrices, vertexConsumer, x2, y1, z2, x2, y2, z2, LineColor.single(color[4], color[5], color[6], color[7])); - vertexLine(matrices, vertexConsumer, x2, y1, z1, x2, y2, z1, LineColor.single(color[8], color[9], color[10], color[11])); - if (exUp) vertexLine(matrices, vertexConsumer, x2, y2, z1, x2, y2, z2, LineColor.single(color[12], color[13], color[14], color[15])); - } - - if (!exNorth) { - if (exDown) vertexLine(matrices, vertexConsumer, x1, y1, z1, x2, y1, z1, LineColor.single(color[0], color[1], color[2], color[3])); - if (exEast) vertexLine(matrices, vertexConsumer, x2, y1, z1, x2, y2, z1, LineColor.single(color[4], color[5], color[6], color[7])); - if (exWest) vertexLine(matrices, vertexConsumer, x1, y1, z1, x1, y2, z1, LineColor.single(color[8], color[9], color[10], color[11])); - if (exUp) vertexLine(matrices, vertexConsumer, x1, y2, z1, x2, y2, z1, LineColor.single(color[12], color[13], color[14], color[15])); - } - - if (!exSouth) { - if (exDown) vertexLine(matrices, vertexConsumer, x1, y1, z2, x2, y1, z2, LineColor.single(color[0], color[1], color[2], color[3])); - if (exEast) vertexLine(matrices, vertexConsumer, x2, y1, z2, x2, y2, z2, LineColor.single(color[4], color[5], color[6], color[7])); - if (exWest) vertexLine(matrices, vertexConsumer, x1, y1, z2, x1, y2, z2, LineColor.single(color[8], color[9], color[10], color[11])); - if (exUp) vertexLine(matrices, vertexConsumer, x1, y2, z2, x2, y2, z2, LineColor.single(color[12], color[13], color[14], color[15])); - } - - if (!exUp) { - vertexLine(matrices, vertexConsumer, x1, y2, z1, x2, y2, z1, LineColor.single(color[0], color[1], color[2], color[3])); - vertexLine(matrices, vertexConsumer, x2, y2, z1, x2, y2, z2, LineColor.single(color[4], color[5], color[6], color[7])); - vertexLine(matrices, vertexConsumer, x2, y2, z2, x1, y2, z2, LineColor.single(color[8], color[9], color[10], color[11])); - vertexLine(matrices, vertexConsumer, x1, y2, z2, x1, y2, z1, LineColor.single(color[12], color[13], color[14], color[15])); - } - } - - public static void vertexLine(MatrixStack matrices, VertexConsumer vertexConsumer, float x1, float y1, float z1, float x2, float y2, float z2, LineColor lineColor) { - Matrix4f model = matrices.peek().getPositionMatrix(); - Matrix3f normal = matrices.peek().getNormalMatrix(); - - // Vec3f normalVec = getNormal(normal, x1, y1, z1, x2, y2, z2); - Vector3f normalVec = getNormal(normal, x1, y1, z1, x2, y2, z2); - - int[] color1 = lineColor.getColor(x1, y1, z1, 0); - int[] color2 = lineColor.getColor(x2, y2, z2, 1); - - // vertexConsumer.vertex(model, x1, y1, z1).color(color1[0], color1[1], color1[2], color1[3]).normal(normal, normalVec.getX(), normalVec.getY(), normalVec.getZ()).next(); - vertexConsumer.vertex(model, x1, y1, z1).color(color1[0], color1[1], color1[2], color1[3]).normal(normal, normalVec.x(), normalVec.y(), normalVec.z()).next(); - // vertexConsumer.vertex(model, x2, y2, z2).color(color2[0], color2[1], color2[2], color2[3]).normal(normal, normalVec.getX(), normalVec.getY(), normalVec.getZ()).next(); - vertexConsumer.vertex(model, x2, y2, z2).color(color2[0], color2[1], color2[2], color2[3]).normal(normal, normalVec.x(), normalVec.y(), normalVec.z()).next(); - } - - // public static Vec3f getNormal(Matrix3f normal, float x1, float y1, float z1, float x2, float y2, float z2) { - public static Vector3f getNormal(Matrix3f normal, float x1, float y1, float z1, float x2, float y2, float z2) { - float xNormal = x2 - x1; - float yNormal = y2 - y1; - float zNormal = z2 - z1; - float normalSqrt = MathHelper.sqrt(xNormal * xNormal + yNormal * yNormal + zNormal * zNormal); - - // return new Vec3f(xNormal / normalSqrt, yNormal / normalSqrt, zNormal / normalSqrt); - return new Vector3f(xNormal / normalSqrt, yNormal / normalSqrt, zNormal / normalSqrt); - } - -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/color/LineColor.java b/src/main/java/me/xmrvizzy/skyblocker/utils/color/LineColor.java deleted file mode 100644 index 42cf5926..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/color/LineColor.java +++ /dev/null @@ -1,66 +0,0 @@ -package me.xmrvizzy.skyblocker.utils.color; - -import com.mojang.datafixers.util.Function4; - -public class LineColor extends RenderColor { - - private final Function4<Float, Float, Float, Integer, int[]> getColorFunc; - - public static LineColor single(float red, float green, float blue, float alpha) { - return LineColor.single((int) (red * 255f), (int) (green * 255f), (int) (blue * 255f), (int) (alpha * 255f)); - } - - public static LineColor single(int color) { - return LineColor.single((color & 0xff0000) >> 16, (color & 0xff00) >> 8, color & 0xff, color >> 24 & 0xff); - } - - public static LineColor single(int red, int green, int blue, int alpha) { - return new LineColor((x, y, z, curVertex) -> new int[] { red, green, blue, alpha }); - } - - public static LineColor gradient(float red1, float green1, float blue1, float alpha1, float red2, float green2, float blue2, float alpha2) { - return LineColor.gradient( - (int) (red1 * 255f), (int) (green1 * 255f), (int) (blue1 * 255f), (int) (alpha1 * 255f), - (int) (red2 * 255f), (int) (green2 * 255f), (int) (blue2 * 255f), (int) (alpha2 * 255f)); - } - - public static LineColor gradient(int color1, int color2) { - return LineColor.gradient( - (color1 & 0xff0000) >> 16, (color1 & 0xff00) >> 8, color1 & 0xff, color1 >> 24 & 0xff, - (color2 & 0xff0000) >> 16, (color2 & 0xff00) >> 8, color2 & 0xff, color2 >> 24 & 0xff); - } - - public static LineColor gradient(int red1, int green1, int blue1, int alpha1, int red2, int green2, int blue2, int alpha2) { - return new LineColor((x, y, z, curVertex) -> { - if (curVertex == 0) { - return new int[] { red1, green1, blue1, alpha1 }; - } - - return new int[] { red2, green2, blue2, alpha2 }; - }); - } - - private LineColor(Function4<Float, Float, Float, Integer, int[]> getColorFunc) { - this.getColorFunc = getColorFunc; - } - - public int[] getColor(float x, float y, float z, int curVertex) { - int[] outColor = getColorFunc.apply(x, y, z, curVertex); - - for (int i = 0; i < 4; i++) { - if (overwriteColor[i] != null) { - outColor[i] = overwriteColor[i]; - } - } - - return outColor; - } - - public LineColor clone() { - LineColor newColor = new LineColor(getColorFunc); - cloneOverwriteTo(newColor); - - return newColor; - } - -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/color/QuadColor.java b/src/main/java/me/xmrvizzy/skyblocker/utils/color/QuadColor.java deleted file mode 100644 index cb1ce66e..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/color/QuadColor.java +++ /dev/null @@ -1,107 +0,0 @@ -package me.xmrvizzy.skyblocker.utils.color; - - -import java.util.function.Function; - -public class QuadColor extends RenderColor { - - private final Function<Integer, int[]> getColorFunc; - - public static QuadColor single(float red, float green, float blue, float alpha) { - return QuadColor.single((int) (red * 255f), (int) (green * 255f), (int) (blue * 255f), (int) (alpha * 255f)); - } - - public static QuadColor single(int red, int green, int blue, int alpha) { - return new QuadColor(curVertex -> new int[]{red, green, blue, alpha}); - } - - public static QuadColor gradient(int red1, int green1, int blue1, int alpha1, int red2, int green2, int blue2, int alpha2, CardinalDirection direction) { - return new QuadColor(curVertex -> { - if (direction.isStartVertex(curVertex)) { - return new int[]{red1, green1, blue1, alpha1}; - } - - return new int[]{red2, green2, blue2, alpha2}; - }); - } - - public static QuadColor custom(float red1, float green1, float blue1, float alpha1, float red2, float green2, float blue2, float alpha2, float red3, float green3, float blue3, float alpha3, float red4, float green4, float blue4, float alpha4) { - return QuadColor.custom( - (int) (red1 * 255f), (int) (green1 * 255f), (int) (blue1 * 255f), (int) (alpha1 * 255f), - (int) (red2 * 255f), (int) (green2 * 255f), (int) (blue2 * 255f), (int) (alpha2 * 255f), - (int) (red3 * 255f), (int) (green3 * 255f), (int) (blue3 * 255f), (int) (alpha3 * 255f), - (int) (red4 * 255f), (int) (green4 * 255f), (int) (blue4 * 255f), (int) (alpha4 * 255f)); - } - - public static QuadColor custom(int color1, int color2, int color3, int color4) { - return QuadColor.custom( - (color1 & 0xff0000) >> 16, (color1 & 0xff00) >> 8, color1 & 0xff, color1 >> 24 & 0xff, - (color2 & 0xff0000) >> 16, (color2 & 0xff00) >> 8, color2 & 0xff, color2 >> 24 & 0xff, - (color3 & 0xff0000) >> 16, (color3 & 0xff00) >> 8, color3 & 0xff, color3 >> 24 & 0xff, - (color4 & 0xff0000) >> 16, (color4 & 0xff00) >> 8, color4 & 0xff, color4 >> 24 & 0xff); - } - - public static QuadColor custom(int red1, int green1, int blue1, int alpha1, int red2, int green2, int blue2, int alpha2, int red3, int green3, int blue3, int alpha3, int red4, int green4, int blue4, int alpha4) { - return new QuadColor(curVertex -> switch (curVertex) { - case 0 -> new int[]{red1, green1, blue1, alpha1}; - case 1 -> new int[]{red2, green2, blue2, alpha2}; - case 2 -> new int[]{red3, green3, blue3, alpha3}; - default -> new int[]{red4, green4, blue4, alpha4}; - }); - } - - private QuadColor(Function<Integer, int[]> getColorFunc) { - this.getColorFunc = getColorFunc; - } - - public int[] getColor(int curVertex) { - int[] outColor = getColorFunc.apply(curVertex); - - for (int i = 0; i < 4; i++) { - if (overwriteColor[i] != null) { - outColor[i] = overwriteColor[i]; - } - } - - return outColor; - } - - public int[] getAllColors() { - int[] outColor = new int[16]; - - for (int i = 0; i < 4; i++) { - int[] curColor = getColor(i); - - System.arraycopy(curColor, 0, outColor, i * 4, 4); - } - - return outColor; - } - - public QuadColor clone() { - QuadColor newColor = new QuadColor(getColorFunc); - cloneOverwriteTo(newColor); - - return newColor; - } - - public enum CardinalDirection { - NORTH(3, 0), - EAST(0, 1), - SOUTH(1, 2), - WEST(2, 3); - - public final int vertex1; - public final int vertex2; - - CardinalDirection(int vertex1, int vertex2) { - this.vertex1 = vertex1; - this.vertex2 = vertex2; - } - - public boolean isStartVertex(int vertex) { - return vertex == vertex1 || vertex == vertex2; - } - } - -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/color/RenderColor.java b/src/main/java/me/xmrvizzy/skyblocker/utils/color/RenderColor.java deleted file mode 100644 index 89dc37ca..00000000 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/color/RenderColor.java +++ /dev/null @@ -1,27 +0,0 @@ -package me.xmrvizzy.skyblocker.utils.color; - -public abstract class RenderColor implements Cloneable { - - protected Integer[] overwriteColor = new Integer[4]; - - public void overwriteRed(Integer red) { - overwriteColor[0] = red; - } - - public void overwriteGreen(Integer green) { - overwriteColor[1] = green; - } - - public void overwriteBlue(Integer blue) { - overwriteColor[2] = blue; - } - - public void overwriteAlpha(Integer alpha) { - overwriteColor[3] = alpha; - } - - protected void cloneOverwriteTo(RenderColor otherColor) { - otherColor.overwriteColor = overwriteColor.clone(); - } - -}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/FrustumUtils.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/FrustumUtils.java index fd8ffdbd..d4dd8b18 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/FrustumUtils.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/FrustumUtils.java @@ -1,4 +1,4 @@ -package me.xmrvizzy.skyblocker.utils; +package me.xmrvizzy.skyblocker.utils.render; import me.xmrvizzy.skyblocker.mixin.accessor.WorldRendererAccessor; import me.xmrvizzy.skyblocker.mixin.accessor.FrustumInvoker; @@ -7,12 +7,11 @@ import net.minecraft.client.render.Frustum; import net.minecraft.util.math.Box; public class FrustumUtils { - public static Frustum getFrustum() { return ((WorldRendererAccessor) MinecraftClient.getInstance().worldRenderer).getFrustum(); } - public static boolean isBoxVisible(Box box) { + public static boolean isVisible(Box box) { return getFrustum().isVisible(box); } diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderHelper.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/RenderHelper.java index 8f0f7860..77da702a 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/RenderHelper.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/RenderHelper.java @@ -1,36 +1,30 @@ -package me.xmrvizzy.skyblocker.utils; +package me.xmrvizzy.skyblocker.utils.render; +import com.mojang.blaze3d.platform.GlStateManager.DstFactor; +import com.mojang.blaze3d.platform.GlStateManager.SrcFactor; +import com.mojang.blaze3d.systems.RenderSystem; import me.x150.renderer.render.Renderer3d; import me.xmrvizzy.skyblocker.mixin.accessor.BeaconBlockEntityRendererInvoker; -import me.xmrvizzy.skyblocker.utils.culling.OcclusionCulling; -import me.xmrvizzy.skyblocker.utils.title.Title; -import me.xmrvizzy.skyblocker.utils.title.TitleContainer; +import me.xmrvizzy.skyblocker.utils.Boxes; +import me.xmrvizzy.skyblocker.utils.render.culling.OcclusionCulling; +import me.xmrvizzy.skyblocker.utils.render.title.Title; +import me.xmrvizzy.skyblocker.utils.render.title.TitleContainer; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.minecraft.client.MinecraftClient; -import net.minecraft.client.render.BufferBuilder; -import net.minecraft.client.render.Camera; -import net.minecraft.client.render.GameRenderer; -import net.minecraft.client.render.Tessellator; -import net.minecraft.client.render.VertexConsumerProvider; -import net.minecraft.client.render.VertexFormats; +import net.minecraft.client.render.*; import net.minecraft.client.render.VertexFormat.DrawMode; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.sound.SoundEvent; -import net.minecraft.text.Text; -import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Box; import net.minecraft.util.math.Vec3d; -import java.awt.*; - import org.joml.Matrix3f; import org.joml.Matrix4f; import org.joml.Vector3f; import org.lwjgl.opengl.GL11; -import com.mojang.blaze3d.platform.GlStateManager.DstFactor; -import com.mojang.blaze3d.platform.GlStateManager.SrcFactor; -import com.mojang.blaze3d.systems.RenderSystem; +import java.awt.*; public class RenderHelper { private static final Vec3d ONE = new Vec3d(1, 1, 1); @@ -44,8 +38,8 @@ public class RenderHelper { public static void renderFilledThroughWalls(WorldRenderContext context, BlockPos pos, float[] colorComponents, float alpha) { if (FrustumUtils.isVisible(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1)) { Renderer3d.renderThroughWalls(); - renderFilled(context, pos, colorComponents, alpha); - Renderer3d.stopRenderThroughWalls(); + renderFilled(context, pos, colorComponents, alpha); + Renderer3d.stopRenderThroughWalls(); } } @@ -63,84 +57,84 @@ public class RenderHelper { if (FrustumUtils.isVisible(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, MAX_OVERWORLD_BUILD_HEIGHT, pos.getZ() + 1)) { MatrixStack matrices = context.matrixStack(); Vec3d camera = context.camera().getPos(); - + matrices.push(); matrices.translate(pos.getX() - camera.x, pos.getY() - camera.y, pos.getZ() - camera.z); - + Tessellator tessellator = RenderSystem.renderThreadTesselator(); BufferBuilder buffer = tessellator.getBuffer(); VertexConsumerProvider.Immediate consumer = VertexConsumerProvider.immediate(buffer); - + BeaconBlockEntityRendererInvoker.renderBeam(matrices, consumer, context.tickDelta(), context.world().getTime(), 0, MAX_OVERWORLD_BUILD_HEIGHT, colorComponents); - + consumer.draw(); matrices.pop(); } } - - /** - * Draws lines from point to point.<br><br> - * - * Tip: To draw lines from the center of a block, offset the X, Y and Z each by 0.5 - * - * @param context The WorldRenderContext which supplies the matrices and tick delta - * @param points The points from which to draw lines between - * @param colorComponents An array of R, G and B color components - * @param alpha The alpha of the lines - * @param lineWidth The width of the lines - */ - public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] points, float[] colorComponents, float alpha, float lineWidth) { - Vec3d camera = context.camera().getPos(); - MatrixStack matrices = context.matrixStack(); - - matrices.push(); - matrices.translate(-camera.x, -camera.y, -camera.z); - - Tessellator tessellator = RenderSystem.renderThreadTesselator(); - BufferBuilder buffer = tessellator.getBuffer(); - Matrix4f projectionMatrix = matrices.peek().getPositionMatrix(); - Matrix3f normalMatrix = matrices.peek().getNormalMatrix(); - - GL11.glEnable(GL11.GL_LINE_SMOOTH); - GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST); - - RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram); - RenderSystem.setShaderColor(1f, 1f, 1f, 1f); - RenderSystem.lineWidth(lineWidth); - RenderSystem.enableBlend(); - RenderSystem.blendFunc(SrcFactor.SRC_ALPHA, DstFactor.ONE_MINUS_SRC_ALPHA); - RenderSystem.disableCull(); - RenderSystem.enableDepthTest(); - - buffer.begin(DrawMode.LINE_STRIP, VertexFormats.LINES); - - for (int i = 0; i < points.length; i++) { - Vec3d point = points[i]; - Vec3d nextPoint = (i + 1 == points.length) ? points[i - 1] : points[i + 1]; - Vector3f normalVec = new Vector3f((float) nextPoint.getX(), (float) nextPoint.getY(), (float) nextPoint.getZ()).sub((float) point.getX(), (float) point.getY(), (float) point.getZ()).normalize(); - - buffer - .vertex(projectionMatrix, (float) point.getX(), (float) point.getY(), (float) point.getZ()) - .color(colorComponents[0], colorComponents[1], colorComponents[2], alpha) - .normal(normalMatrix, normalVec.x, normalVec.y, normalVec.z) - .next(); - } - - tessellator.draw(); - - matrices.pop(); - GL11.glDisable(GL11.GL_LINE_SMOOTH); - RenderSystem.lineWidth(1f); - RenderSystem.disableBlend(); - RenderSystem.defaultBlendFunc(); - RenderSystem.enableCull(); - RenderSystem.disableDepthTest(); - } - - public static void displayTitleAndPlaySound(int stayTicks, int fadeOutTicks, String titleKey, Formatting formatting) { - MinecraftClient.getInstance().inGameHud.setTitleTicks(0, stayTicks, fadeOutTicks); - MinecraftClient.getInstance().inGameHud.setTitle(Text.translatable(titleKey).formatted(formatting)); - playNotificationSound(); + + public static void renderOutline(WorldRenderContext context, Box box, float[] colorComponents) { + if (FrustumUtils.isVisible(box)) { + Renderer3d.renderOutline(context.matrixStack(), new Color(colorComponents[0], colorComponents[1], colorComponents[2]), Boxes.getMinVec(box), Boxes.getLengthVec(box)); + } + } + + /** + * Draws lines from point to point.<br><br> + * <p> + * Tip: To draw lines from the center of a block, offset the X, Y and Z each by 0.5 + * + * @param context The WorldRenderContext which supplies the matrices and tick delta + * @param points The points from which to draw lines between + * @param colorComponents An array of R, G and B color components + * @param alpha The alpha of the lines + * @param lineWidth The width of the lines + */ + public static void renderLinesFromPoints(WorldRenderContext context, Vec3d[] points, float[] colorComponents, float alpha, float lineWidth) { + Vec3d camera = context.camera().getPos(); + MatrixStack matrices = context.matrixStack(); + + matrices.push(); + matrices.translate(-camera.x, -camera.y, -camera.z); + + Tessellator tessellator = RenderSystem.renderThreadTesselator(); + BufferBuilder buffer = tessellator.getBuffer(); + Matrix4f projectionMatrix = matrices.peek().getPositionMatrix(); + Matrix3f normalMatrix = matrices.peek().getNormalMatrix(); + + GL11.glEnable(GL11.GL_LINE_SMOOTH); + GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST); + + RenderSystem.setShader(GameRenderer::getRenderTypeLinesProgram); + RenderSystem.setShaderColor(1f, 1f, 1f, 1f); + RenderSystem.lineWidth(lineWidth); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(SrcFactor.SRC_ALPHA, DstFactor.ONE_MINUS_SRC_ALPHA); + RenderSystem.disableCull(); + RenderSystem.enableDepthTest(); + + buffer.begin(DrawMode.LINE_STRIP, VertexFormats.LINES); + + for (int i = 0; i < points.length; i++) { + Vec3d point = points[i]; + Vec3d nextPoint = (i + 1 == points.length) ? points[i - 1] : points[i + 1]; + Vector3f normalVec = new Vector3f((float) nextPoint.getX(), (float) nextPoint.getY(), (float) nextPoint.getZ()).sub((float) point.getX(), (float) point.getY(), (float) point.getZ()).normalize(); + + buffer + .vertex(projectionMatrix, (float) point.getX(), (float) point.getY(), (float) point.getZ()) + .color(colorComponents[0], colorComponents[1], colorComponents[2], alpha) + .normal(normalMatrix, normalVec.x, normalVec.y, normalVec.z) + .next(); + } + + tessellator.draw(); + + matrices.pop(); + GL11.glDisable(GL11.GL_LINE_SMOOTH); + RenderSystem.lineWidth(1f); + RenderSystem.disableBlend(); + RenderSystem.defaultBlendFunc(); + RenderSystem.enableCull(); + RenderSystem.disableDepthTest(); } /** @@ -173,4 +167,8 @@ public class RenderHelper { MinecraftClient.getInstance().player.playSound(SoundEvent.of(new Identifier("entity.experience_orb.pickup")), 100f, 0.1f); } } + + public static boolean pointIsInArea(double x, double y, double x1, double y1, double x2, double y2) { + return x >= x1 && x <= x2 && y >= y1 && y <= y2; + } } diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/OcclusionCulling.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java index e9a25fc3..5c7f69ad 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/OcclusionCulling.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/OcclusionCulling.java @@ -1,10 +1,9 @@ -package me.xmrvizzy.skyblocker.utils.culling; +package me.xmrvizzy.skyblocker.utils.render.culling; import com.logisticscraft.occlusionculling.OcclusionCullingInstance; import com.logisticscraft.occlusionculling.cache.ArrayOcclusionCache; import com.logisticscraft.occlusionculling.util.Vec3d; - -import me.xmrvizzy.skyblocker.utils.FrustumUtils; +import me.xmrvizzy.skyblocker.utils.render.FrustumUtils; import net.minecraft.client.MinecraftClient; public class OcclusionCulling { @@ -13,9 +12,9 @@ public class OcclusionCulling { private static OcclusionCullingInstance instance = null; // Reused objects to reduce allocation overhead - private static Vec3d cameraPos = new Vec3d(0, 0, 0); - private static Vec3d min = new Vec3d(0, 0, 0); - private static Vec3d max = new Vec3d(0, 0, 0); + private static final Vec3d cameraPos = new Vec3d(0, 0, 0); + private static final Vec3d min = new Vec3d(0, 0, 0); + private static final Vec3d max = new Vec3d(0, 0, 0); /** * Initializes the occlusion culling instance diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/WorldProvider.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java index 47d92c1f..09a7bc79 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/WorldProvider.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/WorldProvider.java @@ -1,7 +1,6 @@ -package me.xmrvizzy.skyblocker.utils.culling; +package me.xmrvizzy.skyblocker.utils.render.culling; import com.logisticscraft.occlusionculling.DataProvider; - import net.minecraft.client.MinecraftClient; import net.minecraft.client.world.ClientWorld; import net.minecraft.util.math.BlockPos; diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/package-info.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java index c25e7f7a..97ebac86 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/culling/package-info.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/culling/package-info.java @@ -1,4 +1,4 @@ /** * Package dedicated to occlusion culling utilities */ -package me.xmrvizzy.skyblocker.utils.culling;
\ No newline at end of file +package me.xmrvizzy.skyblocker.utils.render.culling;
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ColorHighlight.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ColorHighlight.java new file mode 100644 index 00000000..41cd0778 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ColorHighlight.java @@ -0,0 +1,24 @@ +package me.xmrvizzy.skyblocker.utils.render.gui; + +public record ColorHighlight(int slot, int color) { + private static final int RED_HIGHLIGHT = 64 << 24 | 255 << 16; + private static final int YELLOW_HIGHLIGHT = 128 << 24 | 255 << 16 | 255 << 8; + private static final int GREEN_HIGHLIGHT = 128 << 24 | 64 << 16 | 196 << 8 | 64; + private static final int GRAY_HIGHLIGHT = 128 << 24 | 64 << 16 | 64 << 8 | 64; + + public static ColorHighlight red(int slot) { + return new ColorHighlight(slot, RED_HIGHLIGHT); + } + + public static ColorHighlight yellow(int slot) { + return new ColorHighlight(slot, YELLOW_HIGHLIGHT); + } + + public static ColorHighlight green(int slot) { + return new ColorHighlight(slot, GREEN_HIGHLIGHT); + } + + public static ColorHighlight gray(int slot) { + return new ColorHighlight(slot, GRAY_HIGHLIGHT); + } +}
\ No newline at end of file diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolver.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolver.java new file mode 100644 index 00000000..83c0f717 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolver.java @@ -0,0 +1,44 @@ +package me.xmrvizzy.skyblocker.utils.render.gui; + +import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; +import net.minecraft.item.ItemStack; + +import java.util.List; +import java.util.Map; +import java.util.regex.Pattern; + +/** + * Abstract class for gui solvers. Extend this class to add a new gui solver, like terminal solvers or experiment solvers. + */ +public abstract class ContainerSolver { + private final Pattern containerName; + + protected ContainerSolver(String containerName) { + this.containerName = Pattern.compile(containerName); + } + + protected abstract boolean isEnabled(); + + public Pattern getName() { + return containerName; + } + + protected void start(GenericContainerScreen screen) { + } + + protected void reset() { + } + + protected abstract List<ColorHighlight> getColors(String[] groups, Map<Integer, ItemStack> slots); + + protected void trimEdges(Map<Integer, ItemStack> slots, int rows) { + for (int i = 0; i < rows; i++) { + slots.remove(9 * i); + slots.remove(9 * i + 8); + } + for (int i = 1; i < 8; i++) { + slots.remove(i); + slots.remove((rows - 1) * 9 + i); + } + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolverManager.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolverManager.java new file mode 100644 index 00000000..be1d01b4 --- /dev/null +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/gui/ContainerSolverManager.java @@ -0,0 +1,124 @@ +package me.xmrvizzy.skyblocker.utils.render.gui; + +import com.mojang.blaze3d.systems.RenderSystem; +import me.xmrvizzy.skyblocker.mixin.accessor.HandledScreenAccessor; +import me.xmrvizzy.skyblocker.skyblock.dungeon.CroesusHelper; +import me.xmrvizzy.skyblocker.skyblock.dungeon.terminal.ColorTerminal; +import me.xmrvizzy.skyblocker.skyblock.dungeon.terminal.OrderTerminal; +import me.xmrvizzy.skyblocker.skyblock.dungeon.terminal.StartsWithTerminal; +import me.xmrvizzy.skyblocker.skyblock.experiment.ChronomatronSolver; +import me.xmrvizzy.skyblocker.skyblock.experiment.SuperpairsSolver; +import me.xmrvizzy.skyblocker.skyblock.experiment.UltrasequencerSolver; +import me.xmrvizzy.skyblocker.utils.Utils; +import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.ingame.GenericContainerScreen; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.item.ItemStack; +import net.minecraft.screen.slot.Slot; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Map; +import java.util.TreeMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Manager class for {@link ContainerSolver}s like terminal solvers and experiment solvers. To add a new gui solver, extend {@link ContainerSolver} and register it in {@link #ContainerSolverManager()}. + */ +public class ContainerSolverManager { + private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile(""); + private final ContainerSolver[] solvers; + private ContainerSolver currentSolver = null; + private String[] groups; + private List<ColorHighlight> highlights; + + public ContainerSolverManager() { + solvers = new ContainerSolver[]{ + new ColorTerminal(), + new OrderTerminal(), + new StartsWithTerminal(), + new CroesusHelper(), + new ChronomatronSolver(), + new SuperpairsSolver(), + new UltrasequencerSolver() + }; + } + + public ContainerSolver getCurrentSolver() { + return currentSolver; + } + + public void init() { + ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> { + if (Utils.isOnSkyblock() && screen instanceof GenericContainerScreen genericContainerScreen) { + ScreenEvents.afterRender(screen).register((screen1, context, mouseX, mouseY, delta) -> { + MatrixStack matrices = context.getMatrices(); + matrices.push(); + matrices.translate(((HandledScreenAccessor) genericContainerScreen).getX(), ((HandledScreenAccessor) genericContainerScreen).getY(), 300); + onDraw(context, genericContainerScreen.getScreenHandler().slots.subList(0, genericContainerScreen.getScreenHandler().getRows() * 9)); + matrices.pop(); + }); + ScreenEvents.remove(screen).register(screen1 -> clearScreen()); + onSetScreen(genericContainerScreen); + } else { + clearScreen(); + } + }); + } + + public void onSetScreen(@NotNull GenericContainerScreen screen) { + String screenName = screen.getTitle().getString(); + Matcher matcher = PLACEHOLDER_PATTERN.matcher(screenName); + for (ContainerSolver solver : solvers) { + if (solver.isEnabled()) { + matcher.usePattern(solver.getName()); + matcher.reset(); + if (matcher.matches()) { + currentSolver = solver; + groups = new String[matcher.groupCount()]; + for (int i = 0; i < groups.length; i++) { + groups[i] = matcher.group(i + 1); + } + currentSolver.start(screen); + return; + } + } + } + clearScreen(); + } + + public void clearScreen() { + if (currentSolver != null) { + currentSolver.reset(); + currentSolver = null; + } + } + + public void markDirty() { + highlights = null; + } + + public void onDraw(DrawContext context, List<Slot> slots) { + if (currentSolver == null) + return; + if (highlights == null) + highlights = currentSolver.getColors(groups, slotMap(slots)); + RenderSystem.enableDepthTest(); + RenderSystem.colorMask(true, true, true, false); + for (ColorHighlight highlight : highlights) { + Slot slot = slots.get(highlight.slot()); + int color = highlight.color(); + context.fillGradient(slot.x, slot.y, slot.x + 16, slot.y + 16, color, color); + } + RenderSystem.colorMask(true, true, true, true); + } + + private Map<Integer, ItemStack> slotMap(List<Slot> slots) { + Map<Integer, ItemStack> slotMap = new TreeMap<>(); + for (int i = 0; i < slots.size(); i++) + slotMap.put(i, slots.get(i).getStack()); + return slotMap; + } +} diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/title/Title.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/Title.java index 0a3bc845..b48771ee 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/title/Title.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/Title.java @@ -1,4 +1,4 @@ -package me.xmrvizzy.skyblocker.utils.title; +package me.xmrvizzy.skyblocker.utils.render.title; import net.minecraft.text.MutableText; import net.minecraft.text.Text; diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/title/TitleContainer.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/TitleContainer.java index a485d37b..6e15c871 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/title/TitleContainer.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/TitleContainer.java @@ -1,8 +1,8 @@ -package me.xmrvizzy.skyblocker.utils.title; +package me.xmrvizzy.skyblocker.utils.render.title; import me.xmrvizzy.skyblocker.SkyblockerMod; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; -import me.xmrvizzy.skyblocker.utils.Scheduler; +import me.xmrvizzy.skyblocker.utils.scheduler.Scheduler; import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager; import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/title/TitleContainerConfigScreen.java b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/TitleContainerConfigScreen.java index c0c4d63c..8d31e2ea 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/title/TitleContainerConfigScreen.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/render/title/TitleContainerConfigScreen.java @@ -1,8 +1,8 @@ -package me.xmrvizzy.skyblocker.utils.title; +package me.xmrvizzy.skyblocker.utils.render.title; import me.shedaniel.autoconfig.AutoConfig; import me.xmrvizzy.skyblocker.config.SkyblockerConfig; -import me.xmrvizzy.skyblocker.utils.RenderUtils; +import me.xmrvizzy.skyblocker.utils.render.RenderHelper; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.Vector2f; @@ -100,7 +100,7 @@ public class TitleContainerConfigScreen extends Screen { float x2 = boundingBox.getRight().getX(); float y2 = boundingBox.getRight().getY(); - if (RenderUtils.pointExistsInArea((int) mouseX, (int) mouseY, (int) x1, (int) y1, (int) x2, (int) y2) && button == 0) { + if (RenderHelper.pointIsInArea(mouseX, mouseY, x1, y1, x2, y2) && button == 0) { hudX = switch (alignment) { case LEFT -> (int) mouseX - midWidth; case MIDDLE -> (int) mouseX; diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java index ffb16632..bde29c13 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/MessageScheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/MessageScheduler.java @@ -1,4 +1,4 @@ -package me.xmrvizzy.skyblocker.utils; +package me.xmrvizzy.skyblocker.utils.scheduler; import net.minecraft.client.MinecraftClient; diff --git a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java index 1ee3e040..76112e0d 100644 --- a/src/main/java/me/xmrvizzy/skyblocker/utils/Scheduler.java +++ b/src/main/java/me/xmrvizzy/skyblocker/utils/scheduler/Scheduler.java @@ -1,4 +1,4 @@ -package me.xmrvizzy.skyblocker.utils; +package me.xmrvizzy.skyblocker.utils.scheduler; import com.mojang.brigadier.Command; import me.xmrvizzy.skyblocker.SkyblockerMod; |