Skip to content

Commit 2c764c4

Browse files
committed
[PlayUntil] Refactor playuntil
1 parent f806a5e commit 2c764c4

File tree

4 files changed

+95
-32
lines changed

4 files changed

+95
-32
lines changed

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

+5
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
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.InputContainer;
18+
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
19+
import com.minecrafttas.tasmod.registries.TASmodPackets;
20+
21+
/**
22+
* Feature for starting a recording after playing back a certain number of ticks
23+
*
24+
* @author Scribble
25+
*/
26+
public class PlayUntilHandler implements ClientPacketHandler, ServerPacketHandler, EventPlaybackClient.EventPlaybackTick {
27+
28+
/**
29+
* If not null, play until a certain point
30+
*/
31+
private Integer playUntil = null;
32+
33+
@Override
34+
public void onPlaybackTick(long index, InputContainer container) {
35+
/* Playuntil logic */
36+
if (playUntil != null && playUntil == index + 1) {
37+
TASmodClient.tickratechanger.pauseGame(true);
38+
PlaybackControllerClient controller = TASmodClient.controller;
39+
playUntil = null;
40+
controller.setTASState(TASstate.NONE);
41+
for (long i = controller.size() - 1; i >= index; i--) {
42+
controller.getInputs().remove(i);
43+
}
44+
index--;
45+
controller.setTASState(TASstate.RECORDING);
46+
return;
47+
}
48+
}
49+
50+
// ==============================================================
51+
52+
public void setPlayUntil(int until) {
53+
this.playUntil = until;
54+
}
55+
56+
@Override
57+
public PacketID[] getAcceptedPacketIDs() {
58+
return new PacketID[] { 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+
}

src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerClient.java

-28
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_FULLPLAY;
66
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_FULLRECORD;
77
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_LOAD;
8-
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_PLAYUNTIL;
98
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_RESTARTANDPLAY;
109
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_SAVE;
1110
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_STATE;
@@ -142,8 +141,6 @@ public class PlaybackControllerClient implements
142141

143142
// =====================================================================================================
144143

145-
private Integer playUntil = null; // TODO Replace with event
146-
147144
public PlaybackControllerClient() {
148145
tasFileDirectory = TASmodClient.tasfiledirectory;
149146

@@ -491,19 +488,6 @@ private void playbackNextTick() {
491488

492489
index++; // Increase the index and load the next inputs
493490

494-
/* Playuntil logic */
495-
if (playUntil != null && playUntil == index) {
496-
TASmodClient.tickratechanger.pauseGame(true);
497-
playUntil = null;
498-
setTASState(TASstate.NONE);
499-
for (long i = inputs.size() - 1; i >= index; i--) {
500-
inputs.remove(i);
501-
}
502-
index--;
503-
setTASState(TASstate.RECORDING);
504-
return;
505-
}
506-
507491
/* Stop condition */
508492
if (index == inputs.size() || inputs.isEmpty()) {
509493
unpressContainer();
@@ -624,12 +608,6 @@ public void unpressContainer() {
624608

625609
// ==============================================================
626610

627-
public void setPlayUntil(int until) {
628-
this.playUntil = until;
629-
}
630-
631-
// ==============================================================
632-
633611
/**
634612
* Storage class which stores the keyboard, mouse, subticks and comments of a given tick.
635613
*
@@ -837,7 +815,6 @@ public PacketID[] getAcceptedPacketIDs() {
837815
PLAYBACK_FULLPLAY,
838816
PLAYBACK_FULLRECORD,
839817
PLAYBACK_RESTARTANDPLAY,
840-
PLAYBACK_PLAYUNTIL,
841818
PLAYBACK_CLEAR_INPUTS,
842819
PLAYBACK_STATE
843820

@@ -949,11 +926,6 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
949926
});
950927
break;
951928

952-
case PLAYBACK_PLAYUNTIL:
953-
int until = ByteBufferBuilder.readInt(buf);
954-
TASmodClient.controller.setPlayUntil(until);
955-
break;
956-
957929
case PLAYBACK_CLEAR_INPUTS:
958930
TASmodClient.controller.clear();
959931
break;

src/main/java/com/minecrafttas/tasmod/playback/PlaybackControllerServer.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_FULLPLAY;
66
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_FULLRECORD;
77
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_LOAD;
8-
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_PLAYUNTIL;
98
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_RESTARTANDPLAY;
109
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_SAVE;
1110
import static com.minecrafttas.tasmod.registries.TASmodPackets.PLAYBACK_STATE;
@@ -36,17 +35,18 @@ public class PlaybackControllerServer implements ServerPacketHandler {
3635

3736
@Override
3837
public PacketID[] getAcceptedPacketIDs() {
38+
//@formatter:off
3939
return new TASmodPackets[]
4040
{
4141
PLAYBACK_STATE,
4242
PLAYBACK_CLEAR_INPUTS,
4343
PLAYBACK_FULLPLAY,
4444
PLAYBACK_FULLRECORD,
4545
PLAYBACK_RESTARTANDPLAY,
46-
PLAYBACK_PLAYUNTIL,
4746
PLAYBACK_SAVE,
4847
PLAYBACK_LOAD
4948
};
49+
//@formatter:on
5050
}
5151

5252
@Override
@@ -67,12 +67,11 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
6767
case PLAYBACK_FULLPLAY:
6868
case PLAYBACK_FULLRECORD:
6969
case PLAYBACK_RESTARTANDPLAY:
70-
case PLAYBACK_PLAYUNTIL:
7170
case PLAYBACK_SAVE:
7271
case PLAYBACK_LOAD:
7372
TASmod.server.sendToAll(new TASmodBufferBuilder(buf));
7473
break;
75-
74+
7675
default:
7776
throw new PacketNotImplementedException(packet, this.getClass(), Side.SERVER);
7877
}

0 commit comments

Comments
 (0)