diff options
author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-06-13 02:06:51 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-13 02:06:51 -0400 |
commit | 7b288fbfccb93ed8a10242acf6532127c41f6dfb (patch) | |
tree | 2b3842d1eb8b2d45dab7df29919a127dc76fbe09 /src/main/java/de/hysky/skyblocker | |
parent | abd23322c50f449e1cf5b1ae3e5d8b9ca53489c9 (diff) | |
parent | bd11cc01bfa90695ad7498336a90ee4efb5ec813 (diff) | |
download | Skyblocker-7b288fbfccb93ed8a10242acf6532127c41f6dfb.tar.gz Skyblocker-7b288fbfccb93ed8a10242acf6532127c41f6dfb.tar.bz2 Skyblocker-7b288fbfccb93ed8a10242acf6532127c41f6dfb.zip |
Merge pull request #713 from kevinthegreat1/waypoint
Waypoints
Diffstat (limited to 'src/main/java/de/hysky/skyblocker')
19 files changed, 1086 insertions, 28 deletions
diff --git a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java index 8611af33..eff88783 100644 --- a/src/main/java/de/hysky/skyblocker/SkyblockerMod.java +++ b/src/main/java/de/hysky/skyblocker/SkyblockerMod.java @@ -47,6 +47,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; @@ -113,6 +114,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/config/categories/UIAndVisualsCategory.java b/src/main/java/de/hysky/skyblocker/config/categories/UIAndVisualsCategory.java index 369a9a90..a2a0f815 100644 --- a/src/main/java/de/hysky/skyblocker/config/categories/UIAndVisualsCategory.java +++ b/src/main/java/de/hysky/skyblocker/config/categories/UIAndVisualsCategory.java @@ -3,6 +3,7 @@ package de.hysky.skyblocker.config.categories; import de.hysky.skyblocker.config.ConfigUtils; import de.hysky.skyblocker.config.SkyblockerConfig; import de.hysky.skyblocker.skyblock.fancybars.StatusBarsConfigScreen; +import de.hysky.skyblocker.skyblock.waypoint.WaypointsScreen; import de.hysky.skyblocker.utils.render.title.TitleContainerConfigScreen; import de.hysky.skyblocker.config.configs.UIAndVisualsConfig; import de.hysky.skyblocker.utils.waypoint.Waypoint; @@ -226,12 +227,17 @@ public class UIAndVisualsCategory { .option(Option.<Waypoint.Type>createBuilder() .name(Text.translatable("skyblocker.config.uiAndVisuals.waypoints.waypointType")) .description(OptionDescription.of(Text.translatable("skyblocker.config.uiAndVisuals.waypoints.waypointType.@Tooltip"), - Text.translatable("skyblocker.config.uiAndVisuals.waypoints.waypointType"))) + Text.translatable("skyblocker.config.uiAndVisuals.waypoints.waypointType.generalNote"))) .binding(defaults.uiAndVisuals.waypoints.waypointType, () -> config.uiAndVisuals.waypoints.waypointType, newValue -> config.uiAndVisuals.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()) //Teleport Overlays diff --git a/src/main/java/de/hysky/skyblocker/mixins/accessors/CheckboxWidgetAccessor.java b/src/main/java/de/hysky/skyblocker/mixins/accessors/CheckboxWidgetAccessor.java new file mode 100644 index 00000000..5ec4a8e8 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/mixins/accessors/CheckboxWidgetAccessor.java @@ -0,0 +1,11 @@ +package de.hysky.skyblocker.mixins.accessors; + +import net.minecraft.client.gui.widget.CheckboxWidget; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(CheckboxWidget.class) +public interface CheckboxWidgetAccessor { + @Accessor + void setChecked(boolean checked); +} 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..c0e54904 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 @@ -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..3779a66e 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 @@ -5,10 +5,10 @@ import com.mojang.brigadier.context.CommandContext; import com.mojang.serialization.Codec; import com.mojang.serialization.JsonOps; import com.mojang.serialization.codecs.RecordCodecBuilder; -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 +29,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 +43,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 +53,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 +92,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 +107,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/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; @@ -77,6 +78,11 @@ public class ShortcutsConfigListWidget extends ElementListWidget<ShortcutsConfig } @Override + protected boolean isSelectedEntry(int index) { + return Debug.debugEnabled() ? Objects.equals(getSelectedOrNull(), children().get(index)) : super.isSelectedEntry(index); + } + + @Override protected boolean removeEntry(AbstractShortcutEntry entry) { return super.removeEntry(entry); } 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..66735511 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/shortcut/ShortcutsConfigScreen.java @@ -11,7 +11,6 @@ import net.minecraft.screen.ScreenTexts; import net.minecraft.text.Text; public class ShortcutsConfigScreen extends Screen { - private ShortcutsConfigListWidget shortcutsConfigListWidget; private ButtonWidget buttonDelete; private ButtonWidget buttonNew; @@ -41,14 +40,14 @@ public class ShortcutsConfigScreen extends Screen { shortcutsConfigListWidget.setDimensions(width, height - 96); shortcutsConfigListWidget.updatePositions(); } else { - shortcutsConfigListWidget = new ShortcutsConfigListWidget(client, this, width, height - 96, 32, 25); + shortcutsConfigListWidget = new ShortcutsConfigListWidget(client, this, width, height - 96, 32, 24); initialized = true; } addDrawableChild(shortcutsConfigListWidget); GridWidget gridWidget = new GridWidget(); gridWidget.getMainPositioner().marginX(5).marginY(2); GridWidget.Adder adder = gridWidget.createAdder(2); - buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.delete"), button -> { + 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.stringifiedTranslatable("skyblocker.shortcuts.deleteWarning", shortcutEntry), Text.translatable("selectServer.deleteButton"), ScreenTexts.CANCEL)); @@ -57,16 +56,10 @@ public class ShortcutsConfigScreen extends Screen { 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/AbstractWaypointsScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/AbstractWaypointsScreen.java new file mode 100644 index 00000000..da6f52c8 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/AbstractWaypointsScreen.java @@ -0,0 +1,73 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; +import de.hysky.skyblocker.utils.Location; +import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +import java.util.Arrays; + +public abstract class AbstractWaypointsScreen<T extends Screen> extends Screen { + protected final T parent; + protected final Multimap<String, WaypointCategory> waypoints; + protected String island; + protected WaypointsListWidget waypointsListWidget; + protected DropdownWidget<Location> islandWidget; + + public AbstractWaypointsScreen(Text title, T parent) { + this(title, parent, MultimapBuilder.hashKeys().arrayListValues().build()); + } + + public AbstractWaypointsScreen(Text title, T parent, Multimap<String, WaypointCategory> waypoints) { + this(title, parent, waypoints, Utils.getLocationRaw()); + } + + public AbstractWaypointsScreen(Text title, T parent, Multimap<String, WaypointCategory> waypoints, String island) { + super(title); + this.parent = parent; + this.waypoints = waypoints; + this.island = island; + } + + @Override + protected void init() { + super.init(); + waypointsListWidget = addDrawableChild(new WaypointsListWidget(client, this, width, height - 96, 32, 24)); + islandWidget = addDrawableChild(new DropdownWidget<>(client, width - 160, 8, 150, height - 8, Arrays.asList(Location.values()), this::islandChanged, Location.from(island))); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (islandWidget.mouseClicked(mouseX, mouseY, button)) { + return true; + } + boolean mouseClicked = super.mouseClicked(mouseX, mouseY, button); + updateButtons(); + return mouseClicked; + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + if (islandWidget.isMouseOver(mouseX, mouseY) && islandWidget.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount)) { + return true; + } + return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); + } + + protected void islandChanged(Location location) { + island = location.id(); + waypointsListWidget.setIsland(island); + } + + protected abstract boolean isEnabled(NamedWaypoint waypoint); + + protected abstract void enabledChanged(NamedWaypoint waypoint, boolean enabled); + + protected void updateButtons() { + waypointsListWidget.updateButtons(); + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/DropdownWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/DropdownWidget.java new file mode 100644 index 00000000..724cb461 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/DropdownWidget.java @@ -0,0 +1,141 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +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.text.Style; +import net.minecraft.text.Text; + +import java.util.List; +import java.util.function.Consumer; + +public class DropdownWidget<T> extends ElementListWidget<DropdownWidget.Entry<T>> { + private static final MinecraftClient client = MinecraftClient.getInstance(); + public static final int ENTRY_HEIGHT = 15; + protected final List<T> entries; + protected final Consumer<T> selectCallback; + protected T prevSelected; + protected T selected; + protected boolean open; + + public DropdownWidget(MinecraftClient minecraftClient, int x, int y, int width, int maxHeight, List<T> entries, Consumer<T> selectCallback, T selected) { + super(minecraftClient, width, Math.min((entries.size() + 1) * ENTRY_HEIGHT + 8, maxHeight), y, ENTRY_HEIGHT); + setX(x); + this.entries = entries; + this.selectCallback = selectCallback; + this.selected = selected; + setRenderHeader(true, ENTRY_HEIGHT + 4); + for (T entry : entries) { + addEntry(new Entry<>(this, entry)); + } + } + + @Override + public int getRowLeft() { + return getX(); + } + + @Override + public int getRowWidth() { + return getWidth(); + } + + @Override + protected boolean clickedHeader(int x, int y) { + open = !open; + return true; + } + + @Override + protected void renderHeader(DrawContext context, int x, int y) { + context.drawTextWithShadow(client.textRenderer, selected.toString(), x + 4, y + 2, 0xFFFFFFFF); + } + + @Override + public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { + context.getMatrices().push(); + context.getMatrices().translate(0, 0, 100); + + int y = getY() - (int) getScrollAmount(); + int height = getMaxPosition(); + + context.fill(getX(), y, getX() + width, y + headerHeight, 0xFF000000); + context.drawHorizontalLine(getX(), getX() + width, y, 0xFFFFFFFF); + context.drawHorizontalLine(getX(), getX() + width, y + headerHeight, 0xFFFFFFFF); + context.drawVerticalLine(getX(), y, y + headerHeight, 0xFFFFFFFF); + context.drawVerticalLine(getX() + width, y, y + headerHeight, 0xFFFFFFFF); + + if (open) { + context.fill(getX(), y + headerHeight + 1, getX() + width, y + height, 0xFF000000); + context.drawHorizontalLine(getX(), getX() + width, y + height, 0xFFFFFFFF); + context.drawVerticalLine(getX(), y + headerHeight, y + height, 0xFFFFFFFF); + context.drawVerticalLine(getX() + width, y + headerHeight, y + height, 0xFFFFFFFF); + + super.renderWidget(context, mouseX, mouseY, delta); + } else { + renderHeader(context, getRowLeft(), y + 4); + } + + context.getMatrices().pop(); + } + + @Override + protected void drawMenuListBackground(DrawContext context) {} + + @Override + protected void drawHeaderAndFooterSeparators(DrawContext context) {} + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + return open && super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); + } + + protected void select(T entry) { + selected = entry; + open = false; + setScrollAmount(0); + if (selected != prevSelected) { + selectCallback.accept(entry); + prevSelected = selected; + } + } + + static class Entry<T> extends ElementListWidget.Entry<Entry<T>> { + private final DropdownWidget<T> dropdownWidget; + private final T entry; + + public Entry(DropdownWidget<T> dropdownWidget, T entry) { + this.dropdownWidget = dropdownWidget; + this.entry = entry; + } + + @Override + public List<? extends Selectable> selectableChildren() { + return List.of(); + } + + @Override + public List<? extends Element> 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, Text.literal(entry.toString()).fillStyle(Style.EMPTY.withUnderline(hovered)), x + 14, y + 2, 0xFFFFFFFF); + if (dropdownWidget.selected == this.entry) { + context.drawTextWithShadow(client.textRenderer, "✔", x + 4, y + 2, 0xFFFFFFFF); + } + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (button == 0 && dropdownWidget.open) { + dropdownWidget.select(entry); + return true; + } + return super.mouseClicked(mouseX, mouseY, button); + } + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java index bbc9a655..f8930882 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/OrderedWaypoints.java @@ -408,7 +408,7 @@ public class OrderedWaypoints { } @Override - protected float[] getColorComponents() { + public float[] getColorComponents() { if (this.colorComponents.length != 3) { return switch (this.relativeIndex) { case PREVIOUS -> RED; 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..18096117 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/Waypoints.java @@ -0,0 +1,132 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import com.google.common.collect.Multimap; +import com.google.common.collect.MultimapBuilder; +import com.google.common.collect.Multimaps; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import com.mojang.serialization.Codec; +import com.mojang.serialization.JsonOps; +import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.scheduler.Scheduler; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +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 net.minecraft.client.toast.SystemToast; +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.Base64; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.function.Function; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class Waypoints { + public static final Logger LOGGER = LoggerFactory.getLogger(Waypoints.class); + private static final Codec<List<WaypointCategory>> CODEC = WaypointCategory.CODEC.listOf(); + private static final Codec<List<WaypointCategory>> SKYTILS_CODEC = WaypointCategory.SKYTILS_CODEC.listOf(); + protected static final SystemToast.Type WAYPOINTS_TOAST_TYPE = new SystemToast.Type(); + + private static final Path waypointsFile = FabricLoader.getInstance().getConfigDir().resolve(SkyblockerMod.NAMESPACE).resolve("waypoints.json"); + protected static final Multimap<String, WaypointCategory> waypoints = MultimapBuilder.hashKeys().arrayListValues().build(); + + public static void init() { + loadWaypoints(); + ClientLifecycleEvents.CLIENT_STOPPING.register(Waypoints::saveWaypoints); + WorldRenderEvents.AFTER_TRANSLUCENT.register(Waypoints::render); + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(literal(SkyblockerMod.NAMESPACE).then(literal("waypoints").executes(Scheduler.queueOpenScreenCommand(() -> new WaypointsScreen(MinecraftClient.getInstance().currentScreen)))))); + } + + public static void loadWaypoints() { + waypoints.clear(); + try (BufferedReader reader = Files.newBufferedReader(waypointsFile)) { + List<WaypointCategory> 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("[Skyblocker Waypoints] Encountered exception while loading waypoints", e); + } + } + + public static List<WaypointCategory> fromSkytilsBase64(String base64, String defaultIsland) { + try { + if (base64.startsWith("<Skytils-Waypoint-Data>(V")) { + int version = Integer.parseInt(base64.substring(26, base64.indexOf(')'))); + if (version == 1) { + return fromSkytilsJson(new String(Base64.getDecoder().decode(base64.substring(base64.indexOf(':') + 1))), defaultIsland); + } else { + LOGGER.error("[Skyblocker Waypoints] Unknown Skytils waypoint data version: " + version); + } + } else return fromSkytilsJson(new String(Base64.getDecoder().decode(base64)), defaultIsland); + } catch (NumberFormatException e) { + LOGGER.error("[Skyblocker Waypoints] Encountered exception while parsing Skytils waypoint data version", e); + } catch (IllegalArgumentException e) { + LOGGER.error("[Skyblocker Waypoints] Encountered exception while decoding Skytils waypoint data", e); + } + return Collections.emptyList(); + } + + public static List<WaypointCategory> fromSkytilsJson(String waypointCategories, String defaultIsland) { + JsonArray waypointCategoriesJson; + try { + waypointCategoriesJson = SkyblockerMod.GSON.fromJson(waypointCategories, JsonObject.class).getAsJsonArray("categories"); + } catch (JsonSyntaxException e) { + JsonObject waypointCategoryJson = new JsonObject(); + waypointCategoryJson.addProperty("name", "New Category"); + waypointCategoryJson.addProperty("island", defaultIsland); + waypointCategoryJson.add("waypoints", SkyblockerMod.GSON.fromJson(waypointCategories, JsonArray.class)); + waypointCategoriesJson = new JsonArray(); + waypointCategoriesJson.add(waypointCategoryJson); + } + return SKYTILS_CODEC.parse(JsonOps.INSTANCE, waypointCategoriesJson).resultOrPartial(LOGGER::error).orElseThrow(); + } + + public static String toSkytilsBase64(List<WaypointCategory> waypointCategories) { + return Base64.getEncoder().encodeToString(toSkytilsJson(waypointCategories).getBytes()); + } + + public static String toSkytilsJson(List<WaypointCategory> waypointCategories) { + JsonObject waypointCategoriesJson = new JsonObject(); + waypointCategoriesJson.add("categories", SKYTILS_CODEC.encodeStart(JsonOps.INSTANCE, waypointCategories).resultOrPartial(LOGGER::error).orElseThrow()); + return SkyblockerMod.GSON_COMPACT.toJson(waypointCategoriesJson); + } + + public static void saveWaypoints(MinecraftClient client) { + try (BufferedWriter writer = Files.newBufferedWriter(waypointsFile)) { + 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("[Skyblocker Waypoints] Encountered exception while saving waypoints", e); + } + } + + public static Multimap<String, WaypointCategory> waypointsDeepCopy() { + return waypoints.values().stream().map(WaypointCategory::deepCopy).collect(Multimaps.toMultimap(WaypointCategory::island, Function.identity(), () -> MultimapBuilder.hashKeys().arrayListValues().build())); + } + + public static void render(WorldRenderContext context) { + if (SkyblockerConfigManager.get().uiAndVisuals.waypoints.enableWaypoints) { + Collection<WaypointCategory> 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 new file mode 100644 index 00000000..045c694d --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsListWidget.java @@ -0,0 +1,318 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import de.hysky.skyblocker.mixins.accessors.CheckboxWidgetAccessor; +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import it.unimi.dsi.fastutil.ints.Int2ObjectFunction; +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.*; +import net.minecraft.text.Text; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.HitResult; +import net.minecraft.util.math.BlockPos; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +public class WaypointsListWidget extends ElementListWidget<WaypointsListWidget.AbstractWaypointEntry> { + private final AbstractWaypointsScreen<?> screen; + private String island; + private List<WaypointCategory> waypoints; + + public WaypointsListWidget(MinecraftClient client, AbstractWaypointsScreen<?> screen, int width, int height, int y, int itemHeight) { + super(client, width, height, y, itemHeight); + this.screen = screen; + setIsland(screen.island); + } + + @Override + public int getRowWidth() { + return super.getRowWidth() + 100; + } + + @Override + protected int getScrollbarX() { + return super.getScrollbarX(); + } + + Optional<WaypointCategoryEntry> 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 setIsland(String island) { + this.island = island; + waypoints = (List<WaypointCategory>) screen.waypoints.get(island); + updateEntries(); + } + + void addWaypointCategoryAfterSelected() { + WaypointCategoryEntry categoryEntry = new WaypointCategoryEntry(); + Optional<WaypointCategoryEntry> 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); + } + + void updateEntries() { + clearEntries(); + for (WaypointCategory category : waypoints) { + WaypointCategoryEntry categoryEntry = new WaypointCategoryEntry(category); + addEntry(categoryEntry); + for (NamedWaypoint waypoint : category.waypoints()) { + addEntry(new WaypointEntry(categoryEntry, waypoint)); + } + } + } + + void updateButtons() { + for (Entry<AbstractWaypointEntry> entry : children()) { + if (entry instanceof WaypointCategoryEntry categoryEntry && categoryEntry.enabled.isChecked() != categoryEntry.category.waypoints().stream().allMatch(screen::isEnabled)) { + ((CheckboxWidgetAccessor) categoryEntry.enabled).setChecked(!categoryEntry.enabled.isChecked()); + } else if (entry instanceof WaypointEntry waypointEntry && waypointEntry.enabled.isChecked() != screen.isEnabled(waypointEntry.waypoint)) { + waypointEntry.enabled.onPress(); + } + } + } + + private BlockPos getDefaultPos() { + return client.crosshairTarget instanceof BlockHitResult blockHitResult && client.crosshairTarget.getType() == HitResult.Type.BLOCK ? blockHitResult.getBlockPos() : client.player != null ? client.player.getBlockPos() : BlockPos.ORIGIN; + } + + protected abstract static class AbstractWaypointEntry extends Entry<AbstractWaypointEntry> { + } + + protected class WaypointCategoryEntry extends AbstractWaypointEntry { + private WaypointCategory category; + private final List<ClickableWidget> children; + private final CheckboxWidget enabled; + private final TextFieldWidget nameField; + private final ButtonWidget buttonNewWaypoint; + private final ButtonWidget buttonDelete; + + public WaypointCategoryEntry() { + this(new WaypointCategory("New Category", island, new ArrayList<>())); + } + + public WaypointCategoryEntry(WaypointCategory category) { + this.category = category; + enabled = CheckboxWidget.builder(Text.literal(""), client.textRenderer).checked(!category.waypoints().isEmpty() && category.waypoints().stream().allMatch(screen::isEnabled)).callback((checkbox, checked) -> category.waypoints().forEach(waypoint -> screen.enabledChanged(waypoint, checked))).build(); + nameField = new TextFieldWidget(client.textRenderer, 70, 20, Text.literal("Name")); + nameField.setText(category.name()); + nameField.setChangedListener(this::updateName); + 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 < WaypointsListWidget.this.children().size() && !(WaypointsListWidget.this.children().get(entryIndex) instanceof WaypointCategoryEntry)) { + entryIndex++; + } + } + category.waypoints().add(waypointEntry.waypoint); + WaypointsListWidget.this.children().add(entryIndex, waypointEntry); + }).width(72).build(); + buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.deleteButton"), buttonDelete -> { + 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(38).build(); + children = List.of(enabled, nameField, buttonNewWaypoint, buttonDelete); + } + + @Override + public List<? extends Selectable> selectableChildren() { + return children; + } + + @Override + public List<? extends Element> children() { + return children; + } + + private void updateName(String name) { + int index = waypoints.indexOf(category); + category = category.withName(name); + if (index >= 0) { + waypoints.set(index, category); + } + } + + @Override + public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + enabled.setPosition(x, y + 1); + nameField.setPosition(x + 22, y); + buttonNewWaypoint.setPosition(x + entryWidth - 115, y); + buttonDelete.setPosition(x + entryWidth - 38, y); + for (ClickableWidget child : children) { + child.render(context, mouseX, mouseY, tickDelta); + } + } + } + + protected class WaypointEntry extends AbstractWaypointEntry { + private final WaypointCategoryEntry category; + private NamedWaypoint waypoint; + private final List<ClickableWidget> children; + private final CheckboxWidget enabled; + private final TextFieldWidget nameField; + private final TextFieldWidget xField; + private final TextFieldWidget yField; + private final TextFieldWidget zField; + private final TextFieldWidget colorField; + private final ButtonWidget buttonDelete; + + public WaypointEntry(WaypointCategoryEntry category) { + this(category, new NamedWaypoint(getDefaultPos(), "New Waypoint", new float[]{0f, 1f, 0f})); + } + + public WaypointEntry(WaypointCategoryEntry category, NamedWaypoint waypoint) { + this.category = category; + this.waypoint = waypoint; + enabled = CheckboxWidget.builder(Text.literal(""), client.textRenderer).checked(screen.isEnabled(waypoint)).callback((checkbox, checked) -> screen.enabledChanged(waypoint, checked)).build(); + nameField = new TextFieldWidget(client.textRenderer, 65, 20, Text.literal("Name")); + nameField.setText(waypoint.getName().getString()); + nameField.setChangedListener(this::updateName); + xField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("X")); + xField.setText(Integer.toString(waypoint.pos.getX())); + xField.setTextPredicate(this::checkInt); + xField.setChangedListener(this::updateX); + yField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("Y")); + yField.setText(Integer.toString(waypoint.pos.getY())); + yField.setTextPredicate(this::checkInt); + yField.setChangedListener(this::updateY); + zField = new TextFieldWidget(client.textRenderer, 26, 20, Text.literal("Z")); + zField.setText(Integer.toString(waypoint.pos.getZ())); + zField.setTextPredicate(this::checkInt); + zField.setChangedListener(this::updateZ); + colorField = new TextFieldWidget(client.textRenderer, 56, 20, Text.literal("Color")); + colorField.setText(String.format("%02X%02X%02X%02X", (int) (waypoint.alpha * 255), (int) (waypoint.getColorComponents()[0] * 255), (int) (waypoint.getColorComponents()[1] * 255), (int) (waypoint.getColorComponents()[2] * 255))); + colorField.setChangedListener(this::updateColor); + buttonDelete = ButtonWidget.builder(Text.translatable("selectServer.deleteButton"), button -> { + category.category.waypoints().remove(waypoint); + WaypointsListWidget.this.children().remove(this); + }).width(38).build(); + children = List.of(enabled, nameField, xField, yField, zField, colorField, buttonDelete); + } + + @Override + public List<? extends Selectable> selectableChildren() { + return children; + } + + @Override + public List<? extends Element> children() { + return children; + } + + private void updateName(String name) { + if (waypoint.name.getString().equals(name)) return; + int index = category.category.waypoints().indexOf(waypoint); + waypoint = waypoint.withName(name); + if (index >= 0) { + category.category.waypoints().set(index, waypoint); + } + } + + private boolean checkInt(String string) { + try { + parseEmptiableInt(string); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + private void updateX(String xString) { + updateInt(xString, waypoint.pos.getX(), waypoint::withX); + } + + private void updateY(String yString) { + updateInt(yString, waypoint.pos.getY(), waypoint::withY); + } + + private void updateZ(String zString) { + updateInt(zString, waypoint.pos.getZ(), waypoint::withZ); + } + + private void updateInt(String newValueString, int currentValue, Int2ObjectFunction<NamedWaypoint> wither) { + try { + int index = category.category.waypoints().indexOf(waypoint); + int newValue = parseEmptiableInt(newValueString); + if (newValue == currentValue) return; + waypoint = wither.apply(newValue); + if (index >= 0) { + category.category.waypoints().set(index, waypoint); + } + } catch (NumberFormatException e) { + Waypoints.LOGGER.warn("[Skyblocker Waypoints] Failed to parse integer: {}", newValueString, e); + } + } + + private void updateColor(String colorString) { + try { + int index = category.category.waypoints().indexOf(waypoint); + int colorInt = parseEmptiableInt(colorString, 16); + float[] colorComponents = {((colorInt & 0x00FF0000) >> 16) / 255f, ((colorInt & 0x0000FF00) >> 8) / 255f, (colorInt & 0x000000FF) / 255f}; + float alpha = ((colorInt & 0xFF000000) >>> 24) / 255f; + if (Arrays.equals(waypoint.getColorComponents(), colorComponents) && waypoint.alpha == alpha) return; + waypoint = waypoint.withColor(colorComponents, alpha); + if (index >= 0) { + category.category.waypoints().set(index, waypoint); + } + } catch (NumberFormatException e) { + Waypoints.LOGGER.warn("[Skyblocker Waypoints] Failed to parse color: {}", colorString, e); + } + } + + private int parseEmptiableInt(String value) throws NumberFormatException { + return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value); + } + + @SuppressWarnings("SameParameterValue") + private int parseEmptiableInt(String value, int radix) throws NumberFormatException { + return value.isEmpty() || value.equals("-") ? 0 : Integer.parseInt(value, radix); + } + + @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, "X:", width / 2 - 56, y + 6, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, "Y:", width / 2 - 19, y + 6, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, "Z:", width / 2 + 18, y + 6, 0xFFFFFF); + context.drawTextWithShadow(client.textRenderer, "#", x + entryWidth - 105, y + 6, 0xFFFFFF); + enabled.setPosition(x + 10, y + 1); + nameField.setPosition(x + 32, y); + xField.setPosition(width / 2 - 48, y); + yField.setPosition(width / 2 - 11, y); + zField.setPosition(width / 2 + 26, y); + colorField.setPosition(x + entryWidth - 99, y); + buttonDelete.setPosition(x + entryWidth - 38, y); + for (ClickableWidget child : children) { + child.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 new file mode 100644 index 00000000..23a24361 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsScreen.java @@ -0,0 +1,76 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.ConfirmScreen; +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 AbstractWaypointsScreen<Screen> { + private ButtonWidget buttonNew; + private ButtonWidget buttonDone; + + public WaypointsScreen(Screen parent) { + super(Text.translatable("skyblocker.waypoints.config"), parent, Waypoints.waypointsDeepCopy()); + } + + @Override + protected void init() { + super.init(); + 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 -> client.setScreen(new WaypointsShareScreen(this, waypoints))).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 + 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); + } + + @Override + protected boolean isEnabled(NamedWaypoint waypoint) { + return waypoint.shouldRender(); + } + + @Override + protected void enabledChanged(NamedWaypoint waypoint, boolean enabled) { + waypoint.setShouldRender(enabled); + } + + private void saveWaypoints() { + Waypoints.waypoints.clear(); + Waypoints.waypoints.putAll(waypoints); + Waypoints.saveWaypoints(client); + } + + @Override + public void close() { + assert client != null; + if (!waypoints.equals(Waypoints.waypoints)) { + client.setScreen(new ConfirmScreen(confirmedAction -> client.setScreen(confirmedAction ? parent : this), + Text.translatable("text.skyblocker.quit_config"), + Text.translatable("text.skyblocker.quit_config_sure"), + Text.translatable("text.skyblocker.quit_discard"), + ScreenTexts.CANCEL + )); + } else { + client.setScreen(parent); + } + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java new file mode 100644 index 00000000..aee21ec8 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/waypoint/WaypointsShareScreen.java @@ -0,0 +1,86 @@ +package de.hysky.skyblocker.skyblock.waypoint; + +import com.google.common.collect.Multimap; +import de.hysky.skyblocker.utils.waypoint.NamedWaypoint; +import de.hysky.skyblocker.utils.waypoint.WaypointCategory; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.client.gui.widget.GridWidget; +import net.minecraft.client.gui.widget.SimplePositioningWidget; +import net.minecraft.client.toast.SystemToast; +import net.minecraft.screen.ScreenTexts; +import net.minecraft.text.Text; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class WaypointsShareScreen extends AbstractWaypointsScreen<WaypointsScreen> { + private final Set<NamedWaypoint> selectedWaypoints = new HashSet<>(); + + protected WaypointsShareScreen(WaypointsScreen parent, Multimap<String, WaypointCategory> waypoints) { + super(Text.translatable("skyblocker.waypoints.shareWaypoints"), parent, waypoints, parent.island); + } + + @Override + protected void init() { + super.init(); + GridWidget gridWidget = new GridWidget(); + gridWidget.getMainPositioner().marginX(5).marginY(2); + GridWidget.Adder adder = gridWidget.createAdder(2); + adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.importWaypointsSkytils"), buttonImport -> { + try { + List<WaypointCategory> waypointCategories = Waypoints.fromSkytilsBase64(client.keyboard.getClipboard(), island); + for (WaypointCategory waypointCategory : waypointCategories) { + selectedWaypoints.addAll(waypointCategory.waypoints()); + waypoints.put(waypointCategory.island(), waypointCategory); + } + waypointsListWidget.updateEntries(); + SystemToast.show(client.getToastManager(), Waypoints.WAYPOINTS_TOAST_TYPE, Text.translatable("skyblocker.waypoints.importSuccess"), Text.translatable("skyblocker.waypoints.importSuccessText", waypointCategories.stream().map(WaypointCategory::waypoints).mapToInt(List::size).sum(), waypointCategories.size())); + } catch (Exception e) { + Waypoints.LOGGER.error("[Skyblocker Waypoints] Encountered exception while parsing Skytils waypoint data", e); + SystemToast.show(client.getToastManager(), Waypoints.WAYPOINTS_TOAST_TYPE, Text.translatable("skyblocker.waypoints.importError"), Text.translatable("skyblocker.waypoints.importErrorText")); + } + }).tooltip(Tooltip.of(Text.translatable("skyblocker.waypoints.importWaypointsSkytils.tooltip"))).build()); + adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.importWaypointsSnoopy"), buttonImport -> { + }).tooltip(Tooltip.of(Text.translatable("skyblocker.waypoints.importWaypointsSnoopy.tooltip"))).build()); + adder.add(ButtonWidget.builder(ScreenTexts.BACK, buttonBack -> close()).build()); + adder.add(ButtonWidget.builder(Text.translatable("skyblocker.waypoints.exportWaypointsSkytils"), buttonExport -> { + try { + List<WaypointCategory> waypointCategories = waypoints.values().stream().filter(waypointCategory -> waypointCategory.island().equals(island)).map(WaypointCategory.filter(selectedWaypoints::contains)).filter(waypointCategory -> !waypointCategory.waypoints().isEmpty()).toList(); + client.keyboard.setClipboard(Waypoints.toSkytilsBase64(waypointCategories)); + SystemToast.show(client.getToastManager(), Waypoints.WAYPOINTS_TOAST_TYPE, Text.translatable("skyblocker.waypoints.exportSuccess"), Text.translatable("skyblocker.waypoints.exportSuccessText", waypointCategories.stream().map(WaypointCategory::waypoints).mapToInt(List::size).sum(), waypointCategories.size())); + } catch (Exception e) { + Waypoints.LOGGER.error("[Skyblocker Waypoints] Encountered exception while serializing Skytils waypoint data", e); + SystemToast.show(client.getToastManager(), Waypoints.WAYPOINTS_TOAST_TYPE, Text.translatable("skyblocker.waypoints.exportError"), Text.translatable("skyblocker.waypoints.exportErrorText")); + } + }).tooltip(Tooltip.of(Text.translatable("skyblocker.waypoints.exportWaypointsSkytils.tooltip"))).build()); + gridWidget.refreshPositions(); + SimplePositioningWidget.setPos(gridWidget, 0, this.height - 64, this.width, 64); + gridWidget.forEachChild(this::addDrawableChild); + } + + @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); + } + + @Override + protected boolean isEnabled(NamedWaypoint waypoint) { + return selectedWaypoints.contains(waypoint); + } + + @Override + protected void enabledChanged(NamedWaypoint waypoint, boolean enabled) { + if (enabled) selectedWaypoints.add(waypoint); + else selectedWaypoints.remove(waypoint); + } + + @SuppressWarnings("DataFlowIssue") + @Override + public void close() { + client.setScreen(parent); + } +} 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..2f02b51f --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/NamedWaypoint.java @@ -0,0 +1,128 @@ +package de.hysky.skyblocker.utils.waypoint; + +import com.google.common.primitives.Floats; +import com.mojang.datafixers.util.Either; +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.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.Objects; +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.FLOAT.fieldOf("alpha").forGetter(secretWaypoint -> secretWaypoint.alpha), + Codec.BOOL.fieldOf("shouldRender").forGetter(Waypoint::shouldRender) + ).apply(instance, NamedWaypoint::new)); + public static final Codec<NamedWaypoint> SKYTILS_CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.INT.fieldOf("x").forGetter(waypoint -> waypoint.pos.getX()), + Codec.INT.fieldOf("y").forGetter(waypoint -> waypoint.pos.getY()), + Codec.INT.fieldOf("z").forGetter(waypoint -> waypoint.pos.getZ()), + Codec.either(Codec.STRING, Codec.INT).xmap(either -> either.map(str -> str, Object::toString), Either::left).fieldOf("name").forGetter(waypoint -> waypoint.name.getString()), + Codec.INT.fieldOf("color").forGetter(waypoint -> (int) (waypoint.alpha * 255) << 24 | (int) (waypoint.colorComponents[0] * 255) << 16 | (int) (waypoint.colorComponents[1] * 255) << 8 | (int) (waypoint.colorComponents[2] * 255)), + Codec.BOOL.fieldOf("enabled").forGetter(Waypoint::shouldRender) + ).apply(instance, NamedWaypoint::fromSkytils)); + public final Text name; + public 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, name, colorComponents, DEFAULT_HIGHLIGHT_ALPHA, shouldRender); + } + + public NamedWaypoint(BlockPos pos, String name, float[] colorComponents, float alpha, boolean shouldRender) { + this(pos, Text.of(name), colorComponents, alpha, shouldRender); + } + + public NamedWaypoint(BlockPos pos, Text name, float[] colorComponents, float alpha, boolean shouldRender) { + this(pos, name, () -> SkyblockerConfigManager.get().uiAndVisuals.waypoints.waypointType, colorComponents, alpha, shouldRender); + } + + public NamedWaypoint(BlockPos pos, Text name, Supplier<Type> typeSupplier, float[] colorComponents) { + this(pos, name, typeSupplier, colorComponents, DEFAULT_HIGHLIGHT_ALPHA, true); + } + + public NamedWaypoint(BlockPos pos, Text name, Supplier<Type> typeSupplier, float[] colorComponents, float alpha, boolean shouldRender) { + super(pos, typeSupplier, colorComponents, alpha, DEFAULT_LINE_WIDTH, true, shouldRender); + this.name = name; + this.centerPos = pos.toCenterPos(); + } + + public static NamedWaypoint fromSkytils(int x, int y, int z, String name, int color, boolean enabled) { + float alpha = ((color & 0xFF000000) >>> 24) / 255f; + if (alpha == 0) { + alpha = DEFAULT_HIGHLIGHT_ALPHA; + } + return new NamedWaypoint(new BlockPos(x, y, z), name, new float[]{((color & 0x00FF0000) >> 16) / 255f, ((color & 0x0000FF00) >> 8) / 255f, (color & 0x000000FF) / 255f}, alpha, enabled); + } + + public NamedWaypoint copy() { + return new NamedWaypoint(pos, name, typeSupplier, getColorComponents(), alpha, shouldRender()); + } + + @Override + public NamedWaypoint withX(int x) { + return new NamedWaypoint(new BlockPos(x, pos.getY(), pos.getZ()), name, typeSupplier, getColorComponents(), alpha, shouldRender()); + } + + @Override + public NamedWaypoint withY(int y) { + return new NamedWaypoint(pos.withY(y), name, typeSupplier, getColorComponents(), alpha, shouldRender()); + } + + @Override + public NamedWaypoint withZ(int z) { + return new NamedWaypoint(new BlockPos(pos.getX(), pos.getY(), z), name, typeSupplier, getColorComponents(), alpha, shouldRender()); + } + + @Override + public NamedWaypoint withColor(float[] colorComponents, float alpha) { + return new NamedWaypoint(pos, name, typeSupplier, colorComponents, alpha, shouldRender()); + } + + public Text getName() { + return name; + } + + public NamedWaypoint withName(String name) { + return new NamedWaypoint(pos, Text.literal(name), typeSupplier, getColorComponents(), alpha, shouldRender()); + } + + 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 int hashCode() { + return Objects.hash(super.hashCode(), name); + } + + @Override + public boolean equals(Object obj) { + return this == obj || super.equals(obj) && obj instanceof NamedWaypoint waypoint && name.equals(waypoint.name); + } +} 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 622e1658..c991fb9c 100644 --- a/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/Waypoint.java @@ -3,9 +3,12 @@ package de.hysky.skyblocker.utils.waypoint; import de.hysky.skyblocker.utils.render.RenderHelper; import de.hysky.skyblocker.utils.render.Renderable; import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; +import net.minecraft.util.StringIdentifiable; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Box; +import java.util.Arrays; +import java.util.Objects; import java.util.function.Supplier; public class Waypoint implements Renderable { @@ -15,9 +18,9 @@ public class Waypoint implements Renderable { final Box box; final Supplier<Type> typeSupplier; protected final float[] colorComponents; - final float alpha; - final float lineWidth; - final boolean throughWalls; + public final float alpha; + public final float lineWidth; + public final boolean throughWalls; private boolean shouldRender; public Waypoint(BlockPos pos, Type type, float[] colorComponents) { @@ -55,6 +58,22 @@ public class Waypoint implements Renderable { this.shouldRender = shouldRender; } + public Waypoint withX(int x) { + return new Waypoint(new BlockPos(x, pos.getY(), pos.getZ()), typeSupplier, getColorComponents(), alpha, lineWidth, throughWalls, shouldRender()); + } + + public Waypoint withY(int y) { + return new Waypoint(pos.withY(y), typeSupplier, getColorComponents(), alpha, lineWidth, throughWalls, shouldRender()); + } + + public Waypoint withZ(int z) { + return new Waypoint(new BlockPos(pos.getX(), pos.getY(), z), typeSupplier, getColorComponents(), alpha, lineWidth, throughWalls, shouldRender()); + } + + public Waypoint withColor(float[] colorComponents, float alpha) { + return new Waypoint(pos, typeSupplier, colorComponents, alpha, lineWidth, throughWalls, shouldRender()); + } + public boolean shouldRender() { return shouldRender; } @@ -71,7 +90,11 @@ public class Waypoint implements Renderable { this.shouldRender = !this.shouldRender; } - protected float[] getColorComponents() { + public void setShouldRender(boolean shouldRender) { + this.shouldRender = shouldRender; + } + + public float[] getColorComponents() { return colorComponents; } @@ -94,7 +117,17 @@ public class Waypoint implements Renderable { } } - public enum Type { + @Override + public int hashCode() { + return Objects.hash(pos, typeSupplier.get(), Arrays.hashCode(colorComponents), alpha, lineWidth, throughWalls, shouldRender); + } + + @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 implements StringIdentifiable { WAYPOINT, OUTLINED_WAYPOINT, HIGHLIGHT, @@ -102,6 +135,11 @@ public class Waypoint implements Renderable { OUTLINE; @Override + public String asString() { + return name().toLowerCase(); + } + + @Override public String toString() { return switch (this) { case WAYPOINT -> "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..db2a6d82 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/utils/waypoint/WaypointCategory.java @@ -0,0 +1,43 @@ +package de.hysky.skyblocker.utils.waypoint; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; + +import java.util.List; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; +import java.util.stream.Collectors; + +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 final Codec<WaypointCategory> SKYTILS_CODEC = RecordCodecBuilder.create(instance -> instance.group( + Codec.STRING.fieldOf("name").forGetter(WaypointCategory::name), + Codec.STRING.fieldOf("island").forGetter(WaypointCategory::island), + NamedWaypoint.SKYTILS_CODEC.listOf().fieldOf("waypoints").forGetter(WaypointCategory::waypoints) + ).apply(instance, WaypointCategory::new)); + + public static UnaryOperator<WaypointCategory> filter(Predicate<NamedWaypoint> predicate) { + return waypointCategory -> new WaypointCategory(waypointCategory.name(), waypointCategory.island(), waypointCategory.waypoints().stream().filter(predicate).toList()); + } + + public WaypointCategory withName(String name) { + return new WaypointCategory(name, island(), waypoints()); + } + + public WaypointCategory deepCopy() { + return new WaypointCategory(name(), island(), waypoints().stream().map(NamedWaypoint::copy).collect(Collectors.toList())); + } + + public void render(WorldRenderContext context) { + for (NamedWaypoint waypoint : waypoints) { + if (waypoint.shouldRender()) { + waypoint.render(context); + } + } + } +} |