Some code cleanup and repaired mostly whitelist/blacklist commands

Forge-1.14.4
Szum123321 2020-03-24 09:25:39 +01:00
parent 312cb57692
commit a3ab69a1fe
8 changed files with 98 additions and 87 deletions

View File

@ -12,14 +12,16 @@ Available operations are:
* start - just starts backup
* cleanup - forces cleanup procedure (deletes old backups according to config)
* whitelist - here you can add, remove and list player that are allowed to run any operation within this mod despite not having high enough permission level
* whitelist - here you can add, remove and list player that are not allowed to run any operation within this mod despite having high enough permission level
All of above can only be done by server admins(permission level 4) / player on single payer with cheats on
All of above can only be done by server admins(permission level 4 - configurable) / player on single player with cheats on
Feel free to use this mod in your modpack or on server!
### Important
* Time format used by this mod is: dd.MM.yyyy_HH:mm:ss
* Time format deafutly used by this mod is: dd.MM.yyyy_HH-mm-ss although it is configurable
* _This mod contains **Cotton Config** and its dependencies as jars in jar, which are property of **CottonMC**_
If you have any suggestions or found a problem please report it on github.

View File

@ -50,6 +50,7 @@ public class TextileBackup implements ModInitializer {
private void registerCommands(){
CommandRegistry.INSTANCE.register(false, dispatcher -> dispatcher.register(
LiteralArgumentBuilder.<ServerCommandSource>literal("backup")
.requires(ctx -> ctx.hasPermissionLevel(1))
.then(BlacklistCommand.register())
.then(CleanupCommand.register())
.then(StartBackupCommand.register())

View File

@ -10,25 +10,25 @@ import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.core.Utilities;
public class BlacklistCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register(){
return CommandManager.literal("whitelist")
.requires(ctx -> TextileBackup.config.whitelist.contains(ctx.getName()) ||
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel) &&
return LiteralArgumentBuilder.<ServerCommandSource>literal("blacklist")
.requires(ctx -> (TextileBackup.config.whitelist.contains(ctx.getName()) ||
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel)) &&
!TextileBackup.config.blacklist.contains(ctx.getName()))
.then(CommandManager.literal("add")
.then(CommandManager.argument("Player", EntityArgumentType.player()))
.then(CommandManager.argument("player", EntityArgumentType.player())
.executes(BlacklistCommand::executeAdd)
)
.then(CommandManager.literal("remove")
.then(CommandManager.argument("Player", EntityArgumentType.player()))
).then(CommandManager.literal("remove")
.then(CommandManager.argument("player", EntityArgumentType.player())
.executes(BlacklistCommand::executeRemove)
)
.then(CommandManager.literal("list")
).then(CommandManager.literal("list")
.executes(ctx -> executeList(ctx.getSource()))
)
.executes(ctx -> help(ctx.getSource()));
).executes(ctx -> help(ctx.getSource()));
}
private static int help(ServerCommandSource source){
@ -47,16 +47,16 @@ public class BlacklistCommand {
builder.append(", ");
}
source.sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), source);
return 1;
}
private static int executeAdd(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "Player");
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
if(TextileBackup.config.blacklist.contains(player.getEntityName())) {
ctx.getSource().sendFeedback(new TranslatableText("Player: {} is already blacklisted.", player.getEntityName()), false);
ctx.getSource().sendFeedback(new TranslatableText("Player: {} is already blacklisted.", player.getEntityName()), true);
}else{
TextileBackup.config.blacklist.add(player.getEntityName());
ConfigManager.saveConfig(TextileBackup.config);
@ -74,14 +74,14 @@ public class BlacklistCommand {
builder.append(" successfully.");
ctx.getSource().sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), ctx.getSource());
}
return 1;
}
private static int executeRemove(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "Player");
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
if(!TextileBackup.config.blacklist.contains(player.getEntityName())) {
ctx.getSource().sendFeedback(new TranslatableText("Player: {} newer was blacklisted.", player.getEntityName()), false);
@ -95,7 +95,7 @@ public class BlacklistCommand {
builder.append(player.getEntityName());
builder.append(" removed from the blacklist successfully.");
ctx.getSource().sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), ctx.getSource());
}
return 1;

