22
33import static com .minecrafttas .tasmod .TASmod .LOGGER ;
44import static com .minecrafttas .tasmod .registries .TASmodPackets .SAVESTATE_PLAYER ;
5- import static com .minecrafttas .tasmod .registries .TASmodPackets .SAVESTATE_REQUEST_MOTION ;
65
76import java .io .IOException ;
87import java .nio .ByteBuffer ;
9- import java .util .HashMap ;
108import java .util .List ;
11- import java .util .Map ;
129import java .util .UUID ;
13- import java .util .concurrent .CompletableFuture ;
14- import java .util .concurrent .ExecutionException ;
15- import java .util .concurrent .TimeUnit ;
16- import java .util .concurrent .TimeoutException ;
1710
1811import com .minecrafttas .mctcommon .networking .Client .Side ;
1912import com .minecrafttas .mctcommon .networking .exception .PacketNotImplementedException ;
2215import com .minecrafttas .mctcommon .networking .interfaces .PacketID ;
2316import com .minecrafttas .mctcommon .networking .interfaces .ServerPacketHandler ;
2417import com .minecrafttas .tasmod .TASmod ;
25- import com .minecrafttas .tasmod .TASmodClient ;
26- import com .minecrafttas .tasmod .events .EventNBT ;
2718import com .minecrafttas .tasmod .networking .TASmodBufferBuilder ;
2819import com .minecrafttas .tasmod .registries .TASmodPackets ;
2920import com .minecrafttas .tasmod .savestates .SavestateHandlerClient ;
30- import com .minecrafttas .tasmod .savestates .exceptions .SavestateException ;
31- import com .minecrafttas .tasmod .savestates .gui .GuiSavestateSavingScreen ;
3221import com .minecrafttas .tasmod .util .LoggerMarkers ;
3322
3423import net .fabricmc .api .EnvType ;
3524import net .fabricmc .api .Environment ;
3625import net .minecraft .client .Minecraft ;
37- import net .minecraft .client .entity .EntityPlayerSP ;
3826import net .minecraft .entity .Entity ;
3927import net .minecraft .entity .player .EntityPlayerMP ;
4028import net .minecraft .nbt .NBTTagCompound ;
4836/**
4937 * Handles player related savestating methods
5038 */
51- public class SavestatePlayerHandler implements ClientPacketHandler , ServerPacketHandler , EventNBT . EventPlayerRead , EventNBT . EventPlayerWrite {
39+ public class SavestatePlayerHandler implements ClientPacketHandler , ServerPacketHandler {
5240
5341 private final MinecraftServer server ;
5442
55- private final Map <EntityPlayerMP , CompletableFuture <MotionData >> futures ;
56-
57- private final Map <EntityPlayerMP , MotionData > motionData ;
58-
5943 public SavestatePlayerHandler (MinecraftServer server ) {
6044 this .server = server ;
61- this .futures = new HashMap <>();
62- this .motionData = new HashMap <>();
6345 }
6446
6547 /**
@@ -195,39 +177,10 @@ public void clearScoreboard() {
195177 }
196178 }
197179
198- public void requestMotionFromClient () {
199- LOGGER .trace (LoggerMarkers .Savestate , "Request motion from client" );
200-
201- this .futures .clear ();
202-
203- List <EntityPlayerMP > playerList = server .getPlayerList ().getPlayers ();
204- playerList .forEach (player -> {
205- futures .put (player , new CompletableFuture <>());
206- });
207-
208- try {
209- // request client motion
210- TASmod .server .sendToAll (new TASmodBufferBuilder (SAVESTATE_REQUEST_MOTION ));
211- } catch (Exception e ) {
212- e .printStackTrace ();
213- }
214-
215- futures .forEach ((player , future ) -> {
216- try {
217- this .motionData .put (player , future .get (5 , TimeUnit .SECONDS ));
218- } catch (TimeoutException e ) {
219- throw new SavestateException (e , "Writing client motion for %s timed out!" , player .getName ());
220- } catch (ExecutionException | InterruptedException e ) {
221- throw new SavestateException (e , "Writing client motion for %s" , player .getName ());
222- }
223- });
224- }
225-
226180 @ Override
227181 public PacketID [] getAcceptedPacketIDs () {
228182 return new PacketID [] {
229183 //@formatter:off
230- SAVESTATE_REQUEST_MOTION ,
231184 SAVESTATE_PLAYER
232185 //@formatter:on
233186 };
@@ -239,11 +192,6 @@ public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws
239192 EntityPlayerMP player = TASmod .getServerInstance ().getPlayerList ().getPlayerByUsername (username );
240193
241194 switch (packet ) {
242- case SAVESTATE_REQUEST_MOTION :
243- MotionData data = TASmodBufferBuilder .readMotionData (buf );
244- CompletableFuture <MotionData > future = this .futures .get (player );
245- future .complete (data );
246- break ;
247195 case SAVESTATE_PLAYER :
248196 throw new WrongSideException (packet , Side .SERVER );
249197 default :
@@ -275,126 +223,8 @@ public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws
275223 });
276224 break ;
277225
278- case SAVESTATE_REQUEST_MOTION :
279- EntityPlayerSP player = Minecraft .getMinecraft ().player ;
280- if (player != null ) {
281- if (!(Minecraft .getMinecraft ().currentScreen instanceof GuiSavestateSavingScreen )) {
282- Minecraft .getMinecraft ().displayGuiScreen (new GuiSavestateSavingScreen ());
283- }
284- //@formatter:off
285- MotionData motionData = new MotionData (
286- player .motionX ,
287- player .motionY ,
288- player .motionZ ,
289- player .moveForward ,
290- player .moveVertical ,
291- player .moveStrafing ,
292- player .isSprinting (),
293- player .jumpMovementFactor
294- );
295- //@formatter:on
296- TASmodClient .client .send (new TASmodBufferBuilder (TASmodPackets .SAVESTATE_REQUEST_MOTION ).writeMotionData (motionData ));
297- }
298- break ;
299-
300226 default :
301227 break ;
302228 }
303229 }
304-
305- public static class MotionData {
306-
307- private double clientX ;
308- private double clientY ;
309- private double clientZ ;
310- private float clientrX ;
311- private float clientrY ;
312- private float clientrZ ;
313- private boolean sprinting ;
314- private float jumpMovementVector ;
315-
316- public MotionData (double x , double y , double z , float rx , float ry , float rz , boolean sprinting , float jumpMovementVector ) {
317- clientX = x ;
318- clientY = y ;
319- clientZ = z ;
320- clientrX = rx ;
321- clientrY = ry ;
322- clientrZ = rz ;
323- this .sprinting = sprinting ;
324- this .jumpMovementVector = jumpMovementVector ;
325- }
326-
327- public MotionData () {
328- this (0D , 0D , 0D , 0f , 0f , 0f , false , 0f );
329- }
330-
331- public double getClientX () {
332- return clientX ;
333- }
334-
335- public double getClientY () {
336- return clientY ;
337- }
338-
339- public double getClientZ () {
340- return clientZ ;
341- }
342-
343- public float getClientrX () {
344- return clientrX ;
345- }
346-
347- public float getClientrY () {
348- return clientrY ;
349- }
350-
351- public float getClientrZ () {
352- return clientrZ ;
353- }
354-
355- public boolean isSprinting () {
356- return sprinting ;
357- }
358-
359- public float getJumpMovementVector () {
360- return jumpMovementVector ;
361- }
362- }
363-
364- @ Override
365- public void onPlayerWriteNBT (NBTTagCompound compound , EntityPlayerMP player ) {
366- NBTTagCompound nbttagcompound = new NBTTagCompound ();
367-
368- MotionData saver = new MotionData ();
369- if (motionData .containsKey (player )) {
370- saver = motionData .get (player );
371- }
372-
373- nbttagcompound .setDouble ("x" , saver .getClientX ());
374- nbttagcompound .setDouble ("y" , saver .getClientY ());
375- nbttagcompound .setDouble ("z" , saver .getClientZ ());
376- nbttagcompound .setFloat ("RelativeX" , saver .getClientrX ());
377- nbttagcompound .setFloat ("RelativeY" , saver .getClientrY ());
378- nbttagcompound .setFloat ("RelativeZ" , saver .getClientrZ ());
379- nbttagcompound .setBoolean ("Sprinting" , saver .isSprinting ());
380- nbttagcompound .setFloat ("JumpFactor" , saver .getJumpMovementVector ());
381- compound .setTag ("clientMotion" , nbttagcompound );
382- }
383-
384- @ Override
385- public void onPlayerReadNBT (NBTTagCompound compound , EntityPlayerMP player ) {
386- NBTTagCompound nbttagcompound = compound .getCompoundTag ("clientMotion" );
387-
388- double clientmotionX = nbttagcompound .getDouble ("x" );
389- double clientmotionY = nbttagcompound .getDouble ("y" );
390- double clientmotionZ = nbttagcompound .getDouble ("z" );
391- float clientmotionrX = nbttagcompound .getFloat ("RelativeX" );
392- float clientmotionrY = nbttagcompound .getFloat ("RelativeY" );
393- float clientmotionrZ = nbttagcompound .getFloat ("RelativeZ" );
394- boolean sprinting = nbttagcompound .getBoolean ("Sprinting" );
395- float jumpVector = nbttagcompound .getFloat ("JumpFactor" );
396-
397- MotionData motion = new MotionData (clientmotionX , clientmotionY , clientmotionZ , clientmotionrX , clientmotionrY , clientmotionrZ , sprinting , jumpVector );
398- motionData .put (player , motion );
399- }
400230}
0 commit comments