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 --- .../skyblock/shortcut/ShortcutsConfigScreen.java | 12 +-- .../skyblocker/skyblock/waypoint/Waypoints.java | 23 ++++-- .../skyblock/waypoint/WaypointsListWidget.java | 90 ++++++++++++++++++++-- .../skyblock/waypoint/WaypointsScreen.java | 36 ++++++++- 4 files changed, 137 insertions(+), 24 deletions(-) (limited to 'src/main/java/de/hysky/skyblocker/skyblock') 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