Added backup done message, Moved notifyPlayer to Utilities (closed #71)
parent
cb55d5c9a4
commit
51330a036d
|
@ -18,9 +18,15 @@
|
|||
|
||||
package net.szum123321.textile_backup.core;
|
||||
|
||||
import net.minecraft.network.MessageType;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.world.World;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import net.szum123321.textile_backup.TextileLogger;
|
||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||
import net.szum123321.textile_backup.config.ConfigPOJO;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
|
@ -35,9 +41,23 @@ import java.time.*;
|
|||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Utilities {
|
||||
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||
|
||||
|
||||
public static void notifyPlayers(MinecraftServer server, UUID sender, String msg) {
|
||||
MutableText message = log.getPrefixText();
|
||||
message.append(new LiteralText(msg).formatted(Formatting.WHITE));
|
||||
|
||||
server.getPlayerManager().broadcastChatMessage(
|
||||
message,
|
||||
MessageType.SYSTEM,
|
||||
sender
|
||||
);
|
||||
}
|
||||
|
||||
public static String getLevelName(MinecraftServer server) {
|
||||
return ((MinecraftServerSessionAccessor)server).getSession().getDirectoryName();
|
||||
|
|
|
@ -21,9 +21,12 @@ package net.szum123321.textile_backup.core.create;
|
|||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record BackupContext(@NotNull MinecraftServer server,
|
||||
ServerCommandSource commandSource,
|
||||
ActionInitiator initiator,
|
||||
|
@ -54,6 +57,10 @@ public record BackupContext(@NotNull MinecraftServer server,
|
|||
return comment;
|
||||
}
|
||||
|
||||
public UUID getInitiatorUUID() {
|
||||
return initiator.equals(ActionInitiator.Player) && commandSource.getEntity() != null ? commandSource.getEntity().getUuid(): Util.NIL_UUID;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private MinecraftServer server;
|
||||
private ServerCommandSource commandSource;
|
||||
|
|
|
@ -18,17 +18,11 @@
|
|||
|
||||
package net.szum123321.textile_backup.core.create;
|
||||
|
||||
import net.minecraft.network.MessageType;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import net.szum123321.textile_backup.TextileLogger;
|
||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
|
@ -37,14 +31,16 @@ import java.time.LocalDateTime;
|
|||
import java.time.ZoneOffset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.UUID;
|
||||
|
||||
public class BackupHelper {
|
||||
private final static TextileLogger log = new TextileLogger(TextileBackup.MOD_NAME);
|
||||
private final static ConfigHelper config = ConfigHelper.INSTANCE;
|
||||
|
||||
public static Runnable create(BackupContext ctx) {
|
||||
notifyPlayers(ctx);
|
||||
Utilities.notifyPlayers(ctx.getServer(),
|
||||
ctx.getInitiatorUUID(),
|
||||
"Warning! Server backup will begin shortly. You may experience some lag."
|
||||
);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
|
@ -77,23 +73,6 @@ public class BackupHelper {
|
|||
return new MakeBackupRunnable(ctx);
|
||||
}
|
||||
|
||||
private static void notifyPlayers(BackupContext ctx) {
|
||||
MutableText message = log.getPrefixText();
|
||||
message.append(new LiteralText("Warning! Server backup will begin shortly. You may experience some lag.").formatted(Formatting.WHITE));
|
||||
|
||||
UUID uuid;
|
||||
|
||||
if(ctx.getInitiator().equals(ActionInitiator.Player) && ctx.getCommandSource().getEntity() != null)
|
||||
uuid = ctx.getCommandSource().getEntity().getUuid();
|
||||
else uuid = Util.NIL_UUID;
|
||||
|
||||
ctx.getServer().getPlayerManager().broadcastChatMessage(
|
||||
message,
|
||||
MessageType.SYSTEM,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
|
||||
public static int executeFileLimit(ServerCommandSource ctx, String worldName) {
|
||||
File root = Utilities.getBackupRootPath(worldName);
|
||||
int deletedFiles = 0;
|
||||
|
|
|
@ -113,7 +113,11 @@ public class MakeBackupRunnable implements Runnable {
|
|||
|
||||
BackupHelper.executeFileLimit(context.getCommandSource(), Utilities.getLevelName(context.getServer()));
|
||||
|
||||
log.sendInfoAL(context, "Done!");
|
||||
Utilities.notifyPlayers(
|
||||
context.getServer(),
|
||||
context.getInitiatorUUID(),
|
||||
"Done!"
|
||||
);
|
||||
} finally {
|
||||
Utilities.enableWorldSaving(context.getServer());
|
||||
Statics.disableWatchdog = false;
|
||||
|
|
|
@ -21,9 +21,11 @@ package net.szum123321.textile_backup.core.restore;
|
|||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.UUID;
|
||||
|
||||
public record RestoreContext(RestoreHelper.RestoreableFile file,
|
||||
MinecraftServer server,
|
||||
|
@ -48,6 +50,10 @@ public record RestoreContext(RestoreHelper.RestoreableFile file,
|
|||
return initiator;
|
||||
}
|
||||
|
||||
public UUID getInitiatorUUID() {
|
||||
return initiator.equals(ActionInitiator.Player) && commandSource.getEntity() != null ? commandSource.getEntity().getUuid(): Util.NIL_UUID;
|
||||
}
|
||||
|
||||
public ServerCommandSource getCommandSource() {
|
||||
return commandSource;
|
||||
}
|
||||
|
|
|
@ -18,12 +18,7 @@
|
|||
|
||||
package net.szum123321.textile_backup.core.restore;
|
||||
|
||||
import net.minecraft.network.MessageType;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.util.Formatting;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.TextileBackup;
|
||||
import net.szum123321.textile_backup.TextileLogger;
|
||||
import net.szum123321.textile_backup.config.ConfigHelper;
|
||||
|
@ -62,7 +57,11 @@ public class RestoreHelper {
|
|||
else
|
||||
log.info("Backup restoration was initiated form Server Console");
|
||||
|
||||
notifyPlayers(ctx);
|
||||
Utilities.notifyPlayers(
|
||||
ctx.server(),
|
||||
ctx.getInitiatorUUID(),
|
||||
"Warning! The server is going to shut down in " + config.get().restoreDelay + " seconds!"
|
||||
);
|
||||
|
||||
return new AwaitThread(
|
||||
config.get().restoreDelay,
|
||||
|
@ -70,30 +69,6 @@ public class RestoreHelper {
|
|||
);
|
||||
}
|
||||
|
||||
private static void notifyPlayers(RestoreContext ctx) {
|
||||
MutableText message = log.getPrefixText();
|
||||
message.append(
|
||||
new LiteralText(
|
||||
"Warning! The server is going to shut down in " +
|
||||
config.get().restoreDelay +
|
||||
" seconds!"
|
||||
).formatted(Formatting.WHITE)
|
||||
);
|
||||
|
||||
UUID uuid;
|
||||
|
||||
if(ctx.getInitiator().equals(ActionInitiator.Player) && ctx.getCommandSource().getEntity() != null)
|
||||
uuid = ctx.getCommandSource().getEntity().getUuid();
|
||||
else
|
||||
uuid = Util.NIL_UUID;
|
||||
|
||||
ctx.getServer().getPlayerManager().broadcastChatMessage(
|
||||
message,
|
||||
MessageType.SYSTEM,
|
||||
uuid
|
||||
);
|
||||
}
|
||||
|
||||
public static List<RestoreableFile> getAvailableBackups(MinecraftServer server) {
|
||||
File root = Utilities.getBackupRootPath(Utilities.getLevelName(server));
|
||||
|
||||
|
|
Loading…
Reference in New Issue