Skip to content

Commit 87016dd

Browse files
authored
[Savestates] Rewrite Savestate-Indexer (#270)
- Add renaming and date functionality to Savestates - Update folder structure to group savestates by World
2 parents 5d14f86 + 8f25e6a commit 87016dd

File tree

62 files changed

+2915
-1192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2915
-1192
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ [email protected]
1616
# TASmod properties
1717
group=com.minecrafttas
1818
artifact=TASmod-1.12.2
19-
version=Beta1.2
19+
version=Beta2
2020
release=false

src/main/java/com/minecrafttas/mctcommon/Configuration.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.minecrafttas.mctcommon;
22

3+
import java.io.IOException;
34
import java.nio.file.Files;
45
import java.nio.file.Path;
56
import java.util.Properties;
@@ -23,16 +24,37 @@ public Configuration(String comment, Path configFile, ConfigurationRegistry regi
2324
}
2425

2526
@Override
26-
public void loadFromXML() {
27+
public void load() {
28+
2729
if (Files.exists(file)) {
28-
loadFromXML(file);
30+
String in = null;
31+
try {
32+
in = readFile(file);
33+
} catch (IOException e) {
34+
MCTCommon.LOGGER.catching(e);
35+
return;
36+
}
37+
38+
if (in.startsWith("<?xml")) {
39+
MCTCommon.LOGGER.warn("Converting xml config to json");
40+
loadFromXML();
41+
return;
42+
}
43+
44+
loadFromJson(file);
2945
}
46+
3047
if (properties == null || !Files.exists(file)) {
3148
properties = generateDefault();
32-
saveToXML();
49+
saveToJson();
3350
}
3451
}
3552

53+
@Override
54+
public void save() {
55+
super.saveToJson();
56+
}
57+
3658
/**
3759
* Generates the default property list from the values provided in {@link #registry}
3860
* @return The default property list
@@ -67,7 +89,7 @@ public void set(ConfigOptions configOption, String value) {
6789
throw new NullPointerException("Config needs to be loaded first, before trying to set a value");
6890
}
6991
properties.setProperty(configOption.getConfigKey(), value);
70-
saveToXML();
92+
save();
7193
}
7294

7395
public void set(ConfigOptions configOption, int value) {
@@ -86,6 +108,6 @@ public void reset(ConfigOptions configOption) {
86108

87109
public void delete(ConfigOptions configOption) {
88110
properties.remove(configOption);
89-
saveToXML();
111+
saveToJson();
90112
}
91113
}
Lines changed: 73 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.minecrafttas.mctcommon;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
3+
import java.util.HashMap;
4+
import java.util.Map;
55

66
import org.apache.commons.lang3.ArrayUtils;
77

@@ -21,47 +21,7 @@ public class KeybindManager implements EventClientGameLoop {
2121

2222
private final IsKeyDownFunc defaultFunction;
2323

24-
public static class Keybind {
25-
26-
public final KeyBinding vanillaKeyBinding;
27-
private final String category;
28-
private final Runnable onKeyDown;
29-
private final IsKeyDownFunc isKeyDownFunc;
30-
31-
/**
32-
* Initialize keybind
33-
*
34-
* @param name Name of keybind
35-
* @param category Category of keybind
36-
* @param defaultKey Default key of keybind
37-
* @param onKeyDown Will be run when the keybind is pressed
38-
*/
39-
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown) {
40-
this(name, category, defaultKey, onKeyDown, null);
41-
}
42-
43-
/**
44-
* Initialize keybind with a different "isKeyDown" method
45-
*
46-
* @param name Name of keybind
47-
* @param category Category of keybind
48-
* @param defaultKey Default key of keybind
49-
* @param onKeyDown Will be run when the keybind is pressed
50-
*/
51-
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown, IsKeyDownFunc func) {
52-
this.vanillaKeyBinding = new KeyBinding(name, defaultKey, category);
53-
this.category = category;
54-
this.onKeyDown = onKeyDown;
55-
this.isKeyDownFunc = func;
56-
}
57-
58-
@Override
59-
public String toString() {
60-
return this.vanillaKeyBinding.getKeyDescription();
61-
}
62-
}
63-
64-
private List<Keybind> keybindings;
24+
private Map<KeybindID, Keybind> keybindings;
6525

6626
/**
6727
* Initialize keybind manage
@@ -71,15 +31,15 @@ public String toString() {
7131
*/
7232
public KeybindManager(IsKeyDownFunc defaultFunction) {
7333
this.defaultFunction = defaultFunction;
74-
this.keybindings = new ArrayList<>();
34+
this.keybindings = new HashMap<>();
7535
}
7636

