From 450737e98dad2fc1269e8aa23af9c92d7593039a Mon Sep 17 00:00:00 2001 From: Surya Date: Wed, 26 May 2021 23:41:52 +0530 Subject: [PATCH] Add delete functionality --- build.gradle | 5 +++ .../mods/SaveCoordinatesClient.java | 10 ++--- .../me/bionicbeanie/mods/api/IFileStore.java | 4 +- .../java/me/bionicbeanie/mods/api/IGui.java | 2 +- .../{IGuiHandler.java => IViewHandler.java} | 2 +- .../mods/gui/CoordinatesPanel.java | 37 ------------------- .../mods/gui/SaveCoordinatesGui.java | 10 ++--- .../DefaultViewHandler.java} | 10 +++-- .../ListViewHandler.java} | 33 +++++++++++++---- .../ViewHandlerBase.java} | 8 ++-- .../me/bionicbeanie/mods/impl/FileStore.java | 28 ++++++++++---- .../mods/model/PlayerPosition.java | 16 +++++++- .../mods/model/PositionMetadata.java | 25 +++++++++++++ 13 files changed, 113 insertions(+), 77 deletions(-) rename src/main/java/me/bionicbeanie/mods/api/{IGuiHandler.java => IViewHandler.java} (73%) delete mode 100644 src/main/java/me/bionicbeanie/mods/gui/CoordinatesPanel.java rename src/main/java/me/bionicbeanie/mods/gui/{SaveGuiHandler.java => view/DefaultViewHandler.java} (86%) rename src/main/java/me/bionicbeanie/mods/gui/{ListGuiHandler.java => view/ListViewHandler.java} (68%) rename src/main/java/me/bionicbeanie/mods/gui/{GuiHandlerBase.java => view/ViewHandlerBase.java} (63%) create mode 100644 src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java diff --git a/build.gradle b/build.gradle index 00023ce..15b89b9 100644 --- a/build.gradle +++ b/build.gradle @@ -34,6 +34,11 @@ processResources { } } +test { + useJUnit() + maxHeapSize = '1G' +} + tasks.withType(JavaCompile).configureEach { // ensure that the encoding is set to UTF-8, no matter what the system default is // this fixes some edge cases with special characters not displaying correctly diff --git a/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java b/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java index 897ba3a..33d6703 100644 --- a/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java +++ b/src/main/java/me/bionicbeanie/mods/SaveCoordinatesClient.java @@ -4,13 +4,13 @@ import org.lwjgl.glfw.GLFW; import me.bionicbeanie.mods.api.IFileStore; import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IGuiHandler; +import me.bionicbeanie.mods.api.IViewHandler; import me.bionicbeanie.mods.api.IPlayerLocator; import me.bionicbeanie.mods.api.IScreenController; -import me.bionicbeanie.mods.gui.ListGuiHandler; import me.bionicbeanie.mods.gui.SaveCoordinatesGui; -import me.bionicbeanie.mods.gui.SaveGuiHandler; 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 net.fabricmc.api.ClientModInitializer; @@ -50,8 +50,8 @@ public class SaveCoordinatesClient implements ClientModInitializer { private IGui CreateModGui(MinecraftClient client) { IGui gui = new SaveCoordinatesGui(); IFileStore fileStore = new FileStore(client.runDirectory.getAbsolutePath()); - IGuiHandler saveHandler = new SaveGuiHandler(fileStore, locator, client, gui); - IGuiHandler listHandler = new ListGuiHandler(fileStore, gui, client); + IViewHandler saveHandler = new DefaultViewHandler(fileStore, locator, client, gui); + IViewHandler listHandler = new ListViewHandler(fileStore, gui, client); IScreenController screenController = new ScreenController(client); gui.init(saveHandler, listHandler, screenController); diff --git a/src/main/java/me/bionicbeanie/mods/api/IFileStore.java b/src/main/java/me/bionicbeanie/mods/api/IFileStore.java index c8b875a..49f752f 100644 --- a/src/main/java/me/bionicbeanie/mods/api/IFileStore.java +++ b/src/main/java/me/bionicbeanie/mods/api/IFileStore.java @@ -7,9 +7,9 @@ import me.bionicbeanie.mods.model.PlayerPosition; public interface IFileStore { - public void save(List positions) 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 index b7c35fc..1faf88c 100644 --- a/src/main/java/me/bionicbeanie/mods/api/IGui.java +++ b/src/main/java/me/bionicbeanie/mods/api/IGui.java @@ -2,7 +2,7 @@ package me.bionicbeanie.mods.api; public interface IGui { - public void init(IGuiHandler saveHandler, IGuiHandler listHandler, IScreenController screenController); + public void init(IViewHandler saveHandler, IViewHandler listHandler, IScreenController screenController); public void showDefaultView(); diff --git a/src/main/java/me/bionicbeanie/mods/api/IGuiHandler.java b/src/main/java/me/bionicbeanie/mods/api/IViewHandler.java similarity index 73% rename from src/main/java/me/bionicbeanie/mods/api/IGuiHandler.java rename to src/main/java/me/bionicbeanie/mods/api/IViewHandler.java index 570e025..2cb3a4f 100644 --- a/src/main/java/me/bionicbeanie/mods/api/IGuiHandler.java +++ b/src/main/java/me/bionicbeanie/mods/api/IViewHandler.java @@ -1,6 +1,6 @@ package me.bionicbeanie.mods.api; -public interface IGuiHandler { +public interface IViewHandler { void placeWidgets(IRootGridPanel rootPanel); diff --git a/src/main/java/me/bionicbeanie/mods/gui/CoordinatesPanel.java b/src/main/java/me/bionicbeanie/mods/gui/CoordinatesPanel.java deleted file mode 100644 index 6db157d..0000000 --- a/src/main/java/me/bionicbeanie/mods/gui/CoordinatesPanel.java +++ /dev/null @@ -1,37 +0,0 @@ -package me.bionicbeanie.mods.gui; - -import io.github.cottonmc.cotton.gui.widget.WLabel; -import io.github.cottonmc.cotton.gui.widget.WPlainPanel; -import io.github.cottonmc.cotton.gui.widget.WSprite; -import me.bionicbeanie.mods.model.PlayerPosition; -import me.bionicbeanie.mods.util.DimensionSpriteUtil; -import net.minecraft.text.LiteralText; -import net.minecraft.util.Identifier; - -public class CoordinatesPanel extends WPlainPanel { - - private WLabel coordinates; - private WLabel location; - private WSprite icon; - - public CoordinatesPanel() { - this.coordinates = new WLabel("Foo"); - this.location = new WLabel("Foo"); - this.icon = new WSprite(new Identifier("minecraft:textures/item/ender_eye.png")); - - this.add(icon, 0, 0, 1 * 18, 1 * 18); - this.add(coordinates, 1 * 18, 0, 2 * 18, 1 * 18); - this.add(location, 0, 1 * 18, 3 * 18, 1 * 18); - - this.icon.setSize(1 * 18, 1 * 18); - this.coordinates.setSize(2 * 18, 1 * 18); - this.location.setSize(3 * 18, 1 * 18); - this.setSize(3 * 18, 2 * 18); - } - - public void setPosition(PlayerPosition position) { - this.icon.setImage(DimensionSpriteUtil.CreateWorldIconIdentifier(position.getWorldDimension())); - this.location.setText(new LiteralText(position.getLocationName())); - this.coordinates.setText(new LiteralText(position.getX() + "," + position.getY() + "," + position.getZ())); - } -} diff --git a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java b/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java index 5312fab..4b1a9f1 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java +++ b/src/main/java/me/bionicbeanie/mods/gui/SaveCoordinatesGui.java @@ -2,19 +2,19 @@ package me.bionicbeanie.mods.gui; import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription; import me.bionicbeanie.mods.api.IGui; -import me.bionicbeanie.mods.api.IGuiHandler; +import me.bionicbeanie.mods.api.IViewHandler; import me.bionicbeanie.mods.api.IRootGridPanel; import me.bionicbeanie.mods.api.IScreenController; public class SaveCoordinatesGui extends LightweightGuiDescription implements IGui { private IRootGridPanel rootGridPanel; - private IGuiHandler saveHandler; - private IGuiHandler listHandler; + private IViewHandler saveHandler; + private IViewHandler listHandler; private IScreenController screenController; @Override - public void init(IGuiHandler saveHandler, IGuiHandler listHandler, IScreenController screenController) { + public void init(IViewHandler saveHandler, IViewHandler listHandler, IScreenController screenController) { this.rootGridPanel = createRootPanel(); this.saveHandler = saveHandler; this.listHandler = listHandler; @@ -50,7 +50,7 @@ public class SaveCoordinatesGui extends LightweightGuiDescription implements IGu return panel; } - private void showView(IGuiHandler handler) { + private void showView(IViewHandler handler) { rootGridPanel.reset(); handler.placeWidgets(rootGridPanel); rootGridPanel.validate(this); diff --git a/src/main/java/me/bionicbeanie/mods/gui/SaveGuiHandler.java b/src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java similarity index 86% rename from src/main/java/me/bionicbeanie/mods/gui/SaveGuiHandler.java rename to src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java index 94a017b..a29a1c3 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/SaveGuiHandler.java +++ b/src/main/java/me/bionicbeanie/mods/gui/view/DefaultViewHandler.java @@ -1,6 +1,7 @@ -package me.bionicbeanie.mods.gui; +package me.bionicbeanie.mods.gui.view; import java.io.IOException; +import java.util.UUID; import io.github.cottonmc.cotton.gui.widget.WButton; import io.github.cottonmc.cotton.gui.widget.WText; @@ -16,11 +17,11 @@ import me.bionicbeanie.mods.util.DimensionSpriteUtil; import net.minecraft.client.MinecraftClient; import net.minecraft.text.LiteralText; -public class SaveGuiHandler extends GuiHandlerBase { +public class DefaultViewHandler extends ViewHandlerBase { private IPlayerLocator locator; - public SaveGuiHandler(IFileStore fileStore, IPlayerLocator locator, MinecraftClient client, IGui gui) { + public DefaultViewHandler(IFileStore fileStore, IPlayerLocator locator, MinecraftClient client, IGui gui) { super(fileStore, gui, client); this.locator = locator; } @@ -65,7 +66,8 @@ public class SaveGuiHandler extends GuiHandlerBase { @Override public void run() { try { - fileStore.save(new PlayerPosition(rawPosition, textField.getText())); + String id = UUID.randomUUID().toString(); + fileStore.save(new PlayerPosition(id, rawPosition, textField.getText(), null)); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/main/java/me/bionicbeanie/mods/gui/ListGuiHandler.java b/src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java similarity index 68% rename from src/main/java/me/bionicbeanie/mods/gui/ListGuiHandler.java rename to src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java index 19fdb6a..92e4d1e 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/ListGuiHandler.java +++ b/src/main/java/me/bionicbeanie/mods/gui/view/ListViewHandler.java @@ -1,9 +1,10 @@ -package me.bionicbeanie.mods.gui; +package me.bionicbeanie.mods.gui.view; import java.io.IOException; import java.util.List; import java.util.function.BiConsumer; +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; @@ -17,9 +18,9 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; -public class ListGuiHandler extends GuiHandlerBase { +public class ListViewHandler extends ViewHandlerBase { - public ListGuiHandler(IFileStore fileStore, IGui gui, MinecraftClient client) { + public ListViewHandler(IFileStore fileStore, IGui gui, MinecraftClient client) { super(fileStore, gui, client); } @@ -28,7 +29,7 @@ public class ListGuiHandler extends GuiHandlerBase { BiConsumer configurator = (PlayerPosition position, CoordinatesListItemPanel panel) -> { - panel.setPosition(position); + panel.setPosition(position, fileStore, gui); }; List positions; @@ -43,7 +44,7 @@ public class ListGuiHandler extends GuiHandlerBase { CoordinatesListItemPanel::new, configurator); listPanel.setListItemHeight(2 * 18); - + root.add(listPanel, 0, 0, 7, 7); } @@ -52,26 +53,42 @@ public class ListGuiHandler extends GuiHandlerBase { private WLabel coordinates; private WLabel location; private WSprite icon; + private WButton deleteButton; public CoordinatesListItemPanel() { this.coordinates = new WLabel("Foo"); this.location = new WLabel("Foo"); this.icon = new WSprite(new Identifier("minecraft:textures/item/ender_eye.png")); + this.deleteButton = new WButton(new LiteralText("del")); this.add(icon, 0, 0, 1 * 18, 1 * 18); this.add(coordinates, 1 * 18, 0, 2 * 18, 1 * 18); - this.add(location, 0, 1 * 18, 3 * 18, 1 * 18); + this.add(location, 0, 1 * 18, 2 * 18, 1 * 18); + this.add(deleteButton, 5 * 18, 0, 1 * 18, 1 * 18); this.icon.setSize(1 * 18, 1 * 18); this.coordinates.setSize(2 * 18, 1 * 18); this.location.setSize(3 * 18, 1 * 18); - this.setSize(3 * 18, 2 * 18); + this.deleteButton.setSize(1 * 18, 1 * 18); + this.setSize(7 * 18, 2 * 18); } - public void setPosition(PlayerPosition position) { + public void setPosition(PlayerPosition position, IFileStore fileStore, IGui gui) { this.icon.setImage(DimensionSpriteUtil.CreateWorldIconIdentifier(position.getWorldDimension())); this.location.setText(new LiteralText(position.getLocationName())); this.coordinates.setText(new LiteralText(position.getX() + "," + position.getY() + "," + position.getZ())); + this.deleteButton.setOnClick(new Runnable() { + + @Override + public void run() { + try { + fileStore.delete(position.getId()); + gui.showListView(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); } } diff --git a/src/main/java/me/bionicbeanie/mods/gui/GuiHandlerBase.java b/src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java similarity index 63% rename from src/main/java/me/bionicbeanie/mods/gui/GuiHandlerBase.java rename to src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java index f3c6e6a..f96a3ae 100644 --- a/src/main/java/me/bionicbeanie/mods/gui/GuiHandlerBase.java +++ b/src/main/java/me/bionicbeanie/mods/gui/view/ViewHandlerBase.java @@ -1,19 +1,19 @@ -package me.bionicbeanie.mods.gui; +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.IGuiHandler; +import me.bionicbeanie.mods.api.IViewHandler; import net.minecraft.client.MinecraftClient; -public abstract class GuiHandlerBase implements IGuiHandler { +public abstract class ViewHandlerBase implements IViewHandler { protected IFileStore fileStore; protected IGui gui; protected MinecraftClient client; protected WWidget panel; - protected GuiHandlerBase(IFileStore fileStore, IGui gui, MinecraftClient client) { + protected ViewHandlerBase(IFileStore fileStore, IGui gui, MinecraftClient client) { this.fileStore = fileStore; this.gui = gui; this.client = client; diff --git a/src/main/java/me/bionicbeanie/mods/impl/FileStore.java b/src/main/java/me/bionicbeanie/mods/impl/FileStore.java index f528bbc..7d0541c 100644 --- a/src/main/java/me/bionicbeanie/mods/impl/FileStore.java +++ b/src/main/java/me/bionicbeanie/mods/impl/FileStore.java @@ -9,6 +9,8 @@ import java.nio.file.StandardOpenOption; import java.util.LinkedList; import java.util.List; +import org.apache.commons.lang3.StringUtils; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -24,7 +26,10 @@ public class FileStore implements IFileStore { private Gson gson; public FileStore(String baseDir) { - this.gson = new GsonBuilder().setPrettyPrinting().create(); + this.gson = new GsonBuilder() + .setPrettyPrinting() + .setLenient() + .create(); this.saveFilePath = Paths.get(baseDir, DEFAULT_DIR, DEFAULT_FILE); try { @@ -37,12 +42,6 @@ public class FileStore implements IFileStore { } } - @Override - public void save(List positions) { - // TODO Auto-generated method stub - - } - @Override public List list() throws IOException { List lines = Files.readAllLines(saveFilePath); @@ -65,8 +64,21 @@ public class FileStore implements IFileStore { List playerPositions = list(); playerPositions.add(position); + saveAll(playerPositions); + } + + @Override + public void delete(String id) throws IOException { + + List playerPositions = list(); + playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(id, p.getId())); + + saveAll(playerPositions); + } + + private void saveAll(List playerPositions) throws IOException { String serialized = gson.toJson(playerPositions.toArray()); - Files.write(saveFilePath, serialized.getBytes(), StandardOpenOption.WRITE); + Files.write(saveFilePath, serialized.getBytes(), StandardOpenOption.TRUNCATE_EXISTING); } } diff --git a/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java b/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java index 059c5a1..2d1876a 100644 --- a/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java +++ b/src/main/java/me/bionicbeanie/mods/model/PlayerPosition.java @@ -3,10 +3,14 @@ package me.bionicbeanie.mods.model; public class PlayerPosition { private PlayerRawPosition rawPosition; - private String locationName; + private String id, locationName; + private PositionMetadata positionMetadata; - public PlayerPosition(PlayerRawPosition rawPosition, String locationName) { + public PlayerPosition(String id, PlayerRawPosition rawPosition, String locationName, + PositionMetadata positionMetadata) { + this.id = id; this.rawPosition = rawPosition; + this.positionMetadata = positionMetadata; this.locationName = locationName; } @@ -29,4 +33,12 @@ public class PlayerPosition { public String getLocationName() { return locationName; } + + public PositionMetadata getPositionMetadata() { + return positionMetadata; + } + + public String getId() { + return id; + } } diff --git a/src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java b/src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java new file mode 100644 index 0000000..000949b --- /dev/null +++ b/src/main/java/me/bionicbeanie/mods/model/PositionMetadata.java @@ -0,0 +1,25 @@ +package me.bionicbeanie.mods.model; + +import java.util.Date; + +public class PositionMetadata { + + private String worldName, notes; + private Date created, lastModified; + + public String getWorldName() { + return worldName; + } + + public String getNotes() { + return notes; + } + + public Date getCreated() { + return created; + } + + public Date getLastModified() { + return lastModified; + } +}