View File

@ -1,5 +1,6 @@
package net.szum123321.textile_backup.commands;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@ -10,7 +11,7 @@ import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.szum123321.textile_backup.TextileBackup;
import sun.security.krb5.Config;
import net.szum123321.textile_backup.core.Utilities;
public class WhitelistCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register(){
@ -19,17 +20,16 @@ public class WhitelistCommand {
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel) &&
!TextileBackup.config.blacklist.contains(ctx.getName()))
.then(CommandManager.literal("add")
.then(CommandManager.argument("Player", EntityArgumentType.player()))
.then(CommandManager.argument("player", EntityArgumentType.player())
.executes(WhitelistCommand::executeAdd)
)
.then(CommandManager.literal("remove")
.then(CommandManager.argument("Player", EntityArgumentType.player()))
).then(CommandManager.literal("remove")
.then(CommandManager.argument("player", EntityArgumentType.player())
.executes(WhitelistCommand::executeRemove)
)
.then(CommandManager.literal("list")
).then(CommandManager.literal("list")
.executes(ctx -> executeList(ctx.getSource()))
)
.executes(ctx -> help(ctx.getSource()));
).executes(ctx -> help(ctx.getSource()));
}
private static int help(ServerCommandSource source){
@ -48,13 +48,13 @@ public class WhitelistCommand {
builder.append(", ");
}
source.sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), source);
return 1;
}
private static int executeAdd(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "Player");
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
if(TextileBackup.config.whitelist.contains(player.getEntityName())) {
ctx.getSource().sendFeedback(new TranslatableText("Player: {} is already whitelisted.", player.getEntityName()), false);
@ -75,14 +75,14 @@ public class WhitelistCommand {
builder.append(" successfully.");
ctx.getSource().sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), ctx.getSource());
}
return 1;
}
private static int executeRemove(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "Player");
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
if(!TextileBackup.config.whitelist.contains(player.getEntityName())) {
ctx.getSource().sendFeedback(new TranslatableText("Player: {} newer was on the whitelist.", player.getEntityName()), false);
@ -95,7 +95,7 @@ public class WhitelistCommand {
builder.append(player.getEntityName());
builder.append(" removed from the whitelist successfully.");
ctx.getSource().sendFeedback(new TranslatableText(builder.toString()), false);
Utilities.log(builder.toString(), ctx.getSource());
}
return 1;

View File

@ -33,23 +33,6 @@ import java.time.format.DateTimeFormatter;
import java.util.Arrays;
public class BackupHelper {
public static void log(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), false);
if(TextileBackup.config.log)
TextileBackup.logger.info(s);
}
public static void error(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), true);
if(TextileBackup.config.log)
TextileBackup.logger.error(s);
}
public static void create(MinecraftServer server, ServerCommandSource ctx, boolean save, String comment) {
LocalDateTime now = LocalDateTime.now();
@ -62,11 +45,11 @@ public class BackupHelper {
builder.append("SERVER");
builder.append(" on: ");
builder.append(getDateTimeFormatter().format(now));
builder.append(Utilities.getDateTimeFormatter().format(now));
log(builder.toString(), null);
Utilities.log(builder.toString(), null);
log("Saving server...", ctx);
Utilities.log("Saving server...", ctx);
if(save)
server.save(true, false, false);
@ -91,7 +74,7 @@ public class BackupHelper {
try {
creationTime = LocalDateTime.from(
getDateTimeFormatter().parse(
Utilities.getDateTimeFormatter().parse(
f.getName().split(".zip")[0].split("#")[0]
)
);
@ -100,7 +83,7 @@ public class BackupHelper {
System.out.println(e.toString());
creationTime = LocalDateTime.from(
getBackupDateTimeFormatter().parse(
Utilities.getBackupDateTimeFormatter().parse(
f.getName().split(".zip")[0].split("#")[0]
)
);
@ -108,7 +91,7 @@ public class BackupHelper {
}
if(now.toEpochSecond(ZoneOffset.UTC) - creationTime.toEpochSecond(ZoneOffset.UTC) > TextileBackup.config.maxAge) {
log("Deleting: " + f.getName(), ctx);
Utilities.log("Deleting: " + f.getName(), ctx);
f.delete();
}
}
@ -124,7 +107,7 @@ public class BackupHelper {
Arrays.sort(files);
for(int i = 0; i < var1; i++) {
log("Deleting: " + files[i].getName(), ctx);
Utilities.log("Deleting: " + files[i].getName(), ctx);
files[i].delete();
}
}
@ -132,7 +115,7 @@ public class BackupHelper {
if(TextileBackup.config.maxSize > 0 && FileUtils.sizeOfDirectory(root) / 1024 > TextileBackup.config.maxSize){
Arrays.stream(root.listFiles()).sorted().forEach(e -> {
if(FileUtils.sizeOfDirectory(root) / 1024 > TextileBackup.config.maxSize){
log("Deleting: " + e.getName(), ctx);
Utilities.log("Deleting: " + e.getName(), ctx);
e.delete();
}
});
@ -163,20 +146,4 @@ public class BackupHelper {
return path;
}
public static DateTimeFormatter getDateTimeFormatter(){
if(TextileBackup.config.dateTimeFormat != null)
return DateTimeFormatter.ofPattern(TextileBackup.config.dateTimeFormat);
else
return getBackupDateTimeFormatter();
}
public static DateTimeFormatter getBackupDateTimeFormatter(){
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
return DateTimeFormatter.ofPattern("dd.MM.yyyy_HH-mm-ss");
} else {
return DateTimeFormatter.ofPattern("dd.MM.yyyy_HH:mm:ss");
}
}
}

View File

@ -32,7 +32,7 @@ import java.util.zip.ZipOutputStream;
public class Compressor {
public static void createArchive(File in, File out, ServerCommandSource ctx){
BackupHelper.log("Starting compression...", ctx);
Utilities.log("Starting compression...", ctx);
try(ZipOutputStream arc = new ZipOutputStream(new FileOutputStream(out))) {
arc.setLevel(TextileBackup.config.compression);
@ -41,7 +41,7 @@ public class Compressor {
TextileBackup.logger.error(e.getMessage());
}
BackupHelper.log("Compression finished", ctx);
Utilities.log("Compression finished", ctx);
}
private static void addToArchive(ZipOutputStream out, File file, String dir) throws IOException {

View File

@ -55,7 +55,7 @@ public class MakeBackupThread extends Thread {
try {
outFile.createNewFile();
} catch (IOException e) {
BackupHelper.error("Error while trying to create backup file!\n" + e.getMessage(), ctx);
Utilities.error("Error while trying to create backup file!\n" + e.getMessage(), ctx);
return;
}
@ -63,12 +63,12 @@ public class MakeBackupThread extends Thread {
BackupHelper.executeFileLimit(ctx, server.getLevelName());
BackupHelper.log("Done!", ctx);
Utilities.log("Done!", ctx);
}
private String getFileName(){
LocalDateTime now = LocalDateTime.now();
return BackupHelper.getDateTimeFormatter().format(now) + (comment != null ? "#" + comment.replace("#", ""): "") + ".zip";
return Utilities.getDateTimeFormatter().format(now) + (comment != null ? "#" + comment.replace("#", ""): "") + ".zip";
}
}

View File

@ -0,0 +1,41 @@
package net.szum123321.textile_backup.core;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.szum123321.textile_backup.TextileBackup;
import java.time.format.DateTimeFormatter;
public class Utilities {
public static DateTimeFormatter getDateTimeFormatter(){
if(TextileBackup.config.dateTimeFormat != null)
return DateTimeFormatter.ofPattern(TextileBackup.config.dateTimeFormat);
else
return getBackupDateTimeFormatter();
}
public static DateTimeFormatter getBackupDateTimeFormatter(){
String os = System.getProperty("os.name");
if(os.toLowerCase().startsWith("win")){
return DateTimeFormatter.ofPattern("dd.MM.yyyy_HH-mm-ss");
} else {
return DateTimeFormatter.ofPattern("dd.MM.yyyy_HH:mm:ss");
}
}
public static void log(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), false);
if(TextileBackup.config.log)
TextileBackup.logger.info(s);
}
public static void error(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), true);
if(TextileBackup.config.log)
TextileBackup.logger.error(s);
}
}