Add configs

This commit is contained in:
Surya 2021-05-30 21:02:56 +05:30
parent e2d40032c1
commit 30ee79b7c0
23 changed files with 443 additions and 101 deletions

View File

@ -15,10 +15,6 @@ repositories {
name = "CottonMC" name = "CottonMC"
url = "https://server.bbkr.space/artifactory/libs-release" url = "https://server.bbkr.space/artifactory/libs-release"
} }
maven {
name = "ClothConfig"
url = "https://maven.shedaniel.me/"
}
} }
dependencies { dependencies {
@ -29,7 +25,6 @@ dependencies {
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}") modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}")
modImplementation include("me.shedaniel.cloth:cloth-config-fabric:${project.clothconfig_version}")
} }
processResources { processResources {

View File

@ -3,16 +3,21 @@ package me.bionicbeanie.mods.savecoords;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import me.bionicbeanie.mods.savecoords.model.PlayerPosition; import me.bionicbeanie.mods.savecoords.model.PlayerPosition;
public interface IFileStore { public interface IFileStore {
String getDefaultWorld() throws IOException; String readDefaultWorldName() throws IOException;
void setDefaultWorld(String defaultWorldName) throws IOException; ConfigData readConfigData() throws IOException;
void save(PlayerPosition position) throws IOException; void writeDefaultWorldName(String defaultWorldName) throws IOException;
void delete(String id) throws IOException; void writePosition(PlayerPosition position) throws IOException;
List<PlayerPosition> list() throws IOException; void writeConfigs(ConfigData configData) throws IOException;
void deletePosition(String id) throws IOException;
List<PlayerPosition> listPositions() throws IOException;
} }

View File

@ -1,35 +1,25 @@
package me.bionicbeanie.mods.savecoords; package me.bionicbeanie.mods.savecoords;
import org.lwjgl.glfw.GLFW; import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import me.bionicbeanie.mods.savecoords.gui.impl.ModGui; import me.bionicbeanie.mods.savecoords.gui.impl.ModGui;
import me.bionicbeanie.mods.savecoords.impl.Factory;
import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; 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 { public class SaveCoordinatesClient implements ClientModInitializer {
@Override @Override
public void onInitializeClient() { public void onInitializeClient() {
KeyBinding keyBinding = registerKeyBinding(); @SuppressWarnings("resource")
IFileStore fileStore = Factory.createFileStore(MinecraftClient.getInstance().runDirectory.getAbsolutePath());
IKeyBindConfiguration keyBindConfiguration = Factory.createKeyBindConfiguration(fileStore);
ClientTickEvents.END_CLIENT_TICK.register(client -> { ClientTickEvents.END_CLIENT_TICK.register(client -> {
while (keyBinding.wasPressed()) { while (keyBindConfiguration.getDefaultKeyBinding().wasPressed()) {
ModGui.start(client); ModGui.start(client, fileStore, keyBindConfiguration);
} }
}); });
} }
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);
}
} }

View File

@ -0,0 +1,10 @@
package me.bionicbeanie.mods.savecoords.gui;
import net.minecraft.client.options.KeyBinding;
public interface IKeyBindConfiguration {
KeyBinding getDefaultKeyBinding();
void setDefaultKeyBinding(int keyCode);
}

View File

@ -0,0 +1,23 @@
package me.bionicbeanie.mods.savecoords.gui.impl;
import java.util.function.Consumer;
import io.github.cottonmc.cotton.gui.GuiDescription;
import me.bionicbeanie.mods.savecoords.gui.SaveCoordinatesScreen;
public class ConfigScreen extends SaveCoordinatesScreen{
private Consumer<Integer> keyCodeConsumer;
public ConfigScreen(GuiDescription description, Consumer<Integer> keyCodeConsumer) {
super(description);
this.keyCodeConsumer = keyCodeConsumer;
}
@Override
public boolean keyReleased(int ch, int keyCode, int modifiers) {
keyCodeConsumer.accept(ch);
return super.keyReleased(ch, keyCode, modifiers);
}
}

View File

