diff --git a/build.gradle b/build.gradle index 15b89b9..2fd94a5 100644 --- a/build.gradle +++ b/build.gradle @@ -15,15 +15,21 @@ repositories { name = "CottonMC" url = "https://server.bbkr.space/artifactory/libs-release" } + maven { + name = "ClothConfig" + url = "https://maven.shedaniel.me/" + } } dependencies { minecraft "com.mojang:minecraft:${project.minecraft_version}" mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}") + modImplementation include("me.shedaniel.cloth:cloth-config-fabric:${project.clothconfig_version}") } processResources { diff --git a/gradle.properties b/gradle.properties index cc65ca9..f514cbc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G # Mod Properties mod_version = 1.16.5-0.0.3 - maven_group = me.bionicbeanie.mods + maven_group = me.bionicbeanie.mods.savecoords archives_base_name = save-coordinates # Dependencies @@ -18,3 +18,5 @@ org.gradle.jvmargs=-Xmx1G # https://github.com/CottonMC/LibGui/wiki/Setup libgui_version=3.4.0+1.16.5 + + clothconfig_version=4.11.26 diff --git a/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java b/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java deleted file mode 100644 index 6bb780f..0000000 --- a/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java +++ /dev/null @@ -1,62 +0,0 @@ -package me.bionicbeanie.mods; - -import org.lwjgl.glfw.GLFW; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IViewHandler; -import me.bionicbeanie.mods.api.IPlayerLocator; -import me.bionicbeanie.mods.api.IScreenController; -import me.bionicbeanie.mods.gui.SaveCoordinatesGui; -import me.bionicbeanie.mods.gui.ScreenController; -import me.bionicbeanie.mods.gui.view.DefaultViewHandler; -import me.bionicbeanie.mods.gui.view.ListViewHandler; -import me.bionicbeanie.mods.impl.FileStore; -import me.bionicbeanie.mods.impl.PlayerLocator; -import me.bionicbeanie.mods.model.PlayerPosition; -import net.fabricmc.api.ClientModInitializer; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.options.KeyBinding; -import net.minecraft.client.util.InputUtil; - -public class SaveCoordinatesClient implements ClientModInitializer { - - private static IPlayerLocator locator = new PlayerLocator(); - - @Override - public void onInitializeClient() { - - KeyBinding keyBinding = registerKeyBinding(); - - ClientTickEvents.END_CLIENT_TICK.register(client -> { - while (keyBinding.wasPressed()) { - IGui gui = CreateModGui(client); - gui.showDefaultView(); - } - }); - } - - private KeyBinding registerKeyBinding() { - String translationKey = "key.savecoords.coords"; - String category = "category.savecoords.generic"; - int keyBind = GLFW.GLFW_KEY_H; - - KeyBinding keyBinding = new KeyBinding(translationKey, InputUtil.Type.KEYSYM, keyBind, category); - - return KeyBindingHelper.registerKeyBinding(keyBinding); - } - - private IGui CreateModGui(MinecraftClient client) { - IGui gui = new SaveCoordinatesGui(); - IFileStore fileStore = new FileStore(client.runDirectory.getAbsolutePath()); - IViewHandler defaultHandler = new DefaultViewHandler(fileStore, locator, client, gui); - IViewHandler listHandler = new ListViewHandler(fileStore, gui, client); - IScreenController screenController = new ScreenController(client); - - gui.init(defaultHandler, listHandler, screenController); - - return gui; - } -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IFileStore.java b/src/main/java/me/bionicbeanie/mods/api/IFileStore.java deleted file mode 100644 index c22d8b7..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IFileStore.java +++ /dev/null @@ -1,19 +0,0 @@ -package me.bionicbeanie.mods.api; - -import java.io.IOException; -import java.util.List; - -import me.bionicbeanie.mods.model.PlayerPosition; - -public interface IFileStore { - - public String getDefaultWorld() throws IOException; - - public void setDefaultWorld(String defaultWorldName) throws IOException; - - public void save(PlayerPosition position) throws IOException; - - public void delete(String id) throws IOException; - - public List list() throws IOException; -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IGui.java b/src/main/java/me/bionicbeanie/mods/api/IGui.java deleted file mode 100644 index 0f8241a..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IGui.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.bionicbeanie.mods.api; - -import me.bionicbeanie.mods.model.PlayerPosition; - -public interface IGui { - - public void init(IViewHandler saveHandler, IViewHandler listHandler, - IScreenController screenController); - - public void showDefaultView(); - - public void setDefaultViewState(PlayerPosition position); - - public void showListView(); - - public void close(); - -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IPlayerLocator.java b/src/main/java/me/bionicbeanie/mods/api/IPlayerLocator.java deleted file mode 100644 index 7adec47..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IPlayerLocator.java +++ /dev/null @@ -1,9 +0,0 @@ -package me.bionicbeanie.mods.api; - -import me.bionicbeanie.mods.model.PlayerRawPosition; -import net.minecraft.client.MinecraftClient; - -public interface IPlayerLocator { - - public PlayerRawPosition locate(MinecraftClient client); -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IRootGridPanel.java b/src/main/java/me/bionicbeanie/mods/api/IRootGridPanel.java deleted file mode 100644 index a23bb5d..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IRootGridPanel.java +++ /dev/null @@ -1,13 +0,0 @@ -package me.bionicbeanie.mods.api; - -import io.github.cottonmc.cotton.gui.GuiDescription; -import io.github.cottonmc.cotton.gui.widget.WWidget; - -public interface IRootGridPanel { - - public void add(WWidget widget, int x, int y, int w, int h); - - public void validate(GuiDescription gui); - - public void reset(); -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IScreenController.java b/src/main/java/me/bionicbeanie/mods/api/IScreenController.java deleted file mode 100644 index 5da4cbc..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IScreenController.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.bionicbeanie.mods.api; - -import net.minecraft.client.gui.screen.Screen; - -public interface IScreenController { - - public void openScreen(Screen screen); - - public void closeScreen(); -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IViewHandler.java b/src/main/java/me/bionicbeanie/mods/api/IViewHandler.java deleted file mode 100644 index 96ebf14..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IViewHandler.java +++ /dev/null @@ -1,9 +0,0 @@ -package me.bionicbeanie.mods.api; - -public interface IViewHandler { - - void setState(T state); - void clearState(); - void placeWidgets(IRootGridPanel rootPanel); - -} diff --git a/src/main/java/me/bionicbeanie/mods/api/IViewOperation.java b/src/main/java/me/bionicbeanie/mods/api/IViewOperation.java deleted file mode 100644 index 0429878..0000000 --- a/src/main/java/me/bionicbeanie/mods/api/IViewOperation.java +++ /dev/null @@ -1,5 +0,0 @@ -package me.bionicbeanie.mods.api; - -public interface IViewOperation extends Runnable { - -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java b/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java deleted file mode 100644 index 04d9f22..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java +++ /dev/null @@ -1,66 +0,0 @@ -package me.bionicbeanie.mods.gui; - -import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IViewHandler; -import me.bionicbeanie.mods.model.PlayerPosition; -import me.bionicbeanie.mods.api.IRootGridPanel; -import me.bionicbeanie.mods.api.IScreenController; - -public class SaveCoordinatesGui extends LightweightGuiDescription implements IGui { - - private IRootGridPanel rootGridPanel; - private IViewHandler defaultHandler; - private IViewHandler listHandler; - private IScreenController screenController; - - @Override - public void init(IViewHandler saveHandler, IViewHandler listHandler, - IScreenController screenController) { - this.rootGridPanel = createRootPanel(); - this.defaultHandler = saveHandler; - this.listHandler = listHandler; - this.screenController = screenController; - - openScreen(); - } - - @Override - public void showDefaultView() { - showView(defaultHandler); - } - - @Override - public void setDefaultViewState(PlayerPosition position) { - defaultHandler.setState(position); - } - - @Override - public void showListView() { - showView(listHandler); - } - - @Override - public void close() { - screenController.closeScreen(); - } - - private void openScreen() { - screenController.openScreen(new SaveCoordinatesScreen(this)); - } - - private IRootGridPanel createRootPanel() { - RootGridPanel panel = new RootGridPanel(18); - panel.setSize(15 * 18, 10 * 18); - - setRootPanel(panel); - return panel; - } - - private void showView(IViewHandler handler) { - rootGridPanel.reset(); - handler.placeWidgets(rootGridPanel); - rootGridPanel.validate(this); - } - -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java b/src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java deleted file mode 100644 index 82ce041..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java +++ /dev/null @@ -1,149 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import io.github.cottonmc.cotton.gui.widget.WButton; -import io.github.cottonmc.cotton.gui.widget.WLabel; -import io.github.cottonmc.cotton.gui.widget.WText; -import io.github.cottonmc.cotton.gui.widget.WTextField; -import io.github.cottonmc.cotton.gui.widget.WWidget; -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IPlayerLocator; -import me.bionicbeanie.mods.api.IRootGridPanel; -import me.bionicbeanie.mods.model.PlayerPosition; -import me.bionicbeanie.mods.model.PlayerRawPosition; -import me.bionicbeanie.mods.util.DimensionSpriteUtil; -import net.minecraft.client.MinecraftClient; -import net.minecraft.text.LiteralText; - -public class DefaultViewHandler extends ViewHandlerBase { - - private IPlayerLocator locator; - - public DefaultViewHandler(IFileStore fileStore, IPlayerLocator locator, MinecraftClient client, IGui gui) { - super(fileStore, gui, client); - this.locator = locator; - } - - @Override - public void placeWidgets(IRootGridPanel root, PlayerPosition existingPosition) { - - PlayerRawPosition rawPosition = existingPosition == null ? locator.locate(client) : existingPosition; - - WWidget xLabel = CreateLabelForCoorindate("X"); - WWidget yLabel = CreateLabelForCoorindate("Y"); - WWidget zLabel = CreateLabelForCoorindate("Z"); - - WWidget xText = CreateWidgetForCoordinate(rawPosition.getX()); - WWidget yText = CreateWidgetForCoordinate(rawPosition.getY()); - WWidget zText = CreateWidgetForCoordinate(rawPosition.getZ()); - - root.add(xLabel, 2, 1, 2, 1); - root.add(yLabel, 2, 2, 2, 1); - root.add(zLabel, 2, 3, 2, 1); - - root.add(xText, 3, 1, 2, 1); - root.add(yText, 3, 2, 2, 1); - root.add(zText, 3, 3, 2, 1); - - WWidget icon = DimensionSpriteUtil.CreateWorldIcon(rawPosition.getWorldDimension()); - root.add(icon, 8, 1, 2, 2); - - String defaultWorld = getDefaultWorld(existingPosition); - WTextField world = CreateWorldField(defaultWorld); - root.add(world, 0, 4, 4, 1); - - WTextField location = CreateLocationField(existingPosition); - root.add(location, 5, 4, 7, 1); - - WTextField notes = CreateNotesField(existingPosition); - root.add(notes, 0, 6, 12, 1); - - WWidget save = CreateSaveButton(rawPosition, location, notes, world, existingPosition); - root.add(save, 13, 6, 2, 1); - - WWidget list = CreateListButton(); - root.add(list, 13, 9, 2, 1); - - WWidget ping = CreatePingButton(rawPosition); - root.add(ping, 13, 1, 2, 1); - - WWidget close = CreateCloseButton(); - root.add(close, 0, 9, 2, 1); - } - - private String getDefaultWorld(PlayerPosition existingPosition) { - if (existingPosition != null && existingPosition.getPositionMetadata().getWorldName() != null) { - return existingPosition.getPositionMetadata().getWorldName(); - } - - try { - return fileStore.getDefaultWorld(); - } catch (Exception e) { - e.printStackTrace(); - } - - return ""; - } - - private WWidget CreateLabelForCoorindate(String label) { - return new WLabel(label, 0xb80000); - } - - private WWidget CreateWidgetForCoordinate(long l) { - return new WText(new LiteralText(String.valueOf(l)), 0x3939ac); - } - - private WTextField CreateLocationField(PlayerPosition existingPosition) { - WTextField location = new WTextField(new LiteralText("location name")); - - if (existingPosition != null) { - location.setText(existingPosition.getLocationName()); - } - - location.setMaxLength(20); - - return location; - } - - private WTextField CreateNotesField(PlayerPosition existingPosition) { - WTextField notes = new WTextField(new LiteralText("additional notes")); - - if (existingPosition != null && existingPosition.getPositionMetadata() != null) { - notes.setText(existingPosition.getPositionMetadata().getNotes()); - } - - return notes; - } - - private WTextField CreateWorldField(String defaultWorld) { - WTextField world = new WTextField(new LiteralText("world name")); - world.setMaxLength(7); - world.setText(defaultWorld); - return world; - } - - private WWidget CreateSaveButton(PlayerRawPosition rawPosition, WTextField location, WTextField notes, - WTextField world, PlayerPosition existingPosition) { - WButton button = new WButton(new LiteralText("SAVE")); - button.setOnClick(new SaveOperation(fileStore, gui, rawPosition, world, location, notes, existingPosition)); - return button; - } - - private WWidget CreateListButton() { - WButton button = new WButton(new LiteralText("LIST")); - button.setOnClick(gui::showListView); - return button; - } - - private WWidget CreatePingButton(PlayerRawPosition rawPosition) { - WButton button = new WButton(new LiteralText("PING")); - button.setOnClick(new PingOperation(fileStore, gui, rawPosition)); - return button; - } - - private WWidget CreateCloseButton() { - WButton button = new WButton(new LiteralText("CLOSE")); - button.setOnClick(gui::close); - return button; - } -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/DeleteOperation.java b/src/main/java/me/bionicbeanie/mods/gui/view/DeleteOperation.java deleted file mode 100644 index 063e87d..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/DeleteOperation.java +++ /dev/null @@ -1,18 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.model.PlayerPosition; - -public class DeleteOperation extends ViewPositionOperationBase { - - public DeleteOperation(IFileStore fileStore, IGui gui, PlayerPosition position) { - super(fileStore, gui, position); - } - - @Override - protected void executeOperation(IFileStore fileStore, IGui gui, PlayerPosition position) throws Exception { - fileStore.delete(position.getId()); - gui.showListView(); - } -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/PingOperation.java b/src/main/java/me/bionicbeanie/mods/gui/view/PingOperation.java deleted file mode 100644 index 0c69121..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/PingOperation.java +++ /dev/null @@ -1,23 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.model.PlayerRawPosition; -import net.minecraft.client.MinecraftClient; - -public class PingOperation extends ViewOperationBase{ - - private PlayerRawPosition position; - - public PingOperation(IFileStore fileStore, IGui gui, PlayerRawPosition position) { - super(fileStore, gui); - - this.position = position; - } - - @SuppressWarnings("resource") - @Override - protected void executeOperation(IFileStore fileStore, IGui gui) throws Exception { - MinecraftClient.getInstance().player.sendChatMessage(position.toString()); - } -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/SaveOperation.java b/src/main/java/me/bionicbeanie/mods/gui/view/SaveOperation.java deleted file mode 100644 index 961b35d..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/SaveOperation.java +++ /dev/null @@ -1,54 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import java.util.UUID; - -import io.github.cottonmc.cotton.gui.widget.WTextField; -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.model.PlayerPosition; -import me.bionicbeanie.mods.model.PlayerRawPosition; -import me.bionicbeanie.mods.model.PositionMetadata; - -public class SaveOperation extends ViewOperationBase { - - private PlayerRawPosition rawPosition; - private WTextField location, notes; - private WTextField world; - private PlayerPosition existingPosition; - - public SaveOperation(IFileStore fileStore, IGui gui, PlayerRawPosition rawPosition, WTextField world, - WTextField location, WTextField notes, PlayerPosition existingPosition) { - super(fileStore, gui); - this.rawPosition = rawPosition; - this.existingPosition = existingPosition; - this.world = world; - this.location = location; - this.notes = notes; - } - - @Override - protected void executeOperation(IFileStore fileStore, IGui gui) throws Exception { - - String id = existingPosition == null ? UUID.randomUUID().toString() : existingPosition.getId(); - PositionMetadata metadata = CreateMetadata(existingPosition); - - PlayerPosition position = new PlayerPosition(id, rawPosition, this.location.getText(), metadata); - - fileStore.save(position); - fileStore.setDefaultWorld(position.getPositionMetadata().getWorldName()); - gui.showListView(); - gui.setDefaultViewState(null); - } - - private PositionMetadata CreateMetadata(PlayerPosition existingPosition) { - if(existingPosition == null) { - return new PositionMetadata(this.world.getText(), this.notes.getText()); - } - - existingPosition.getPositionMetadata().setWorldName(this.world.getText()); - existingPosition.getPositionMetadata().setNotes(this.notes.getText()); - existingPosition.getPositionMetadata().updateLastModified(); - - return existingPosition.getPositionMetadata(); - } -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/ShowDetailOperation.java b/src/main/java/me/bionicbeanie/mods/gui/view/ShowDetailOperation.java deleted file mode 100644 index eb62b8e..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/ShowDetailOperation.java +++ /dev/null @@ -1,19 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.model.PlayerPosition; - -public class ShowDetailOperation extends ViewPositionOperationBase { - - public ShowDetailOperation(IFileStore fileStore, IGui gui, PlayerPosition position) { - super(fileStore, gui, position); - } - - @Override - protected void executeOperation(IFileStore fileStore, IGui gui, PlayerPosition position) throws Exception { - gui.setDefaultViewState(position); - gui.showDefaultView(); - } - -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java b/src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java deleted file mode 100644 index 89542fd..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java +++ /dev/null @@ -1,41 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import io.github.cottonmc.cotton.gui.widget.WWidget; -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IRootGridPanel; -import me.bionicbeanie.mods.api.IViewHandler; -import net.minecraft.client.MinecraftClient; - -public abstract class ViewHandlerBase implements IViewHandler { - - protected IFileStore fileStore; - protected IGui gui; - protected MinecraftClient client; - protected WWidget panel; - private T state; - - protected ViewHandlerBase(IFileStore fileStore, IGui gui, MinecraftClient client) { - this.fileStore = fileStore; - this.gui = gui; - this.client = client; - } - - protected abstract void placeWidgets(IRootGridPanel rootPanel, T state); - - @Override - public void clearState() { - state = null; - } - - @Override - public void setState(T state) { - this.state = state; - } - - @Override - public void placeWidgets(IRootGridPanel rootPanel) { - placeWidgets(rootPanel, state); - } - -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/ViewOperationBase.java b/src/main/java/me/bionicbeanie/mods/gui/view/ViewOperationBase.java deleted file mode 100644 index 676645e..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/ViewOperationBase.java +++ /dev/null @@ -1,26 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; - -public abstract class ViewOperationBase implements Runnable { - - private IFileStore fileStore; - private IGui gui; - - public ViewOperationBase(IFileStore fileStore, IGui gui) { - this.fileStore = fileStore; - this.gui = gui; - } - - @Override - public void run() { - try { - executeOperation(fileStore, gui); - } catch (Exception e) { - e.printStackTrace(); - } - } - - protected abstract void executeOperation(IFileStore fileStore, IGui gui) throws Exception; -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/ViewPositionOperationBase.java b/src/main/java/me/bionicbeanie/mods/gui/view/ViewPositionOperationBase.java deleted file mode 100644 index 23c64d5..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/view/ViewPositionOperationBase.java +++ /dev/null @@ -1,22 +0,0 @@ -package me.bionicbeanie.mods.gui.view; - -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.model.PlayerPosition; - -public abstract class ViewPositionOperationBase extends ViewOperationBase{ - - private PlayerPosition position; - - public ViewPositionOperationBase(IFileStore fileStore, IGui gui, PlayerPosition position) { - super(fileStore, gui); - this.position = position; - } - - @Override - protected void executeOperation(IFileStore fileStore, IGui gui) throws Exception { - executeOperation(fileStore, gui, position); - } - - protected abstract void executeOperation(IFileStore fileStore, IGui gui, PlayerPosition position) throws Exception; -} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/IFileStore.java b/src/main/java/me/bionicbeanie/mods/savecoords/IFileStore.java new file mode 100644 index 0000000..5266416 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/IFileStore.java @@ -0,0 +1,18 @@ +package me.bionicbeanie.mods.savecoords; + +import java.io.IOException; +import java.util.List; + +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; + +public interface IFileStore { + String getDefaultWorld() throws IOException; + + void setDefaultWorld(String defaultWorldName) throws IOException; + + void save(PlayerPosition position) throws IOException; + + void delete(String id) throws IOException; + + List list() throws IOException; +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/IPlayerLocator.java b/src/main/java/me/bionicbeanie/mods/savecoords/IPlayerLocator.java new file mode 100644 index 0000000..31f8198 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/IPlayerLocator.java @@ -0,0 +1,7 @@ +package me.bionicbeanie.mods.savecoords; + +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; + +public interface IPlayerLocator { + PlayerRawPosition locate(); +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/SaveCoordinatesClient.java b/src/main/java/me/bionicbeanie/mods/savecoords/SaveCoordinatesClient.java new file mode 100644 index 0000000..a4a93d4 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/SaveCoordinatesClient.java @@ -0,0 +1,35 @@ +package me.bionicbeanie.mods.savecoords; + +import org.lwjgl.glfw.GLFW; + +import me.bionicbeanie.mods.savecoords.gui.impl.ModGui; +import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.options.KeyBinding; +import net.minecraft.client.util.InputUtil; + +public class SaveCoordinatesClient implements ClientModInitializer { + + @Override + public void onInitializeClient() { + + KeyBinding keyBinding = registerKeyBinding(); + + ClientTickEvents.END_CLIENT_TICK.register(client -> { + while (keyBinding.wasPressed()) { + ModGui.start(client); + } + }); + } + + private KeyBinding registerKeyBinding() { + String translationKey = "key.savecoords.coords"; + String category = "category.savecoords.generic"; + int keyBind = GLFW.GLFW_KEY_H; + + KeyBinding keyBinding = new KeyBinding(translationKey, InputUtil.Type.KEYSYM, keyBind, category); + + return KeyBindingHelper.registerKeyBinding(keyBinding); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/IGuiController.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IGuiController.java new file mode 100644 index 0000000..01a3cf0 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IGuiController.java @@ -0,0 +1,9 @@ +package me.bionicbeanie.mods.savecoords.gui; + +import net.minecraft.client.gui.screen.Screen; + +public interface IGuiController { + void openScreen(Screen screen); + + void closeScreen(); +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/IRootPanel.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IRootPanel.java new file mode 100644 index 0000000..943a114 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IRootPanel.java @@ -0,0 +1,11 @@ +package me.bionicbeanie.mods.savecoords.gui; + +import io.github.cottonmc.cotton.gui.widget.WWidget; + +public interface IRootPanel { + void add(WWidget widget, int x, int y, int w, int h); + + void validate(); + + void reset(); +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/IViewHandler.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IViewHandler.java new file mode 100644 index 0000000..971e928 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/IViewHandler.java @@ -0,0 +1,9 @@ +package me.bionicbeanie.mods.savecoords.gui; + +import net.minecraft.client.gui.screen.Screen; + +public interface IViewHandler { + Screen createView(T state); + + T getState(); +} diff --git a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesScreen.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/SaveCoordinatesScreen.java similarity index 86% rename from src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesScreen.java rename to src/main/java/me/bionicbeanie/mods/savecoords/gui/SaveCoordinatesScreen.java index d009cd3..fd3f457 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesScreen.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/SaveCoordinatesScreen.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.gui; +package me.bionicbeanie.mods.savecoords.gui; import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.client.CottonClientScreen; @@ -8,5 +8,4 @@ public class SaveCoordinatesScreen extends CottonClientScreen { public SaveCoordinatesScreen(GuiDescription description) { super(description); } - } diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DefaultViewHandler.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DefaultViewHandler.java new file mode 100644 index 0000000..484edc6 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DefaultViewHandler.java @@ -0,0 +1,193 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.UUID; +import java.util.function.Supplier; + +import io.github.cottonmc.cotton.gui.widget.WButton; +import io.github.cottonmc.cotton.gui.widget.WLabel; +import io.github.cottonmc.cotton.gui.widget.WText; +import io.github.cottonmc.cotton.gui.widget.WTextField; +import io.github.cottonmc.cotton.gui.widget.WWidget; +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.IPlayerLocator; +import me.bionicbeanie.mods.savecoords.gui.IRootPanel; +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; +import me.bionicbeanie.mods.savecoords.model.PositionMetadata; +import me.bionicbeanie.mods.savecoords.util.ResourceUtils; +import net.minecraft.text.LiteralText; + +class DefaultViewHandler extends ViewHandlerBase { + + private IPlayerLocator locator; + private IFileStore fileStore; + private WButton saveButton; + private WButton listButton; + private WButton pingButton; + private WButton closeButton; + private WButton configButton; + + public DefaultViewHandler(IFileStore fileStore, IPlayerLocator locator) { + this.fileStore = fileStore; + this.locator = locator; + + this.listButton = CreateButton("LIST"); + this.saveButton = CreateButton("SAVE"); + this.pingButton = CreatePingButton(); + this.closeButton = CreateButton("CLOSE"); + this.configButton = CreateButton("CONF"); + } + + @Override + public Supplier placeWidgets(IRootPanel root, PlayerPosition existingPosition) { + + PlayerRawPosition rawPosition = existingPosition == null ? locator.locate() : existingPosition; + + WWidget xLabel = CreateLabelForCoorindate("X"); + WWidget yLabel = CreateLabelForCoorindate("Y"); + WWidget zLabel = CreateLabelForCoorindate("Z"); + + WWidget xText = CreateWidgetForCoordinate(rawPosition.getX()); + WWidget yText = CreateWidgetForCoordinate(rawPosition.getY()); + WWidget zText = CreateWidgetForCoordinate(rawPosition.getZ()); + + root.add(xLabel, 2, 1, 2, 1); + root.add(yLabel, 2, 2, 2, 1); + root.add(zLabel, 2, 3, 2, 1); + + root.add(xText, 3, 1, 2, 1); + root.add(yText, 3, 2, 2, 1); + root.add(zText, 3, 3, 2, 1); + + WWidget icon = ResourceUtils.CreateWorldIcon(rawPosition.getWorldDimension()); + root.add(icon, 8, 1, 2, 2); + + String defaultWorldName = getDefaultWorldName(existingPosition); + WTextField world = CreateWorldField(defaultWorldName); + root.add(world, 0, 4, 4, 1); + + WTextField location = CreateLocationField(existingPosition); + root.add(location, 5, 4, 7, 1); + + WTextField notes = CreateNotesField(existingPosition); + root.add(notes, 0, 6, 12, 1); + + root.add(saveButton, 13, 6, 2, 1); + root.add(listButton, 13, 9, 2, 1); + root.add(pingButton, 13, 1, 1, 1); + root.add(closeButton, 0, 9, 2, 1); + root.add(configButton, 10, 9, 2, 1); + + return createPlayerPositionSupplier(existingPosition, rawPosition, world, location, notes); + } + + public void onSave(Runnable runnable) { + this.saveButton.setOnClick(runnable); + } + + public void onClose(Runnable runnable) { + this.closeButton.setOnClick(runnable); + } + + public void onList(Runnable runnable) { + this.listButton.setOnClick(runnable); + } + + public void onPing(Runnable runnable) { + this.pingButton.setOnClick(runnable); + } + + public void onConfig(Runnable runnable) { + this.configButton.setOnClick(runnable); + } + + private Supplier createPlayerPositionSupplier(PlayerPosition existingPosition, + PlayerRawPosition rawPosition, WTextField world, WTextField location, WTextField notes) { + + return () -> { + String id = CreateId(existingPosition); + PositionMetadata metadata = CreateMetadata(existingPosition, world, notes); + + return new PlayerPosition(id, rawPosition, location.getText(), metadata); + }; + } + + private String CreateId(PlayerPosition existingPosition) { + return existingPosition == null ? UUID.randomUUID().toString() : existingPosition.getId(); + } + + private PositionMetadata CreateMetadata(PlayerPosition existingPosition, WTextField world, WTextField notes) { + if (existingPosition == null) { + return new PositionMetadata(world.getText(), notes.getText()); + } + + existingPosition.getPositionMetadata().setWorldName(world.getText()); + existingPosition.getPositionMetadata().setNotes(notes.getText()); + existingPosition.getPositionMetadata().updateLastModified(); + + return existingPosition.getPositionMetadata(); + } + + private String getDefaultWorldName(PlayerPosition existingPosition) { + if (existingPosition != null && existingPosition.getPositionMetadata().getWorldName() != null) { + return existingPosition.getPositionMetadata().getWorldName(); + } + + try { + return fileStore.getDefaultWorld(); + } catch (Exception e) { + e.printStackTrace(); + } + + return ""; + } + + private WWidget CreateLabelForCoorindate(String label) { + return new WLabel(label, 0xb80000); + } + + private WWidget CreateWidgetForCoordinate(long l) { + return new WText(new LiteralText(String.valueOf(l)), 0x3939ac); + } + + private WTextField CreateLocationField(PlayerPosition existingPosition) { + WTextField location = new WTextField(new LiteralText("location name")); + + if (existingPosition != null) { + location.setText(existingPosition.getLocationName()); + } + + location.setMaxLength(20); + + return location; + } + + private WTextField CreateNotesField(PlayerPosition existingPosition) { + WTextField notes = new WTextField(new LiteralText("additional notes")); + + if (existingPosition != null && existingPosition.getPositionMetadata() != null) { + notes.setText(existingPosition.getPositionMetadata().getNotes()); + } + + return notes; + } + + private WTextField CreateWorldField(String defaultWorld) { + WTextField world = new WTextField(new LiteralText("world name")); + world.setMaxLength(7); + world.setText(defaultWorld); + return world; + } + + private WButton CreatePingButton() { + WButton button = CreateButton(""); + button.setIcon(ResourceUtils.CreatePingIcon()); + return button; + } + + private WButton CreateButton(String name) { + WButton button = new WButton(new LiteralText(name)); + + return button; + } +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DeleteOperation.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DeleteOperation.java new file mode 100644 index 0000000..4f6d5df --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/DeleteOperation.java @@ -0,0 +1,17 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.function.Supplier; + +import me.bionicbeanie.mods.savecoords.IFileStore; + +class DeleteOperation extends ViewOperationBase { + + public DeleteOperation(IFileStore fileStore, Supplier stateSupplier) { + super(fileStore, stateSupplier); + } + + @Override + protected void executeOperation(IFileStore fileStore, String positionId) throws Exception { + fileStore.delete(positionId); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/gui/ScreenController.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/GuiController.java similarity index 63% rename from src/main/java/me/bionicbeanie/mods/gui/ScreenController.java rename to src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/GuiController.java index 3997293..f43bf6d 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/ScreenController.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/GuiController.java @@ -1,14 +1,14 @@ -package me.bionicbeanie.mods.gui; +package me.bionicbeanie.mods.savecoords.gui.impl; -import me.bionicbeanie.mods.api.IScreenController; +import me.bionicbeanie.mods.savecoords.gui.IGuiController; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; -public class ScreenController implements IScreenController { +class GuiController implements IGuiController { private MinecraftClient client; - public ScreenController(MinecraftClient client) { + public GuiController(MinecraftClient client) { this.client = client; } diff --git a/src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ListViewHandler.java similarity index 51% rename from src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java rename to src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ListViewHandler.java index 36d8887..b550e77 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ListViewHandler.java @@ -1,61 +1,80 @@ -package me.bionicbeanie.mods.gui.view; +package me.bionicbeanie.mods.savecoords.gui.impl; import java.io.IOException; import java.util.Collections; import java.util.List; import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Supplier; import io.github.cottonmc.cotton.gui.widget.WButton; import io.github.cottonmc.cotton.gui.widget.WLabel; import io.github.cottonmc.cotton.gui.widget.WListPanel; import io.github.cottonmc.cotton.gui.widget.WPlainPanel; import io.github.cottonmc.cotton.gui.widget.WSprite; -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IRootGridPanel; -import me.bionicbeanie.mods.model.PlayerPosition; -import me.bionicbeanie.mods.util.DimensionSpriteUtil; -import net.minecraft.client.MinecraftClient; +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.gui.IRootPanel; +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; +import me.bionicbeanie.mods.savecoords.util.ResourceUtils; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; -public class ListViewHandler extends ViewHandlerBase { +class ListViewHandler extends ViewHandlerBase { - public ListViewHandler(IFileStore fileStore, IGui gui, MinecraftClient client) { - super(fileStore, gui, client); + private IFileStore fileStore; + private WButton backButton; + private Consumer onDelete; + private Consumer onEdit; + private Consumer onPing; + + public ListViewHandler(IFileStore fileStore, Consumer onDelete, Consumer onEdit, + Consumer onPing) { + this.fileStore = fileStore; + this.backButton = createBackButton(); + this.onDelete = onDelete; + this.onEdit = onEdit; + this.onPing = onPing; } @Override - public void placeWidgets(IRootGridPanel root, Void nullState) { + public Supplier placeWidgets(IRootPanel root, Void nullState) { List positions = getPositions(fileStore); WListPanel listPanel = createListPane(positions); - WButton backButton = createBackButton(); - root.add(listPanel, 0, 0, 15, 9); root.add(backButton, 0, 9, 2, 1); + + return () -> (Void) null; + } + + public void onBack(Runnable runnable) { + this.backButton.setOnClick(runnable); } private WButton createBackButton() { - WButton backButton = new WButton(new LiteralText("BACK")); - backButton.setOnClick(gui::showDefaultView); - return backButton; + return new WButton(new LiteralText("BACK")); } private WListPanel createListPane(List positions) { - BiConsumer configurator = (position, panel) -> { - panel.setPosition(position, fileStore, gui); - }; - - WListPanel panel = new WListPanel<>(positions, - CoordinatesListItemPanel::new, configurator); + BiConsumer configurator = (pos, p) -> p.setPosition(pos, fileStore); + WListPanel panel = CreateListPanel(positions, configurator); panel.setListItemHeight(2 * 18); return panel; } + private WListPanel CreateListPanel(List positions, + BiConsumer config) { + return new WListPanel<>(positions, this::createListPanel, config); + } + + private CoordinatesListItemPanel createListPanel() { + return new CoordinatesListItemPanel(onDelete, onEdit, onPing); + } + private List getPositions(IFileStore fileStore) { try { List positions = fileStore.list(); @@ -68,7 +87,7 @@ public class ListViewHandler extends ViewHandlerBase { } } - public static class CoordinatesListItemPanel extends WPlainPanel { + private static class CoordinatesListItemPanel extends WPlainPanel { private WLabel coordinates; private WLabel location; @@ -77,22 +96,35 @@ public class ListViewHandler extends ViewHandlerBase { private WButton pingButton; private WButton detailButton; private WLabel world; + private Consumer onPing; + private Consumer onEdit; + private Consumer onDelete; + + CoordinatesListItemPanel(Consumer onDelete, Consumer onEdit, + Consumer onPing) { + + this.onDelete = onDelete; + this.onEdit = onEdit; + this.onPing = onPing; - public CoordinatesListItemPanel() { this.coordinates = new WLabel("Foo"); this.location = new WLabel("Foo"); this.world = new WLabel("Foo"); this.icon = new WSprite(new Identifier("minecraft:textures/item/ender_eye.png")); this.deleteButton = new WButton(new LiteralText("x")); - this.pingButton = new WButton(new LiteralText("!")); - this.detailButton = new WButton(new LiteralText("+")); + this.pingButton = new WButton(new LiteralText("")); + this.detailButton = new WButton(new LiteralText("")); + + this.deleteButton.setIcon(ResourceUtils.CreateCloseIcon()); + this.pingButton.setIcon(ResourceUtils.CreatePingIcon()); + this.detailButton.setIcon(ResourceUtils.CreateDetailsIcon()); this.add(icon, 0, 0, 1 * 9, 1 * 9); this.add(world, 1 * 18, 0, 3 * 18, 1 * 18); this.add(location, 4 * 18, 0, 4 * 18, 1 * 18); - this.add(coordinates, 1 * 18, 1 * 18, 9 * 18, 1 * 18); + this.add(coordinates, 3 * 18, 1 * 18, 9 * 18, 1 * 18); this.add(deleteButton, 13 * 18, 0, 1 * 18, 1 * 18); - this.add(pingButton, 12 * 18, 0, 1 * 18, 1 * 18); + this.add(pingButton, 1 * 18, 1 * 18, 1 * 18, 1 * 18); this.add(detailButton, 11 * 18, 0, 1 * 18, 1 * 18); this.icon.setSize(1 * 15, 1 * 15); @@ -106,17 +138,17 @@ public class ListViewHandler extends ViewHandlerBase { this.setSize(15 * 18, 2 * 18); } - public void setPosition(PlayerPosition position, IFileStore fileStore, IGui gui) { - this.icon.setImage(DimensionSpriteUtil.CreateWorldIconIdentifier(position.getWorldDimension())); + void setPosition(PlayerPosition position, IFileStore fileStore) { + this.icon.setImage(ResourceUtils.CreateWorldIconIdentifier(position.getWorldDimension())); this.location.setText(new LiteralText(position.getLocationName())); this.location.setColor(0x3939ac); this.world.setText(new LiteralText("[" + position.getPositionMetadata().getWorldName() + "]")); this.world.setColor(0xb80000); this.coordinates .setText(new LiteralText(position.getX() + ", " + position.getY() + ", " + position.getZ())); - this.deleteButton.setOnClick(new DeleteOperation(fileStore, gui, position)); - this.pingButton.setOnClick(new PingOperation(fileStore, gui, position)); - this.detailButton.setOnClick(new ShowDetailOperation(fileStore, gui, position)); + this.deleteButton.setOnClick(() -> onDelete.accept(position)); + this.pingButton.setOnClick(() -> onPing.accept(position)); + this.detailButton.setOnClick(() -> onEdit.accept(position)); } } } diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ModGui.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ModGui.java new file mode 100644 index 0000000..57c2e9b --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ModGui.java @@ -0,0 +1,18 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.IPlayerLocator; +import me.bionicbeanie.mods.savecoords.gui.IGuiController; +import me.bionicbeanie.mods.savecoords.impl.Factory; +import net.minecraft.client.MinecraftClient; + +public class ModGui { + + public static void start(MinecraftClient client) { + IGuiController controller = new GuiController(client); + IPlayerLocator locator = Factory.CreatePlayerLocator(client); + IFileStore fileStore = Factory.createFileStore(client.runDirectory.getAbsolutePath()); + + new SaveCoordinatesGui(fileStore, locator, controller); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/PingOperation.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/PingOperation.java new file mode 100644 index 0000000..bb614fb --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/PingOperation.java @@ -0,0 +1,20 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.function.Supplier; + +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; +import net.minecraft.client.MinecraftClient; + +class PingOperation extends ViewOperationBase{ + + public PingOperation(IFileStore fileStore, Supplier stateSupplier) { + super(fileStore, stateSupplier); + } + + @SuppressWarnings("resource") + @Override + protected void executeOperation(IFileStore fileStore, PlayerRawPosition position) throws Exception { + MinecraftClient.getInstance().player.sendChatMessage(position.toString()); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/gui/RootGridPanel.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/RootGridPanel.java similarity index 57% rename from src/main/java/me/bionicbeanie/mods/gui/RootGridPanel.java rename to src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/RootGridPanel.java index fcf378b..8b752a8 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/RootGridPanel.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/RootGridPanel.java @@ -1,18 +1,22 @@ -package me.bionicbeanie.mods.gui; +package me.bionicbeanie.mods.savecoords.gui.impl; import java.util.ArrayList; import java.util.List; +import io.github.cottonmc.cotton.gui.GuiDescription; import io.github.cottonmc.cotton.gui.widget.WGridPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; -import me.bionicbeanie.mods.api.IRootGridPanel; +import me.bionicbeanie.mods.savecoords.gui.IRootPanel; -public class RootGridPanel extends WGridPanel implements IRootGridPanel{ +class RootGridPanel extends WGridPanel implements IRootPanel{ private List children; + private GuiDescription guiDescription; - public RootGridPanel() { + public RootGridPanel(GuiDescription guiDescription) { this(18); + + this.guiDescription = guiDescription; } public RootGridPanel(int gridSize){ @@ -32,4 +36,8 @@ public class RootGridPanel extends WGridPanel implements IRootGridPanel{ children.clear(); } + @Override + public void validate() { + validate(guiDescription); + } } diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveCoordinatesGui.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveCoordinatesGui.java new file mode 100644 index 0000000..1f23ed2 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveCoordinatesGui.java @@ -0,0 +1,94 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.IPlayerLocator; +import me.bionicbeanie.mods.savecoords.gui.IGuiController; +import me.bionicbeanie.mods.savecoords.gui.IViewHandler; +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; + +public class SaveCoordinatesGui { + + private IGuiController screenController; + private IFileStore fileStore; + private IViewHandler defaultHandler; + private IViewHandler listHandler; + private IPlayerLocator locator; + + SaveCoordinatesGui(IFileStore fileStore, IPlayerLocator locator, IGuiController screenController) { + this.screenController = screenController; + this.fileStore = fileStore; + this.locator = locator; + + this.defaultHandler = CreateDefaultViewHandler(); + this.listHandler = CreateListViewHandler(); + + showDefaultView(null); + } + + private IViewHandler CreateDefaultViewHandler() { + DefaultViewHandler handler = new DefaultViewHandler(fileStore, locator); + + handler.onClose(screenController::closeScreen); + handler.onSave(this::onSavePosition); + handler.onList(this::showListView); + handler.onPing(new PingOperation(fileStore, locator::locate)); + + return handler; + } + + private IViewHandler CreateListViewHandler() { + ListViewHandler handler = new ListViewHandler(fileStore, this::onDeletePosition, this::onEditPosition, + this::onPingPosition); + + handler.onBack(() -> showDefaultView(null)); + + return handler; + } + + private void onSavePosition() { + new SaveOperation(fileStore, defaultHandler::getState).run(); + showListView(); + } + + private void onDeletePosition(PlayerPosition position) { + new DeleteOperation(fileStore, () -> position.getId()).run(); + showListView(); + } + + private void onEditPosition(PlayerPosition position) { + showDefaultView(position); + } + + private void onPingPosition(PlayerRawPosition position) { + new PingOperation(fileStore, () -> position).run(); + } + + private void showDefaultView(PlayerPosition position) { + screenController.openScreen(this.defaultHandler.createView(position)); + } + + private void showListView() { + screenController.openScreen(this.listHandler.createView(null)); + } + +// public void showConfigView() { +// +// ConfigBuilder builder = ConfigBuilder.create() +// .setParentScreen(this.screen) +// .setTitle(new LiteralText("Save Coordinates config")); +// +// ConfigCategory general = builder.getOrCreateCategory(new LiteralText("Generic")); +// ConfigEntryBuilder entryBuilder = builder.entryBuilder(); +// +// general.addEntry(entryBuilder.startKeyCodeField(new LiteralText("Default Keybind"), +// InputUtil.Type.KEYSYM.createFromCode(GLFW.GLFW_KEY_H)) +// .setDefaultValue(InputUtil.Type.KEYSYM.createFromCode(GLFW.GLFW_KEY_H)) +// .setTooltip(new LiteralText("Keybind to open Save Coordinates menu")) +// .setSaveConsumer(newValue -> newValue = newValue) +// .build()); +// +// screenController.openScreen(builder.build()); +// } + +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveOperation.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveOperation.java new file mode 100644 index 0000000..acbb605 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/SaveOperation.java @@ -0,0 +1,19 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.function.Supplier; + +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; + +class SaveOperation extends ViewOperationBase { + + public SaveOperation(IFileStore fileStore, Supplier stateSupplier) { + super(fileStore, stateSupplier); + } + + @Override + protected void executeOperation(IFileStore fileStore, PlayerPosition position) throws Exception { + fileStore.save(position); + fileStore.setDefaultWorld(position.getPositionMetadata().getWorldName()); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewHandlerBase.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewHandlerBase.java new file mode 100644 index 0000000..0d1adb9 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewHandlerBase.java @@ -0,0 +1,42 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.function.Supplier; + +import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; +import me.bionicbeanie.mods.savecoords.gui.IRootPanel; +import me.bionicbeanie.mods.savecoords.gui.IViewHandler; +import me.bionicbeanie.mods.savecoords.gui.SaveCoordinatesScreen; +import net.minecraft.client.gui.screen.Screen; + +abstract class ViewHandlerBase extends LightweightGuiDescription implements IViewHandler { + + private IRootPanel rootGridPanel; + private Supplier stateSupplier; + + protected ViewHandlerBase() { + this.rootGridPanel = createRootPanel(); + } + + protected abstract Supplier placeWidgets(IRootPanel rootGridPanel, T state); + + @Override + public Screen createView(T state) { + rootGridPanel.reset(); + this.stateSupplier = placeWidgets(rootGridPanel, state); + rootGridPanel.validate(); + return new SaveCoordinatesScreen(this); + } + + @Override + public T getState() { + return stateSupplier.get(); + } + + private IRootPanel createRootPanel() { + RootGridPanel panel = new RootGridPanel(this); + panel.setSize(15 * 18, 10 * 18); + + setRootPanel(panel); + return panel; + } +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewOperationBase.java b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewOperationBase.java new file mode 100644 index 0000000..c2ce5c3 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/gui/impl/ViewOperationBase.java @@ -0,0 +1,27 @@ +package me.bionicbeanie.mods.savecoords.gui.impl; + +import java.util.function.Supplier; + +import me.bionicbeanie.mods.savecoords.IFileStore; + +abstract class ViewOperationBase implements Runnable { + + private IFileStore fileStore; + private Supplier stateSupplier; + + public ViewOperationBase(IFileStore fileStore,Supplier stateSupplier) { + this.fileStore = fileStore; + this.stateSupplier = stateSupplier; + } + + @Override + public void run() { + try { + executeOperation(fileStore, stateSupplier.get()); + } catch (Exception e) { + e.printStackTrace(); + } + } + + protected abstract void executeOperation(IFileStore fileStore, T state) throws Exception; +} diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/impl/Factory.java b/src/main/java/me/bionicbeanie/mods/savecoords/impl/Factory.java new file mode 100644 index 0000000..ca53b93 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/impl/Factory.java @@ -0,0 +1,16 @@ +package me.bionicbeanie.mods.savecoords.impl; + +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.IPlayerLocator; +import net.minecraft.client.MinecraftClient; + +public class Factory { + + public static IFileStore createFileStore(String baseDirectory) { + return new FileStore(baseDirectory); + } + + public static IPlayerLocator CreatePlayerLocator(MinecraftClient client) { + return new PlayerLocator(client); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/impl/FileStore.java b/src/main/java/me/bionicbeanie/mods/savecoords/impl/FileStore.java similarity index 92% rename from src/main/java/me/bionicbeanie/mods/impl/FileStore.java rename to src/main/java/me/bionicbeanie/mods/savecoords/impl/FileStore.java index aeb6eee..410d645 100644 --- a/src/main/java/me/bionicbeanie/mods/impl/FileStore.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/impl/FileStore.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.impl; +package me.bionicbeanie.mods.savecoords.impl; import java.io.IOException; import java.nio.file.FileAlreadyExistsException; @@ -14,11 +14,11 @@ import org.apache.commons.lang3.StringUtils; import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import me.bionicbeanie.mods.api.IFileStore; -import me.bionicbeanie.mods.model.ModData; -import me.bionicbeanie.mods.model.PlayerPosition; +import me.bionicbeanie.mods.savecoords.IFileStore; +import me.bionicbeanie.mods.savecoords.model.ModData; +import me.bionicbeanie.mods.savecoords.model.PlayerPosition; -public class FileStore implements IFileStore { +class FileStore implements IFileStore { private static String DEFAULT_DIR = "saveCoordinates"; private static String DEFAULT_FILE = "coordiates.json"; diff --git a/src/main/java/me/bionicbeanie/mods/impl/PlayerLocator.java b/src/main/java/me/bionicbeanie/mods/savecoords/impl/PlayerLocator.java similarity index 52% rename from src/main/java/me/bionicbeanie/mods/impl/PlayerLocator.java rename to src/main/java/me/bionicbeanie/mods/savecoords/impl/PlayerLocator.java index c95d5fe..9e99500 100644 --- a/src/main/java/me/bionicbeanie/mods/impl/PlayerLocator.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/impl/PlayerLocator.java @@ -1,13 +1,19 @@ -package me.bionicbeanie.mods.impl; +package me.bionicbeanie.mods.savecoords.impl; -import me.bionicbeanie.mods.api.IPlayerLocator; -import me.bionicbeanie.mods.model.PlayerRawPosition; +import me.bionicbeanie.mods.savecoords.IPlayerLocator; +import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; import net.minecraft.client.MinecraftClient; import net.minecraft.util.math.Vec3d; -public class PlayerLocator implements IPlayerLocator { +class PlayerLocator implements IPlayerLocator { - public PlayerRawPosition locate(MinecraftClient client) { + private MinecraftClient client; + + public PlayerLocator(MinecraftClient client) { + this.client = client; + } + + public PlayerRawPosition locate() { Vec3d pos = client.player.getPos(); long x = Math.round(pos.x); long y = Math.round(pos.y); diff --git a/src/main/java/me/bionicbeanie/mods/model/ModData.java b/src/main/java/me/bionicbeanie/mods/savecoords/model/ModData.java similarity index 69% rename from src/main/java/me/bionicbeanie/mods/model/ModData.java rename to src/main/java/me/bionicbeanie/mods/savecoords/model/ModData.java index 144c016..a304109 100644 --- a/src/main/java/me/bionicbeanie/mods/model/ModData.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/model/ModData.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.model; +package me.bionicbeanie.mods.savecoords.model; public class ModData { diff --git a/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java b/src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerPosition.java similarity index 95% rename from src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java rename to src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerPosition.java index 0b98386..9667522 100644 --- a/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerPosition.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.model; +package me.bionicbeanie.mods.savecoords.model; public class PlayerPosition extends PlayerRawPosition { diff --git a/src/main/java/me/bionicbeanie/mods/model/PlayerRawPosition.java b/src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerRawPosition.java similarity index 93% rename from src/main/java/me/bionicbeanie/mods/model/PlayerRawPosition.java rename to src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerRawPosition.java index 5b896d9..bb777f7 100644 --- a/src/main/java/me/bionicbeanie/mods/model/PlayerRawPosition.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/model/PlayerRawPosition.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.model; +package me.bionicbeanie.mods.savecoords.model; public class PlayerRawPosition { private long x, y, z; diff --git a/src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java b/src/main/java/me/bionicbeanie/mods/savecoords/model/PositionMetadata.java similarity index 95% rename from src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java rename to src/main/java/me/bionicbeanie/mods/savecoords/model/PositionMetadata.java index 110b3c4..e9eec81 100644 --- a/src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java +++ b/src/main/java/me/bionicbeanie/mods/savecoords/model/PositionMetadata.java @@ -1,4 +1,4 @@ -package me.bionicbeanie.mods.model; +package me.bionicbeanie.mods.savecoords.model; import java.time.LocalDateTime; diff --git a/src/main/java/me/bionicbeanie/mods/savecoords/util/ResourceUtils.java b/src/main/java/me/bionicbeanie/mods/savecoords/util/ResourceUtils.java new file mode 100644 index 0000000..9e63116 --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/savecoords/util/ResourceUtils.java @@ -0,0 +1,45 @@ +package me.bionicbeanie.mods.savecoords.util; + +import io.github.cottonmc.cotton.gui.widget.WSprite; +import io.github.cottonmc.cotton.gui.widget.WWidget; +import io.github.cottonmc.cotton.gui.widget.icon.Icon; +import io.github.cottonmc.cotton.gui.widget.icon.TextureIcon; +import net.minecraft.util.Identifier; + +public class ResourceUtils { + + public static WWidget CreateWorldIcon(String dimension) { + return new WSprite(CreateWorldIconIdentifier(dimension)); + } + + public static Identifier CreateWorldIconIdentifier(String dimension) { + + if (dimension == null) + return CreateIdentifier("nonExistent"); + if (dimension.contains("overworld")) + return CreateIdentifier("overworld"); + if (dimension.contains("nether")) + return CreateIdentifier("nether"); + if (dimension.contains("end")) + return CreateIdentifier("end"); + + return CreateIdentifier("nonExistent"); + + } + + public static Icon CreatePingIcon() { + return new TextureIcon(CreateIdentifier("ping")); + } + + public static Icon CreateCloseIcon() { + return new TextureIcon(CreateIdentifier("close")); + } + + public static Icon CreateDetailsIcon() { + return new TextureIcon(CreateIdentifier("more")); + } + + private static Identifier CreateIdentifier(String file) { + return new Identifier("savecoords", "textures/gui/" + file + ".png"); + } +} diff --git a/src/main/java/me/bionicbeanie/mods/util/DimensionSpriteUtil.java b/src/main/java/me/bionicbeanie/mods/util/DimensionSpriteUtil.java deleted file mode 100644 index 56c2138..0000000 --- a/src/main/java/me/bionicbeanie/mods/util/DimensionSpriteUtil.java +++ /dev/null @@ -1,27 +0,0 @@ -package me.bionicbeanie.mods.util; - -import io.github.cottonmc.cotton.gui.widget.WSprite; -import io.github.cottonmc.cotton.gui.widget.WWidget; -import net.minecraft.util.Identifier; - -public class DimensionSpriteUtil { - - public static WWidget CreateWorldIcon(String dimension) { - return new WSprite(CreateWorldIconIdentifier(dimension)); - } - - public static Identifier CreateWorldIconIdentifier(String dimension) { - String dimensionItem = "netherite_ingot"; - - if(dimension == null) { - dimensionItem = "barrier"; - } - else if (dimension.contains("overworld")) { - dimensionItem = "totem_of_undying"; - } else if (dimension.contains("end")) { - dimensionItem = "ender_eye"; - } - - return new Identifier("minecraft:textures/item/" + dimensionItem + ".png"); - } -} diff --git a/src/main/resources/assets/savecoords/textures/gui/close.png b/src/main/resources/assets/savecoords/textures/gui/close.png new file mode 100644 index 0000000..cfa3644 Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/close.png differ diff --git a/src/main/resources/assets/savecoords/textures/gui/end.png b/src/main/resources/assets/savecoords/textures/gui/end.png new file mode 100644 index 0000000..a718f6d Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/end.png differ diff --git a/src/main/resources/assets/savecoords/textures/gui/more.png b/src/main/resources/assets/savecoords/textures/gui/more.png new file mode 100644 index 0000000..4cb908f Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/more.png differ diff --git a/src/main/resources/assets/savecoords/textures/gui/nether.png b/src/main/resources/assets/savecoords/textures/gui/nether.png new file mode 100644 index 0000000..d46f1aa Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/nether.png differ diff --git a/src/main/resources/assets/savecoords/textures/gui/overworld.png b/src/main/resources/assets/savecoords/textures/gui/overworld.png new file mode 100644 index 0000000..c5e5da1 Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/overworld.png differ diff --git a/src/main/resources/assets/savecoords/textures/gui/ping.png b/src/main/resources/assets/savecoords/textures/gui/ping.png new file mode 100644 index 0000000..1511c5b Binary files /dev/null and b/src/main/resources/assets/savecoords/textures/gui/ping.png differ diff --git a/src/main/resources/data/savecoords/end.png b/src/main/resources/data/savecoords/end.png new file mode 100644 index 0000000..ffa304d Binary files /dev/null and b/src/main/resources/data/savecoords/end.png differ