Skip to content

Commit 631dbe0

Browse files
committed
[PlayUntil] Refactor playuntil
1 parent f806a5e commit 631dbe0

File tree

5 files changed

+96
-36
lines changed

5 files changed

+96
-36
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 & 4 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,8 +36,7 @@ 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
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
public void setPlayUntil(int until) {
51+
this.playUntil = until;
52+
}
53+
54+
@Override
55+
public PacketID[] getAcceptedPacketIDs() {
56+
return new TASmodPackets[] { TASmodPackets.PLAYBACK_PLAYUNTIL };
57+
}
58+
59+
@Override
60+
public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
61+
TASmodPackets packet = (TASmodPackets) id;
62+
63+
switch (packet) {
64+
case PLAYBACK_PLAYUNTIL:
65+
int until = ByteBufferBuilder.readInt(buf);
66+
setPlayUntil(until);
67+
break;
68+
default:
69+
throw new PacketNotImplementedException(packet, this.getClass(), Side.SERVER);
70+
}
71+
}
72+
73+
@Override
74+
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
75+
TASmodPackets packet = (TASmodPackets) id;
76+
77+
switch (packet) {
78+
case PLAYBACK_PLAYUNTIL:
79+
TASmod.server.sendToAll(new TASmodBufferBuilder(buf));
80+
break;
81+
default:
82+
throw new PacketNotImplementedException(packet, this.getClass(), Side.SERVER);
83+
}
84+
}
85+
}

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

Lines changed: 0 additions & 28 deletions
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

Lines changed: 3 additions & 4 deletions
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)