@ -0,0 +1,107 @@
package me.bionicbeanie.mods.savecoords.gui.impl;
import java.util.function.Supplier;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import me.bionicbeanie.mods.savecoords.gui.IRootPanel;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.InputUtil.Key;
import net.minecraft.text.LiteralText;
class ConfigViewHandler extends ViewHandlerBase<ConfigData> {
private WButton keyBindingButton;
private WButton saveButton;
private WButton backButton;
private boolean focussing = false;
private int newKeyBinding = -1;
private WButton resetButton;
public ConfigViewHandler() {
keyBindingButton = new WButton();
saveButton = new WButton(new LiteralText("SAVE"));
backButton = new WButton(new LiteralText("BACK"));
resetButton = new WButton(new LiteralText("RESET"));
}
@Override
protected Supplier<ConfigData> setupView(IRootPanel rootGridPanel, ConfigData state) {
if (state == null) {
state = new ConfigData();
}
newKeyBinding = state.getDefaultKeyBindingCode();
focussing = false;
WLabel configLabel = new WLabel("Default Key Binding");
rootGridPanel.add(configLabel, 4, 3, 3, 1);
setConfigValue(state.getDefaultKeyBindingCode());
keyBindingButton.setOnClick(() -> {
focussing = !focussing;
if (focussing) {
keyBindingButton.setLabel(new LiteralText("_"));
}
});
resetButton.setOnClick(() -> {
focussing = true;
setConfigValue(new ConfigData().getDefaultKeyBindingCode());
focussing = false;
});
rootGridPanel.add(keyBindingButton, 4, 4, 3, 1);
rootGridPanel.add(saveButton, 13, 9, 2, 1);
rootGridPanel.add(backButton, 0, 9, 2, 1);
rootGridPanel.add(resetButton, 8, 4, 2, 1);
return () -> {
ConfigData data = new ConfigData();
data.setDefaultKeyBindingCode(newKeyBinding);
return data;
};
}
@Override
protected Screen createScreen() {
return new ConfigScreen(this, (k) -> {
if (focussing) {
setConfigValue(k);
focussing = false;
}
});
}
public void onBack(Runnable runnable) {
this.backButton.setOnClick(runnable);
}
public void onSave(Runnable runnable) {
this.saveButton.setOnClick(runnable);
}
private void setConfigValue(Integer k) {
newKeyBinding = k;
keyBindingButton.setLabel(new LiteralText(parseKeyName(k)));
}
private String parseKeyName(Integer k) {
Key key = InputUtil.Type.KEYSYM.createFromCode(k);
String keyString = key.toString();
try {
return keyString.split("\\.")[2];
} catch (Exception e) {
e.printStackTrace();
}
return keyString;
}
}

View File

@ -39,7 +39,7 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
} }
@Override @Override
public Supplier<PlayerPosition> placeWidgets(IRootPanel root, PlayerPosition existingPosition) { public Supplier<PlayerPosition> setupView(IRootPanel root, PlayerPosition existingPosition) {
PlayerRawPosition rawPosition = existingPosition == null ? locator.locate() : existingPosition; PlayerRawPosition rawPosition = existingPosition == null ? locator.locate() : existingPosition;
@ -134,7 +134,7 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
} }
try { try {
return fileStore.getDefaultWorld(); return fileStore.readDefaultWorldName();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -186,8 +186,6 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
} }
private WButton CreateButton(String name) { private WButton CreateButton(String name) {
WButton button = new WButton(new LiteralText(name)); return new WButton(new LiteralText(name));
return button;
} }
} }

View File

@ -4,14 +4,14 @@ import java.util.function.Supplier;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
class DeleteOperation extends ViewOperationBase<String> { class DeletePositionOperation extends ViewOperationBase<String> {
public DeleteOperation(IFileStore fileStore, Supplier<String> stateSupplier) { public DeletePositionOperation(IFileStore fileStore, Supplier<String> stateSupplier) {
super(fileStore, stateSupplier); super(fileStore, stateSupplier);
} }
@Override @Override
protected void executeOperation(IFileStore fileStore, String positionId) throws Exception { protected void executeOperation(IFileStore fileStore, String positionId) throws Exception {
fileStore.delete(positionId); fileStore.deletePosition(positionId);
} }
} }

