Skip to content

Commit dbd53a7

Browse files
authored
[PlayUntil] Refactor playuntil (#233)
- Fixed continuing a recording desyncing the next playback - Fixed playback stopping at a higher index - Fixed playuntil - Fixed toString on InputContainer not working correctly for debugging - Added EventPlaybackTickPre - Switched pitch and yaw in some places
2 parents 43e19fa + 39cf454 commit dbd53a7

File tree

12 files changed

+223
-112
lines changed

12 files changed

+223
-112
lines changed

src/main/java/com/minecrafttas/tasmod/TASmod.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.minecrafttas.tasmod.commands.CommandSavestate;
2626
import com.minecrafttas.tasmod.commands.CommandTickrate;
2727
import com.minecrafttas.tasmod.commands.TabCompletionUtils;
28+
import com.minecrafttas.tasmod.handlers.PlayUntilHandler;
2829
import com.minecrafttas.tasmod.playback.PlaybackControllerServer;
2930
import com.minecrafttas.tasmod.playback.metadata.builtin.StartpositionMetadataExtension;
3031
import com.minecrafttas.tasmod.registries.TASmodPackets;
@@ -78,6 +79,8 @@ public class TASmod implements ModInitializer, EventServerInit, EventServerStop
7879

7980
public static final CommandFileCommand commandFileCommand = new CommandFileCommand();
8081

82+
public static final PlayUntilHandler playUntil = new PlayUntilHandler();
83+
8184
@Override
8285
public void onInitialize() {
8386

@@ -117,6 +120,8 @@ public void onInitialize() {
117120
SavestateMotionStorage motionStorage = new SavestateMotionStorage();
118121
PacketHandlerRegistry.register(motionStorage);
119122
EventListenerRegistry.register(motionStorage);
123+
PacketHandlerRegistry.register(playUntil);
124+
EventListenerRegistry.register(playUntil);
120125
}
121126

122127
@Override

src/main/java/com/minecrafttas/tasmod/commands/CommandPlayUntil.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import net.minecraft.server.MinecraftServer;
1111
import net.minecraft.util.text.TextComponentString;
1212

13-
public class CommandPlayUntil extends CommandBase{
13+
public class CommandPlayUntil extends CommandBase {
1414

1515
@Override
1616
public String getName() {
@@ -24,7 +24,7 @@ public String getUsage(ICommandSender sender) {
2424

2525
@Override
2626
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
27-
if(args.length==1) {
27+
if (args.length == 1) {
2828
int i = 0;
2929
try {
3030
i = Integer.parseInt(args[0]);
@@ -36,10 +36,8 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
3636
} catch (Exception e) {
3737
e.printStackTrace();
3838
}
39-
}
40-
else {
39+
} else {
4140
sender.sendMessage(new TextComponentString("Stops the next playback one tick before the specified tick and lets you record from there:\n\n/playuntil 10, runs the playback until tick 9 and will record from there. Useful when you can't savestate"));
4241
}
4342
}
44-
4543
}

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.minecrafttas.mctcommon.events.EventListenerRegistry.EventBase;
44
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
5-
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
65
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.InputContainer;
6+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
77

88
public interface EventPlaybackClient {
99

@@ -65,10 +65,10 @@ public interface EventRecordTick extends EventBase {
6565
public interface EventPlaybackTick extends EventBase {
6666

6767
/**
68-
* Fired when a tick is being recorded
68+
* Fired when a tick is played back
6969
*
70-
* @param index The index of the tick that is being recorded
71-
* @param container The {@link InputContainer} that is being recorded
70+
* @param index The index of the tick that is played back
71+
* @param container The {@link InputContainer} that is played back
7272
*/
7373
public void onPlaybackTick(long index, InputContainer container);
7474
}
@@ -84,4 +84,33 @@ public interface EventRecordClear extends EventBase {
8484
*/
8585
public void onRecordingClear();
8686
}
87+
88+
/**
89+
* Fired when an input is deleted
90+
*/
91+
@FunctionalInterface
92+
public interface EventInputDelete extends EventBase {
93+
94+
/**
95+
* Fired when an input is deleted
96+
*
97+
* @param The index of the input
98+
*/
99+
public void onInputDelete(long index);
100+
}
101+
102+
/**
103+
* Fired when a tick is being played back before reading the inputs
104+
*/
105+
@FunctionalInterface
106+
public interface EventPlaybackTickPre extends EventBase {
107+
108+
/**
109+
* Fired when a tick is being played back before reading the inputs
110+
*
111+
* @param index The index of the tick that is played back
112+
* @param container The {@link InputContainer} that is played back
113+
*/
114+
public void onPlaybackTickPre(long index);
115+
}
87116
}

src/main/java/com/minecrafttas/tasmod/handlers/InterpolationHandler.java

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.minecrafttas.tasmod.handlers;
2+
3+
import java.nio.ByteBuffer;
4+
5+
import com.minecrafttas.mctcommon.networking.ByteBufferBuilder;
6+
import com.minecrafttas.mctcommon.networking.Client.Side;
7+
import com.minecrafttas.mctcommon.networking.exception.PacketNotImplementedException;
8+
import com.minecrafttas.mctcommon.networking.exception.WrongSideException;
9+
import com.minecrafttas.mctcommon.networking.interfaces.ClientPacketHandler;
10+
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
11+
import com.minecrafttas.mctcommon.networking.interfaces.ServerPacketHandler;
12+
import com.minecrafttas.tasmod.TASmod;
13+
import com.minecrafttas.tasmod.TASmodClient;
14+
import com.minecrafttas.tasmod.events.EventPlaybackClient;
15+
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
16+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
17+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
18+
import com.minecrafttas.tasmod.registries.TASmodPackets;
19+
20+
/**
21+
* Feature for starting a recording after playing back a certain number of ticks
22+
*
23+
* @author Scribble
24+
*/
25+
public class PlayUntilHandler implements ClientPacketHandler, ServerPacketHandler, EventPlaybackClient.EventPlaybackTickPre {
26+
27+
/**
28+
* If not null, play until a certain point
29+
*/
30+
private Integer playUntil = null;
31+
32+
@Override
33+
public void onPlaybackTickPre(long index) {
34+
/* Playuntil logic */
35+
if (playUntil != null && playUntil == index) {
36+
TASmodClient.tickratechanger.pauseGame(true);
37+
PlaybackControllerClient controller = TASmodClient.controller;
38+
controller.setTASState(TASstate.NONE);
39+
controller.setIndex(controller.index() - 1);
40+
for (long i = controller.size() - 1; i >= index; i--) {
41+
controller.remove(i);
42+
}
43+
controller.setTASState(TASstate.RECORDING);
44+
playUntil = null;
45+
}
46+
}
47+
48+
public boolean isActive() {
49+
return playUntil != null;
50+
}
51+
52+
public void setPlayUntil(int until) {
53+
this.playUntil = until;
54+
}
55+
56+
@Override
57+
public PacketID[] getAcceptedPacketIDs() {
58+
return new TASmodPackets[] { TASmodPackets.PLAYBACK_PLAYUNTIL };
59+
}
60+
61+
@Override
62+
public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
63+
TASmodPackets packet = (TASmodPackets) id;
64+
65+
switch (packet) {
66+
case PLAYBACK_PLAYUNTIL:
67+
int until = ByteBufferBuilder.readInt(buf);
68+
setPlayUntil(until);
69+
break;
70+
default:
71+
throw new PacketNotImplementedException(packet, this.getClass(), Side.SERVER);
72+
}
73+
}
74+
75+
@Override
76+
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
77+
TASmodPackets packet = (TASmodPackets) id;
78+
79+
switch (packet) {
80+
case PLAYBACK_PLAYUNTIL:
81+
TASmod.server.sendToAll(new TASmodBufferBuilder(buf));
82+
break;
83+
default:
84+
throw new PacketNotImplementedException(packet, this.getClass(), Side.SERVER);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)