aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/hysky/skyblocker/utils')
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/PosUtils.java5
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java79
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java6
-rw-r--r--src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java26
4 files changed, 116 insertions, 0 deletions
diff --git a/src/main/java/de/hysky/skyblocker/utils/PosUtils.java b/src/main/java/de/hysky/skyblocker/utils/PosUtils.java
index 73ada889..4ca37a83 100644
--- a/src/main/java/de/hysky/skyblocker/utils/PosUtils.java
+++ b/src/main/java/de/hysky/skyblocker/utils/PosUtils.java
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.utils;
+import com.google.gson.JsonObject;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@@ -17,6 +18,10 @@ public final class PosUtils {
return new BlockPos(Integer.parseInt(posArray[0]), Integer.parseInt(posArray[1]), Integer.parseInt(posArray[2]));
}
+ public static BlockPos parsePosJson(JsonObject posJson) {
+ return new BlockPos(posJson.get("x").getAsInt(), posJson.get("y").getAsInt(), posJson.get("z").getAsInt());
+ }
+
public static String getPosString(BlockPos blockPos) {
return blockPos.getX() + "," + blockPos.getY() + "," + blockPos.getZ();
}
diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java
new file mode 100644
index 00000000..f35ad95d
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java
@@ -0,0 +1,79 @@
+package de.hysky.skyblocker.utils.waypoint;
+
+import com.google.common.primitives.Floats;
+import com.google.gson.JsonObject;
+import com.mojang.serialization.Codec;
+import com.mojang.serialization.DataResult;
+import com.mojang.serialization.codecs.RecordCodecBuilder;
+import de.hysky.skyblocker.config.SkyblockerConfigManager;
+import de.hysky.skyblocker.utils.PosUtils;
+import de.hysky.skyblocker.utils.render.RenderHelper;
+import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
+import net.minecraft.text.Text;
+import net.minecraft.text.TextCodecs;
+import net.minecraft.util.math.BlockPos;
+import net.minecraft.util.math.Vec3d;
+
+import java.util.function.Supplier;
+
+public class NamedWaypoint extends Waypoint {
+ public static final Codec<NamedWaypoint> CODEC = RecordCodecBuilder.create(instance -> instance.group(
+ BlockPos.CODEC.fieldOf("pos").forGetter(secretWaypoint -> secretWaypoint.pos),
+ TextCodecs.CODEC.fieldOf("name").forGetter(secretWaypoint -> secretWaypoint.name),
+ Codec.floatRange(0, 1).listOf().comapFlatMap(
+ colorComponentsList -> colorComponentsList.size() == 3 ? DataResult.success(Floats.toArray(colorComponentsList)) : DataResult.error(() -> "Expected 3 color components, got " + colorComponentsList.size() + " instead"),
+ Floats::asList
+ ).fieldOf("colorComponents").forGetter(secretWaypoint -> secretWaypoint.colorComponents),
+ Codec.BOOL.fieldOf("shouldRender").forGetter(Waypoint::shouldRender)
+ ).apply(instance, NamedWaypoint::new));
+ protected final Text name;
+ protected final Vec3d centerPos;
+
+ public NamedWaypoint(BlockPos pos, String name, float[] colorComponents, boolean shouldRender) {
+ this(pos, Text.of(name), colorComponents, shouldRender);
+ }
+
+ public NamedWaypoint(BlockPos pos, Text name, float[] colorComponents, boolean shouldRender) {
+ this(pos, name, () -> SkyblockerConfigManager.get().general.waypoints.waypointType, colorComponents, shouldRender);
+ }
+
+ public NamedWaypoint(BlockPos pos, String name, Supplier<Type> typeSupplier, float[] colorComponents, boolean shouldRender) {
+ this(pos, Text.of(name), typeSupplier, colorComponents, shouldRender);
+ }
+
+ public NamedWaypoint(BlockPos pos, Text name, Supplier<Type> typeSupplier, float[] colorComponents) {
+ this(pos, name, typeSupplier, colorComponents, true);
+ }
+
+ public NamedWaypoint(BlockPos pos, Text name, Supplier<Type> typeSupplier, float[] colorComponents, boolean shouldRender) {
+ super(pos, typeSupplier, colorComponents, DEFAULT_HIGHLIGHT_ALPHA, DEFAULT_LINE_WIDTH, true, shouldRender);
+ this.name = name;
+ this.centerPos = pos.toCenterPos();
+ }
+
+ public static NamedWaypoint fromSkytilsJson(JsonObject waypointJson) {
+ int color = waypointJson.get("color").getAsInt();
+ return new NamedWaypoint(PosUtils.parsePosJson(waypointJson), waypointJson.get("name").getAsString(), () -> SkyblockerConfigManager.get().general.waypoints.waypointType, new float[]{((color & 0x00FF0000) >> 16) / 255f, ((color & 0x0000FF00) >> 8) / 255f, (color & 0x000000FF) / 255f}, waypointJson.get("enabled").getAsBoolean());
+ }
+
+ public Text getName() {
+ return name;
+ }
+
+ protected boolean shouldRenderName() {
+ return true;
+ }
+
+ @Override
+ public void render(WorldRenderContext context) {
+ super.render(context);
+ if (shouldRenderName()) {
+ RenderHelper.renderText(context, name, centerPos.add(0, 1, 0), true);
+ }
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return this == obj || obj instanceof NamedWaypoint waypoint && name.equals(waypoint.name);
+ }
+}
diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java
index 622e1658..1a83f175 100644
--- a/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java
+++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java
@@ -6,6 +6,7 @@ import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
+import java.util.Arrays;
import java.util.function.Supplier;
public class Waypoint implements Renderable {
@@ -94,6 +95,11 @@ public class Waypoint implements Renderable {
}
}
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj) || obj instanceof Waypoint other && pos.equals(other.pos) && typeSupplier.get() == other.typeSupplier.get() && Arrays.equals(colorComponents, other.colorComponents) && alpha == other.alpha && lineWidth == other.lineWidth && throughWalls == other.throughWalls && shouldRender == other.shouldRender;
+ }
+
public enum Type {
WAYPOINT,
OUTLINED_WAYPOINT,
diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java
new file mode 100644
index 00000000..0597fc95
--- /dev/null
+++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java
@@ -0,0 +1,26 @@
+package de.hysky.skyblocker.utils.waypoint;
+
+import com.google.gson.JsonObject;
+import com.mojang.serialization.Codec;
+import com.mojang.serialization.codecs.RecordCodecBuilder;
+
+import java.util.List;
+
+public record WaypointCategory(String name, String island, List<NamedWaypoint> waypoints) {
+ public static final Codec<WaypointCategory> CODEC = RecordCodecBuilder.create(instance -> instance.group(
+ Codec.STRING.fieldOf("name").forGetter(WaypointCategory::name),
+ Codec.STRING.fieldOf("island").forGetter(WaypointCategory::island),
+ NamedWaypoint.CODEC.listOf().fieldOf("waypoints").forGetter(WaypointCategory::waypoints)
+ ).apply(instance, WaypointCategory::new));
+
+ public static WaypointCategory fromSkytilsJson(JsonObject waypointCategory) {
+ return new WaypointCategory(
+ waypointCategory.get("name").getAsString(),
+ waypointCategory.get("island").getAsString(),
+ waypointCategory.getAsJsonArray("waypoints").asList().stream()
+ .map(JsonObject.class::cast)
+ .map(NamedWaypoint::fromSkytilsJson)
+ .toList()
+ );
+ }
+}