Skip to content

Commit

Permalink
test done
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadron67 committed May 15, 2020
1 parent a48da20 commit 4b8ffd7
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 89 deletions.
103 changes: 72 additions & 31 deletions src/main/java/com/hadroncfy/sreplay/command/SReplayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import com.replaymod.replaystudio.data.Marker;

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -30,6 +31,7 @@
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandSource.suggestMatching;
import static com.hadroncfy.sreplay.recording.Photographer.MCPR;
import static com.hadroncfy.sreplay.config.TextRenderer.render;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -59,7 +61,9 @@ public void register(CommandDispatcher<ServerCommandSource> d) {
.then(buildPlayerParameterCommand())
.then(literal("pause").executes(this::pause))
.then(literal("resume").executes(this::resume))
.then(literal("marker").then(argument("marker", StringArgumentType.word()).executes(this::marker)))))
.then(literal("marker").executes(this::getMarkers)
.then(literal("add").then(argument("marker", StringArgumentType.word()).executes(this::marker)))
.then(literal("remove").then(argument("markerId", IntegerArgumentType.integer(1)).executes(this::removeMarker))))))
.then(literal("list").executes(this::listRecordings))
.then(literal("delete").then(argument("recording", StringArgumentType.word())
.suggests(this::suggestRecordingFile)
Expand All @@ -82,21 +86,52 @@ private CompletableFuture<Suggestions> suggestRecordingFile(CommandContext<Serve
return suggestMatching(SReplayMod.listRecordings().stream().map(f -> f.getName()), sb);
}

private int getMarkers(CommandContext<ServerCommandSource> ctx){
final Photographer p = requirePlayer(ctx);
if (p != null){
final String name = p.getGameProfile().getName();
final ServerCommandSource src = ctx.getSource();
src.sendFeedback(render(SReplayMod.getFormats().markerListTitle, name), false);
int i = 1;
for (Marker marker: p.getRecorder().getMarkers()){
src.sendFeedback(render(SReplayMod.getFormats().markerListItem, name, Integer.toString(i), marker.getName()), false);
i++;
}
}
return 0;
}

private int removeMarker(CommandContext<ServerCommandSource> ctx){
final Photographer p = requirePlayer(ctx);
if (p != null){
final String name = p.getGameProfile().getName();
final ServerCommandSource src = ctx.getSource();
final int id = IntegerArgumentType.getInteger(ctx, "markerId") - 1;
if (id < 0 || id >= p.getRecorder().getMarkers().size()){
src.sendError(SReplayMod.getFormats().invalidMarkerId);
return 1;
}
p.getRecorder().removeMarker(id);
src.sendFeedback(render(SReplayMod.getFormats().markerRemoved, name, Integer.toString(id + 1)), true);
}
return 0;
}

private int getFile(CommandContext<ServerCommandSource> ctx){
final String fName = StringArgumentType.getString(ctx, "fileName");
final File f = new File(SReplayMod.getConfig().savePath, fName);
if (f.exists()){
final String path = SReplayMod.getServer().addFile(f, 30000);
final String path = SReplayMod.getServer().addFile(f, SReplayMod.getConfig().downloadTimeout);
String url = "http://" + SReplayMod.getConfig().serverHostName;
final int port = SReplayMod.getConfig().serverPort;
if (port != 80){
url += ":" + port;
}
url += path;
ctx.getSource().sendFeedback(TextRenderer.render(SReplayMod.getFormats().downloadUrl, url), false);
ctx.getSource().sendFeedback(render(SReplayMod.getFormats().downloadUrl, url), false);
}
else {
ctx.getSource().sendError(TextRenderer.render(SReplayMod.getFormats().fileNotFound, fName));
ctx.getSource().sendError(render(SReplayMod.getFormats().fileNotFound, fName));
return 1;
}
return 0;
Expand All @@ -105,15 +140,20 @@ private int getFile(CommandContext<ServerCommandSource> ctx){
private int startServer(CommandContext<ServerCommandSource> ctx){
final ServerCommandSource src = ctx.getSource();
final MinecraftServer server = src.getMinecraftServer();
final ChannelFuture ch = SReplayMod.getServer().bind(SReplayMod.getConfig().serverHost, SReplayMod.getConfig().serverPort);
ch.addListener(future -> {
if (future.isSuccess()){
server.getPlayerManager().broadcastChatMessage(SReplayMod.getFormats().serverStarted, true);
}
else {
src.sendError(TextRenderer.render(SReplayMod.getFormats().serverStartFailed, future.cause().getMessage()));
}
});
try {
final ChannelFuture ch = SReplayMod.getServer().bind(SReplayMod.getConfig().serverHost, SReplayMod.getConfig().serverPort);
ch.addListener(future -> {
if (future.isSuccess()){
server.getPlayerManager().broadcastChatMessage(SReplayMod.getFormats().serverStarted, true);
}
else {
src.sendError(render(SReplayMod.getFormats().serverStartFailed, future.cause().getMessage()));
}
});
}
catch(Exception e){
e.printStackTrace();
}
return 0;
}

Expand All @@ -126,7 +166,7 @@ private int stopServer(CommandContext<ServerCommandSource> ctx){
server.getPlayerManager().broadcastChatMessage(SReplayMod.getFormats().serverStopped, true);
}
else {
src.sendError(TextRenderer.render(SReplayMod.getFormats().serverStopFailed, future.cause().getMessage()));
src.sendError(render(SReplayMod.getFormats().serverStopFailed, future.cause().getMessage()));
}
});
return 0;
Expand All @@ -135,7 +175,7 @@ private int stopServer(CommandContext<ServerCommandSource> ctx){
private int getName(CommandContext<ServerCommandSource> ctx){
Photographer p = requirePlayer(ctx);
if (p != null){
ctx.getSource().sendFeedback(TextRenderer.render(SReplayMod.getFormats().recordingFile, p.getGameProfile().getName(), p.getSaveName()), false);
ctx.getSource().sendFeedback(render(SReplayMod.getFormats().recordingFile, p.getGameProfile().getName(), p.getSaveName()), false);
return 1;
}
return 0;
Expand All @@ -149,10 +189,11 @@ private int setName(CommandContext<ServerCommandSource> ctx){
name = name.substring(0, name.length() - MCPR.length());
}
if (Photographer.checkForSaveFileDupe(ctx.getSource().getMinecraftServer(), SReplayMod.getConfig().savePath, name)){
ctx.getSource().sendError(TextRenderer.render(SReplayMod.getFormats().recordFileExists, name));
ctx.getSource().sendError(render(SReplayMod.getFormats().recordFileExists, name));
return 0;
}
p.setSaveName(name);
ctx.getSource().sendFeedback(render(SReplayMod.getFormats().renamedFile, p.getGameProfile().getName(), name), true);
return 1;
}
return 0;
Expand Down Expand Up @@ -217,7 +258,7 @@ private static Photographer requirePlayer(CommandContext<ServerCommandSource> ct
}
else {
try {
ctx.getSource().sendFeedback(TextRenderer.render(SReplayMod.getConfig().formats.playerNotFound, name), true);
ctx.getSource().sendFeedback(render(SReplayMod.getConfig().formats.playerNotFound, name), true);
}
catch(Exception e){
e.printStackTrace();
Expand All @@ -231,7 +272,7 @@ public int marker(CommandContext<ServerCommandSource> ctx){
if (p != null){
String name = StringArgumentType.getString(ctx, "marker");
p.getRecorder().addMarker(name);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(TextRenderer.render(SReplayMod.getFormats().markerAdded, p.getGameProfile().getName(), name), false);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(render(SReplayMod.getFormats().markerAdded, p.getGameProfile().getName(), name), false);
return 1;
}
else {
Expand All @@ -243,7 +284,7 @@ public int pause(CommandContext<ServerCommandSource> ctx){
Photographer p = requirePlayer(ctx);
if (p != null){
p.getRecorder().pauseRecording();
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(TextRenderer.render(SReplayMod.getFormats().recordingPaused, p.getGameProfile().getName()), false);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(render(SReplayMod.getFormats().recordingPaused, p.getGameProfile().getName()), false);
return 1;
}
else {
Expand All @@ -255,7 +296,7 @@ public int resume(CommandContext<ServerCommandSource> ctx){
Photographer p = requirePlayer(ctx);
if (p != null){
p.getRecorder().resumeRecording();
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(TextRenderer.render(SReplayMod.getFormats().recordingResumed, p.getGameProfile().getName()), false);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(render(SReplayMod.getFormats().recordingResumed, p.getGameProfile().getName()), false);
return 1;
}
else {
Expand All @@ -266,11 +307,11 @@ public int resume(CommandContext<ServerCommandSource> ctx){
public int reload(CommandContext<ServerCommandSource> ctx){
try {
SReplayMod.loadConfig();
ctx.getSource().sendFeedback(TextRenderer.render(SReplayMod.getFormats().reloadedConfig), false);
ctx.getSource().sendFeedback(render(SReplayMod.getFormats().reloadedConfig), false);
return 1;
} catch (IOException e) {
e.printStackTrace();
ctx.getSource().sendFeedback(TextRenderer.render(SReplayMod.getFormats().failedToReloadConfig, e.toString()), false);
ctx.getSource().sendFeedback(render(SReplayMod.getFormats().failedToReloadConfig, e.toString()), false);
return 0;
}
}
Expand All @@ -293,10 +334,10 @@ public int cancel(CommandContext<ServerCommandSource> ctx) {

public int listRecordings(CommandContext<ServerCommandSource> ctx) {
final ServerCommandSource src = ctx.getSource();
src.sendFeedback(SReplayMod.getFormats().recordingFileListHead, true);
src.sendFeedback(SReplayMod.getFormats().recordingFileListHead, false);
SReplayMod.listRecordings().forEach(f -> {
String size = String.format("%.2f", f.length() / 1024F / 1024F);
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().recordingFileItem, f.getName(), size), false);
src.sendFeedback(render(SReplayMod.getFormats().recordingFileItem, f.getName(), size), false);
});

return 0;
Expand All @@ -308,14 +349,14 @@ public int deleteRecording(CommandContext<ServerCommandSource> ctx) {
final MinecraftServer server = src.getMinecraftServer();
if (rec.exists()) {
String code = Integer.toString(rand.nextInt(100));
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().aboutToDeleteRecording, rec.getName()), true);
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().confirmingHint, code), false);
src.sendFeedback(render(SReplayMod.getFormats().aboutToDeleteRecording, rec.getName()), true);
src.sendFeedback(render(SReplayMod.getFormats().confirmingHint, code), false);
cm.submit(src.getName(), code, (match, cancelled) -> {
if (!cancelled) {
if (match) {
rec.delete();
server.getPlayerManager()
.sendToAll(TextRenderer.render(SReplayMod.getFormats().deletedRecordingFile, src.getName(), rec.getName()));
.sendToAll(render(SReplayMod.getFormats().deletedRecordingFile, src.getName(), rec.getName()));
} else {
src.sendFeedback(SReplayMod.getFormats().incorrectConfirmationCode, false);
}
Expand All @@ -324,7 +365,7 @@ public int deleteRecording(CommandContext<ServerCommandSource> ctx) {
}
});
} else {
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().fileNotFound, rec.getName()), true);
src.sendFeedback(render(SReplayMod.getFormats().fileNotFound, rec.getName()), true);
}
return 0;
}
Expand All @@ -334,7 +375,7 @@ public int playerTp(CommandContext<ServerCommandSource> ctx) {
if (p != null){
Vec3d pos = ctx.getSource().getPosition();
p.tp(ctx.getSource().getWorld().getDimension().getType(), pos.x, pos.y, pos.z);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(TextRenderer.render(SReplayMod.getFormats().teleportedBotToYou, p.getGameProfile().getName(), ctx.getSource().getName()), true);
ctx.getSource().getMinecraftServer().getPlayerManager().broadcastChatMessage(render(SReplayMod.getFormats().teleportedBotToYou, p.getGameProfile().getName(), ctx.getSource().getName()), true);
LOGGER.info("Teleported " + p.getGameProfile().getName() + " to " + ctx.getSource().getName());
return 1;
}
Expand All @@ -357,7 +398,7 @@ public int playerSpawn(CommandContext<ServerCommandSource> ctx) {
return 0;
}
if (server.getPlayerManager().getPlayer(pName) != null) {
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().playerIsLoggedIn, pName), true);
src.sendFeedback(render(SReplayMod.getFormats().playerIsLoggedIn, pName), true);
return 0;
}

Expand All @@ -373,7 +414,7 @@ public int playerSpawn(CommandContext<ServerCommandSource> ctx) {
Photographer photographer = Photographer.create(pName, server, src.getWorld().getDimension().getType(), src.getPosition(), SReplayMod.getConfig().savePath);
photographer.connect(saveName);
} catch (IOException e) {
src.sendFeedback(TextRenderer.render(SReplayMod.getFormats().failedToStartRecording, e.toString()), false);
src.sendFeedback(render(SReplayMod.getFormats().failedToStartRecording, e.toString()), false);
e.printStackTrace();
}
return 1;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/hadroncfy/sreplay/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Config {
public InetAddress serverHost = InetAddress.getLoopbackAddress();
public String serverHostName = "localhost";
public int serverPort = 12346;
public long downloadTimeout = 60000;
public long sizeLimit = -1;
public long timeLimit = -1;
public boolean autoReconnect = true;
Expand Down
Loading

0 comments on commit 4b8ffd7

Please sign in to comment.