Some code cleanup and repaired mostly whitelist/blacklist commands. pt2

Forge-1.14.4
Szum123321 2020-03-24 21:48:04 +01:00
parent a3ab69a1fe
commit b026c1331a
8 changed files with 30 additions and 36 deletions

View File

@ -21,7 +21,7 @@ Feel free to use this mod in your modpack or on server!
### Important
* Time format deafutly used by this mod is: dd.MM.yyyy_HH-mm-ss although it is configurable
* Time format defaultly 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,8 +50,17 @@ 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())
.requires((ctx) -> {
try {
return (config.whitelist.contains(ctx.getEntityOrThrow().getEntityName()) ||
ctx.hasPermissionLevel(config.permissionLevel)) &&
!config.blacklist.contains(ctx.getEntityOrThrow().getEntityName());
}catch (Exception e){ //Command was called from server console.
System.out.println("Server!");
return true;
}
}
).then(BlacklistCommand.register())
.then(CleanupCommand.register())
.then(StartBackupCommand.register())
.then(WhitelistCommand.register())

View File

@ -8,16 +8,14 @@ import net.minecraft.command.arguments.EntityArgumentType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
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 LiteralArgumentBuilder.<ServerCommandSource>literal("blacklist")
.requires(ctx -> (TextileBackup.config.whitelist.contains(ctx.getName()) ||
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel)) &&
!TextileBackup.config.blacklist.contains(ctx.getName()))
return CommandManager.literal("blacklist")
.then(CommandManager.literal("add")
.then(CommandManager.argument("player", EntityArgumentType.player())
.executes(BlacklistCommand::executeAdd)
@ -32,7 +30,7 @@ public class BlacklistCommand {
}
private static int help(ServerCommandSource source){
source.sendFeedback(new TranslatableText("Available command are: add [player], remove [player], list."), false);
source.sendFeedback(new LiteralText("Available command are: add [player], remove [player], list."), false);
return 1;
}
@ -56,7 +54,7 @@ public class BlacklistCommand {
PlayerEntity player = EntityArgumentType.getPlayer(ctx, "player");
if(TextileBackup.config.blacklist.contains(player.getEntityName())) {
ctx.getSource().sendFeedback(new TranslatableText("Player: {} is already blacklisted.", player.getEntityName()), true);
ctx.getSource().sendFeedback(new TranslatableText("Player: %s is already blacklisted.", player.getEntityName()), false);
}else{
TextileBackup.config.blacklist.add(player.getEntityName());
ConfigManager.saveConfig(TextileBackup.config);
@ -84,7 +82,7 @@ public class BlacklistCommand {
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);
ctx.getSource().sendFeedback(new TranslatableText("Player: %s newer was blacklisted.", player.getEntityName()), false);
}else{
TextileBackup.config.blacklist.remove(player.getEntityName());
ConfigManager.saveConfig(TextileBackup.config);

View File

@ -21,22 +21,18 @@ package net.szum123321.textile_backup.commands;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
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.minecraft.text.LiteralText;
import net.szum123321.textile_backup.core.BackupHelper;
public class CleanupCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register(){
return CommandManager.literal("cleanup")
.requires(ctx -> TextileBackup.config.whitelist.contains(ctx.getName()) ||
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel) &&
!TextileBackup.config.blacklist.contains(ctx.getName()))
.executes(ctx -> execute(ctx.getSource()));
}
private static int execute(ServerCommandSource source){
BackupHelper.executeFileLimit(source, source.getMinecraftServer().getLevelName());
source.sendFeedback(new TranslatableText("Done"), false);
source.sendFeedback(new LiteralText("Done"), false);
return 1;
}

View File

@ -23,22 +23,18 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.core.BackupHelper;
public class StartBackupCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register(){
return CommandManager.literal("start")
.requires(ctx -> TextileBackup.config.whitelist.contains(ctx.getName()) ||
ctx.hasPermissionLevel(TextileBackup.config.permissionLevel) &&
!TextileBackup.config.blacklist.contains(ctx.getName()))
.then(CommandManager.argument("Comment", StringArgumentType.word())
.then(CommandManager.argument("comment", StringArgumentType.string())
.executes(StartBackupCommand::executeWithComment)
).executes(ctx -> execute(ctx.getSource()));
}
private static int executeWithComment(CommandContext<ServerCommandSource> source) {
BackupHelper.create(source.getSource().getMinecraftServer(), source.getSource(), true, StringArgumentType.getString(source, "Comment"));
BackupHelper.create(source.getSource().getMinecraftServer(), source.getSource(), true, StringArgumentType.getString(source, "comment").replace("#", ""));
return 1;
}

View File

@ -1,6 +1,5 @@
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;
@ -9,6 +8,7 @@ import net.minecraft.command.arguments.EntityArgumentType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText;
import net.minecraft.text.TranslatableText;
import net.szum123321.textile_backup.TextileBackup;
import net.szum123321.textile_backup.core.Utilities;
@ -16,9 +16,6 @@ import net.szum123321.textile_backup.core.Utilities;
public class WhitelistCommand {
public static LiteralArgumentBuilder<ServerCommandSource> register(){
return CommandManager.literal("whitelist")
.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())
.executes(WhitelistCommand::executeAdd)
@ -33,7 +30,7 @@ public class WhitelistCommand {
}
private static int help(ServerCommandSource source){
source.sendFeedback(new TranslatableText("Available command are: add [player], remove [player], list."), false);
source.sendFeedback(new LiteralText("Available command are: add [player], remove [player], list."), false);
return 1;
}
@ -57,7 +54,7 @@ public class WhitelistCommand {
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);
ctx.getSource().sendFeedback(new TranslatableText("Player: %s is already whitelisted.", player.getEntityName()), false);
}else{
TextileBackup.config.whitelist.add(player.getEntityName());
ConfigManager.saveConfig(TextileBackup.config);
@ -85,7 +82,7 @@ public class WhitelistCommand {
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);
ctx.getSource().sendFeedback(new TranslatableText("Player: %s newer was on the whitelist.", player.getEntityName()), false);
}else{
TextileBackup.config.whitelist.remove(player.getEntityName());
ConfigManager.saveConfig(TextileBackup.config);

View File

@ -21,7 +21,6 @@ package net.szum123321.textile_backup.core;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.szum123321.textile_backup.TextileBackup;
import org.apache.commons.io.FileUtils;
@ -29,7 +28,6 @@ import java.io.File;
import java.io.FileFilter;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
public class BackupHelper {

View File

@ -1,14 +1,14 @@
package net.szum123321.textile_backup.core;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.TranslatableText;
import net.minecraft.text.LiteralText;
import net.szum123321.textile_backup.TextileBackup;
import java.time.format.DateTimeFormatter;
public class Utilities {
public static DateTimeFormatter getDateTimeFormatter(){
if(TextileBackup.config.dateTimeFormat != null)
if(!TextileBackup.config.dateTimeFormat.equals(""))
return DateTimeFormatter.ofPattern(TextileBackup.config.dateTimeFormat);
else
return getBackupDateTimeFormatter();
@ -25,7 +25,7 @@ public class Utilities {
public static void log(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), false);
ctx.sendFeedback(new LiteralText(s), false);
if(TextileBackup.config.log)
TextileBackup.logger.info(s);
@ -33,7 +33,7 @@ public class Utilities {
public static void error(String s, ServerCommandSource ctx){
if(ctx != null)
ctx.sendFeedback(new TranslatableText(s), true);
ctx.sendFeedback(new LiteralText(s), true);
if(TextileBackup.config.log)
TextileBackup.logger.error(s);