Fix back-compat bugs

This commit is contained in:
Surya 2021-06-05 12:02:59 +05:30
parent 02e2916ce1
commit b51b9828ac
4 changed files with 64 additions and 32 deletions

View File

@ -9,6 +9,7 @@ import me.bionicbeanie.mods.savecoords.IKeyBinds;
import me.bionicbeanie.mods.savecoords.IKeyBinds.IKeyBinding;
import me.bionicbeanie.mods.savecoords.model.ConfigData;
import me.bionicbeanie.mods.savecoords.model.ConfigData.Config;
import net.minecraft.client.util.InputUtil.Type;
public class SaveConfigsOperation extends ViewOperationBase<List<IKeyBinding>>{
@ -29,7 +30,7 @@ public class SaveConfigsOperation extends ViewOperationBase<List<IKeyBinding>>{
for (IKeyBinding binding : state) {
Config config = new Config();
config.setName(binding.getName());
config.setType(binding.getType());
config.setType(parseTypeInt(binding.getType()));
config.setCode(binding.getCode());
keyBinds.updateKeyBind(binding.getName(), binding.getType(), binding.getCode());
@ -41,4 +42,12 @@ public class SaveConfigsOperation extends ViewOperationBase<List<IKeyBinding>>{
fileStore.writeConfigs(configData);
}
private int parseTypeInt(Type type) {
if(type == Type.KEYSYM) {
return 0;
}
return 1;
}
}

View File

@ -44,31 +44,31 @@ class FileStore implements IFileStore {
@Override
public String readDefaultWorldName() throws IOException {
ModData data = load();
return data.getDefaultWorldName();
}
@Override
public ConfigData readConfigData() throws IOException {
ModData data = load();
return data.getConfigData();
}
@Override
public void writeDefaultWorldName(String defaultWorldName) throws IOException {
ModData data = load();
data.setDefaultWorldName(defaultWorldName);;
data.setDefaultWorldName(defaultWorldName);
;
dump(data);
}
@Override
public void writeConfigs(ConfigData configData) throws IOException {
ModData data = load();
data.setConfigData(configData);
dump(data);
}
@ -116,26 +116,30 @@ class FileStore implements IFileStore {
private ModData load() throws IOException {
List<String> lines = Files.readAllLines(saveFilePath);
try {
ModData data = gson.fromJson(String.join("", lines), ModData.class);
if(data == null) {
ModData data = gson.fromJson(String.join("", lines), ModData.class);
if (data == null) {
data = new ModData();
}
if(data.getDefaultWorldName() == null) {
if (data.getDefaultWorldName() == null) {
data.setDefaultWorldName("");
}
if(data.getPositions() == null) {
if (data.getPositions() == null) {
data.setPositions(new PlayerPosition[] {});
}
if(data.getConfigData() == null) {
if (data.getConfigData() == null) {
data.setConfigData(new ConfigData());
}
if (data.getConfigData().getKeyConfigs() == null) {
data.getConfigData().setKeyConfigs(new ConfigData.Config[] {});
}
return data;
} catch (Exception e) {
// Fallback for old versions
ModData data = new ModData();

View File

@ -94,12 +94,10 @@ class KeyBinds implements IKeyBinds {
public int getDefaultCode() {
return defaultCode;
}
public void setBoundKey(Type type, int code) {
super.setBoundKey(type.createFromCode(code));
this.type = type;
this.code = code;
@Override
public boolean wasPressed() {
return super.wasPressed();
}
@Override
@ -116,11 +114,24 @@ class KeyBinds implements IKeyBinds {
public Text getNameLocalizedText() {
return new TranslatableText(getTranslationKey());
}
@Override
public Text getBoundKeyLocalizedText() {
return super.getBoundKeyLocalizedText();
}
void setBoundKey(Type type, int code) {
super.setBoundKey(type.createFromCode(code));
this.type = type;
this.code = code;
}
}
private void initialize() {
try {
ConfigData configData = fileStore.readConfigData();
Config[] configs = configData.getKeyConfigs();
if (configs == null) {
@ -129,7 +140,7 @@ class KeyBinds implements IKeyBinds {
for (int i = 0; i < configs.length; ++i) {
String name = configs[i].getName();
Type type = configs[i].getType();
Type type = parseType(configs[i].getType());
int code = configs[i].getCode();
if (keyBinds.containsKey(name)) {
@ -142,4 +153,12 @@ class KeyBinds implements IKeyBinds {
}
private Type parseType(int type) {
if(type == 0) {
return Type.KEYSYM;
}
return Type.MOUSE;
}
}

View File

@ -2,8 +2,6 @@ package me.bionicbeanie.mods.savecoords.model;
import org.lwjgl.glfw.GLFW;
import net.minecraft.client.util.InputUtil.Type;
public class ConfigData {
private Config[] keyConfigs;
@ -12,13 +10,15 @@ public class ConfigData {
private int defaultKeyBindingCode;
public ConfigData() {
this.defaultKeyBindingCode = GLFW.GLFW_KEY_H; //Default value
this.defaultKeyBindingCode = GLFW.GLFW_KEY_H;
}
@Deprecated
public void setDefaultKeyBindingCode(int defaultKeyBindingCode) {
this.defaultKeyBindingCode = defaultKeyBindingCode;
}
@Deprecated
public int getDefaultKeyBindingCode() {
return defaultKeyBindingCode;
}
@ -33,7 +33,7 @@ public class ConfigData {
public static class Config{
private String name;
private Type type;
private int type;
private int code;
public int getCode() {
@ -42,10 +42,10 @@ public class ConfigData {
public void setCode(int code) {
this.code = code;
}
public Type getType() {
public int getType() {
return type;
}
public void setType(Type type) {
public void setType(int type) {
this.type = type;
}
public String getName() {