Skip to content

Commit

Permalink
[PlaybackSerialiser] Various crash fixes (MinecraftTAS#209)
Browse files Browse the repository at this point in the history
- Fixed crash when playing a recording without saving and loading a file
- Fixed start position not being stored when using fullrecord
- Formatted files
  • Loading branch information
ScribbleTAS authored Jul 12, 2024
2 parents 80f128d + eeb192d commit bf874c5
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public String getUsage(ICommandSender sender) {

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
sender.sendMessage(new TextComponentString(TextFormatting.RED + "This feature doesn't work at the moment!"));
try {
TASmod.savestateHandlerServer.loadState(0, false, false);
} catch (LoadstateException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class PlaybackControllerClient implements ClientPacketHandler, EventClien
private VirtualCameraAngle camera = new VirtualCameraAngle();

public final File directory = new File(Minecraft.getMinecraft().mcDataDir.getAbsolutePath() + File.separator + "saves" + File.separator + "tasfiles");

/**
* The place where all inputs get stored
*/
Expand Down Expand Up @@ -150,7 +150,7 @@ public String setTASStateClient(TASstate stateIn) {
*/
public String setTASStateClient(TASstate stateIn, boolean verbose) {
EventListenerRegistry.fireEvent(EventControllerStateChange.class, stateIn, state);

if (state == stateIn) {
switch (stateIn) {
case PLAYBACK:
Expand Down Expand Up @@ -238,12 +238,12 @@ public String setTASStateClient(TASstate stateIn, boolean verbose) {

private void startRecording() {
LOGGER.debug(LoggerMarkers.Playback, "Starting recording");
if(this.inputs.isEmpty()) {
if (this.inputs.isEmpty()) {
VirtualCameraAngleInput CAMERA_ANGLE = TASmodClient.virtual.CAMERA_ANGLE;
Float pitch = CAMERA_ANGLE.getCurrentPitch();
Float yaw = CAMERA_ANGLE.getCurrentYaw();
this.camera.set(pitch, yaw);

inputs.add(new TickContainer());
}
}
Expand Down Expand Up @@ -320,7 +320,7 @@ public boolean isNothingPlaying() {
public TASstate getState() {
return state;
}

public TASstate getStateAfterPause() {
return stateAfterPause;
}
Expand Down Expand Up @@ -407,7 +407,7 @@ private void recordNextTick() {
} else {
inputs.set(index, container);
}

EventListenerRegistry.fireEvent(EventRecordTick.class, index, container);
}

Expand Down Expand Up @@ -447,7 +447,7 @@ private void playbackNextTick() {
this.camera = container.getCameraAngle().clone();
EventListenerRegistry.fireEvent(EventPlaybackTick.class, index, container);
}

}
// =====================================================================================================
// Methods to manipulate inputs
Expand All @@ -467,11 +467,11 @@ public long index() {
public BigArrayList<TickContainer> getInputs() {
return inputs;
}

public void setInputs(BigArrayList<TickContainer> inputs) {
this.setInputs(inputs, 0);
}

public void setInputs(BigArrayList<TickContainer> inputs, long index) {
try {
this.inputs.clearMemory();
Expand Down Expand Up @@ -573,13 +573,13 @@ public static class TickContainer implements Serializable {
private VirtualMouse mouse;

private VirtualCameraAngle cameraAngle;

private CommentContainer comments;

public TickContainer(VirtualKeyboard keyboard, VirtualMouse mouse, VirtualCameraAngle subticks) {
this(keyboard, mouse, subticks, new CommentContainer());
}

public TickContainer(VirtualKeyboard keyboard, VirtualMouse mouse, VirtualCameraAngle camera, CommentContainer comments) {
this.keyboard = keyboard;
this.mouse = mouse;
Expand Down Expand Up @@ -612,7 +612,7 @@ public VirtualCameraAngle getCameraAngle() {
public CommentContainer getComments() {
return comments;
}

@Override
public TickContainer clone() {
return new TickContainer(keyboard, mouse, cameraAngle);
Expand All @@ -627,9 +627,9 @@ public boolean equals(Object other) {
return super.equals(other);
}
}
public static class CommentContainer implements Serializable{

public static class CommentContainer implements Serializable {

/**
* List of all inline comments in a tick.<br>
* These comments take the form:
Expand All @@ -652,7 +652,7 @@ public static class CommentContainer implements Serializable{
* </pre>
*/
private List<String> inlineComments;

/**
* List of all endline comments.<br>
* These comments take the form:
Expand All @@ -665,44 +665,44 @@ public static class CommentContainer implements Serializable{
* Endline comments are supposed to describe individual subticks.<br>
*/
private List<String> endlineComments;

public CommentContainer() {
this(new ArrayList<>(), new ArrayList<>());
}

public CommentContainer(List<String> inlineComments, List<String> endlineComments) {
this.inlineComments=inlineComments;
this.endlineComments=endlineComments;
this.inlineComments = inlineComments;
this.endlineComments = endlineComments;
}

public void addInlineComment(String inlineComment) {
inlineComments.add(inlineComment);
}

public void addEndlineComment(String endlineComment) {
endlineComments.add(endlineComment);
}

public List<String> getInlineComments() {
return inlineComments;
}

public List<String> getEndlineComments() {
return endlineComments;
}

@Override
public boolean equals(Object obj) {
if(obj instanceof CommentContainer) {
if (obj instanceof CommentContainer) {
CommentContainer other = (CommentContainer) obj;
return inlineComments.equals(other.inlineComments) && endlineComments.equals(other.endlineComments);
}
return super.equals(obj);
}

@Override
public String toString() {
return inlineComments.toString()+"\n\n"+endlineComments.toString();
return inlineComments.toString() + "\n\n" + endlineComments.toString();
}
}

Expand Down Expand Up @@ -749,17 +749,19 @@ public void setStateWhenOpened(TASstate state) {

@Override
public PacketID[] getAcceptedPacketIDs() {
return new TASmodPackets[] {
PLAYBACK_SAVE,
PLAYBACK_LOAD,
PLAYBACK_FULLPLAY,
PLAYBACK_FULLRECORD,
PLAYBACK_RESTARTANDPLAY,
PLAYBACK_PLAYUNTIL,
PLAYBACK_CLEAR_INPUTS,
PLAYBACK_STATE

//@formatter:off
return new TASmodPackets[] {
PLAYBACK_SAVE,
PLAYBACK_LOAD,
PLAYBACK_FULLPLAY,
PLAYBACK_FULLRECORD,
PLAYBACK_RESTARTANDPLAY,
PLAYBACK_PLAYUNTIL,
PLAYBACK_CLEAR_INPUTS,
PLAYBACK_STATE

};
//@formatter:on
}

@Override
Expand All @@ -774,7 +776,7 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
case PLAYBACK_SAVE:
name = TASmodBufferBuilder.readString(buf);
flavor = TASmodBufferBuilder.readString(buf);

try {
PlaybackSerialiser.saveToFile(new File(directory, name + ".mctas"), this, flavor);
} catch (PlaybackSaveException e) {
Expand All @@ -788,35 +790,34 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
LOGGER.catching(e);
return;
}

if (mc.world != null) {
TextComponentString confirm = new TextComponentString(TextFormatting.GREEN + "Saved inputs to " + name + ".mctas" + TextFormatting.RESET + " [" + TextFormatting.YELLOW + "Open folder" + TextFormatting.RESET + "]");
confirm.getStyle().setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/folder tasfiles"));
mc.ingameGUI.getChatGUI().printChatMessage(confirm);
}
else
} else
LOGGER.debug(LoggerMarkers.Playback, "Saved inputs to " + name + ".mctas");
break;

case PLAYBACK_LOAD:
name = TASmodBufferBuilder.readString(buf);
flavor = TASmodBufferBuilder.readString(buf);

try {
TASmodClient.controller.setInputs(PlaybackSerialiser.loadFromFile(new File(directory, name + ".mctas"), flavor));
} catch (PlaybackLoadException e) {
if (mc.world != null) {
TextComponentString textComponent = new TextComponentString(e.getMessage());
TextComponentString textComponent = new TextComponentString(e.getMessage());
mc.ingameGUI.getChatGUI().printChatMessage(textComponent);
}
LOGGER.catching(e);
return;
} catch (Exception e) {
if (mc.world != null)
if (mc.world != null)
mc.ingameGUI.getChatGUI().printChatMessage(new TextComponentString(TextFormatting.RED + "Loading failed, something went very wrong"));
LOGGER.catching(e);
}

if (mc.world != null)
mc.ingameGUI.getChatGUI().printChatMessage(new TextComponentString(TextFormatting.GREEN + "Loaded inputs from " + name + ".mctas"));
else
Expand All @@ -843,6 +844,7 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws

// Schedule code to be executed on the next tick
TASmodClient.tickSchedulerClient.add(() -> {
TASmodClient.startpositionMetadataExtension.updateStartPosition();
if (mc.world != null) { // Exit the server if you are in one
mc.world.sendQuittingDisconnectingPacket();
mc.loadWorld((WorldClient) null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public void onDeserialiseInlineComment(long tick, TickContainer container, Playb

@Override
public void onPlayback(long tick, TickContainer tickContainer) {
PlaybackFileCommandContainer containerInTick = label.get(tick - 1);
if (label.size() <= tick) {
return;
}
PlaybackFileCommandContainer containerInTick = label.get(tick);
if (containerInTick == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class OptionsFileCommandExtension extends PlaybackFileCommandExtension {
private boolean shouldRenderHud = true;

BigArrayList<PlaybackFileCommandContainer> hud = new BigArrayList<>();

public OptionsFileCommandExtension() {
enabled = true;
}
Expand All @@ -40,13 +40,16 @@ public void onDeserialiseInlineComment(long tick, TickContainer container, Playb

@Override
public void onPlayback(long tick, TickContainer tickContainer) {
if (hud.size() <= tick) {
return;
}
PlaybackFileCommandContainer containerInTick = hud.get(tick);
if(containerInTick == null) {
if (containerInTick == null) {
return;
}

PlaybackFileCommandLine line = containerInTick.get("hud");
if(line == null) {
if (line == null) {
return;
}

Expand Down Expand Up @@ -83,7 +86,7 @@ public void onClear() {
hud = new BigArrayList<>();
shouldRenderHud = true;
}

public boolean shouldRenderHud() {
return shouldRenderHud;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ private PlaybackMetadata(String extensionName, LinkedHashMap<String, String> dat
}

public void setValue(String key, String value) {
if (key.contains(SEPERATOR)) {
throw new IllegalArgumentException(String.format("%sKeyname %s can't contain %s", extensionName != null ? extensionName + ": " : "", key, SEPERATOR));
}
data.put(key, value);
}

Expand Down
Loading

0 comments on commit bf874c5

Please sign in to comment.