7737
/**
7838
* Handle registered keybindings on game loop
7939
*/
8040
@Override
8141
public void onRunClientGameLoop(Minecraft mc) {
82-
for (Keybind keybind : this.keybindings) {
42+
for (Keybind keybind : this.keybindings.values()) {
8343
IsKeyDownFunc keyDown = keybind.isKeyDownFunc != null ? keybind.isKeyDownFunc : defaultFunction;
8444
if (keyDown.isKeyDown(keybind.vanillaKeyBinding)) {
8545
keybind.onKeyDown.run();
@@ -88,26 +48,87 @@ public void onRunClientGameLoop(Minecraft mc) {
8848

8949
}
9050

51+
public void registerKeybinds(GameSettings options, Class<? extends KeybindID> keybindIDclass) {
52+
if (keybindIDclass.isEnum())
53+
registerKeybinds(options, keybindIDclass.getEnumConstants());
54+
}
55+
56+
public void registerKeybinds(GameSettings options, KeybindID... keybind) {
57+
for (KeybindID keybindEnum : keybind) {
58+
registerKeybind(options, keybindEnum, keybindEnum.getKeybind());
59+
}
60+
}
61+
9162
/**
92-
* Register new keybind
63+
* Register a new keybind
9364
*
94-
* @param keybind Keybind to register
95-
* @param options
65+
* @param keybindID The {@link KeybindID} to register this underI
66+
* @param keybind The {@link Keybind} to register
9667
*/
97-
public void registerKeybind(Keybind keybind, GameSettings options) {
98-
this.keybindings.add(keybind);
68+
public void registerKeybind(GameSettings options, KeybindID keybindID, Keybind keybind) {
69+
this.keybindings.put(keybindID, keybind);
9970
KeyBinding keyBinding = keybind.vanillaKeyBinding;
10071

101-
if (!AccessorKeyBinding.getCategoryOrder().containsKey(keybind.category))
102-
AccessorKeyBinding.getCategoryOrder().put(keybind.category, AccessorKeyBinding.getCategoryOrder().size() + 1);
72+
Map<String, Integer> categoryOrder = AccessorKeyBinding.getCategoryOrder();
73+
74+
if (!categoryOrder.containsKey(keybind.category))
75+
categoryOrder.put(keybind.category, categoryOrder.size() + 1);
10376

10477
// add keybinding
10578
options.keyBindings = ArrayUtils.add(options.keyBindings, keyBinding);
10679
}
10780

81+
public Keybind getKeybind(KeybindID id) {
82+
return keybindings.get(id);
83+
}
84+
10885
@FunctionalInterface
10986
public static interface IsKeyDownFunc {
11087

11188
public boolean isKeyDown(KeyBinding keybind);
11289
}
90+
91+
public static interface KeybindID {
92+
public Keybind getKeybind();
93+
}
94+
95+
public static class Keybind {
96+
97+
public final KeyBinding vanillaKeyBinding;
98+
private final String category;
99+
private final Runnable onKeyDown;
100+
private final IsKeyDownFunc isKeyDownFunc;
101+
102+
/**
103+
* Initialize keybind
104+
*
105+
* @param name Name of keybind
106+
* @param category Category of keybind
107+
* @param defaultKey Default key of keybind
108+
* @param onKeyDown Will be run when the keybind is pressed
109+
*/
110+
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown) {
111+
this(name, category, defaultKey, onKeyDown, null);
112+
}
113+
114+
/**
115+
* Initialize keybind with a different "isKeyDown" method
116+
*
117+
* @param name Name of keybind
118+
* @param category Category of keybind
119+
* @param defaultKey Default key of keybind
120+
* @param onKeyDown Will be run when the keybind is pressed
121+
*/
122+
public Keybind(String name, String category, int defaultKey, Runnable onKeyDown, IsKeyDownFunc func) {
123+
this.vanillaKeyBinding = new KeyBinding(name, defaultKey, category);
124+
this.category = category;
125+
this.onKeyDown = onKeyDown;
126+
this.isKeyDownFunc = func;
127+
}
128+
129+
@Override
130+
public String toString() {
131+
return this.vanillaKeyBinding.getKeyDescription();
132+
}
133+
}
113134
}

src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase>
255255
if (newReturnValue != null)
256256
returnValue = newReturnValue;
257257
} catch (IllegalAccessException | InvocationTargetException e) {
258-
throw new EventException(eventClass, e);
258+
throw new EventException(eventClass, e.getCause());
259259
} catch (IllegalArgumentException e) {
260260
throw new EventException(String.format("Event fired with the wrong number of parameters. Expected: %s, Actual: %s", method.getParameterCount(), eventParams.length), eventClass, e);
261261
}

0 commit comments

Comments
 (0)