diff options
Diffstat (limited to 'src/main/java/de/hysky/skyblocker')
7 files changed, 151 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() + ); + } +} |