From 92bb441b31cd72d142adee578cc253708cf0101c Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 26 Jan 2024 00:12:07 -0500 Subject: Add NamedWaypoint and WaypointCategory --- .../skyblocker/skyblock/dungeon/secrets/Room.java | 4 +- .../skyblock/dungeon/secrets/SecretWaypoint.java | 15 ++-- .../skyblocker/skyblock/waypoint/Waypoints.java | 25 +++++++ .../java/de/hysky/skyblocker/utils/PosUtils.java | 5 ++ .../skyblocker/utils/waypoint/NamedWaypoint.java | 79 ++++++++++++++++++++++ .../hysky/skyblocker/utils/waypoint/Waypoint.java | 6 ++ .../utils/waypoint/WaypointCategory.java | 26 +++++++ 7 files changed, 151 insertions(+), 9 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java create mode 100644 src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java (limited to 'src/main/java/de') 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 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 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_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 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> CODEC = WaypointCategory.CODEC.listOf(); + private static final Map waypoints = new HashMap<>(); + + public static Collection fromSkytilsBase64(String base64) { + return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); + } + + public static Collection 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 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 typeSupplier, float[] colorComponents, boolean shouldRender) { + this(pos, Text.of(name), typeSupplier, colorComponents, shouldRender); + } + + public NamedWaypoint(BlockPos pos, Text name, Supplier typeSupplier, float[] colorComponents) { + this(pos, name, typeSupplier, colorComponents, true); + } + + public NamedWaypoint(BlockPos pos, Text name, Supplier 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 waypoints) { + public static final Codec 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() + ); + } +} -- cgit From 6ad1335dd95e5df4db38e1d8d022a6c2925d6921 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 26 Jan 2024 21:43:29 -0500 Subject: Add waypoints loading and saving --- .../skyblocker/skyblock/waypoint/Waypoints.java | 51 +++++++++++++++++++++- .../utils/waypoint/WaypointCategory.java | 7 +++ 2 files changed, 57 insertions(+), 1 deletion(-) (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java index 070cacaa..1a9d6836 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -1,16 +1,49 @@ package de.hysky.skyblocker.skyblock.waypoint; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.mojang.serialization.Codec; +import com.mojang.serialization.JsonOps; import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents; +import net.fabricmc.loader.api.FabricLoader; +import net.minecraft.client.MinecraftClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; public class Waypoints { - Codec> CODEC = WaypointCategory.CODEC.listOf(); + private static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class); + private static final Codec> CODEC = WaypointCategory.CODEC.listOf(); + private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json"); private static final Map waypoints = new HashMap<>(); + public static void init() { + loadWaypoints(); + ClientLifecycleEvents.CLIENT_STOPPING.register(Waypoints::saveWaypoints); + WorldRenderEvents.AFTER_TRANSLUCENT.register(Waypoints::render); + } + + public static void loadWaypoints() { + waypoints.clear(); + try (BufferedReader reader = Files.newBufferedReader(waypointsFile)) { + List waypoints = CODEC.parse(JsonOps.INSTANCE, SkyblockerMod.GSON.fromJson(reader, JsonArray.class)).resultOrPartial(LOGGER::error).orElseThrow(); + waypoints.forEach(waypointCategory -> Waypoints.waypoints.put(waypointCategory.island(), waypointCategory)); + } catch (Exception e) { + LOGGER.error("Encountered exception while loading waypoints", e); + } + } + public static Collection fromSkytilsBase64(String base64) { return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); } @@ -22,4 +55,20 @@ public class Waypoints { .map(WaypointCategory::fromSkytilsJson) .toList(); } + + public static void saveWaypoints(MinecraftClient client) { + try (BufferedWriter writer = Files.newBufferedWriter(waypointsFile)) { + JsonElement waypointsJson = CODEC.encodeStart(JsonOps.INSTANCE, new ArrayList<>(waypoints.values())).resultOrPartial(LOGGER::error).orElseThrow(); + SkyblockerMod.GSON.toJson(waypointsJson, writer); + } catch (Exception e) { + LOGGER.error("Encountered exception while saving waypoints", e); + } + } + + public static void render(WorldRenderContext context) { + WaypointCategory category = waypoints.get(Utils.getLocationRaw()); + if (category != null) { + category.render(context); + } + } } diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java index 0597fc95..16a22796 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java @@ -3,6 +3,7 @@ package de.hysky.skyblocker.utils.waypoint; import com.google.gson.JsonObject; import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; import java.util.List; @@ -23,4 +24,10 @@ public record WaypointCategory(String name, String island, List w .toList() ); } + + public void render(WorldRenderContext context) { + for (NamedWaypoint waypoint : waypoints) { + waypoint.render(context); + } + } } -- cgit From bf4034da76adcc7760628d49e518404273efceee Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 28 Jan 2024 22:14:27 -0500 Subject: Add waypoints gui --- .../config/categories/GeneralCategory.java | 29 +++++++++ .../skyblock/waypoint/WaypointsListWidget.java | 76 ++++++++++++++++++++++ .../skyblock/waypoint/WaypointsScreen.java | 37 +++++++++++ .../skyblocker/utils/waypoint/NamedWaypoint.java | 4 ++ .../utils/waypoint/ProfileAwareWaypoint.java | 2 +- .../hysky/skyblocker/utils/waypoint/Waypoint.java | 2 +- 6 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java create mode 100644 src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java index 1477d669..305c85f2 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/GeneralCategory.java @@ -4,6 +4,9 @@ import de.hysky.skyblocker.config.ConfigUtils; import de.hysky.skyblocker.config.SkyblockerConfig; import de.hysky.skyblocker.config.configs.GeneralConfig; import de.hysky.skyblocker.skyblock.shortcut.ShortcutsConfigScreen; +import de.hysky.skyblocker.skyblock.waypoint.WaypointsScreen; +import de.hysky.skyblocker.utils.render.title.TitleContainerConfigScreen; +import de.hysky.skyblocker.utils.waypoint.Waypoint; import dev.isxander.yacl3.api.*; import dev.isxander.yacl3.api.controller.FloatSliderControllerBuilder; import net.minecraft.client.MinecraftClient; @@ -67,6 +70,32 @@ public class GeneralCategory { .build()) .build()) + //Waypoints + .group(OptionGroup.createBuilder() + .name(Text.translatable("text.autoconfig.skyblocker.option.general.waypoints")) + .collapsed(true) + .option(Option.createBuilder() + .name(Text.translatable("text.autoconfig.skyblocker.option.general.waypoints.enableWaypoints")) + .binding(defaults.general.waypoints.enableWaypoints, + () -> config.general.waypoints.enableWaypoints, + newValue -> config.general.waypoints.enableWaypoints = newValue) + .controller(ConfigUtils::createBooleanController) + .build()) + .option(Option.createBuilder() + .name(Text.translatable("text.autoconfig.skyblocker.option.general.waypoints.waypointType")) + .description(OptionDescription.of(Text.translatable("text.autoconfig.skyblocker.option.general.waypoints.waypointType.@Tooltip"), Text.translatable("text.autoconfig.skyblocker.option.general.waypoints.waypointType.generalNote"))) + .binding(defaults.general.waypoints.waypointType, + () -> config.general.waypoints.waypointType, + newValue -> config.general.waypoints.waypointType = newValue) + .controller(ConfigUtils::createEnumCyclingListController) + .build()) + .option(ButtonOption.createBuilder() + .name(Text.translatable("skyblocker.waypoints.config")) + .text(Text.translatable("text.skyblocker.open")) + .action((screen, opt) -> MinecraftClient.getInstance().setScreen(new WaypointsScreen(screen))) + .build()) + .build()) + //Quiver Warning .group(OptionGroup.createBuilder() .name(Text.translatable("skyblocker.config.general.quiverWarning")) diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java new file mode 100644 index 00000000..a1410397 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java @@ -0,0 +1,76 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.util.math.BlockPos; + +import java.util.List; + +public class WaypointsListWidget extends ElementListWidget { + public WaypointsListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) { + super(client, width, height, y, itemHeight); + } + + protected static abstract class AbstractWaypointEntry extends ElementListWidget.Entry { + } + + protected class WaypointCategoryEntry extends AbstractWaypointEntry { + private final WaypointCategory category; + + public WaypointCategoryEntry(WaypointCategory category) { + this.category = category; + } + + @Override + public List selectableChildren() { + return List.of(); + } + + @Override + public List children() { + return List.of(); + } + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + context.drawTextWithShadow(client.textRenderer, category.name(), width / 2 - 150, y + 5, 0xFFFFFF); + } + } + + protected class WaypointEntry extends AbstractWaypointEntry { + private final WaypointCategoryEntry category; + private final NamedWaypoint waypoint; + + public WaypointEntry(WaypointCategoryEntry category) { + this(category, new NamedWaypoint(BlockPos.ORIGIN, "New Waypoint", new float[]{0f, 1f, 0f})); + } + + public WaypointEntry(WaypointCategoryEntry category, NamedWaypoint waypoint) { + this.category = category; + this.waypoint = waypoint; + } + + @Override + public List selectableChildren() { + return null; + } + + @Override + public List children() { + return null; + } + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + context.drawTextWithShadow(client.textRenderer, waypoint.getName(), width / 2 - 125, y + 5, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, waypoint.pos.toString(), width / 2 - 50, y + 5, 0xFFFFFF); + float[] colorComponents = waypoint.getColorComponents(); + context.drawTextWithShadow(client.textRenderer, String.format("#%02X%02X%02X", (int) (colorComponents[0] * 255), (int) (colorComponents[1] * 255), (int) (colorComponents[2] * 255)), width / 2 + 10, y + 5, 0xFFFFFF); + } + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java new file mode 100644 index 00000000..f98addda --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java @@ -0,0 +1,37 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public class WaypointsScreen extends Screen { + private WaypointsListWidget waypointsListWidget; + private final Screen parent; + + protected WaypointsScreen() { + this(null); + } + + public WaypointsScreen(Screen parent) { + super(Text.translatable("skyblocker.waypoints.config")); + this.parent = parent; + } + + @Override + protected void init() { + super.init(); + waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, width, height - 96, 32, 25)); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + super.render(context, mouseX, mouseY, delta); + context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 16, 0xFFFFFF); + } + + @SuppressWarnings("DataFlowIssue") + @Override + public void close() { + client.setScreen(parent); + } +} diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java index f35ad95d..db95634b 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java @@ -29,6 +29,10 @@ public class NamedWaypoint extends Waypoint { protected final Text name; protected final Vec3d centerPos; + public NamedWaypoint(BlockPos pos, String name, float[] colorComponents) { + this(pos, name, colorComponents, true); + } + public NamedWaypoint(BlockPos pos, String name, float[] colorComponents, boolean shouldRender) { this(pos, Text.of(name), colorComponents, shouldRender); } diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/ProfileAwareWaypoint.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/ProfileAwareWaypoint.java index 7aa99d14..7369a2ef 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/ProfileAwareWaypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/ProfileAwareWaypoint.java @@ -38,7 +38,7 @@ public class ProfileAwareWaypoint extends Waypoint { } @Override - protected float[] getColorComponents() { + public float[] getColorComponents() { return foundProfiles.contains(Utils.getProfile()) ? foundColor : missingColor; } } 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 1a83f175..75e2edcf 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java @@ -72,7 +72,7 @@ public class Waypoint implements Renderable { this.shouldRender = !this.shouldRender; } - protected float[] getColorComponents() { + public float[] getColorComponents() { return colorComponents; } -- cgit From d84b0ce5169c1aea99c6e4842665c6e3598e97d8 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 4 Feb 2024 16:10:04 -0500 Subject: Add waypoint adding --- .../java/de/hysky/skyblocker/SkyblockerMod.java | 2 + .../skyblock/shortcut/ShortcutsConfigScreen.java | 12 +-- .../skyblocker/skyblock/waypoint/Waypoints.java | 23 ++++-- .../skyblock/waypoint/WaypointsListWidget.java | 90 ++++++++++++++++++++-- .../skyblock/waypoint/WaypointsScreen.java | 36 ++++++++- 5 files changed, 139 insertions(+), 24 deletions(-) (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index a9aab3b6..bdb1766c 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -46,6 +46,7 @@ import de.hysky.skyblocker.skyblock.waypoint.FairySouls; import de.hysky.skyblocker.skyblock.waypoint.MythologicalRitual; import de.hysky.skyblocker.skyblock.waypoint.OrderedWaypoints; import de.hysky.skyblocker.skyblock.waypoint.Relics; +import de.hysky.skyblocker.skyblock.waypoint.Waypoints; import de.hysky.skyblocker.utils.*; import de.hysky.skyblocker.utils.chat.ChatMessageListener; import de.hysky.skyblocker.utils.discord.DiscordRPCManager; @@ -112,6 +113,7 @@ public class SkyblockerMod implements ClientModInitializer { ItemTooltip.init(); AccessoriesHelper.init(); WikiLookup.init(); + Waypoints.init(); FairySouls.init(); Relics.init(); MythologicalRitual.init(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java index 120eb099..c73836ab 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java @@ -51,22 +51,16 @@ public class ShortcutsConfigScreen extends Screen { buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.delete"), button -> { if (client != null && shortcutsConfigListWidget.getSelectedOrNull() instanceof ShortcutsConfigListWidget.ShortcutEntry shortcutEntry) { scrollAmount = shortcutsConfigListWidget.getScrollAmount(); - client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.stringifiedTranslatable("skyblocker.shortcuts.deleteWarning", shortcutEntry), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); + client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", shortcutEntry.toString()), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); } }).build(); adder.add(buttonDelete); buttonNew = ButtonWidget.builder(Text.translatable("skyblocker.shortcuts.new"), buttonNew -> shortcutsConfigListWidget.addShortcutAfterSelected()).build(); adder.add(buttonNew); - adder.add(ButtonWidget.builder(ScreenTexts.CANCEL, button -> { - if (client != null) { - close(); - } - }).build()); + adder.add(ButtonWidget.builder(ScreenTexts.CANCEL, button -> close()).build()); buttonDone = ButtonWidget.builder(ScreenTexts.DONE, button -> { shortcutsConfigListWidget.saveShortcuts(); - if (client != null) { - close(); - } + close(); }).tooltip(Tooltip.of(Text.translatable("skyblocker.shortcuts.commandSuggestionTooltip"))).build(); adder.add(buttonDone); gridWidget.refreshPositions(); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java index 1a9d6836..89c3f051 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -1,5 +1,7 @@ package de.hysky.skyblocker.skyblock.waypoint; +import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -20,13 +22,15 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.nio.file.Files; import java.nio.file.Path; -import java.util.*; +import java.util.Base64; +import java.util.Collection; +import java.util.List; public class Waypoints { private static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class); private static final Codec> CODEC = WaypointCategory.CODEC.listOf(); private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json"); - private static final Map waypoints = new HashMap<>(); + static final Multimap waypoints = MultimapBuilder.hashKeys().arrayListValues().build(); public static void init() { loadWaypoints(); @@ -40,7 +44,7 @@ public class Waypoints { List waypoints = CODEC.parse(JsonOps.INSTANCE, SkyblockerMod.GSON.fromJson(reader, JsonArray.class)).resultOrPartial(LOGGER::error).orElseThrow(); waypoints.forEach(waypointCategory -> Waypoints.waypoints.put(waypointCategory.island(), waypointCategory)); } catch (Exception e) { - LOGGER.error("Encountered exception while loading waypoints", e); + LOGGER.error("[Skyblocker Waypoints] Encountered exception while loading waypoints", e); } } @@ -58,17 +62,20 @@ public class Waypoints { public static void saveWaypoints(MinecraftClient client) { try (BufferedWriter writer = Files.newBufferedWriter(waypointsFile)) { - JsonElement waypointsJson = CODEC.encodeStart(JsonOps.INSTANCE, new ArrayList<>(waypoints.values())).resultOrPartial(LOGGER::error).orElseThrow(); + JsonElement waypointsJson = CODEC.encodeStart(JsonOps.INSTANCE, List.copyOf(waypoints.values())).resultOrPartial(LOGGER::error).orElseThrow(); SkyblockerMod.GSON.toJson(waypointsJson, writer); + LOGGER.info("[Skyblocker Waypoints] Saved waypoints"); } catch (Exception e) { - LOGGER.error("Encountered exception while saving waypoints", e); + LOGGER.error("[Skyblocker Waypoints] Encountered exception while saving waypoints", e); } } public static void render(WorldRenderContext context) { - WaypointCategory category = waypoints.get(Utils.getLocationRaw()); - if (category != null) { - category.render(context); + Collection categories = waypoints.get(Utils.getLocationRaw()); + for (WaypointCategory category : categories) { + if (category != null) { + category.render(context); + } } } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java index a1410397..22d3e2bb 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java @@ -1,43 +1,123 @@ package de.hysky.skyblocker.skyblock.waypoint; +import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; import de.hysky.skyblocker.utils.waypoint.WaypointCategory; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.ClickableWidget; import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.text.Text; import net.minecraft.util.math.BlockPos; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; +import java.util.Optional; public class WaypointsListWidget extends ElementListWidget { - public WaypointsListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) { + private final WaypointsScreen screen; + private final String island; + private final List waypoints; + + public WaypointsListWidget(MinecraftClient client, WaypointsScreen screen, int width, int height, int y, int itemHeight) { super(client, width, height, y, itemHeight); + this.screen = screen; + island = Utils.getLocationRaw(); + waypoints = (List) screen.waypoints.get(island); + for (WaypointCategory category : waypoints) { + WaypointCategoryEntry categoryEntry = new WaypointCategoryEntry(category); + addEntry(categoryEntry); + for (NamedWaypoint waypoint : category.waypoints()) { + addEntry(new WaypointEntry(categoryEntry, waypoint)); + } + } + } + + Optional getCategory() { + if (getSelectedOrNull() instanceof WaypointCategoryEntry category) { + return Optional.of(category); + } else if (getSelectedOrNull() instanceof WaypointEntry waypointEntry) { + return Optional.of(waypointEntry.category); + } + return Optional.empty(); + } + + void addWaypointCategoryAfterSelected() { + WaypointCategoryEntry categoryEntry = new WaypointCategoryEntry(); + Optional selectedCategoryEntryOptional = getCategory(); + int index = waypoints.size(); + int entryIndex = children().size(); + if (selectedCategoryEntryOptional.isPresent()) { + WaypointCategoryEntry selectedCategoryEntry = selectedCategoryEntryOptional.get(); + index = waypoints.indexOf(selectedCategoryEntry.category) + 1; + entryIndex = children().indexOf(selectedCategoryEntry) + 1; + while (entryIndex < children().size() && !(children().get(entryIndex) instanceof WaypointCategoryEntry)) { + entryIndex++; + } + } + waypoints.add(index, categoryEntry.category); + children().add(entryIndex, categoryEntry); + } + + @Override + protected boolean isSelectedEntry(int index) { + return Objects.equals(getSelectedOrNull(), children().get(index)); } protected static abstract class AbstractWaypointEntry extends ElementListWidget.Entry { + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + super.mouseClicked(mouseX, mouseY, button); + return true; + } } protected class WaypointCategoryEntry extends AbstractWaypointEntry { private final WaypointCategory category; + private final List children; + private final ButtonWidget buttonNewWaypoint; + + public WaypointCategoryEntry() { + this(new WaypointCategory("New Category", island, new ArrayList<>())); + } public WaypointCategoryEntry(WaypointCategory category) { this.category = category; + buttonNewWaypoint = ButtonWidget.builder(Text.translatable("skyblocker.waypoints.new"), buttonNewWaypoint -> { + WaypointEntry waypointEntry = new WaypointEntry(this); + int entryIndex; + if (getSelectedOrNull() instanceof WaypointEntry selectedWaypointEntry && selectedWaypointEntry.category == this) { + entryIndex = WaypointsListWidget.this.children().indexOf(selectedWaypointEntry) + 1; + } else { + entryIndex = WaypointsListWidget.this.children().indexOf(this) + 1; + while (entryIndex < children().size() && !(children().get(entryIndex) instanceof WaypointCategoryEntry)) { + entryIndex++; + } + } + category.waypoints().add(waypointEntry.waypoint); + WaypointsListWidget.this.children().add(entryIndex, waypointEntry); + }).width(100).build(); + children = List.of(buttonNewWaypoint); } @Override public List selectableChildren() { - return List.of(); + return children; } @Override public List children() { - return List.of(); + return children; } @Override public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + buttonNewWaypoint.setPosition(x + entryWidth - 30, y + 6); + buttonNewWaypoint.render(context, mouseX, mouseY, tickDelta); context.drawTextWithShadow(client.textRenderer, category.name(), width / 2 - 150, y + 5, 0xFFFFFF); } } @@ -57,12 +137,12 @@ public class WaypointsListWidget extends ElementListWidget selectableChildren() { - return null; + return List.of(); } @Override public List children() { - return null; + return List.of(); } @Override diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java index f98addda..4f760995 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java @@ -1,12 +1,22 @@ package de.hysky.skyblocker.skyblock.waypoint; +import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.SimplePositioningWidget; +import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; public class WaypointsScreen extends Screen { - private WaypointsListWidget waypointsListWidget; private final Screen parent; + final Multimap waypoints = MultimapBuilder.hashKeys().arrayListValues().build(Waypoints.waypoints); // TODO deep copy + private WaypointsListWidget waypointsListWidget; + private ButtonWidget buttonNew; + private ButtonWidget buttonDone; protected WaypointsScreen() { this(null); @@ -20,7 +30,21 @@ public class WaypointsScreen extends Screen { @Override protected void init() { super.init(); - waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, width, height - 96, 32, 25)); + waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 25)); + GridWidget gridWidget = new GridWidget(); + gridWidget.getMainPositioner().marginX(5).marginY(2); + GridWidget.Adder adder = gridWidget.createAdder(2); + adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.share"), buttonShare -> {}).build()); + buttonNew = adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.newCategory"), buttonNew -> waypointsListWidget.addWaypointCategoryAfterSelected()).build()); + adder.add(ButtonWidget.builder(ScreenTexts.CANCEL, button -> close()).build()); + buttonDone = adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> { + saveWaypoints(); + close(); + }).build()); + gridWidget.refreshPositions(); + SimplePositioningWidget.setPos(gridWidget, 0, this.height - 64, this.width, 64); + gridWidget.forEachChild(this::addDrawableChild); + updateButtons(); } @Override @@ -29,6 +53,14 @@ public class WaypointsScreen extends Screen { context.drawCenteredTextWithShadow(this.textRenderer, this.title, this.width / 2, 16, 0xFFFFFF); } + private void saveWaypoints() { + Waypoints.waypoints.clear(); + Waypoints.waypoints.putAll(waypoints); + Waypoints.saveWaypoints(client); + } + + private void updateButtons() {} + @SuppressWarnings("DataFlowIssue") @Override public void close() { -- cgit From f6ba429409ac73ee45992fd80e527c96b39e52e3 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 4 Feb 2024 17:35:52 -0500 Subject: Add waypoint deleting --- .../shortcut/ShortcutsConfigListWidget.java | 6 +++ .../skyblock/shortcut/ShortcutsConfigScreen.java | 5 +- .../skyblock/waypoint/WaypointsListWidget.java | 56 ++++++++++++++++------ .../skyblock/waypoint/WaypointsScreen.java | 5 +- .../utils/waypoint/WaypointCategory.java | 5 ++ 5 files changed, 57 insertions(+), 20 deletions(-) (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigListWidget.java index 3918037f..a6b5e62d 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigListWidget.java @@ -1,5 +1,6 @@ package de.hysky.skyblocker.skyblock.shortcut; +import de.hysky.skyblocker.debug.Debug; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.Element; @@ -76,6 +77,11 @@ public class ShortcutsConfigListWidget extends ElementListWidget { + buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.deleteButton"), button -> { if (client != null && shortcutsConfigListWidget.getSelectedOrNull() instanceof ShortcutsConfigListWidget.ShortcutEntry shortcutEntry) { scrollAmount = shortcutsConfigListWidget.getScrollAmount(); client.setScreen(new ConfirmScreen(this::deleteEntry, Text.translatable("skyblocker.shortcuts.deleteQuestion"), Text.translatable("skyblocker.shortcuts.deleteWarning", shortcutEntry.toString()), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java index 22d3e2bb..7a01a494 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java @@ -1,5 +1,6 @@ package de.hysky.skyblocker.skyblock.waypoint; +import de.hysky.skyblocker.debug.Debug; import de.hysky.skyblocker.utils.Utils; import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; import de.hysky.skyblocker.utils.waypoint.WaypointCategory; @@ -37,6 +38,16 @@ public class WaypointsListWidget extends ElementListWidget getCategory() { if (getSelectedOrNull() instanceof WaypointCategoryEntry category) { return Optional.of(category); @@ -65,21 +76,17 @@ public class WaypointsListWidget extends ElementListWidget { - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - super.mouseClicked(mouseX, mouseY, button); - return true; - } } protected class WaypointCategoryEntry extends AbstractWaypointEntry { private final WaypointCategory category; private final List children; private final ButtonWidget buttonNewWaypoint; + private final ButtonWidget buttonDelete; public WaypointCategoryEntry() { this(new WaypointCategory("New Category", island, new ArrayList<>())); @@ -94,14 +101,22 @@ public class WaypointsListWidget extends ElementListWidget { + int entryIndex = WaypointsListWidget.this.children().indexOf(this) + 1; + while (entryIndex < WaypointsListWidget.this.children().size() && !(WaypointsListWidget.this.children().get(entryIndex) instanceof WaypointCategoryEntry)) { + WaypointsListWidget.this.children().remove(entryIndex); + } + WaypointsListWidget.this.children().remove(this); + waypoints.remove(category); + }).width(50).build(); + children = List.of(buttonNewWaypoint, buttonDelete); } @Override @@ -116,15 +131,19 @@ public class WaypointsListWidget extends ElementListWidget children; + private final ButtonWidget buttonDelete; public WaypointEntry(WaypointCategoryEntry category) { this(category, new NamedWaypoint(BlockPos.ORIGIN, "New Waypoint", new float[]{0f, 1f, 0f})); @@ -133,24 +152,31 @@ public class WaypointsListWidget extends ElementListWidget { + category.category.waypoints().remove(waypoint); + WaypointsListWidget.this.children().remove(this); + }).width(50).build(); + children = List.of(buttonDelete); } @Override public List selectableChildren() { - return List.of(); + return children; } @Override public List children() { - return List.of(); + return children; } @Override public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { context.drawTextWithShadow(client.textRenderer, waypoint.getName(), width / 2 - 125, y + 5, 0xFFFFFF); - context.drawTextWithShadow(client.textRenderer, waypoint.pos.toString(), width / 2 - 50, y + 5, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, waypoint.pos.toShortString(), width / 2 - 25, y + 5, 0xFFFFFF); float[] colorComponents = waypoint.getColorComponents(); - context.drawTextWithShadow(client.textRenderer, String.format("#%02X%02X%02X", (int) (colorComponents[0] * 255), (int) (colorComponents[1] * 255), (int) (colorComponents[2] * 255)), width / 2 + 10, y + 5, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, String.format("#%02X%02X%02X", (int) (colorComponents[0] * 255), (int) (colorComponents[1] * 255), (int) (colorComponents[2] * 255)), width / 2 + 25, y + 5, 0xFFFFFF); + buttonDelete.setPosition(x + entryWidth - 54, y); + buttonDelete.render(context, mouseX, mouseY, tickDelta); } } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java index 4f760995..9f82f7a2 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java @@ -13,7 +13,7 @@ import net.minecraft.text.Text; public class WaypointsScreen extends Screen { private final Screen parent; - final Multimap waypoints = MultimapBuilder.hashKeys().arrayListValues().build(Waypoints.waypoints); // TODO deep copy + final Multimap waypoints = MultimapBuilder.hashKeys().arrayListValues().build(); private WaypointsListWidget waypointsListWidget; private ButtonWidget buttonNew; private ButtonWidget buttonDone; @@ -25,12 +25,13 @@ public class WaypointsScreen extends Screen { public WaypointsScreen(Screen parent) { super(Text.translatable("skyblocker.waypoints.config")); this.parent = parent; + Waypoints.waypoints.forEach((island, category) -> waypoints.put(island, new WaypointCategory(category))); } @Override protected void init() { super.init(); - waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 25)); + waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 24)); GridWidget gridWidget = new GridWidget(); gridWidget.getMainPositioner().marginX(5).marginY(2); GridWidget.Adder adder = gridWidget.createAdder(2); diff --git a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java index 16a22796..d31455f6 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java @@ -5,6 +5,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; +import java.util.ArrayList; import java.util.List; public record WaypointCategory(String name, String island, List waypoints) { @@ -14,6 +15,10 @@ public record WaypointCategory(String name, String island, List w NamedWaypoint.CODEC.listOf().fieldOf("waypoints").forGetter(WaypointCategory::waypoints) ).apply(instance, WaypointCategory::new)); + public WaypointCategory(WaypointCategory waypointCategory) { + this(waypointCategory.name(), waypointCategory.island(), new ArrayList<>(waypointCategory.waypoints())); + } + public static WaypointCategory fromSkytilsJson(JsonObject waypointCategory) { return new WaypointCategory( waypointCategory.get("name").getAsString(), -- cgit From 7675f7569b381c8b9cbf36e2e69e716737455069 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Fri, 8 Mar 2024 19:35:56 -0500 Subject: Update Skytils format to codecs --- .../skyblocker/skyblock/waypoint/Waypoints.java | 21 ++++++++++++++------- .../skyblocker/utils/waypoint/NamedWaypoint.java | 16 +++++++++++----- .../skyblocker/utils/waypoint/WaypointCategory.java | 17 +++++------------ 3 files changed, 30 insertions(+), 24 deletions(-) (limited to 'src/main/java/de') diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java index 89c3f051..8eeae2aa 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -29,6 +29,7 @@ import java.util.List; public class Waypoints { private static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class); private static final Codec> CODEC = WaypointCategory.CODEC.listOf(); + private static final Codec> SKYTILS_CODEC = WaypointCategory.SKYTILS_CODEC.listOf(); private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json"); static final Multimap waypoints = MultimapBuilder.hashKeys().arrayListValues().build(); @@ -48,16 +49,22 @@ public class Waypoints { } } - public static Collection fromSkytilsBase64(String base64) { + public static List fromSkytilsBase64(String base64) { return fromSkytilsJson(new String(Base64.getDecoder().decode(base64))); } - public static Collection<