Skip to content

Commit 2ead617

Browse files
committed
[Flavor] Add keyboard and mouse serialisation to AlphaFlavor
- [PlaybackSersialiser] Switched from using toString() in keyboard and mouse to the same implementation. Reason is that toString in VirtualKeyboard might change in the fututure while the SerialiserFlavorBase is supposed to be backwards compatible
1 parent 57f441d commit 2ead617

File tree

2 files changed

+135
-2
lines changed

2 files changed

+135
-2
lines changed

src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/SerialiserFlavorBase.java

+15-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.util.concurrent.LinkedBlockingQueue;
1111
import java.util.regex.Matcher;
1212
import java.util.regex.Pattern;
13+
import java.util.stream.Collectors;
14+
15+
import org.apache.commons.lang3.StringUtils;
1316

1417
import com.dselent.bigarraylist.BigArrayList;
1518
import com.minecrafttas.mctcommon.registry.Registerable;
@@ -372,7 +375,7 @@ protected List<String> serialiseKeyboard(VirtualKeyboard keyboard) {
372375
pruneListEndEmptySubtickable(subticks);
373376

374377
for (VirtualKeyboard subtick : subticks) {
375-
out.add(subtick.toString2());
378+
out.add(String.format("%s;%s", String.join(",", subtick.getCurrentPresses()), charListToString(subtick.getCharList())));
376379
}
377380
return out;
378381
}
@@ -384,7 +387,7 @@ protected List<String> serialiseMouse(VirtualMouse mouse) {
384387
pruneListEndEmptySubtickable(subticks);
385388

386389
for (VirtualMouse subtick : subticks) {
387-
out.add(subtick.toString2());
390+
out.add(String.format("%s;%s,%s,%s", String.join(",", subtick.getCurrentPresses()), subtick.getScrollWheel(), subtick.getCursorX(), subtick.getCursorY()));
388391
}
389392
return out;
390393
}
@@ -1303,4 +1306,14 @@ public boolean equals(Object obj) {
13031306
public void setProcessExtensions(boolean processExtensions) {
13041307
this.processExtensions = processExtensions;
13051308
}
1309+
1310+
protected String charListToString(List<Character> charList) {
1311+
String charString = "";
1312+
if (!charList.isEmpty()) {
1313+
charString = charList.stream().map(Object::toString).collect(Collectors.joining());
1314+
charString = StringUtils.replace(charString, "\r", "\\n");
1315+
charString = StringUtils.replace(charString, "\n", "\\n");
1316+
}
1317+
return charString;
1318+
}
13061319
}

src/main/java/com/minecrafttas/tasmod/playback/tasfile/flavor/builtin/AlphaFlavor.java

+120
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
1919
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
2020
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
21+
import com.minecrafttas.tasmod.virtual.VirtualCameraAngle;
22+
import com.minecrafttas.tasmod.virtual.VirtualKey;
23+
import com.minecrafttas.tasmod.virtual.VirtualKeyboard;
24+
import com.minecrafttas.tasmod.virtual.VirtualMouse;
2125

