Finally no double output!
This commit is contained in:
parent
d27568c20f
commit
7c07d2934c
@ -78,7 +78,7 @@ public class TextileBackup implements ModInitializer {
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer(server)
|
||||
.setInitiator(BackupContext.BackupInitiator.Shutdown)
|
||||
.setInitiator(ActionInitiator.Shutdown)
|
||||
.setComment("shutdown")
|
||||
.build()
|
||||
).run();
|
||||
|
@ -19,6 +19,7 @@
|
||||
package net.szum123321.textile_backup.commands.restore;
|
||||
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.server.command.CommandManager;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
@ -30,11 +31,16 @@ public class KillRestoreCommand {
|
||||
if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
|
||||
Statics.restoreAwaitThread.interrupt();
|
||||
Statics.globalShutdownBackupFlag.set(true);
|
||||
Statics.LOGGER.sendInfo(ctx.getSource(), "Backup restoration successfully stopped.");
|
||||
Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() != null ?
|
||||
Statics.untouchableFile = null;
|
||||
|
||||
Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() instanceof PlayerEntity ?
|
||||
"Player: " + ctx.getSource().getName() :
|
||||
"SERVER"
|
||||
);
|
||||
|
||||
if(ctx.getSource().getEntity() instanceof PlayerEntity)
|
||||
Statics.LOGGER.sendInfo(ctx.getSource(), "Backup restoration successfully stopped.");
|
||||
|
||||
} else {
|
||||
Statics.LOGGER.sendInfo(ctx.getSource(), "Failed to stop backup restoration");
|
||||
}
|
||||
|
@ -124,7 +124,7 @@ public class RestoreBackupCommand {
|
||||
String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter);
|
||||
|
||||
if(formattedCreationTime.startsWith(remaining)) {
|
||||
if(ctx.getSource().getEntity() != null) { //was typed by player
|
||||
if(ctx.getSource().getEntity() instanceof PlayerEntity) { //was typed by player
|
||||
if(file.getComment() != null) {
|
||||
builder.suggest(formattedCreationTime, new LiteralMessage("Comment: " + file.getComment()));
|
||||
} else {
|
||||
|
@ -18,7 +18,7 @@
|
||||
|
||||
package net.szum123321.textile_backup.core;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.text.MutableText;
|
||||
@ -35,7 +35,7 @@ import org.apache.logging.log4j.spi.StandardLevel;
|
||||
This is practically just a copy-pate of Cotton's ModLogger with a few changes
|
||||
*/
|
||||
public class CustomLogger {
|
||||
private final boolean isDev = FabricLoader.getInstance().isDevelopmentEnvironment();
|
||||
//private final boolean isDev = FabricLoader.getInstance().isDevelopmentEnvironment();
|
||||
|
||||
private final MessageFactory messageFactory;
|
||||
private final Logger logger;
|
||||
@ -82,38 +82,22 @@ public class CustomLogger {
|
||||
log(Level.FATAL, msg, data);
|
||||
}
|
||||
|
||||
public void devError(String msg, Object... data) {
|
||||
if (isDev) error(msg, data);
|
||||
}
|
||||
|
||||
public void devWarn(String msg, Object... data) {
|
||||
if (isDev) warn(msg, data);
|
||||
}
|
||||
|
||||
public void devInfo(String msg, Object... data) {
|
||||
if (isDev) info(msg, data);
|
||||
}
|
||||
|
||||
public void devDebug(String msg, Object... data) {
|
||||
if (isDev) debug(msg, data);
|
||||
}
|
||||
|
||||
public void devTrace(String msg, Object... data) {
|
||||
if(isDev) trace(msg, data);
|
||||
}
|
||||
|
||||
private void sendToPlayer(Level level, ServerCommandSource source, String msg, Object... args) {
|
||||
if(source != null && source.getEntity() != null) {
|
||||
boolean sendToPlayer(Level level, ServerCommandSource source, String msg, Object... args) {
|
||||
if(source != null && source.getEntity() instanceof PlayerEntity) {
|
||||
LiteralText text = new LiteralText(messageFactory.newMessage(msg, args).getFormattedMessage());
|
||||
|
||||
if(level.intLevel() <= StandardLevel.WARN.intLevel())
|
||||
if(level.intLevel() < StandardLevel.WARN.intLevel())
|
||||
text.formatted(Formatting.RED);
|
||||
else
|
||||
text.formatted(Formatting.WHITE);
|
||||
|
||||
source.sendFeedback(prefixText.shallowCopy().append(text), false);
|
||||
|
||||
return true;
|
||||
} else {
|
||||
logger.log(level, msg, args);
|
||||
log(level, msg, args);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,4 +116,26 @@ public class CustomLogger {
|
||||
public void sendError(BackupContext context, String msg, Object... args) {
|
||||
sendError(context.getCommandSource(), msg, args);
|
||||
}
|
||||
|
||||
public void sendToPlayerAndLog(Level level, ServerCommandSource source, String msg, Object... args) {
|
||||
if(sendToPlayer(level, source, msg, args))
|
||||
log(level, msg, args);
|
||||
}
|
||||
|
||||
//send info and log
|
||||
public void sendInfoAL(ServerCommandSource source, String msg, Object... args) {
|
||||
sendToPlayerAndLog(Level.INFO, source, msg, args);
|
||||
}
|
||||
|
||||
public void sendInfoAL(BackupContext context, String msg, Object... args) {
|
||||
sendInfoAL(context.getCommandSource(), msg, args);
|
||||
}
|
||||
|
||||
public void sendErrorAL(ServerCommandSource source, String msg, Object... args) {
|
||||
sendToPlayerAndLog(Level.ERROR, source, msg, args);
|
||||
}
|
||||
|
||||
public void sendErrorAL(BackupContext context, String msg, Object... args) {
|
||||
sendErrorAL(context.getCommandSource(), msg, args);
|
||||
}
|
||||
}
|
||||
|
@ -18,18 +18,20 @@
|
||||
|
||||
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.szum123321.textile_backup.core.ActionInitiator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class BackupContext {
|
||||
private final MinecraftServer server;
|
||||
private final ServerCommandSource commandSource;
|
||||
private final BackupInitiator initiator;
|
||||
private final ActionInitiator initiator;
|
||||
private final boolean save;
|
||||
private final String comment;
|
||||
|
||||
protected BackupContext(@NotNull MinecraftServer server, ServerCommandSource commandSource, @NotNull BackupInitiator initiator, boolean save, String comment) {
|
||||
protected BackupContext(@NotNull MinecraftServer server, ServerCommandSource commandSource, @NotNull ActionInitiator initiator, boolean save, String comment) {
|
||||
this.server = server;
|
||||
this.commandSource = commandSource;
|
||||
this.initiator = initiator;
|
||||
@ -45,12 +47,12 @@ public class BackupContext {
|
||||
return commandSource;
|
||||
}
|
||||
|
||||
public BackupInitiator getInitiator() {
|
||||
public ActionInitiator getInitiator() {
|
||||
return initiator;
|
||||
}
|
||||
|
||||
public boolean startedByPlayer() {
|
||||
return initiator == BackupInitiator.Player;
|
||||
return initiator == ActionInitiator.Player;
|
||||
}
|
||||
|
||||
public boolean shouldSave() {
|
||||
@ -64,7 +66,7 @@ public class BackupContext {
|
||||
public static class Builder {
|
||||
private MinecraftServer server;
|
||||
private ServerCommandSource commandSource;
|
||||
private BackupInitiator initiator;
|
||||
private ActionInitiator initiator;
|
||||
private boolean save;
|
||||
private String comment;
|
||||
|
||||
@ -80,6 +82,10 @@ public class BackupContext {
|
||||
guessInitiator = false;
|
||||
}
|
||||
|
||||
public static Builder newBackupContextBuilder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public Builder setCommandSource(ServerCommandSource commandSource) {
|
||||
this.commandSource = commandSource;
|
||||
return this;
|
||||
@ -90,7 +96,7 @@ public class BackupContext {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder setInitiator(BackupInitiator initiator) {
|
||||
public Builder setInitiator(ActionInitiator initiator) {
|
||||
this.initiator = initiator;
|
||||
return this;
|
||||
}
|
||||
@ -112,9 +118,9 @@ public class BackupContext {
|
||||
|
||||
public BackupContext build() {
|
||||
if(guessInitiator) {
|
||||
initiator = commandSource.getEntity() == null ? BackupInitiator.ServerConsole : BackupInitiator.Player;
|
||||
initiator = commandSource.getEntity() instanceof PlayerEntity ? ActionInitiator.Player : ActionInitiator.ServerConsole;
|
||||
} else if(initiator == null) {
|
||||
initiator = BackupInitiator.Null;
|
||||
initiator = ActionInitiator.Null;
|
||||
}
|
||||
|
||||
if(server == null) {
|
||||
|
@ -23,6 +23,7 @@ import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
@ -32,7 +33,6 @@ import java.time.ZoneOffset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class BackupHelper {
|
||||
@ -57,8 +57,7 @@ public class BackupHelper {
|
||||
Statics.LOGGER.info(builder.toString());
|
||||
|
||||
if (ctx.shouldSave()) {
|
||||
Statics.LOGGER.sendInfo(ctx.getCommandSource(), "Saving server...");
|
||||
Statics.LOGGER.info( "Saving server...");
|
||||
Statics.LOGGER.sendInfoAL(ctx, "Saving server...");
|
||||
|
||||
ctx.getServer().save(true, true, true);
|
||||
|
||||
@ -72,14 +71,11 @@ public class BackupHelper {
|
||||
MutableText message = Statics.LOGGER.getPrefixText().shallowCopy();
|
||||
message.append("Warning! Server backup will begin shortly. You may experience some lag.");
|
||||
|
||||
UUID uuid;
|
||||
|
||||
if(ctx.getCommandSource().getEntity() != null)
|
||||
uuid = ctx.getCommandSource().getEntity().getUuid();
|
||||
else
|
||||
uuid = Util.NIL_UUID;
|
||||
|
||||
ctx.getServer().getPlayerManager().broadcastChatMessage(message, MessageType.GAME_INFO, uuid);
|
||||
ctx.getServer().getPlayerManager().broadcastChatMessage(
|
||||
message,
|
||||
MessageType.GAME_INFO,
|
||||
ctx.getInitiator() == ActionInitiator.Player ? ctx.getCommandSource().getEntity().getUuid() : Util.NIL_UUID
|
||||
);
|
||||
}
|
||||
|
||||
public static int executeFileLimit(ServerCommandSource ctx, String worldName) {
|
||||
@ -134,11 +130,10 @@ public class BackupHelper {
|
||||
private static boolean deleteFile(File f, ServerCommandSource ctx) {
|
||||
if(f != Statics.untouchableFile) {
|
||||
if(f.delete()) {
|
||||
Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName());
|
||||
Statics.LOGGER.info("Deleting: {}", f.getName());
|
||||
Statics.LOGGER.sendInfoAL(ctx, "Deleting: {}", f.getName());
|
||||
return true;
|
||||
} else {
|
||||
Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName());
|
||||
Statics.LOGGER.sendErrorAL(ctx, "Something went wrong while deleting: {}.", f.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ package net.szum123321.textile_backup.core.create;
|
||||
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@ -42,7 +43,7 @@ public class BackupScheduler {
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer(server)
|
||||
.setInitiator(BackupContext.BackupInitiator.Timer)
|
||||
.setInitiator(ActionInitiator.Timer)
|
||||
.saveServer()
|
||||
.build()
|
||||
)
|
||||
@ -60,7 +61,7 @@ public class BackupScheduler {
|
||||
BackupHelper.create(
|
||||
new BackupContext.Builder()
|
||||
.setServer(server)
|
||||
.setInitiator(BackupContext.BackupInitiator.Timer)
|
||||
.setInitiator(ActionInitiator.Timer)
|
||||
.saveServer()
|
||||
.build()
|
||||
)
|
||||
|
@ -61,7 +61,9 @@ public class MakeBackupRunnable implements Runnable {
|
||||
outFile.createNewFile();
|
||||
} catch (IOException e) {
|
||||
Statics.LOGGER.error("An exception occurred when trying to create new backup file!", e);
|
||||
Statics.LOGGER.sendError(context.getCommandSource(), "An exception occurred when trying to create new backup file!");
|
||||
|
||||
if(context.getInitiator() == ActionInitiator.Player)
|
||||
Statics.LOGGER.sendError(context, "An exception occurred when trying to create new backup file!");
|
||||
|
||||
return;
|
||||
}
|
||||
@ -99,7 +101,9 @@ public class MakeBackupRunnable implements Runnable {
|
||||
|
||||
default:
|
||||
Statics.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", Statics.CONFIG.format);
|
||||
Statics.LOGGER.sendError(context.getCommandSource(), "Error! No correct compression format specified! Using default compressor!");
|
||||
|
||||
if(context.getInitiator() == ActionInitiator.Player)
|
||||
Statics.LOGGER.sendError(context.getCommandSource(), "Error! No correct compression format specified! Using default compressor!");
|
||||
|
||||
ZipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
|
||||
break;
|
||||
@ -107,8 +111,7 @@ public class MakeBackupRunnable implements Runnable {
|
||||
|
||||
BackupHelper.executeFileLimit(context.getCommandSource(), Utilities.getLevelName(context.getServer()));
|
||||
|
||||
Statics.LOGGER.sendInfo(context, "Done!");
|
||||
Statics.LOGGER.info("Done!");
|
||||
Statics.LOGGER.sendInfoAL(context, "Done!");
|
||||
} finally {
|
||||
Utilities.enableWorldSaving(context.getServer());
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
package net.szum123321.textile_backup.core.create.compressors;
|
||||
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
import net.szum123321.textile_backup.core.create.BackupContext;
|
||||
|
||||
@ -46,19 +47,23 @@ public abstract class AbstractCompressor {
|
||||
addEntry(file, inputFile.toPath().relativize(file.toPath()).toString(), arc);
|
||||
} catch (IOException e) {
|
||||
Statics.LOGGER.error("An exception occurred while trying to compress: {}", file.getName(), e);
|
||||
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
|
||||
|
||||
if(ctx.getInitiator() == ActionInitiator.Player)
|
||||
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
|
||||
}
|
||||
});
|
||||
|
||||
finish(arc);
|
||||
} catch (IOException | InterruptedException | ExecutionException e) {
|
||||
Statics.LOGGER.error("An exception occurred!", e);
|
||||
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
|
||||
|
||||
if(ctx.getInitiator() == ActionInitiator.Player)
|
||||
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
|
||||
}
|
||||
|
||||
close();
|
||||
|
||||
Statics.LOGGER.sendInfo(ctx, "Compression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||
Statics.LOGGER.sendInfoAL(ctx, "Compression took: {} seconds.", Utilities.formatDuration(Duration.between(start, Instant.now())));
|
||||
}
|
||||
|
||||
protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException;
|
||||
|
@ -24,6 +24,7 @@ import net.minecraft.text.MutableText;
|
||||
import net.minecraft.util.Util;
|
||||
import net.szum123321.textile_backup.ConfigHandler;
|
||||
import net.szum123321.textile_backup.Statics;
|
||||
import net.szum123321.textile_backup.core.ActionInitiator;
|
||||
import net.szum123321.textile_backup.core.Utilities;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user