Add delete functionality

This commit is contained in:
Surya 2021-05-26 23:41:52 +05:30
parent 5b381ea907
commit 450737e98d
13 changed files with 113 additions and 77 deletions

View File

@ -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

View File

@ -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);

View File

@ -7,9 +7,9 @@ import me.bionicbeanie.mods.model.PlayerPosition;
public interface IFileStore {
public void save(List<PlayerPosition> positions) throws IOException;
public void save(PlayerPosition position) throws IOException;
public void delete(String id) throws IOException;
public List<PlayerPosition> list() throws IOException;
}

View File

@ -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();

View File

@ -1,6 +1,6 @@
package me.bionicbeanie.mods.api;
public interface IGuiHandler {
public interface IViewHandler {
void placeWidgets(IRootGridPanel rootPanel);

View File

@ -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()));
}
}

View File

@ -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);

View File

@ -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();
}

View File

@ -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<PlayerPosition, CoordinatesListItemPanel> configurator = (PlayerPosition position,
CoordinatesListItemPanel panel) -> {
panel.setPosition(position);
panel.setPosition(position, fileStore, gui);
};
List<PlayerPosition> 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();
}
}
});
}
}

View File

@ -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;

View File

@ -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<PlayerPosition> positions) {
// TODO Auto-generated method stub
}
@Override
public List<PlayerPosition> list() throws IOException {
List<String> lines = Files.readAllLines(saveFilePath);
@ -65,8 +64,21 @@ public class FileStore implements IFileStore {
List<PlayerPosition> playerPositions = list();
playerPositions.add(position);
saveAll(playerPositions);
}
@Override
public void delete(String id) throws IOException {
List<PlayerPosition> playerPositions = list();
playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(id, p.getId()));
saveAll(playerPositions);
}
private void saveAll(List<PlayerPosition> playerPositions) throws IOException {
String serialized = gson.toJson(playerPositions.toArray());
Files.write(saveFilePath, serialized.getBytes(), StandardOpenOption.WRITE);
Files.write(saveFilePath, serialized.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}