View File

@ -38,7 +38,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
} }
@Override @Override
public Supplier<Void> placeWidgets(IRootPanel root, Void nullState) { public Supplier<Void> setupView(IRootPanel root, Void nullState) {
List<PlayerPosition> positions = getPositions(fileStore); List<PlayerPosition> positions = getPositions(fileStore);
WListPanel<PlayerPosition, CoordinatesListItemPanel> listPanel = createListPane(positions); WListPanel<PlayerPosition, CoordinatesListItemPanel> listPanel = createListPane(positions);
@ -77,7 +77,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
private List<PlayerPosition> getPositions(IFileStore fileStore) { private List<PlayerPosition> getPositions(IFileStore fileStore) {
try { try {
List<PlayerPosition> positions = fileStore.list(); List<PlayerPosition> positions = fileStore.listPositions();
Collections.sort(positions, (p1, p2) -> p2.getPositionMetadata().getLastModified() Collections.sort(positions, (p1, p2) -> p2.getPositionMetadata().getLastModified()
.compareTo(p1.getPositionMetadata().getLastModified())); .compareTo(p1.getPositionMetadata().getLastModified()));
return positions; return positions;
@ -111,7 +111,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
this.location = new WLabel("Foo"); this.location = new WLabel("Foo");
this.world = new WLabel("Foo"); this.world = new WLabel("Foo");
this.icon = new WSprite(new Identifier("minecraft:textures/item/ender_eye.png")); this.icon = new WSprite(new Identifier("minecraft:textures/item/ender_eye.png"));
this.deleteButton = new WButton(new LiteralText("x")); this.deleteButton = createDeleteButton();
this.pingButton = new WButton(new LiteralText("")); this.pingButton = new WButton(new LiteralText(""));
this.detailButton = new WButton(new LiteralText("")); this.detailButton = new WButton(new LiteralText(""));
@ -138,6 +138,13 @@ class ListViewHandler extends ViewHandlerBase<Void> {
this.setSize(15 * 18, 2 * 18); this.setSize(15 * 18, 2 * 18);
} }
private WButton createDeleteButton() {
TexturedButton button = new TexturedButton(new LiteralText("x"));
button.setTexture(ResourceUtils.CreateIdentifier("close"));
return button;
}
void setPosition(PlayerPosition position, IFileStore fileStore) { void setPosition(PlayerPosition position, IFileStore fileStore) {
this.icon.setImage(ResourceUtils.CreateWorldIconIdentifier(position.getWorldDimension())); this.icon.setImage(ResourceUtils.CreateWorldIconIdentifier(position.getWorldDimension()));
this.location.setText(new LiteralText(position.getLocationName())); this.location.setText(new LiteralText(position.getLocationName()));

View File

@ -3,16 +3,16 @@ package me.bionicbeanie.mods.savecoords.gui.impl;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.IPlayerLocator; import me.bionicbeanie.mods.savecoords.IPlayerLocator;
import me.bionicbeanie.mods.savecoords.gui.IGuiController; import me.bionicbeanie.mods.savecoords.gui.IGuiController;
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import me.bionicbeanie.mods.savecoords.impl.Factory; import me.bionicbeanie.mods.savecoords.impl.Factory;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
public class ModGui { public class ModGui {
public static void start(MinecraftClient client) { public static void start(MinecraftClient client, IFileStore fileStore, IKeyBindConfiguration keyBindConfiguration) {
IGuiController controller = new GuiController(client); IGuiController controller = new GuiController(client);
IPlayerLocator locator = Factory.CreatePlayerLocator(client); IPlayerLocator locator = Factory.CreatePlayerLocator(client);
IFileStore fileStore = Factory.createFileStore(client.runDirectory.getAbsolutePath());
new SaveCoordinatesGui(fileStore, locator, controller); new SaveCoordinatesGui(fileStore, locator, keyBindConfiguration, controller);
} }
} }

View File

@ -6,9 +6,9 @@ import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
class PingOperation extends ViewOperationBase<PlayerRawPosition>{ class PingPositionOperation extends ViewOperationBase<PlayerRawPosition>{
public PingOperation(IFileStore fileStore, Supplier<PlayerRawPosition> stateSupplier) { public PingPositionOperation(IFileStore fileStore, Supplier<PlayerRawPosition> stateSupplier) {
super(fileStore, stateSupplier); super(fileStore, stateSupplier);
} }

View File

@ -0,0 +1,24 @@
package me.bionicbeanie.mods.savecoords.gui.impl;
import java.util.function.Supplier;
import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
public class SaveConfigsOperation extends ViewOperationBase<ConfigData>{
private IKeyBindConfiguration keyBindConfiguration;
public SaveConfigsOperation(IFileStore fileStore, IKeyBindConfiguration keyBindConfiguration, Supplier<ConfigData> stateSupplier) {
super(fileStore, stateSupplier);
this.keyBindConfiguration = keyBindConfiguration;
}
@Override
protected void executeOperation(IFileStore fileStore, ConfigData state) throws Exception {
fileStore.writeConfigs(state);
this.keyBindConfiguration.setDefaultKeyBinding(state.getDefaultKeyBindingCode());
}
}

View File

@ -1,9 +1,13 @@
package me.bionicbeanie.mods.savecoords.gui.impl; package me.bionicbeanie.mods.savecoords.gui.impl;
import java.io.IOException;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.IPlayerLocator; import me.bionicbeanie.mods.savecoords.IPlayerLocator;
import me.bionicbeanie.mods.savecoords.gui.IGuiController; import me.bionicbeanie.mods.savecoords.gui.IGuiController;
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import me.bionicbeanie.mods.savecoords.gui.IViewHandler; import me.bionicbeanie.mods.savecoords.gui.IViewHandler;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import me.bionicbeanie.mods.savecoords.model.PlayerPosition; import me.bionicbeanie.mods.savecoords.model.PlayerPosition;
import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition; import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition;
@ -13,15 +17,20 @@ public class SaveCoordinatesGui {
private IFileStore fileStore; private IFileStore fileStore;
private IViewHandler<PlayerPosition> defaultHandler; private IViewHandler<PlayerPosition> defaultHandler;
private IViewHandler<Void> listHandler; private IViewHandler<Void> listHandler;
private IViewHandler<ConfigData> configHandler;
private IPlayerLocator locator; private IPlayerLocator locator;
private IKeyBindConfiguration keyBindConfiguration;
SaveCoordinatesGui(IFileStore fileStore, IPlayerLocator locator, IGuiController screenController) { SaveCoordinatesGui(IFileStore fileStore, IPlayerLocator locator, IKeyBindConfiguration keyBindConfiguration,
IGuiController screenController) {
this.screenController = screenController; this.screenController = screenController;
this.fileStore = fileStore; this.fileStore = fileStore;
this.keyBindConfiguration = keyBindConfiguration;
this.locator = locator; this.locator = locator;
this.defaultHandler = CreateDefaultViewHandler(); this.defaultHandler = CreateDefaultViewHandler();
this.listHandler = CreateListViewHandler(); this.listHandler = CreateListViewHandler();
this.configHandler = CreateConfigHandler();
showDefaultView(null); showDefaultView(null);
} }
@ -29,10 +38,11 @@ public class SaveCoordinatesGui {
private IViewHandler<PlayerPosition> CreateDefaultViewHandler() { private IViewHandler<PlayerPosition> CreateDefaultViewHandler() {
DefaultViewHandler handler = new DefaultViewHandler(fileStore, locator); DefaultViewHandler handler = new DefaultViewHandler(fileStore, locator);
handler.onClose(screenController::closeScreen);
handler.onSave(this::onSavePosition); handler.onSave(this::onSavePosition);
handler.onList(this::showListView); handler.onList(this::showListView);
handler.onPing(new PingOperation(fileStore, locator::locate)); handler.onConfig(this::showConfigView);
handler.onPing(new PingPositionOperation(fileStore, locator::locate));
handler.onClose(screenController::closeScreen);
return handler; return handler;
} }
@ -46,13 +56,22 @@ public class SaveCoordinatesGui {
return handler; return handler;
} }
private IViewHandler<ConfigData> CreateConfigHandler() {
ConfigViewHandler handler = new ConfigViewHandler();
handler.onBack(() -> showDefaultView(null));
handler.onSave(this::onSaveConfigs);
return handler;
}
private void onSavePosition() { private void onSavePosition() {
new SaveOperation(fileStore, defaultHandler::getState).run(); new SavePositionOperation(fileStore, defaultHandler::getState).run();
showListView(); showListView();
} }
private void onDeletePosition(PlayerPosition position) { private void onDeletePosition(PlayerPosition position) {
new DeleteOperation(fileStore, () -> position.getId()).run(); new DeletePositionOperation(fileStore, () -> position.getId()).run();
showListView(); showListView();
} }
@ -61,7 +80,12 @@ public class SaveCoordinatesGui {
} }
private void onPingPosition(PlayerRawPosition position) { private void onPingPosition(PlayerRawPosition position) {
new PingOperation(fileStore, () -> position).run(); new PingPositionOperation(fileStore, () -> position).run();
}
private void onSaveConfigs() {
new SaveConfigsOperation(fileStore, keyBindConfiguration, configHandler::getState).run();
showDefaultView(null);
} }
private void showDefaultView(PlayerPosition position) { private void showDefaultView(PlayerPosition position) {
@ -72,23 +96,11 @@ public class SaveCoordinatesGui {
screenController.openScreen(this.listHandler.createView(null)); screenController.openScreen(this.listHandler.createView(null));
} }
// public void showConfigView() { private void showConfigView() {
// try {
// ConfigBuilder builder = ConfigBuilder.create() screenController.openScreen(this.configHandler.createView(fileStore.readConfigData()));
// .setParentScreen(this.screen) } catch (IOException e) {
// .setTitle(new LiteralText("Save Coordinates config")); e.printStackTrace();
// }
// 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());
// }
} }

View File

@ -5,15 +5,15 @@ import java.util.function.Supplier;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.model.PlayerPosition; import me.bionicbeanie.mods.savecoords.model.PlayerPosition;
class SaveOperation extends ViewOperationBase<PlayerPosition> { class SavePositionOperation extends ViewOperationBase<PlayerPosition> {
public SaveOperation(IFileStore fileStore, Supplier<PlayerPosition> stateSupplier) { public SavePositionOperation(IFileStore fileStore, Supplier<PlayerPosition> stateSupplier) {
super(fileStore, stateSupplier); super(fileStore, stateSupplier);
} }
@Override @Override
protected void executeOperation(IFileStore fileStore, PlayerPosition position) throws Exception { protected void executeOperation(IFileStore fileStore, PlayerPosition position) throws Exception {
fileStore.save(position); fileStore.writePosition(position);
fileStore.setDefaultWorld(position.getPositionMetadata().getWorldName()); fileStore.writeDefaultWorldName(position.getPositionMetadata().getWorldName());
} }
} }

View File

@ -0,0 +1,42 @@
package me.bionicbeanie.mods.savecoords.gui.impl;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
public class TexturedButton extends WButton {
private Identifier texture;
public TexturedButton(Text label) {
super(label);
}
public void setTexture(Identifier texture) {
this.texture = texture;
}
@Override
public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) {
super.paint(matrices, x, y, mouseX, mouseY);
if (texture != null) {
ScreenDrawing.texturedGuiRect(matrices, x + 1, y + 1, getWidth() - 2, getHeight() - 2, texture, -1, -1,
0xFFFFFF);
}
if (getLabel() != null) {
int color = 0xE0E0E0;
if (!isEnabled()) {
color = 0xA0A0A0;
}
int xOffset = (getIcon() != null && alignment == HorizontalAlignment.LEFT) ? 18 : 0;
ScreenDrawing.drawStringWithShadow(matrices, getLabel().asOrderedText(), alignment, x + xOffset,
y + ((20 - 8) / 2), width, color);
}
}
}

View File

@ -17,14 +17,19 @@ abstract class ViewHandlerBase<T> extends LightweightGuiDescription implements I
this.rootGridPanel = createRootPanel(); this.rootGridPanel = createRootPanel();
} }
protected abstract Supplier<T> placeWidgets(IRootPanel rootGridPanel, T state); protected abstract Supplier<T> setupView(IRootPanel rootGridPanel, T state);
protected Screen createScreen() {
return new SaveCoordinatesScreen(this);
}
@Override @Override
public Screen createView(T state) { public Screen createView(T state) {
rootGridPanel.reset(); rootGridPanel.reset();
this.stateSupplier = placeWidgets(rootGridPanel, state); this.stateSupplier = setupView(rootGridPanel, state);
rootGridPanel.validate(); rootGridPanel.validate();
return new SaveCoordinatesScreen(this);
return createScreen();
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package me.bionicbeanie.mods.savecoords.impl;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.IPlayerLocator; import me.bionicbeanie.mods.savecoords.IPlayerLocator;
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
public class Factory { public class Factory {
@ -13,4 +14,8 @@ public class Factory {
public static IPlayerLocator CreatePlayerLocator(MinecraftClient client) { public static IPlayerLocator CreatePlayerLocator(MinecraftClient client) {
return new PlayerLocator(client); return new PlayerLocator(client);
} }
public static IKeyBindConfiguration createKeyBindConfiguration(IFileStore fileStore) {
return new KeyBindConfiguration(fileStore);
}
} }

View File

@ -15,6 +15,7 @@ import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import me.bionicbeanie.mods.savecoords.IFileStore; import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import me.bionicbeanie.mods.savecoords.model.ModData; import me.bionicbeanie.mods.savecoords.model.ModData;
import me.bionicbeanie.mods.savecoords.model.PlayerPosition; import me.bionicbeanie.mods.savecoords.model.PlayerPosition;
@ -41,28 +42,44 @@ class FileStore implements IFileStore {
} }
@Override @Override
public String getDefaultWorld() throws IOException { public String readDefaultWorldName() throws IOException {
ModData data = load(); ModData data = load();
return data.defaultWorldName; return data.getDefaultWorldName();
} }
@Override @Override
public void setDefaultWorld(String defaultWorldName) throws IOException { public ConfigData readConfigData() throws IOException {
ModData data = load(); ModData data = load();
data.defaultWorldName = defaultWorldName;
return data.getConfigData();
}
@Override
public void writeDefaultWorldName(String defaultWorldName) throws IOException {
ModData data = load();
data.setDefaultWorldName(defaultWorldName);;
dump(data); dump(data);
} }
@Override @Override
public List<PlayerPosition> list() throws IOException { public void writeConfigs(ConfigData configData) throws IOException {
ModData data = load();
data.setConfigData(configData);
dump(data);
}
@Override
public List<PlayerPosition> listPositions() throws IOException {
ModData data = load(); ModData data = load();
List<PlayerPosition> playerPositionList = new LinkedList<>(); List<PlayerPosition> playerPositionList = new LinkedList<>();
if (data.positions != null) { if (data.getPositions() != null) {
for (int i = 0; i < data.positions.length; ++i) { for (int i = 0; i < data.getPositions().length; ++i) {
playerPositionList.add(data.positions[i]); playerPositionList.add(data.getPositions()[i]);
} }
} }
@ -70,8 +87,8 @@ class FileStore implements IFileStore {
} }
@Override @Override
public void save(PlayerPosition position) throws IOException { public void writePosition(PlayerPosition position) throws IOException {
List<PlayerPosition> playerPositions = list(); List<PlayerPosition> playerPositions = listPositions();
playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(position.getId(), p.getId())); playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(position.getId(), p.getId()));
playerPositions.add(position); playerPositions.add(position);
@ -79,8 +96,8 @@ class FileStore implements IFileStore {
} }
@Override @Override
public void delete(String id) throws IOException { public void deletePosition(String id) throws IOException {
List<PlayerPosition> playerPositions = list(); List<PlayerPosition> playerPositions = listPositions();
playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(id, p.getId())); playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(id, p.getId()));
savePositions(playerPositions); savePositions(playerPositions);
} }
@ -92,7 +109,7 @@ class FileStore implements IFileStore {
private void savePositions(List<PlayerPosition> playerPositions) throws IOException { private void savePositions(List<PlayerPosition> playerPositions) throws IOException {
ModData data = load(); ModData data = load();
data.positions = playerPositions.toArray(new PlayerPosition[playerPositions.size()]); data.setPositions(playerPositions.toArray(new PlayerPosition[playerPositions.size()]));
dump(data); dump(data);
} }
@ -103,8 +120,8 @@ class FileStore implements IFileStore {
} catch (Exception e) { } catch (Exception e) {
// Fallback for old versions // Fallback for old versions
ModData data = new ModData(); ModData data = new ModData();
data.defaultWorldName = ""; data.setDefaultWorldName("");
data.positions = gson.fromJson(String.join("", lines), PlayerPosition[].class); data.setPositions(gson.fromJson(String.join("", lines), PlayerPosition[].class));
dump(data); dump(data);

View File

@ -0,0 +1,56 @@
package me.bionicbeanie.mods.savecoords.impl;
import java.io.IOException;
import me.bionicbeanie.mods.savecoords.IFileStore;
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import net.minecraft.client.options.KeyBinding;
import net.minecraft.client.util.InputUtil;
class KeyBindConfiguration implements IKeyBindConfiguration {
KeyBinding defaultKeyBinding;
KeyBindConfiguration(IFileStore fileStore) {
try {
defaultKeyBinding = createDefaultKeyBinding(fileStore);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
@Override
public KeyBinding getDefaultKeyBinding() {
return defaultKeyBinding;
}
@Override
public void setDefaultKeyBinding(int keyCode) {
defaultKeyBinding = createKeyBinding(keyCode);
}
private KeyBinding createDefaultKeyBinding(IFileStore fileStore) throws IOException {
ConfigData configData = fileStore.readConfigData();
if (configData == null) {
configData = new ConfigData();
}
int keyBind = configData.getDefaultKeyBindingCode();
return createKeyBinding(keyBind);
}
private KeyBinding createKeyBinding(int keyBind) {
String translationKey = "key.savecoords.coords";
String category = "category.savecoords.generic";
KeyBinding keyBinding = new KeyBinding(translationKey, InputUtil.Type.KEYSYM, keyBind, category);
return keyBinding;
}
}

View File

@ -0,0 +1,20 @@
package me.bionicbeanie.mods.savecoords.model;
import org.lwjgl.glfw.GLFW;
public class ConfigData {
private int defaultKeyBindingCode;
public ConfigData() {
this.defaultKeyBindingCode = GLFW.GLFW_KEY_H; //Default value
}
public void setDefaultKeyBindingCode(int defaultKeyBindingCode) {
this.defaultKeyBindingCode = defaultKeyBindingCode;
}
public int getDefaultKeyBindingCode() {
return defaultKeyBindingCode;
}
}

View File

@ -2,7 +2,33 @@ package me.bionicbeanie.mods.savecoords.model;
public class ModData { public class ModData {
public String defaultWorldName; private String defaultWorldName;
public PlayerPosition[] positions; private PlayerPosition[] positions;
private ConfigData configData;
public String getDefaultWorldName() {
return defaultWorldName;
}
public void setDefaultWorldName(String defaultWorldName) {
this.defaultWorldName = defaultWorldName;
}
public PlayerPosition[] getPositions() {
return positions;
}
public void setPositions(PlayerPosition[] positions) {
this.positions = positions;
}
public ConfigData getConfigData() {
return configData;
}
public void setConfigData(ConfigData configData) {
this.configData = configData;
}
} }

View File

@ -39,7 +39,7 @@ public class ResourceUtils {
return new TextureIcon(CreateIdentifier("more")); return new TextureIcon(CreateIdentifier("more"));
} }
private static Identifier CreateIdentifier(String file) { public static Identifier CreateIdentifier(String file) {
return new Identifier("savecoords", "textures/gui/" + file + ".png"); return new Identifier("savecoords", "textures/gui/" + file + ".png");
} }
} }

View File

@ -19,7 +19,7 @@
"environment": "*", "environment": "*",
"entrypoints": { "entrypoints": {
"client": [ "client": [
"me.bionicbeanie.mods.SaveCoordinatesClient" "me.bionicbeanie.mods.savecoords.SaveCoordinatesClient"
] ]
}, },
"mixins": [ "mixins": [