2226
public class AlphaFlavor extends SerialiserFlavorBase {
2327

@@ -106,6 +110,122 @@ public boolean checkFlavorName(List<String> headerLines) {
106110
return false;
107111
}
108112

113+
@Override
114+
protected List<String> serialiseKeyboard(VirtualKeyboard keyboard) {
115+
/*
116+
* Old code from when I did not know String.join exists,
117+
* kept relatively unaltered because I want to.
118+
*/
119+
List<String> out = new ArrayList<>();
120+
121+
List<String> stringy = keyboard.getCurrentPresses();
122+
String keyString = "";
123+
if (!stringy.isEmpty()) {
124+
String seperator = ",";
125+
for (int i = 0; i < stringy.size(); i++) {
126+
if (i == stringy.size() - 1) {
127+
seperator = "";
128+
}
129+
keyString = keyString.concat(stringy.get(i) + seperator);
130+
}
131+
}
132+
List<Character> charList = keyboard.getCharList();
133+
String charString = "";
134+
if (!charList.isEmpty()) {
135+
for (int i = 0; i < charList.size(); i++) {
136+
charString = charString.concat(Character.toString(charList.get(i)));
137+
}
138+
charString = charString.replace("\r", "\\n");
139+
charString = charString.replace("\n", "\\n");
140+
}
141+
142+
out.add("Keyboard:" + keyString + ";" + charString); // Keyboard didn't support subticks, only the current key is processed
143+
144+
return out;
145+
}
146+
147+
@Override
148+
protected List<String> serialiseMouse(VirtualMouse mouse) {
149+
/*
150+
* Old code from when I did not know String.join exists,
151+
* kept relatively unaltered because I want to.
152+
*/
153+
List<String> out = new ArrayList<>();
154+
List<String> stringy = mouse.getCurrentPresses();
155+
String keyString = "";
156+
if (!stringy.isEmpty()) {
157+
String seperator = ",";
158+
for (int i = 0; i < stringy.size(); i++) {
159+
if (i == stringy.size() - 1) {
160+
seperator = "";
161+
}
162+
keyString = keyString.concat(stringy.get(i) + seperator);
163+
}
164+
}
165+
166+
List<VirtualMouse> path = new ArrayList<>(mouse.getSubticks()); // I previously called subticks "paths" as it was mainly used for the mouse...
167+
// pruneListEndEmptySubtickable(path);
168+
169+
/*
170+
* The mouse supported subticks,
171+
* but it was handled differently in alpha...
172+
* The subticks where added in square brackets, seperated by a "->"
173+
* Not the best solution in hindsight,
174+
* but that was apparently the first thing that came to my mind back then...
175+
*/
176+
String pathString = "";
177+
if (!path.isEmpty()) {
178+
String seperator = "->";
179+
for (int i = 0; i < path.size(); i++) {
180+
if (i == path.size() - 1) {
181+
seperator = "";
182+
}
183+
184+
VirtualMouse singlePath = path.get(i);
185+
186+
pathString = pathString.concat("[" + serialisePath(singlePath) + "]" + seperator);
187+
}
188+
}
189+
out.add("Mouse:" + keyString + ";" + pathString);
190+
return out;
191+
}
192+
193+
protected String serialisePath(VirtualMouse path) {
194+
String keyString = "";
195+
List<String> strings = new ArrayList<String>();
196+
197+
path.getPressedKeys().forEach((virtualkeys) -> {
198+
strings.add(VirtualKey.getName(virtualkeys));
199+
});
200+
if (!strings.isEmpty()) {
201+
String seperator = ",";
202+
for (int i = 0; i < strings.size(); i++) {
203+
if (i == strings.size() - 1) {
204+
seperator = "";
205+
}
206+
keyString = keyString.concat(strings.get(i) + seperator);
207+
}
208+
}
209+
if (keyString.isEmpty()) {
210+
return "MOUSEMOVED," + path.getScrollWheel() + "," + path.getCursorX() + "," + path.getCursorY();
211+
} else {
212+
return keyString + "," + path.getScrollWheel() + "," + path.getCursorX() + "," + path.getCursorY();
213+
}
214+
}
215+
216+
@Override
217+
protected List<String> serialiseCameraAngle(VirtualCameraAngle subticks) {
218+
List<String> out = new ArrayList<>();
219+
220+
/*
221+
* The camera was called "subticks" in previous iterations of this code.
222+
* To honor this fact, it is also called subticks here, even though
223+
* actual subticks were not supported
224+
*/
225+
out.add("Camera:" + subticks.getPitch() + ";" + subticks.getYaw());
226+
return out;
227+
}
228+
109229
@Override
110230
protected void deserialiseMetadata(List<String> headerLines) {
111231
String author = "Insert author here";

0 commit comments

Comments
 (0)