Skip to content

Commit

Permalink
[PlaybackSerialiser] Starting with CommandFileCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed Jul 9, 2024
1 parent bd2e0f0 commit 102a8a8
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,135 @@
package com.minecrafttas.tasmod.commands;

public class CommandFileCommand {
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import com.minecrafttas.mctcommon.networking.exception.PacketNotImplementedException;
import com.minecrafttas.mctcommon.networking.exception.WrongSideException;
import com.minecrafttas.mctcommon.networking.interfaces.ClientPacketHandler;
import com.minecrafttas.mctcommon.networking.interfaces.PacketID;
import com.minecrafttas.mctcommon.networking.interfaces.ServerPacketHandler;
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
import com.minecrafttas.tasmod.registries.TASmodPackets;

import static com.minecrafttas.tasmod.registries.TASmodPackets.*;

import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.command.PlayerNotFoundException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;

public class CommandFileCommand extends CommandBase implements ClientPacketHandler, ServerPacketHandler {

CompletableFuture<List<String>> fileCommandList = null;

@Override
public String getName() {
return "filecommand";
}

@Override
public String getUsage(ICommandSender iCommandSender) {
return "/filecommand <filecommandname> [enable|disable]";
}

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
if (sender instanceof EntityPlayer) {
if (sender.canUseCommand(2, "fileCommand")) {

// Get the list of file commands from the server
List<String> names;
try {
names = TASmod.tabCompletionUtils.getFileCommandList(getCommandSenderAsPlayer(sender).getName());
} catch (PlayerNotFoundException | InterruptedException | ExecutionException | TimeoutException e) {
sender.sendMessage(new TextComponentString(e.getMessage()));
return;
}

if (args.length == 0) {
sender.sendMessage(new TextComponentString(String.join(" ", names)));
}
if (args.length == 1) {
sender.sendMessage(new TextComponentString(TextFormatting.RED + "Please add a filecommand " + getUsage(sender)));
} else if (args.length == 1) {
String filename = args[0];
try {
TASmod.server.sendToAll(new TASmodBufferBuilder(PLAYBACK_LOAD).writeString(filename).writeString(""));
} catch (Exception e) {
e.printStackTrace();
}
} else if (args.length == 2) {
String filename = args[0];
String flavorname = args[1];
try {
TASmod.server.sendToAll(new TASmodBufferBuilder(PLAYBACK_LOAD).writeString(filename).writeString(flavorname));
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
sender.sendMessage(new TextComponentString(TextFormatting.RED + "You have no permission to use this command"));
}
}
}

private List<PlaybackFileCommandExtension> getExtensions(String playername) throws InterruptedException, ExecutionException, TimeoutException {
List<PlaybackFileCommandExtension> out = new ArrayList<>();
fileCommandList = new CompletableFuture<>();

List<String> commands = fileCommandList.get(2, TimeUnit.SECONDS);

return out;
}

@Override
public PacketID[] getAcceptedPacketIDs() {
return new PacketID[] { COMMAND_FLAVORLIST };
}

@Override
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
switch (packet) {
case COMMAND_FILECOMMANDLIST:
String filecommandnames = TASmodBufferBuilder.readString(buf);
fileCommandList.complete(Arrays.asList(filecommandnames.split("|")));
break;
default:
break;
}
}

@Override
public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
switch (packet) {
case COMMAND_FILECOMMANDLIST:
String filecommandnames = String.join("|", getFileCommandNames(TASmodAPIRegistry.PLAYBACK_FILE_COMMAND.getAll()));
TASmodClient.client.send(new TASmodBufferBuilder(COMMAND_FILECOMMANDLIST).writeString(filecommandnames));
}
}

private List<String> getFileCommandNames(List<PlaybackFileCommandExtension> fileCommands) {
List<String> out = new ArrayList<>();
fileCommands.forEach(element -> {
out.add(String.format("%s_%s", element.isEnabled() ? "E" : "D", element.toString()));
});
return out;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,78 +21,97 @@
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.networking.TASmodBufferBuilder;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
import com.minecrafttas.tasmod.registries.TASmodAPIRegistry;
import com.minecrafttas.tasmod.registries.TASmodPackets;

import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextFormatting;

public class TabCompletionUtils implements ServerPacketHandler, ClientPacketHandler{
public class TabCompletionUtils implements ServerPacketHandler, ClientPacketHandler {

private volatile CompletableFuture<List<String>> fileList = null;
private volatile CompletableFuture<List<String>> flavorList = null;

private volatile CompletableFuture<List<String>> fileCommandList = null;

@Override
public PacketID[] getAcceptedPacketIDs() {
return new PacketID[] { COMMAND_TASFILELIST, COMMAND_FLAVORLIST };
}

//======== SERVER SIDE

@Override
public void onServerPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
switch (packet) {
case COMMAND_TASFILELIST:
String filenames = TASmodBufferBuilder.readString(buf);
fileList.complete(Arrays.asList(filenames.split("/")));
fileList.complete(Arrays.asList(filenames.split("|")));
break;
case COMMAND_FLAVORLIST:
String flavornames = TASmodBufferBuilder.readString(buf);
flavorList.complete(Arrays.asList(flavornames.split("/")));
flavorList.complete(Arrays.asList(flavornames.split("|")));
break;
case COMMAND_FILECOMMANDLIST:
String filecommandnames = TASmodBufferBuilder.readString(buf);
fileCommandList.complete(Arrays.asList(filecommandnames.split("|")));
default:
break;
}
}

public List<String> getTASFileList(String playername) throws InterruptedException, ExecutionException, TimeoutException {
fileList = new CompletableFuture<>();
try {
TASmod.server.sendTo(playername, new TASmodBufferBuilder(COMMAND_TASFILELIST));
} catch (Exception e) {
e.printStackTrace();
TASmod.LOGGER.catching(e);
}
return fileList.get(2,TimeUnit.SECONDS);
return fileList.get(2, TimeUnit.SECONDS);
}

public List<String> getFlavorList(String playername) throws InterruptedException, ExecutionException, TimeoutException {
flavorList = new CompletableFuture<>();
try {
TASmod.server.sendTo(playername, new TASmodBufferBuilder(COMMAND_FLAVORLIST));
} catch (Exception e) {
e.printStackTrace();
TASmod.LOGGER.catching(e);
}
return flavorList.get(2, TimeUnit.SECONDS);
}

public List<String> getFileCommandList(String playername) throws InterruptedException, ExecutionException, TimeoutException {
fileCommandList = new CompletableFuture<>();
try {
TASmod.server.sendTo(playername, new TASmodBufferBuilder(COMMAND_FILECOMMANDLIST));
} catch (Exception e) {
TASmod.LOGGER.catching(e);
}
return flavorList.get(2,TimeUnit.SECONDS);
return fileCommandList.get(2, TimeUnit.SECONDS);
}

//======== CLIENT SIDE

@Override
public void onClientPacket(PacketID id, ByteBuffer buf, String username) throws PacketNotImplementedException, WrongSideException, Exception {
TASmodPackets packet = (TASmodPackets) id;
switch (packet) {
case COMMAND_TASFILELIST:
String filenames = String.join("/", getFilenames());
String filenames = String.join("|", getFilenames());
TASmodClient.client.send(new TASmodBufferBuilder(COMMAND_TASFILELIST).writeString(filenames));
break;

case COMMAND_FLAVORLIST:
String flavornames = String.join("/", TASmodAPIRegistry.SERIALISER_FLAVOR.getFlavorNames());
String flavornames = String.join("|", TASmodAPIRegistry.SERIALISER_FLAVOR.getFlavorNames());
TASmodClient.client.send(new TASmodBufferBuilder(COMMAND_FLAVORLIST).writeString(flavornames));
break;

default:
break;
}
}

private List<String> getFilenames() {
List<String> tab = new ArrayList<String>();
File folder = new File(Minecraft.getMinecraft().mcDataDir, "saves" + File.separator + "tasfiles");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public PlaybackFileCommand(String name, String... args) {
public String getName() {
return name;
}

public String[] getArgs() {
return args;
}
Expand Down Expand Up @@ -95,6 +95,11 @@ public void setEnabled(boolean enabled) {
onDisable();
this.enabled = enabled;
}

@Override
public String toString() {
return getExtensionName();
}
}

public static class PlaybackFileCommandContainer extends LinkedHashMap<String, PlaybackFileCommandLine> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ public List<PlaybackFileCommandExtension> getEnabled() {

return out;
}


public List<PlaybackFileCommandExtension> getAll(){
return new ArrayList<>(REGISTRY.values());
}

@Override
public void onRecordTick(long index, TickContainer container) {
enabledExtensions.forEach(extension -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.minecrafttas.tasmod.commands.CommandFolder;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TASstate;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.PlayerHandler.MotionData;
import com.minecrafttas.tasmod.tickratechanger.TickrateChangerServer.TickratePauseState;

Expand Down Expand Up @@ -184,17 +187,25 @@ public enum TASmodPackets implements PacketID {
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Server->Client</strong> None<br>
* <strong>Client->Server</strong> String The string of TASfilenames seperated with /
* <strong>Client->Server</strong> String The string of TASfilenames seperated with |
*/
COMMAND_TASFILELIST,
/**
* <p>Requests the list of SerialiserFlavors from the client for use in tab completions
* <p>Requests the list of {@link SerialiserFlavorBase SerialiserFlavors} from the client for use in tab completions
* <p>SIDE: Both<br>
* ARGS: <br>
* <strong>Server->Client</strong> None<br>
* <strong>Client->Server</strong> String The string of flavors seperated with /
* <strong>Client->Server</strong> String The string of flavors seperated with |
*/
COMMAND_FLAVORLIST,
/**
* <p>Requests the list of {@link PlaybackFileCommandExtension PlaybackFileCommandExtensions} from the client for use in tab completions
* <p>SIDE: Bith<br>
* ARGS: <br>
* <strong>Server->Client</strong> None<br>
* <strong>Client->Server</strong> String The string of file command names, seperated with |
*/
COMMAND_FILECOMMANDLIST,
/**
* <p>Sets the KillTheRNG seed
* <p>SIDE: Both<br>
Expand Down

0 comments on commit 102a8a8

Please sign in to comment.