diff options
author | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-01-26 00:12:07 -0500 |
---|---|---|
committer | Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> | 2024-05-24 19:51:15 -0400 |
commit | 92bb441b31cd72d142adee578cc253708cf0101c (patch) | |
tree | 774b4c2d6147164289513f5e9fbcc0c17c4384c7 /src | |
parent | 5d5d1347644a798551100d22feddc92d4049cb59 (diff) | |
download | Skyblocker-92bb441b31cd72d142adee578cc253708cf0101c.tar.gz Skyblocker-92bb441b31cd72d142adee578cc253708cf0101c.tar.bz2 Skyblocker-92bb441b31cd72d142adee578cc253708cf0101c.zip |
Add NamedWaypoint and WaypointCategory
Diffstat (limited to 'src')
10 files changed, 217 insertions, 9 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java index e6a8b9d1..3f07ccf2 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/Room.java @@ -208,7 +208,7 @@ public class Room implements Tickable, Renderable { SecretWaypoint.Category category = SecretWaypoint.Category.CategoryArgumentType.getCategory(context, "category"); Text waypointName = context.getArgument("name", Text.class); addCustomWaypoint(secretIndex, category, waypointName, pos); - context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.stringifiedTranslatable("skyblocker.dungeons.secrets.customWaypointAdded", pos.getX(), pos.getY(), pos.getZ(), name, secretIndex, category, waypointName))); + context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointAdded", pos.getX(), pos.getY(), pos.getZ(), name, secretIndex, category.asString(), waypointName))); } /** @@ -242,7 +242,7 @@ public class Room implements Tickable, Renderable { protected void removeCustomWaypoint(CommandContext<FabricClientCommandSource> context, BlockPos pos) { SecretWaypoint waypoint = removeCustomWaypoint(pos); if (waypoint != null) { - context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.stringifiedTranslatable("skyblocker.dungeons.secrets.customWaypointRemoved", pos.getX(), pos.getY(), pos.getZ(), name, waypoint.secretIndex, waypoint.category, waypoint.name))); + context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointRemoved", pos.getX(), pos.getY(), pos.getZ(), name, waypoint.secretIndex, waypoint.category.asString(), waypoint.getName()))); } else { context.getSource().sendFeedback(Constants.PREFIX.get().append(Text.translatable("skyblocker.dungeons.secrets.customWaypointNotFound", pos.getX(), pos.getY(), pos.getZ(), name))); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/SecretWaypoint.java b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/SecretWaypoint.java index 42fe6dbe..0e4f5f1d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/SecretWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/dungeon/secrets/SecretWaypoint.java @@ -9,6 +9,7 @@ import de.hysky.skyblocker.config.SkyblockerConfig; import de.hysky.skyblocker.config.SkyblockerConfigManager; import de.hysky.skyblocker.config.configs.DungeonsConfig; import de.hysky.skyblocker.utils.render.RenderHelper; +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; import de.hysky.skyblocker.utils.waypoint.Waypoint; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import net.minecraft.client.MinecraftClient; @@ -29,7 +30,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.ToDoubleFunction; -public class SecretWaypoint extends Waypoint { +public class SecretWaypoint extends NamedWaypoint { private static final Logger LOGGER = LoggerFactory.getLogger(SecretWaypoint.class); public static final Codec<SecretWaypoint> CODEC = RecordCodecBuilder.create(instance -> instance.group( Codec.INT.fieldOf("secretIndex").forGetter(secretWaypoint -> secretWaypoint.secretIndex), @@ -43,8 +44,6 @@ public class SecretWaypoint extends Waypoint { static final Supplier<Type> TYPE_SUPPLIER = () -> CONFIG.get().waypointType; final int secretIndex; final Category category; - final Text name; - private final Vec3d centerPos; SecretWaypoint(int secretIndex, JsonObject waypoint, String name, BlockPos pos) { this(secretIndex, Category.get(waypoint), name, pos); @@ -55,11 +54,9 @@ public class SecretWaypoint extends Waypoint { } SecretWaypoint(int secretIndex, Category category, Text name, BlockPos pos) { - super(pos, TYPE_SUPPLIER, category.colorComponents); + super(pos, name, TYPE_SUPPLIER, category.colorComponents); this.secretIndex = secretIndex; this.category = category; - this.name = name; - this.centerPos = pos.toCenterPos(); } static ToDoubleFunction<SecretWaypoint> getSquaredDistanceToFunction(Entity entity) { @@ -96,6 +93,11 @@ public class SecretWaypoint extends Waypoint { return super.equals(obj) || obj instanceof SecretWaypoint other && secretIndex == other.secretIndex && category == other.category && name.equals(other.name) && pos.equals(other.pos); } + @Override + protected boolean shouldRenderName() { + return CONFIG.get().showSecretText; + } + /** * Renders the secret waypoint, including a waypoint through {@link Waypoint#render(WorldRenderContext)}, the name, and the distance from the player. */ @@ -106,7 +108,6 @@ public class SecretWaypoint extends Waypoint { if (CONFIG.get().showSecretText) { Vec3d posUp = centerPos.add(0, 1, 0); - RenderHelper.renderText(context, name, posUp, true); double distance = context.camera().getPos().distanceTo(centerPos); RenderHelper.renderText(context, Text.literal(Math.round(distance) + "m").formatted(Formatting.YELLOW), posUp, 1, MinecraftClient.getInstance().textRenderer.fontHeight + 1, true); } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java new file mode 100644 index 00000000..070cacaa --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -0,0 +1,25 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import com.google.gson.JsonObject; +import com.mojang.serialization.Codec; +import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; + +import java.util.*; + +public class Waypoints { + Codec<List<WaypointCategory>> CODEC = WaypointCategory.CODEC.listOf(); + private static final Map<String, WaypointCategory> waypoints = new HashMap<>(); + + public static Collection<WaypointCategory> fromSkytilsBase64(String base64) { + return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); + } + + public static Collection<WaypointCategory> fromSkytilsJson(String waypointCategories) { + JsonObject waypointCategoriesJson = SkyblockerMod.GSON.fromJson(waypointCategories, JsonObject.class); + return waypointCategoriesJson.getAsJsonArray("categories").asList().stream() + .map(JsonObject.class::cast) + .map(WaypointCategory::fromSkytilsJson) + .toList(); + } +} 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() + ); + } +} diff --git a/src/test/java/de/hysky/skyblocker/utils/PosUtilsTest.java b/src/test/java/de/hysky/skyblocker/utils/PosUtilsTest.java index 1f433af3..470ea079 100644 --- a/src/test/java/de/hysky/skyblocker/utils/PosUtilsTest.java +++ b/src/test/java/de/hysky/skyblocker/utils/PosUtilsTest.java @@ -1,5 +1,7 @@ package de.hysky.skyblocker.utils; +import com.google.gson.JsonObject; +import de.hysky.skyblocker.SkyblockerMod; import net.minecraft.util.math.BlockPos; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -11,6 +13,11 @@ public class PosUtilsTest { } @Test + void testParsePosJson() { + Assertions.assertEquals(PosUtils.parsePosJson(SkyblockerMod.GSON.fromJson("{\"x\":-1,\"y\":0,\"z\":1}", JsonObject.class)), new BlockPos(-1, 0, 1)); + } + + @Test void testGetPosString() { Assertions.assertEquals(PosUtils.getPosString(new BlockPos(-1, 0, 1)), "-1,0,1"); } diff --git a/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointCategoryTest.java b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointCategoryTest.java new file mode 100644 index 00000000..65304e0c --- /dev/null +++ b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointCategoryTest.java @@ -0,0 +1,39 @@ +package de.hysky.skyblocker.utils.waypoint; + +import com.google.gson.JsonElement; +import com.mojang.serialization.JsonOps; +import de.hysky.skyblocker.SkyblockerMod; +import net.minecraft.Bootstrap; +import net.minecraft.SharedConstants; +import net.minecraft.util.math.BlockPos; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class WaypointCategoryTest { + @BeforeAll + static void beforeAll() { + SharedConstants.createGameVersion(); + Bootstrap.initialize(); + } + + @Test + void testCodecEncode() { + WaypointCategory category = new WaypointCategory("category", "hub", List.of(new NamedWaypoint(BlockPos.ORIGIN, "waypoint", new float[]{0f, 0.5f, 1f}, false), new NamedWaypoint(new BlockPos(-1, 0, 1), "waypoint", new float[]{0f, 0f, 0f}, true))); + Object categoryJson = WaypointCategory.CODEC.encodeStart(JsonOps.INSTANCE, category).result().orElseThrow(); + String expectedJson = "{\"name\":\"category\",\"island\":\"hub\",\"waypoints\":[{\"pos\":[0,0,0],\"name\":\"waypoint\",\"colorComponents\":[0.0,0.5,1.0],\"shouldRender\":false},{\"pos\":[-1,0,1],\"name\":\"waypoint\",\"colorComponents\":[0.0,0.0,0.0],\"shouldRender\":true}]}"; + + Assertions.assertEquals(expectedJson, categoryJson.toString()); + } + + @Test + void testCodecDecode() { + String categoryJson = "{\"name\":\"category\",\"island\":\"hub\",\"waypoints\":[{\"pos\":[0,0,0],\"name\":\"waypoint\",\"colorComponents\":[0.0,0.5,1.0],\"shouldRender\":false},{\"pos\":[-1,0,1],\"name\":\"waypoint\",\"colorComponents\":[0.0,0.0,0.0],\"shouldRender\":true}]}"; + WaypointCategory category = WaypointCategory.CODEC.parse(JsonOps.INSTANCE, SkyblockerMod.GSON.fromJson(categoryJson, JsonElement.class)).result().orElseThrow(); + WaypointCategory expectedCategory = new WaypointCategory("category", "hub", List.of(new NamedWaypoint(BlockPos.ORIGIN, "waypoint", new float[]{0f, 0.5f, 1f}, false), new NamedWaypoint(new BlockPos(-1, 0, 1), "waypoint", new float[]{0f, 0f, 0f}, true))); + + Assertions.assertEquals(expectedCategory, category); + } +} diff --git a/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java new file mode 100644 index 00000000..91470224 --- /dev/null +++ b/src/test/java/de/hysky/skyblocker/utils/waypoint/WaypointsTest.java @@ -0,0 +1,20 @@ +package de.hysky.skyblocker.utils.waypoint; + +import de.hysky.skyblocker.skyblock.waypoint.Waypoints; +import net.minecraft.util.math.BlockPos; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collection; +import java.util.List; + +public class WaypointsTest { + @Test + void testFromSkytilsBase64() { + String waypointCategoriesSkytilsBase64 = "eyJjYXRlZ29yaWVzIjpbeyJuYW1lIjoiY2F0ZWdvcnkiLCJ3YXlwb2ludHMiOlt7Im5hbWUiOiJ3YXlwb2ludCIsIngiOjAsInkiOjAsInoiOjAsImVuYWJsZWQiOmZhbHNlLCJjb2xvciI6MzMwMjMsImFkZGVkQXQiOjF9LHsibmFtZSI6MSwieCI6LTEsInkiOjAsInoiOjEsImVuYWJsZWQiOnRydWUsImNvbG9yIjowLCJhZGRlZEF0IjoxfV0sImlzbGFuZCI6Imh1YiJ9XX0="; + Collection<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(waypointCategoriesSkytilsBase64); + Collection<WaypointCategory> expectedWaypointCategories = List.of(new WaypointCategory("category", "hub", List.of(new NamedWaypoint(BlockPos.ORIGIN, "waypoint", new float[]{0f, 0.5f, 1f}, false), new NamedWaypoint(new BlockPos(-1, 0, 1), "1", new float[]{0f, 0f, 0f}, true)))); + + Assertions.assertEquals(expectedWaypointCategories, waypointCategories); + } +} |