Finally no double output!

56-bugfix
szymon 2020-12-02 20:57:49 +01:00
parent d27568c20f
commit 7c07d2934c
10 changed files with 84 additions and 61 deletions

View File

@ -78,7 +78,7 @@ public class TextileBackup implements ModInitializer {
BackupHelper.create( BackupHelper.create(
new BackupContext.Builder() new BackupContext.Builder()
.setServer(server) .setServer(server)
.setInitiator(BackupContext.BackupInitiator.Shutdown) .setInitiator(ActionInitiator.Shutdown)
.setComment("shutdown") .setComment("shutdown")
.build() .build()
).run(); ).run();

View File

@ -19,6 +19,7 @@
package net.szum123321.textile_backup.commands.restore; package net.szum123321.textile_backup.commands.restore;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
@ -30,11 +31,16 @@ public class KillRestoreCommand {
if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) { if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
Statics.restoreAwaitThread.interrupt(); Statics.restoreAwaitThread.interrupt();
Statics.globalShutdownBackupFlag.set(true); Statics.globalShutdownBackupFlag.set(true);
Statics.LOGGER.sendInfo(ctx.getSource(), "Backup restoration successfully stopped."); Statics.untouchableFile = null;
Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() != null ?
Statics.LOGGER.info("{} cancelled backup restoration.", ctx.getSource().getEntity() instanceof PlayerEntity ?
"Player: " + ctx.getSource().getName() : "Player: " + ctx.getSource().getName() :
"SERVER" "SERVER"
); );
if(ctx.getSource().getEntity() instanceof PlayerEntity)
Statics.LOGGER.sendInfo(ctx.getSource(), "Backup restoration successfully stopped.");
} else { } else {
Statics.LOGGER.sendInfo(ctx.getSource(), "Failed to stop backup restoration"); Statics.LOGGER.sendInfo(ctx.getSource(), "Failed to stop backup restoration");
} }

View File

