Make coordinates editable, improve gui

This commit is contained in:
Surya 2021-08-29 19:42:56 +05:30
parent 4fa01e6979
commit 441dfb5ffb
10 changed files with 224 additions and 99 deletions

View File

@ -20,4 +20,4 @@ org.gradle.jvmargs=-Xmx1G
libgui_version=4.0.0+1.17
#https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu/
modmenu_version=1.16.9
modmenu_version=2.0.2

View File

@ -101,13 +101,13 @@ class ConfigViewHandler extends ViewHandlerBase<List<IKeyBinding>> {
});
}
void onBack(Runnable runnable) {
void onBackButtonClick(Runnable runnable) {
this.backButton.setOnClick(() -> {
runnable.run();
});
}
void onSave(Runnable runnable) {
void onSaveButtonClick(Runnable runnable) {
this.saveButton.setOnClick(() -> {
this.shouldNotUpdateBinding = true;
if(this.focussingConfig != null) {

View File

@ -39,12 +39,12 @@ public class DIContainer {
modMenuScreenFactory = (parent) -> {
ConfigViewHandler handler = new ConfigViewHandler();
handler.onSave(() -> {
new SaveConfigsOperation(keyBinds, fileStore, handler::getState).run();
handler.onSaveButtonClick(() -> {
new SaveConfigsOperation(keyBinds, fileStore, handler::getState).call();
guiController.closeScreen();
});
handler.onBack(() -> guiController.closeScreen());
handler.onBackButtonClick(() -> guiController.closeScreen());
return handler.createView(keyBinds.getAllBinds());
};
@ -54,7 +54,13 @@ public class DIContainer {
public static Runnable getPingPositionOperation() {
initialize();
return pingPositionOperation;
return () -> {
try {
pingPositionOperation.call();
}catch (Exception e) {
e.printStackTrace();
}
};
}
private static void initialize() {

View File

@ -5,7 +5,7 @@ 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.WSprite;
import io.github.cottonmc.cotton.gui.widget.WTextField;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import me.bionicbeanie.mods.savecoords.IFileStore;
@ -15,6 +15,7 @@ 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.DimensionKeys;
import me.bionicbeanie.mods.savecoords.util.ResourceUtils;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
@ -45,75 +46,117 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
PlayerRawPosition rawPosition = existingPosition == null ? locator.locate() : existingPosition;
WWidget xLabel = CreateLabelForCoorindate("X");
WWidget yLabel = CreateLabelForCoorindate("Y");
WWidget zLabel = CreateLabelForCoorindate("Z");
WWidget xLabel = CreateLabelForCoordinate("X");
WWidget yLabel = CreateLabelForCoordinate("Y");
WWidget zLabel = CreateLabelForCoordinate("Z");
WWidget xText = CreateWidgetForCoordinate(rawPosition.getX());
WWidget yText = CreateWidgetForCoordinate(rawPosition.getY());
WWidget zText = CreateWidgetForCoordinate(rawPosition.getZ());
WTextField xText = CreateWidgetForCoordinate(rawPosition.getX());
WTextField yText = CreateWidgetForCoordinate(rawPosition.getY());
WTextField 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(xLabel, 0, 1, 1, 1);
root.add(yLabel, 0, 2, 1, 1);
root.add(zLabel, 0, 3, 1, 1);
root.add(xText, 3, 1, 2, 1);
root.add(yText, 3, 2, 2, 1);
root.add(zText, 3, 3, 2, 1);
root.add(xText, 1, 1, 6, 1);
root.add(yText, 1, 2, 6, 1);
root.add(zText, 1, 3, 6, 1);
WWidget icon = ResourceUtils.CreateWorldIcon(rawPosition.getWorldDimension());
root.add(icon, 8, 1, 2, 2);
WSprite worldIcon = ResourceUtils.createWorldIcon(getDimension(rawPosition));
root.add(worldIcon, 9, 1, 2, 2);
WWidget worldButton = CreateWorldButton(worldIcon);
root.add(worldButton, 8, 3, 4, 1);
String defaultWorldName = getDefaultWorldName(existingPosition);
WTextField world = CreateWorldField(defaultWorldName);
root.add(world, 0, 4, 4, 1);
root.add(world, 0, 5, 4, 1);
WTextField location = CreateLocationField(existingPosition);
root.add(location, 5, 4, 7, 1);
root.add(location, 5, 5, 7, 1);
WTextField notes = CreateNotesField(existingPosition);
root.add(notes, 0, 6, 12, 1);
root.add(notes, 0, 7, 12, 1);
root.add(saveButton, 13, 6, 2, 1);
root.add(saveButton, 13, 7, 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);
return createPlayerPositionSupplier(existingPosition, xText, yText, zText, world, location, notes);
}
public void onSave(Runnable runnable) {
private WWidget CreateWorldButton(WSprite worldIcon) {
WButton button = new WButton(new LiteralText(DimensionKeys.OVERWORLD));
button.setOnClick(() -> button.setLabel(new LiteralText(DimensionKeys.NETHER)));
return button;
}
private String getDimension(PlayerRawPosition rawPosition) {
String dimensionIdentifier = rawPosition.getWorldDimension();
if(isDimension(DimensionKeys.OVERWORLD, dimensionIdentifier)) {
return DimensionKeys.OVERWORLD;
}
if(isDimension(DimensionKeys.NETHER, dimensionIdentifier)) {
return DimensionKeys.NETHER;
}
if(isDimension(DimensionKeys.END, dimensionIdentifier)) {
return DimensionKeys.END;
}
return DimensionKeys.UNKNOWN;
}
private boolean isDimension(String dimension, String dimensionIdentifier) {
return dimensionIdentifier != null && dimensionIdentifier.contains(dimension);
}
public void onSaveButtonClick(Runnable runnable) {
this.saveButton.setOnClick(runnable);
}
public void onClose(Runnable runnable) {
public void onCloseButtonClick(Runnable runnable) {
this.closeButton.setOnClick(runnable);
}
public void onList(Runnable runnable) {
public void onListButtonClick(Runnable runnable) {
this.listButton.setOnClick(runnable);
}
public void onPing(Runnable runnable) {
public void onPingButtonClick(Runnable runnable) {
this.pingButton.setOnClick(runnable);
}
public void onConfig(Runnable runnable) {
public void onConfigButtonClick(Runnable runnable) {
this.configButton.setOnClick(runnable);
}
private Supplier<PlayerPosition> createPlayerPositionSupplier(PlayerPosition existingPosition,
PlayerRawPosition rawPosition, WTextField world, WTextField location, WTextField notes) {
WTextField xText, WTextField yText, WTextField zText, WTextField world,
WTextField location, WTextField notes) {
return () -> {
String id = CreateId(existingPosition);
long x = parseLong(xText);
long y = parseLong(yText);
long z = parseLong(zText);
PlayerRawPosition rawPosition = new PlayerRawPosition(x, y, z, "overworld");
PositionMetadata metadata = CreateMetadata(existingPosition, world, notes);
return new PlayerPosition(id, rawPosition, location.getText(), metadata);
};
}
private long parseLong(WTextField xText) {
String value = xText.getText();
return Long.parseLong(value);
}
private String CreateId(PlayerPosition existingPosition) {
return existingPosition == null ? UUID.randomUUID().toString() : existingPosition.getId();
}
@ -144,12 +187,19 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
return "";
}
private WWidget CreateLabelForCoorindate(String label) {
return new WLabel(label, 0xb80000);
private WWidget CreateLabelForCoordinate(String label) {
//return new WLabel(label, 0xb80000);
WButton labelButton = new WButton(new LiteralText(label));
labelButton.setEnabled(false);
return labelButton;
}
private WWidget CreateWidgetForCoordinate(long l) {
return new WText(new LiteralText(String.valueOf(l)), 0x3939ac);
private WTextField CreateWidgetForCoordinate(long l) {
WTextField textField = new WTextField();
textField.setText(String.valueOf(l));
return textField;
}
private WTextField CreateLocationField(PlayerPosition existingPosition) {
@ -183,7 +233,7 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
private WButton CreatePingButton() {
WButton button = CreateButton("");
button.setIcon(ResourceUtils.CreatePingIcon());
button.setIcon(ResourceUtils.createPingIcon());
return button;
}

View File

@ -51,7 +51,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
return () -> (Void) null;
}
public void onBack(Runnable runnable) {
public void onBackButtonClick(Runnable runnable) {
this.backButton.setOnClick(runnable);
}
@ -123,8 +123,8 @@ class ListViewHandler extends ViewHandlerBase<Void> {
this.pingButton = new WButton(new LiteralText(""));
this.detailButton = new WButton(new LiteralText(""));
this.pingButton.setIcon(ResourceUtils.CreatePingIcon());
this.detailButton.setIcon(ResourceUtils.CreateDetailsIcon());
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);
@ -147,13 +147,13 @@ class ListViewHandler extends ViewHandlerBase<Void> {
private WButton createDeleteButton() {
TexturedButton button = new TexturedButton(new LiteralText("x"));
button.setTexture(ResourceUtils.CreateIdentifier("close"));
button.setTexture(ResourceUtils.getIdentifier("close"));
return button;
}
void setPosition(PlayerPosition position, IFileStore fileStore) {
this.icon.setImage(ResourceUtils.CreateWorldIconIdentifier(position.getWorldDimension()));
this.icon.setImage(ResourceUtils.getIdentifier(position.getWorldDimension()));
this.location.setText(new LiteralText(position.getLocationName()));
this.location.setColor(0x3939ac);
if (position.getPositionMetadata() != null) {

View File

@ -32,7 +32,6 @@ public class SaveCoordinatesGui implements IModGui {
this.defaultHandler = CreateDefaultViewHandler();
this.listHandler = CreateListViewHandler();
this.configHandler = CreateConfigHandler();
}
@Override
@ -40,27 +39,23 @@ public class SaveCoordinatesGui implements IModGui {
showDefaultView(null);
}
private Screen createConfigScreen() {
return this.configHandler.createView(keyBinds.getAllBinds());
}
private IViewHandler<PlayerPosition> CreateDefaultViewHandler() {
DefaultViewHandler handler = new DefaultViewHandler(fileStore, locator);
handler.onSave(this::onSavePosition);
handler.onList(this::showListView);
handler.onConfig(this::showConfigView);
handler.onPing(new PingPositionOperation(fileStore, locator::locate));
handler.onClose(screenController::closeScreen);
handler.onSaveButtonClick(this::savePosition);
handler.onListButtonClick(this::showListView);
handler.onConfigButtonClick(this::showConfigView);
handler.onPingButtonClick(this::pingPosition);
handler.onCloseButtonClick(screenController::closeScreen);
return handler;
}
private IViewHandler<Void> CreateListViewHandler() {
ListViewHandler handler = new ListViewHandler(fileStore, this::onDeletePosition, this::onEditPosition,
this::onPingPosition);
this::pingPosition);
handler.onBack(() -> showDefaultView(null));
handler.onBackButtonClick(() -> showDefaultView(null));
return handler;
}
@ -68,19 +63,21 @@ public class SaveCoordinatesGui implements IModGui {
private IViewHandler<List<IKeyBinding>> CreateConfigHandler() {
ConfigViewHandler handler = new ConfigViewHandler();
handler.onBack(() -> showDefaultView(null));
handler.onSave(this::onSaveConfigs);
handler.onBackButtonClick(() -> showDefaultView(null));
handler.onSaveButtonClick(this::saveConfigs);
return handler;
}
private void onSavePosition() {
new SavePositionOperation(fileStore, defaultHandler::getState).run();
showListView();
private void savePosition() {
ErrorResponse response = new SavePositionOperation(fileStore, defaultHandler::getState).call();
if (!response.isFailed()) {
showListView();
}
}
private void onDeletePosition(PlayerPosition position) {
new DeletePositionOperation(fileStore, () -> position.getId()).run();
new DeletePositionOperation(fileStore, position::getId).call();
showListView();
}
@ -88,12 +85,16 @@ public class SaveCoordinatesGui implements IModGui {
showDefaultView(position);
}
private void onPingPosition(PlayerRawPosition position) {
new PingPositionOperation(fileStore, () -> position).run();
private void pingPosition() {
new PingPositionOperation(fileStore, locator::locate).call();
}
private void onSaveConfigs() {
new SaveConfigsOperation(keyBinds, fileStore, configHandler::getState).run();
private void pingPosition(PlayerRawPosition position) {
new PingPositionOperation(fileStore, () -> position).call();
}
private void saveConfigs() {
new SaveConfigsOperation(keyBinds, fileStore, configHandler::getState).call();
showDefaultView(null);
}
@ -109,4 +110,7 @@ public class SaveCoordinatesGui implements IModGui {
screenController.openScreen(createConfigScreen());
}
private Screen createConfigScreen() {
return this.configHandler.createView(keyBinds.getAllBinds());
}
}

View File

@ -1,10 +1,11 @@
package me.bionicbeanie.mods.savecoords.gui.impl;
import java.util.concurrent.Callable;
import java.util.function.Supplier;
import me.bionicbeanie.mods.savecoords.IFileStore;
abstract class ViewOperationBase<T> implements Runnable {
abstract class ViewOperationBase<T> implements Callable<ErrorResponse> {
private IFileStore fileStore;
private Supplier<T> stateSupplier;
@ -15,13 +16,48 @@ abstract class ViewOperationBase<T> implements Runnable {
}
@Override
public void run() {
public ErrorResponse call() {
try {
executeOperation(fileStore, stateSupplier.get());
return ErrorResponse.CreateSuccess();
} catch (Exception e) {
e.printStackTrace();
return ErrorResponse.CreateFailure(e.getMessage());
}
}
protected abstract void executeOperation(IFileStore fileStore, T state) throws Exception;
}
class ErrorResponse {
private static ErrorResponse SUCCESS = new ErrorResponse(false);
private String code;
private boolean failed;
public static ErrorResponse CreateSuccess() {
return SUCCESS;
}
public static ErrorResponse CreateFailure(String code) {
return new ErrorResponse(true, code);
}
private ErrorResponse(boolean failed) {
this(failed, null);
}
private ErrorResponse(boolean failed, String code) {
this.failed = failed;
this.code = code;
}
public String getCode(){
return code;
}
public boolean isFailed() {
return failed;
}
}

View File

@ -0,0 +1,9 @@
package me.bionicbeanie.mods.savecoords.util;
public class DimensionKeys {
public static String OVERWORLD = "overworld";
public static String NETHER = "nether";
public static String END = "end";
public static String UNKNOWN = "unknown";
}

View File

@ -0,0 +1,34 @@
package me.bionicbeanie.mods.savecoords.util;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.util.Identifier;
public class IdentifiersCache {
private String DEFAULT = "nonexistent";
private Map<String, Identifier> cache = new HashMap<>();
public IdentifiersCache() {
add(DEFAULT, cache);
add("overworld", cache);
add("nether", cache);
add("end", cache);
add("ping", cache);
add("close", cache);
add("more", cache);
}
public Identifier get(String resourceName) {
return cache.getOrDefault(resourceName, cache.get(DEFAULT));
}
private static void add(String resourceName, Map<String, Identifier> cache) {
cache.put(resourceName, createIdentifier(resourceName));
}
private static Identifier createIdentifier(String file) {
return new Identifier("savecoords", "textures/gui/" + file + ".png");
}
}

View File

@ -1,45 +1,31 @@
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));
private static IdentifiersCache cache = new IdentifiersCache();
public static WSprite createWorldIcon(String dimension) {
return new WSprite(getIdentifier(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 Identifier getIdentifier(String resourceName) {
return cache.get(resourceName);
}
public static Icon CreatePingIcon() {
return new TextureIcon(CreateIdentifier("ping"));
public static Icon createPingIcon() {
return new TextureIcon(cache.get("ping"));
}
public static Icon CreateCloseIcon() {
return new TextureIcon(CreateIdentifier("close"));
public static Icon createCloseIcon() {
return new TextureIcon(cache.get("close"));
}
public static Icon CreateDetailsIcon() {
return new TextureIcon(CreateIdentifier("more"));
}
public static Identifier CreateIdentifier(String file) {
return new Identifier("savecoords", "textures/gui/" + file + ".png");
public static Icon createDetailsIcon() {
return new TextureIcon(cache.get("more"));
}
}