Skip to content

Commit 4601a2d

Browse files
committed
[FileCommands] More abstraction and bugfixes
- Added default serialisation fixes - Fixed #231 - Fixed multiple inline FileCommands failing - Fixed loading FileCommands not clearing the previous FileCommands
1 parent f96b966 commit 4601a2d

File tree

8 files changed

+103
-86
lines changed

8 files changed

+103
-86
lines changed

src/main/java/com/minecrafttas/tasmod/events/EventPlaybackClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ public interface EventRecordClear extends EventBase {
8282
/**
8383
* Fired when a recording is cleared
8484
*/
85-
public void onClear();
85+
public void onRecordingClear();
8686
}
8787
}

src/main/java/com/minecrafttas/tasmod/playback/filecommands/PlaybackFileCommand.java

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,32 @@ public String toString() {
5454
return String.format("$%s(%s);", name, String.join(", ", args));
5555
}
5656

57+
/**
58+
* <p>Abstract class for a FileCommandExtension.
59+
* <p>Allows for creating custom FileCommands that can be stored within the TASfile<br>
60+
* to trigger custom behaviour when a playback is reaching that point
61+
62+
* @author Scribble
63+
*/
5764
public static abstract class PlaybackFileCommandExtension implements Registerable {
58-
65+
/**
66+
* The temporary directory of the {@link #fileCommandStorage}
67+
*/
5968
protected final Path tempDir;
6069

70+
/**
71+
* The list where all filecommands for this extension are stored
72+
*/
73+
protected BigArrayList<SortedFileCommandContainer> inlineFileCommandStorage;
74+
75+
/**
76+
* The list where all filecommands for this extension are stored
77+
*/
78+
protected BigArrayList<SortedFileCommandContainer> endlineFileCommandStorage;
79+
80+
/**
81+
* Creates a new extension with the default {@link #tempDir}
82+
*/
6183
public PlaybackFileCommandExtension() {
6284
this((Path) null);
6385
}
@@ -75,6 +97,8 @@ public PlaybackFileCommandExtension(String tempFolderName) {
7597
public PlaybackFileCommandExtension(Path tempDirectory) {
7698
if (tempDirectory == null) {
7799
tempDir = null;
100+
inlineFileCommandStorage = new BigArrayList<>();
101+
endlineFileCommandStorage = new BigArrayList<>();
78102
return;
79103
}
80104

@@ -84,6 +108,8 @@ public PlaybackFileCommandExtension(Path tempDirectory) {
84108
} catch (IOException e) {
85109
e.printStackTrace();
86110
}
111+
inlineFileCommandStorage = new BigArrayList<>(tempDir.toString());
112+
endlineFileCommandStorage = new BigArrayList<>(tempDir.toString());
87113
}
88114

89115
protected boolean enabled = false;
@@ -97,6 +123,14 @@ public void onDisable() {
97123
};
98124

99125
public void onClear() {
126+
try {
127+
inlineFileCommandStorage.clearMemory();
128+
endlineFileCommandStorage.clearMemory();
129+
} catch (IOException e) {
130+
e.printStackTrace();
131+
}
132+
inlineFileCommandStorage = new BigArrayList<>();
133+
endlineFileCommandStorage = new BigArrayList<>();
100134
};
101135

102136
public void onRecord(long tick, InputContainer inputContainer) {
@@ -106,17 +140,49 @@ public void onPlayback(long tick, InputContainer inputContainer) {
106140
};
107141

108142
public SortedFileCommandContainer onSerialiseInlineComment(long tick, InputContainer inputContainer) {
109-
return null;
143+
SortedFileCommandContainer out = new SortedFileCommandContainer();
144+
if (tick >= inlineFileCommandStorage.size())
145+
return out;
146+
147+
SortedFileCommandContainer currentTick = inlineFileCommandStorage.get(tick);
148+
if (currentTick == null)
149+
return out;
150+
151+
for (String name : getFileCommandNames()) {
152+
if (currentTick.get(name) != null)
153+
out.putAll(currentTick.split(name));
154+
}
155+
return out;
110156
}
111157

112-
public SortedFileCommandContainer onSerialiseEndlineComment(long currentTick, InputContainer inputContainer) {
113-
return null;
158+
public SortedFileCommandContainer onSerialiseEndlineComment(long tick, InputContainer inputContainer) {
159+
SortedFileCommandContainer out = new SortedFileCommandContainer();
160+
if (tick >= endlineFileCommandStorage.size())
161+
return out;
162+
163+
SortedFileCommandContainer currentTick = endlineFileCommandStorage.get(tick);
164+
if (currentTick == null)
165+
return out;
166+
167+
for (String name : getFileCommandNames()) {
168+
if (currentTick.get(name) != null)
169+
out.putAll(currentTick.split(name));
170+
}
171+
return out;
114172
}
115173

116174
public void onDeserialiseInlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
175+
if (fileCommandContainer == null)
176+
return;
177+
178+
inlineFileCommandStorage.add(fileCommandContainer);
117179
}
118180

119181
public void onDeserialiseEndlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
182+
if (fileCommandContainer == null)
183+
return;
184+
185+
endlineFileCommandStorage.add(fileCommandContainer);
120186
}
121187

122188
public boolean isEnabled() {
@@ -331,7 +397,8 @@ public SortedFileCommandContainer split(String... keys) {
331397
public SortedFileCommandContainer split(Iterable<String> keys) {
332398
SortedFileCommandContainer out = new SortedFileCommandContainer();
333399
for (String key : keys) {
334-
out.put(key, this.get(key));
400+
if (this.containsKey(key))
401+
out.put(key, this.get(key));
335402
}
336403
return out;
337404
}

src/main/java/com/minecrafttas/tasmod/playback/filecommands/PlaybackFileCommandsRegistry.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,13 @@ public void handleOnDeserialiseEndline(long currentTick, InputContainer deserial
150150
}
151151

152152
@Override
153+
public void onRecordingClear() {
154+
onClear();
155+
}
156+
153157
public void onClear() {
154-
REGISTRY.values().forEach(fc -> {
155-
fc.onClear();
158+
REGISTRY.values().forEach(fileCommandExtension -> {
159+
fileCommandExtension.onClear();
156160
});
157161
}
158162

src/main/java/com/minecrafttas/tasmod/playback/filecommands/builtin/DesyncMonitorFileCommandExtension.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
import com.dselent.bigarraylist.BigArrayList;
1212
import com.minecrafttas.tasmod.TASmodClient;
1313
import com.minecrafttas.tasmod.events.EventPlaybackClient;
14-
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
1514
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.InputContainer;
15+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
1616
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand;
17-
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.SortedFileCommandContainer;
1817
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
18+
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.SortedFileCommandContainer;
1919
import com.minecrafttas.tasmod.playback.tasfile.exception.PlaybackLoadException;
2020

2121
import net.minecraft.client.Minecraft;

src/main/java/com/minecrafttas/tasmod/playback/filecommands/builtin/LabelFileCommandExtension.java

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.minecrafttas.tasmod.playback.filecommands.builtin;
22

3-
import java.io.IOException;
43
import java.nio.file.Path;
54

6-
import com.dselent.bigarraylist.BigArrayList;
75
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.InputContainer;
86
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand;
97
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.FileCommandsInTickList;
@@ -14,21 +12,17 @@ public class LabelFileCommandExtension extends PlaybackFileCommandExtension {
1412

1513
private String labelText = "";
1614

17-
BigArrayList<SortedFileCommandContainer> label = new BigArrayList<>();
18-
1915
public LabelFileCommandExtension() {
2016
this("label");
2117
}
2218

2319
public LabelFileCommandExtension(String tempDirName) {
2420
super(tempDirName);
25-
this.label = new BigArrayList<>(tempDir.toString());
2621
enabled = true;
2722
}
2823

2924
public LabelFileCommandExtension(Path tempDir) {
3025
super(tempDir);
31-
this.label = new BigArrayList<>(tempDir.toString());
3226
enabled = true;
3327
}
3428

@@ -42,28 +36,12 @@ public String[] getFileCommandNames() {
4236
return new String[] { "label" };
4337
}
4438

45-
@Override
46-
public SortedFileCommandContainer onSerialiseInlineComment(long tick, InputContainer inputContainer) {
47-
SortedFileCommandContainer fileCommandContainer = new SortedFileCommandContainer();
48-
if (label.size() != 0 && label.get(tick).get("label") != null) {
49-
fileCommandContainer = label.get(tick);
50-
}
51-
return fileCommandContainer;
52-
}
53-
54-
@Override
55-
public void onDeserialiseInlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
56-
if (fileCommandContainer.containsKey("label")) {
57-
label.add(fileCommandContainer.split("label"));
58-
}
59-
}
60-
6139
@Override
6240
public void onPlayback(long tick, InputContainer inputContainer) {
63-
if (label.size() <= tick) {
41+
if (inlineFileCommandStorage.size() <= tick) {
6442
return;
6543
}
66-
SortedFileCommandContainer containerInTick = label.get(tick);
44+
SortedFileCommandContainer containerInTick = inlineFileCommandStorage.get(tick);
6745
if (containerInTick == null) {
6846
return;
6947
}
@@ -74,19 +52,15 @@ public void onPlayback(long tick, InputContainer inputContainer) {
7452
}
7553

7654
for (PlaybackFileCommand command : line) {
55+
if (command == null)
56+
continue;
7757
labelText = String.join(", ", command.getArgs());
7858
}
7959
}
8060

8161
@Override
8262
public void onClear() {
83-
try {
84-
label.clearMemory();
85-
} catch (IOException e) {
86-
e.printStackTrace();
87-
}
88-
89-
label = new BigArrayList<>();
63+
super.onClear();
9064
labelText = "";
9165
}
9266

src/main/java/com/minecrafttas/tasmod/playback/filecommands/builtin/OptionsFileCommandExtension.java

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.minecrafttas.tasmod.playback.filecommands.builtin;
22

3-
import java.io.IOException;
43
import java.nio.file.Path;
54

6-
import com.dselent.bigarraylist.BigArrayList;
75
import com.minecrafttas.tasmod.TASmod;
86
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.InputContainer;
97
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand;
@@ -16,21 +14,17 @@ public class OptionsFileCommandExtension extends PlaybackFileCommandExtension {
1614

1715
private boolean shouldRenderHud = true;
1816

19-
BigArrayList<SortedFileCommandContainer> hud;
20-
2117
public OptionsFileCommandExtension() {
2218
this("hud");
2319
}
2420

2521
public OptionsFileCommandExtension(String tempDirName) {
2622
super(tempDirName);
27-
hud = new BigArrayList<>(tempDir.toString());
2823
enabled = true;
2924
}
3025

3126
public OptionsFileCommandExtension(Path tempDir) {
3227
super(tempDir);
33-
this.hud = new BigArrayList<>(tempDir.toString());
3428
enabled = true;
3529
}
3630

@@ -44,28 +38,12 @@ public String[] getFileCommandNames() {
4438
return new String[] { "hud" };
4539
}
4640

47-
@Override
48-
public SortedFileCommandContainer onSerialiseInlineComment(long tick, InputContainer inputContainer) {
49-
SortedFileCommandContainer fileCommandContainer = new SortedFileCommandContainer();
50-
if (hud.size() != 0 && hud.get(tick).get("hud") != null) {
51-
fileCommandContainer = hud.get(tick);
52-
}
53-
return fileCommandContainer;
54-
}
55-
56-
@Override
57-
public void onDeserialiseInlineComment(long tick, InputContainer container, SortedFileCommandContainer fileCommandContainer) {
58-
if (fileCommandContainer.containsKey("hud")) {
59-
hud.add(fileCommandContainer.split("hud"));
60-
}
61-
}
62-
6341
@Override
6442
public void onPlayback(long tick, InputContainer inputContainer) {
65-
if (hud.size() <= tick) {
43+
if (inlineFileCommandStorage.size() <= tick) {
6644
return;
6745
}
68-
SortedFileCommandContainer containerInTick = hud.get(tick);
46+
SortedFileCommandContainer containerInTick = inlineFileCommandStorage.get(tick);
6947
if (containerInTick == null) {
7048
return;
7149
}
@@ -76,6 +54,8 @@ public void onPlayback(long tick, InputContainer inputContainer) {
7654
}
7755

7856
for (PlaybackFileCommand command : line) {
57+
if (command == null)
58+
continue;
7959
String[] args = command.getArgs();
8060
if (args.length == 1) {
8161
/*
@@ -102,21 +82,9 @@ public void onPlayback(long tick, InputContainer inputContainer) {
10282
}
10383
}
10484

105-
@Override
106-
public void onRecord(long tick, InputContainer inputContainer) {
107-
// TODO Auto-generated method stub
108-
super.onRecord(tick, inputContainer);
109-
}
110-
11185
@Override
11286
public void onClear() {
113-
try {
114-
hud.clearMemory();
115-
} catch (IOException e) {
116-
e.printStackTrace();
117-
}
118-
119-
hud = new BigArrayList<>();
87+
super.onClear();
12088
shouldRenderHud = true;
12189
}
12290

src/main/java/com/minecrafttas/tasmod/playback/metadata/PlaybackMetadataRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void handleOnLoad(List<PlaybackMetadata> meta) {
5252
}
5353

5454
@Override
55-
public void onClear() {
55+
public void onRecordingClear() {
5656
REGISTRY.forEach((key, extension) ->{
5757
extension.onClear();
5858
});

0 commit comments

Comments
 (0)