@ -124,7 +124,7 @@ public class RestoreBackupCommand {
String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter); String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter);
if(formattedCreationTime.startsWith(remaining)) { 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) { if(file.getComment() != null) {
builder.suggest(formattedCreationTime, new LiteralMessage("Comment: " + file.getComment())); builder.suggest(formattedCreationTime, new LiteralMessage("Comment: " + file.getComment()));
} else { } else {

View File

@ -18,7 +18,7 @@
package net.szum123321.textile_backup.core; 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.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText; 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 This is practically just a copy-pate of Cotton's ModLogger with a few changes
*/ */
public class CustomLogger { public class CustomLogger {
private final boolean isDev = FabricLoader.getInstance().isDevelopmentEnvironment(); //private final boolean isDev = FabricLoader.getInstance().isDevelopmentEnvironment();
private final MessageFactory messageFactory; private final MessageFactory messageFactory;
private final Logger logger; private final Logger logger;
@ -82,38 +82,22 @@ public class CustomLogger {
log(Level.FATAL, msg, data); log(Level.FATAL, msg, data);
} }
public void devError(String msg, Object... data) { boolean sendToPlayer(Level level, ServerCommandSource source, String msg, Object... args) {
if (isDev) error(msg, data); if(source != null && source.getEntity() instanceof PlayerEntity) {
}
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) {
LiteralText text = new LiteralText(messageFactory.newMessage(msg, args).getFormattedMessage()); 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); text.formatted(Formatting.RED);
else else
text.formatted(Formatting.WHITE); text.formatted(Formatting.WHITE);
source.sendFeedback(prefixText.shallowCopy().append(text), false); source.sendFeedback(prefixText.shallowCopy().append(text), false);
return true;
} else { } 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) { public void sendError(BackupContext context, String msg, Object... args) {
sendError(context.getCommandSource(), msg, 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);
}
} }

View File

@ -18,18 +18,20 @@
package net.szum123321.textile_backup.core.create; package net.szum123321.textile_backup.core.create;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource; import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.core.ActionInitiator;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class BackupContext { public class BackupContext {
private final MinecraftServer server; private final MinecraftServer server;
private final ServerCommandSource commandSource; private final ServerCommandSource commandSource;
private final BackupInitiator initiator; private final ActionInitiator initiator;
private final boolean save; private final boolean save;
private final String comment; 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.server = server;
this.commandSource = commandSource; this.commandSource = commandSource;
this.initiator = initiator; this.initiator = initiator;
@ -45,12 +47,12 @@ public class BackupContext {
return commandSource; return commandSource;
} }
public BackupInitiator getInitiator() { public ActionInitiator getInitiator() {
return initiator; return initiator;
} }
public boolean startedByPlayer() { public boolean startedByPlayer() {
return initiator == BackupInitiator.Player; return initiator == ActionInitiator.Player;
} }
public boolean shouldSave() { public boolean shouldSave() {
@ -64,7 +66,7 @@ public class BackupContext {
public static class Builder { public static class Builder {
private MinecraftServer server; private MinecraftServer server;
private ServerCommandSource commandSource; private ServerCommandSource commandSource;
private BackupInitiator initiator; private ActionInitiator initiator;
private boolean save; private boolean save;
private String comment; private String comment;
@ -80,6 +82,10 @@ public class BackupContext {
guessInitiator = false; guessInitiator = false;
} }
public static Builder newBackupContextBuilder() {
return new Builder();
}
public Builder setCommandSource(ServerCommandSource commandSource) { public Builder setCommandSource(ServerCommandSource commandSource) {
this.commandSource = commandSource; this.commandSource = commandSource;
return this; return this;
@ -90,7 +96,7 @@ public class BackupContext {
return this; return this;
} }
public Builder setInitiator(BackupInitiator initiator) { public Builder setInitiator(ActionInitiator initiator) {
this.initiator = initiator; this.initiator = initiator;
return this; return this;
} }
@ -112,9 +118,9 @@ public class BackupContext {
public BackupContext build() { public BackupContext build() {
if(guessInitiator) { if(guessInitiator) {
initiator = commandSource.getEntity() == null ? BackupInitiator.ServerConsole : BackupInitiator.Player; initiator = commandSource.getEntity() instanceof PlayerEntity ? ActionInitiator.Player : ActionInitiator.ServerConsole;
} else if(initiator == null) { } else if(initiator == null) {
initiator = BackupInitiator.Null; initiator = ActionInitiator.Null;
} }
if(server == null) { if(server == null) {

View File

@ -23,6 +23,7 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.MutableText; import net.minecraft.text.MutableText;
import net.minecraft.util.Util; import net.minecraft.util.Util;
import net.szum123321.textile_backup.Statics; 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.Utilities;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -32,7 +33,6 @@ import java.time.ZoneOffset;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator; import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class BackupHelper { public class BackupHelper {
@ -57,8 +57,7 @@ public class BackupHelper {
Statics.LOGGER.info(builder.toString()); Statics.LOGGER.info(builder.toString());
if (ctx.shouldSave()) { if (ctx.shouldSave()) {
Statics.LOGGER.sendInfo(ctx.getCommandSource(), "Saving server..."); Statics.LOGGER.sendInfoAL(ctx, "Saving server...");
Statics.LOGGER.info( "Saving server...");
ctx.getServer().save(true, true, true); ctx.getServer().save(true, true, true);
@ -72,14 +71,11 @@ public class BackupHelper {
MutableText message = Statics.LOGGER.getPrefixText().shallowCopy(); MutableText message = Statics.LOGGER.getPrefixText().shallowCopy();
message.append("Warning! Server backup will begin shortly. You may experience some lag."); message.append("Warning! Server backup will begin shortly. You may experience some lag.");
UUID uuid; ctx.getServer().getPlayerManager().broadcastChatMessage(
message,
if(ctx.getCommandSource().getEntity() != null) MessageType.GAME_INFO,
uuid = ctx.getCommandSource().getEntity().getUuid(); ctx.getInitiator() == ActionInitiator.Player ? ctx.getCommandSource().getEntity().getUuid() : Util.NIL_UUID
else );
uuid = Util.NIL_UUID;
ctx.getServer().getPlayerManager().broadcastChatMessage(message, MessageType.GAME_INFO, uuid);
} }
public static int executeFileLimit(ServerCommandSource ctx, String worldName) { public static int executeFileLimit(ServerCommandSource ctx, String worldName) {
@ -134,11 +130,10 @@ public class BackupHelper {
private static boolean deleteFile(File f, ServerCommandSource ctx) { private static boolean deleteFile(File f, ServerCommandSource ctx) {
if(f != Statics.untouchableFile) { if(f != Statics.untouchableFile) {
if(f.delete()) { if(f.delete()) {
Statics.LOGGER.sendInfo(ctx, "Deleting: {}", f.getName()); Statics.LOGGER.sendInfoAL(ctx, "Deleting: {}", f.getName());
Statics.LOGGER.info("Deleting: {}", f.getName());
return true; return true;
} else { } else {
Statics.LOGGER.sendError(ctx, "Something went wrong while deleting: {}.", f.getName()); Statics.LOGGER.sendErrorAL(ctx, "Something went wrong while deleting: {}.", f.getName());
} }
} }

View File

@ -20,6 +20,7 @@ package net.szum123321.textile_backup.core.create;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.core.ActionInitiator;
import java.time.Instant; import java.time.Instant;
@ -42,7 +43,7 @@ public class BackupScheduler {
BackupHelper.create( BackupHelper.create(
new BackupContext.Builder() new BackupContext.Builder()
.setServer(server) .setServer(server)
.setInitiator(BackupContext.BackupInitiator.Timer) .setInitiator(ActionInitiator.Timer)
.saveServer() .saveServer()
.build() .build()
) )
@ -60,7 +61,7 @@ public class BackupScheduler {
BackupHelper.create( BackupHelper.create(
new BackupContext.Builder() new BackupContext.Builder()
.setServer(server) .setServer(server)
.setInitiator(BackupContext.BackupInitiator.Timer) .setInitiator(ActionInitiator.Timer)
.saveServer() .saveServer()
.build() .build()
) )

View File

@ -61,7 +61,9 @@ public class MakeBackupRunnable implements Runnable {
outFile.createNewFile(); outFile.createNewFile();
} catch (IOException e) { } catch (IOException e) {
Statics.LOGGER.error("An exception occurred when trying to create new backup file!", 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; return;
} }
@ -99,6 +101,8 @@ public class MakeBackupRunnable implements Runnable {
default: default:
Statics.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", Statics.CONFIG.format); Statics.LOGGER.warn("Specified compressor ({}) is not supported! Zip will be used instead!", Statics.CONFIG.format);
if(context.getInitiator() == ActionInitiator.Player)
Statics.LOGGER.sendError(context.getCommandSource(), "Error! No correct compression format specified! Using default compressor!"); Statics.LOGGER.sendError(context.getCommandSource(), "Error! No correct compression format specified! Using default compressor!");
ZipCompressor.getInstance().createArchive(world, outFile, context, coreCount); ZipCompressor.getInstance().createArchive(world, outFile, context, coreCount);
@ -107,8 +111,7 @@ public class MakeBackupRunnable implements Runnable {
BackupHelper.executeFileLimit(context.getCommandSource(), Utilities.getLevelName(context.getServer())); BackupHelper.executeFileLimit(context.getCommandSource(), Utilities.getLevelName(context.getServer()));
Statics.LOGGER.sendInfo(context, "Done!"); Statics.LOGGER.sendInfoAL(context, "Done!");
Statics.LOGGER.info("Done!");
} finally { } finally {
Utilities.enableWorldSaving(context.getServer()); Utilities.enableWorldSaving(context.getServer());
} }

View File

@ -19,6 +19,7 @@
package net.szum123321.textile_backup.core.create.compressors; package net.szum123321.textile_backup.core.create.compressors;
import net.szum123321.textile_backup.Statics; 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.Utilities;
import net.szum123321.textile_backup.core.create.BackupContext; import net.szum123321.textile_backup.core.create.BackupContext;
@ -46,6 +47,8 @@ public abstract class AbstractCompressor {
addEntry(file, inputFile.toPath().relativize(file.toPath()).toString(), arc); addEntry(file, inputFile.toPath().relativize(file.toPath()).toString(), arc);
} catch (IOException e) { } catch (IOException e) {
Statics.LOGGER.error("An exception occurred while trying to compress: {}", file.getName(), e); Statics.LOGGER.error("An exception occurred while trying to compress: {}", file.getName(), e);
if(ctx.getInitiator() == ActionInitiator.Player)
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!"); Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
} }
}); });
@ -53,12 +56,14 @@ public abstract class AbstractCompressor {
finish(arc); finish(arc);
} catch (IOException | InterruptedException | ExecutionException e) { } catch (IOException | InterruptedException | ExecutionException e) {
Statics.LOGGER.error("An exception occurred!", e); Statics.LOGGER.error("An exception occurred!", e);
if(ctx.getInitiator() == ActionInitiator.Player)
Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!"); Statics.LOGGER.sendError(ctx, "Something went wrong while compressing files!");
} }
close(); 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; protected abstract OutputStream createArchiveOutputStream(OutputStream stream, BackupContext ctx, int coreLimit) throws IOException;

View File

@ -24,6 +24,7 @@ import net.minecraft.text.MutableText;
import net.minecraft.util.Util; import net.minecraft.util.Util;
import net.szum123321.textile_backup.ConfigHandler; import net.szum123321.textile_backup.ConfigHandler;
import net.szum123321.textile_backup.Statics; 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.Utilities;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;