Add configs
This commit is contained in:
parent
e2d40032c1
commit
30ee79b7c0
@ -15,10 +15,6 @@ repositories {
|
||||
name = "CottonMC"
|
||||
url = "https://server.bbkr.space/artifactory/libs-release"
|
||||
}
|
||||
maven {
|
||||
name = "ClothConfig"
|
||||
url = "https://maven.shedaniel.me/"
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -29,7 +25,6 @@ dependencies {
|
||||
|
||||
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 {
|
||||
|
||||
@ -3,16 +3,21 @@ package me.bionicbeanie.mods.savecoords;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import me.bionicbeanie.mods.savecoords.model.ConfigData;
|
||||
import me.bionicbeanie.mods.savecoords.model.PlayerPosition;
|
||||
|
||||
public interface IFileStore {
|
||||
String getDefaultWorld() throws IOException;
|
||||
String readDefaultWorldName() throws IOException;
|
||||
|
||||
ConfigData readConfigData() throws IOException;
|
||||
|
||||
void setDefaultWorld(String defaultWorldName) throws IOException;
|
||||
void writeDefaultWorldName(String defaultWorldName) throws IOException;
|
||||
|
||||
void save(PlayerPosition position) throws IOException;
|
||||
void writePosition(PlayerPosition position) throws IOException;
|
||||
|
||||
void writeConfigs(ConfigData configData) throws IOException;
|
||||
|
||||
void delete(String id) throws IOException;
|
||||
void deletePosition(String id) throws IOException;
|
||||
|
||||
List<PlayerPosition> list() throws IOException;
|
||||
List<PlayerPosition> listPositions() throws IOException;
|
||||
}
|
||||
|
||||
@ -1,35 +1,25 @@
|
||||
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.impl.Factory;
|
||||
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;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
|
||||
public class SaveCoordinatesClient implements ClientModInitializer {
|
||||
|
||||
@Override
|
||||
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 -> {
|
||||
while (keyBinding.wasPressed()) {
|
||||
ModGui.start(client);
|
||||
while (keyBindConfiguration.getDefaultKeyBinding().wasPressed()) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -39,7 +39,7 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<PlayerPosition> placeWidgets(IRootPanel root, PlayerPosition existingPosition) {
|
||||
public Supplier<PlayerPosition> setupView(IRootPanel root, PlayerPosition existingPosition) {
|
||||
|
||||
PlayerRawPosition rawPosition = existingPosition == null ? locator.locate() : existingPosition;
|
||||
|
||||
@ -134,7 +134,7 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
|
||||
}
|
||||
|
||||
try {
|
||||
return fileStore.getDefaultWorld();
|
||||
return fileStore.readDefaultWorldName();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -186,8 +186,6 @@ class DefaultViewHandler extends ViewHandlerBase<PlayerPosition> {
|
||||
}
|
||||
|
||||
private WButton CreateButton(String name) {
|
||||
WButton button = new WButton(new LiteralText(name));
|
||||
|
||||
return button;
|
||||
return new WButton(new LiteralText(name));
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,14 +4,14 @@ import java.util.function.Supplier;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeOperation(IFileStore fileStore, String positionId) throws Exception {
|
||||
fileStore.delete(positionId);
|
||||
fileStore.deletePosition(positionId);
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Supplier<Void> placeWidgets(IRootPanel root, Void nullState) {
|
||||
public Supplier<Void> setupView(IRootPanel root, Void nullState) {
|
||||
|
||||
List<PlayerPosition> positions = getPositions(fileStore);
|
||||
WListPanel<PlayerPosition, CoordinatesListItemPanel> listPanel = createListPane(positions);
|
||||
@ -77,7 +77,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
|
||||
|
||||
private List<PlayerPosition> getPositions(IFileStore fileStore) {
|
||||
try {
|
||||
List<PlayerPosition> positions = fileStore.list();
|
||||
List<PlayerPosition> positions = fileStore.listPositions();
|
||||
Collections.sort(positions, (p1, p2) -> p2.getPositionMetadata().getLastModified()
|
||||
.compareTo(p1.getPositionMetadata().getLastModified()));
|
||||
return positions;
|
||||
@ -111,7 +111,7 @@ class ListViewHandler extends ViewHandlerBase<Void> {
|
||||
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.deleteButton = createDeleteButton();
|
||||
this.pingButton = 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);
|
||||
}
|
||||
|
||||
private WButton createDeleteButton() {
|
||||
TexturedButton button = new TexturedButton(new LiteralText("x"));
|
||||
button.setTexture(ResourceUtils.CreateIdentifier("close"));
|
||||
|
||||
return button;
|
||||
}
|
||||
|
||||
void setPosition(PlayerPosition position, IFileStore fileStore) {
|
||||
this.icon.setImage(ResourceUtils.CreateWorldIconIdentifier(position.getWorldDimension()));
|
||||
this.location.setText(new LiteralText(position.getLocationName()));
|
||||
|
||||
@ -3,16 +3,16 @@ 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.IKeyBindConfiguration;
|
||||
import me.bionicbeanie.mods.savecoords.impl.Factory;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
|
||||
public class ModGui {
|
||||
|
||||
public static void start(MinecraftClient client) {
|
||||
public static void start(MinecraftClient client, IFileStore fileStore, IKeyBindConfiguration keyBindConfiguration) {
|
||||
IGuiController controller = new GuiController(client);
|
||||
IPlayerLocator locator = Factory.CreatePlayerLocator(client);
|
||||
IFileStore fileStore = Factory.createFileStore(client.runDirectory.getAbsolutePath());
|
||||
|
||||
new SaveCoordinatesGui(fileStore, locator, controller);
|
||||
new SaveCoordinatesGui(fileStore, locator, keyBindConfiguration, controller);
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,9 +6,9 @@ import me.bionicbeanie.mods.savecoords.IFileStore;
|
||||
import me.bionicbeanie.mods.savecoords.model.PlayerRawPosition;
|
||||
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);
|
||||
}
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,13 @@
|
||||
package me.bionicbeanie.mods.savecoords.gui.impl;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
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.IKeyBindConfiguration;
|
||||
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.PlayerRawPosition;
|
||||
|
||||
@ -13,15 +17,20 @@ public class SaveCoordinatesGui {
|
||||
private IFileStore fileStore;
|
||||
private IViewHandler<PlayerPosition> defaultHandler;
|
||||
private IViewHandler<Void> listHandler;
|
||||
private IViewHandler<ConfigData> configHandler;
|
||||
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.fileStore = fileStore;
|
||||
this.keyBindConfiguration = keyBindConfiguration;
|
||||
this.locator = locator;
|
||||
|
||||
this.defaultHandler = CreateDefaultViewHandler();
|
||||
this.listHandler = CreateListViewHandler();
|
||||
this.configHandler = CreateConfigHandler();
|
||||
|
||||
showDefaultView(null);
|
||||
}
|
||||
@ -29,10 +38,11 @@ public class SaveCoordinatesGui {
|
||||
private IViewHandler<PlayerPosition> 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));
|
||||
handler.onConfig(this::showConfigView);
|
||||
handler.onPing(new PingPositionOperation(fileStore, locator::locate));
|
||||
handler.onClose(screenController::closeScreen);
|
||||
|
||||
return handler;
|
||||
}
|
||||
@ -40,19 +50,28 @@ public class SaveCoordinatesGui {
|
||||
private IViewHandler<Void> CreateListViewHandler() {
|
||||
ListViewHandler handler = new ListViewHandler(fileStore, this::onDeletePosition, this::onEditPosition,
|
||||
this::onPingPosition);
|
||||
|
||||
|
||||
handler.onBack(() -> showDefaultView(null));
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
private IViewHandler<ConfigData> CreateConfigHandler() {
|
||||
ConfigViewHandler handler = new ConfigViewHandler();
|
||||
|
||||
handler.onBack(() -> showDefaultView(null));
|
||||
handler.onSave(this::onSaveConfigs);
|
||||
|
||||
return handler;
|
||||
}
|
||||
|
||||
private void onSavePosition() {
|
||||
new SaveOperation(fileStore, defaultHandler::getState).run();
|
||||
new SavePositionOperation(fileStore, defaultHandler::getState).run();
|
||||
showListView();
|
||||
}
|
||||
|
||||
private void onDeletePosition(PlayerPosition position) {
|
||||
new DeleteOperation(fileStore, () -> position.getId()).run();
|
||||
new DeletePositionOperation(fileStore, () -> position.getId()).run();
|
||||
showListView();
|
||||
}
|
||||
|
||||
@ -61,9 +80,14 @@ public class SaveCoordinatesGui {
|
||||
}
|
||||
|
||||
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) {
|
||||
screenController.openScreen(this.defaultHandler.createView(position));
|
||||
}
|
||||
@ -72,23 +96,11 @@ public class SaveCoordinatesGui {
|
||||
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());
|
||||
// }
|
||||
|
||||
private void showConfigView() {
|
||||
try {
|
||||
screenController.openScreen(this.configHandler.createView(fileStore.readConfigData()));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,15 +5,15 @@ import java.util.function.Supplier;
|
||||
import me.bionicbeanie.mods.savecoords.IFileStore;
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executeOperation(IFileStore fileStore, PlayerPosition position) throws Exception {
|
||||
fileStore.save(position);
|
||||
fileStore.setDefaultWorld(position.getPositionMetadata().getWorldName());
|
||||
fileStore.writePosition(position);
|
||||
fileStore.writeDefaultWorldName(position.getPositionMetadata().getWorldName());
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -17,16 +17,21 @@ abstract class ViewHandlerBase<T> extends LightweightGuiDescription implements I
|
||||
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
|
||||
public Screen createView(T state) {
|
||||
rootGridPanel.reset();
|
||||
this.stateSupplier = placeWidgets(rootGridPanel, state);
|
||||
this.stateSupplier = setupView(rootGridPanel, state);
|
||||
rootGridPanel.validate();
|
||||
return new SaveCoordinatesScreen(this);
|
||||
|
||||
return createScreen();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public T getState() {
|
||||
return stateSupplier.get();
|
||||
|
||||
@ -2,6 +2,7 @@ package me.bionicbeanie.mods.savecoords.impl;
|
||||
|
||||
import me.bionicbeanie.mods.savecoords.IFileStore;
|
||||
import me.bionicbeanie.mods.savecoords.IPlayerLocator;
|
||||
import me.bionicbeanie.mods.savecoords.gui.IKeyBindConfiguration;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
|
||||
public class Factory {
|
||||
@ -13,4 +14,8 @@ public class Factory {
|
||||
public static IPlayerLocator CreatePlayerLocator(MinecraftClient client) {
|
||||
return new PlayerLocator(client);
|
||||
}
|
||||
|
||||
public static IKeyBindConfiguration createKeyBindConfiguration(IFileStore fileStore) {
|
||||
return new KeyBindConfiguration(fileStore);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
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.PlayerPosition;
|
||||
|
||||
@ -41,28 +42,44 @@ class FileStore implements IFileStore {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefaultWorld() throws IOException {
|
||||
public String readDefaultWorldName() throws IOException {
|
||||
ModData data = load();
|
||||
|
||||
return data.defaultWorldName;
|
||||
return data.getDefaultWorldName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConfigData readConfigData() throws IOException {
|
||||
ModData data = load();
|
||||
|
||||
return data.getConfigData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDefaultWorld(String defaultWorldName) throws IOException {
|
||||
ModData data = load();
|
||||
data.defaultWorldName = defaultWorldName;
|
||||
|
||||
@Override
|
||||
public void writeDefaultWorldName(String defaultWorldName) throws IOException {
|
||||
ModData data = load();
|
||||
data.setDefaultWorldName(defaultWorldName);;
|
||||
|
||||
dump(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeConfigs(ConfigData configData) throws IOException {
|
||||
ModData data = load();
|
||||
data.setConfigData(configData);
|
||||
|
||||
dump(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PlayerPosition> list() throws IOException {
|
||||
public List<PlayerPosition> listPositions() throws IOException {
|
||||
ModData data = load();
|
||||
List<PlayerPosition> playerPositionList = new LinkedList<>();
|
||||
|
||||
if (data.positions != null) {
|
||||
for (int i = 0; i < data.positions.length; ++i) {
|
||||
playerPositionList.add(data.positions[i]);
|
||||
if (data.getPositions() != null) {
|
||||
for (int i = 0; i < data.getPositions().length; ++i) {
|
||||
playerPositionList.add(data.getPositions()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,8 +87,8 @@ class FileStore implements IFileStore {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(PlayerPosition position) throws IOException {
|
||||
List<PlayerPosition> playerPositions = list();
|
||||
public void writePosition(PlayerPosition position) throws IOException {
|
||||
List<PlayerPosition> playerPositions = listPositions();
|
||||
playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(position.getId(), p.getId()));
|
||||
playerPositions.add(position);
|
||||
|
||||
@ -79,8 +96,8 @@ class FileStore implements IFileStore {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String id) throws IOException {
|
||||
List<PlayerPosition> playerPositions = list();
|
||||
public void deletePosition(String id) throws IOException {
|
||||
List<PlayerPosition> playerPositions = listPositions();
|
||||
playerPositions.removeIf(p -> StringUtils.equalsIgnoreCase(id, p.getId()));
|
||||
savePositions(playerPositions);
|
||||
}
|
||||
@ -92,7 +109,7 @@ class FileStore implements IFileStore {
|
||||
|
||||
private void savePositions(List<PlayerPosition> playerPositions) throws IOException {
|
||||
ModData data = load();
|
||||
data.positions = playerPositions.toArray(new PlayerPosition[playerPositions.size()]);
|
||||
data.setPositions(playerPositions.toArray(new PlayerPosition[playerPositions.size()]));
|
||||
dump(data);
|
||||
}
|
||||
|
||||
@ -103,8 +120,8 @@ class FileStore implements IFileStore {
|
||||
} catch (Exception e) {
|
||||
// Fallback for old versions
|
||||
ModData data = new ModData();
|
||||
data.defaultWorldName = "";
|
||||
data.positions = gson.fromJson(String.join("", lines), PlayerPosition[].class);
|
||||
data.setDefaultWorldName("");
|
||||
data.setPositions(gson.fromJson(String.join("", lines), PlayerPosition[].class));
|
||||
|
||||
dump(data);
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,33 @@ package me.bionicbeanie.mods.savecoords.model;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -39,7 +39,7 @@ public class ResourceUtils {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"client": [
|
||||
"me.bionicbeanie.mods.SaveCoordinatesClient"
|
||||
"me.bionicbeanie.mods.savecoords.SaveCoordinatesClient"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user