Skip to content

Commit

Permalink
added logger filter | added show backup detail | fixed single player …
Browse files Browse the repository at this point in the history
…database is not independent
  • Loading branch information
SkyDynamic committed Apr 30, 2024
1 parent ba4bbb0 commit bbcee56
Show file tree
Hide file tree
Showing 21 changed files with 327 additions and 175 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ loader_version=0.14.24

mod_id=quickbackupmulti
mod_name=QuickBackupMulti
mod_version=2.0.0
mod_version=2.0.1
maven_group=dev.skydynamic
archives_base_name=QuickBackupMulti
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.skydynamic.quickbackupmulti;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.filter.AbstractFilter;
import org.jetbrains.annotations.NotNull;

import java.util.logging.Filter;
import java.util.logging.LogRecord;

public class JavaUtilLog4jFilter extends AbstractFilter implements Filter {
public boolean isLoggable(@NotNull LogRecord record) {
return !QuickBackupMulti.shouldFilterMessage(record.getMessage());
}

public Result filter(@NotNull LogEvent event) {
return QuickBackupMulti.shouldFilterMessage("[" + event.getLoggerName() + "]: " + event.getMessage().getFormattedMessage()) ? Result.DENY : Result.NEUTRAL;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.skydynamic.quickbackupmulti;

import dev.skydynamic.quickbackupmulti.i18n.Translate;
import dev.skydynamic.quickbackupmulti.utils.DataBase;
import dev.skydynamic.quickbackupmulti.utils.config.Config;

import dev.skydynamic.quickbackupmulti.utils.config.ConfigStorage;
Expand All @@ -19,10 +20,10 @@
import net.minecraft.network.PacketByteBuf;

//#if MC>=11900
import org.apache.logging.log4j.LogManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//#else
//$$ import org.apache.logging.log4j.LogManager;
//$$ import org.apache.logging.log4j.Logger;
//#endif

Expand All @@ -31,18 +32,22 @@
import static dev.skydynamic.quickbackupmulti.command.QuickBackupMultiCommand.RegisterCommand;
import static dev.skydynamic.quickbackupmulti.utils.QbmManager.*;

public final class QuickBackupMulti implements ModInitializer {
public class QuickBackupMulti implements ModInitializer {

//#if MC>=11900
public static final Logger LOGGER = LoggerFactory.getLogger("QuickBackupMulti");
public static final Logger LOGGER = LoggerFactory.getLogger(QuickBackupMulti.class);
//#else
//$$ public static final Logger LOGGER = LogManager.getLogger("QuickBackupMulti");
//$$ public static final Logger LOGGER = LogManager.getLogger(QuickBackupMulti.class);
//#endif

EnvType env = FabricLoader.getInstance().getEnvironmentType();

@Override
public void onInitialize() {
final var filter = new JavaUtilLog4jFilter();
java.util.logging.Logger.getLogger("").setFilter(filter);
((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(filter);

Config.INSTANCE.load();
Translate.handleResourceReload(Config.INSTANCE.getLang());

Expand Down Expand Up @@ -90,4 +95,16 @@ public static void registerPacketHandler() {
}
});
}

public static boolean shouldFilterMessage(String message) {
return message.contains("Mongo") || message.contains("H2Backend") || message.contains("cluster");
}

public static DataBase getDataBase() {
return Config.TEMP_CONFIG.dataBase;
}

public static void setDataBase(String worldName) {
Config.TEMP_CONFIG.setDataBase(new DataBase(worldName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.LiteralCommandNode;
//#if MC<=11820
//$$ import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -36,8 +37,7 @@
import static dev.skydynamic.quickbackupmulti.command.MakeCommand.makeCommand;
import static dev.skydynamic.quickbackupmulti.command.SettingCommand.settingCommand;
import static dev.skydynamic.quickbackupmulti.i18n.Translate.tr;
import static dev.skydynamic.quickbackupmulti.utils.ListUtils.list;
import static dev.skydynamic.quickbackupmulti.utils.ListUtils.search;
import static dev.skydynamic.quickbackupmulti.utils.ListUtils.*;
import static dev.skydynamic.quickbackupmulti.utils.QbmManager.*;
import static dev.skydynamic.quickbackupmulti.QuickBackupMulti.LOGGER;
import static net.minecraft.server.command.CommandManager.literal;
Expand Down Expand Up @@ -76,14 +76,24 @@ public static void RegisterCommand(CommandDispatcher<ServerCommandSource> dispat
.then(CommandManager.argument("name", StringArgumentType.string())
.executes(it -> deleteSaveBackup(it.getSource(), StringArgumentType.getString(it, "name")))))


.then(settingCommand)

.then(literal("show")
.then(CommandManager.argument("name", StringArgumentType.string())
.executes(it -> showBackupDetail(it.getSource(), StringArgumentType.getString(it, "name")))))
);

dispatcher.register(literal("quickbackupm").redirect(QuickBackupMultiShortCommand));
}

public static final ConcurrentHashMap<String, ConcurrentHashMap<String, Object>> QbDataHashMap = new ConcurrentHashMap<>();

private static int showBackupDetail(ServerCommandSource commandSource, String name) {
Messenger.sendMessage(commandSource, show(name));
return 1;
}

private static int searchSaveBackups(ServerCommandSource commandSource, String string) {
List<String> backupsList = getBackupsList();
List<String> result = backupsList.stream()
Expand Down Expand Up @@ -204,11 +214,18 @@ private static int listSaveBackups(ServerCommandSource commandSource, int page)
}

public static boolean checkPermission(@NotNull ServerCommandSource source) {
//
try {
return getPermission(source);
} catch (CommandSyntaxException e) {
return false;
}
}

private static boolean getPermission(ServerCommandSource source) throws CommandSyntaxException{
boolean flag = source.hasPermissionLevel(2);
ServerPlayerEntity player;
MinecraftServer server;
if (!flag && (server = source.getServer()).isSingleplayer() && source.isExecutedByPlayer() && (player = source.getPlayer()) != null) {
if (!flag && (server = source.getServer()).isSingleplayer() && (player = source.getPlayer()) != null) {
flag = server.isHost(player.getGameProfile());
}
return flag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import dev.skydynamic.quickbackupmulti.i18n.LangSuggestionProvider;
import dev.skydynamic.quickbackupmulti.i18n.Translate;
import dev.skydynamic.quickbackupmulti.utils.Messenger;
import dev.skydynamic.quickbackupmulti.utils.QbmManager;
import dev.skydynamic.quickbackupmulti.utils.ScheduleUtils;
import dev.skydynamic.quickbackupmulti.utils.config.Config;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -17,7 +17,7 @@

import static dev.skydynamic.quickbackupmulti.i18n.Translate.supportLanguage;
import static dev.skydynamic.quickbackupmulti.i18n.Translate.tr;
import static dev.skydynamic.quickbackupmulti.utils.QbmManager.*;
import static dev.skydynamic.quickbackupmulti.utils.ScheduleUtils.*;
import static dev.skydynamic.quickbackupmulti.utils.schedule.CronUtil.*;
import static net.minecraft.server.command.CommandManager.literal;

Expand Down Expand Up @@ -90,15 +90,15 @@ private static int switchMode(ServerCommandSource commandSource, String mode) {

private static int setScheduleCron(ServerCommandSource commandSource, String value) {
try {
return QbmManager.setScheduleCron(commandSource, value);
return ScheduleUtils.setScheduleCron(commandSource, value);
} catch (SchedulerException e) {
return 0;
}
}

private static int setScheduleInterval(ServerCommandSource commandSource, int value, String type) {
try {
return QbmManager.setScheduleInterval(commandSource, value, type);
return ScheduleUtils.setScheduleInterval(commandSource, value, type);
} catch (SchedulerException e) {
Messenger.sendMessage(commandSource,
Messenger.literal(tr("quickbackupmulti.schedule.cron.set_fail", e)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import java.nio.file.Path;

import static dev.skydynamic.quickbackupmulti.QuickBackupMulti.getDataBase;
import static dev.skydynamic.quickbackupmulti.QuickBackupMulti.setDataBase;
import static dev.skydynamic.quickbackupmulti.utils.QbmManager.createBackupDir;
import static dev.skydynamic.quickbackupmulti.utils.schedule.ScheduleUtils.*;

Expand All @@ -29,11 +31,13 @@ private void initQuickBackupMultiClient(CallbackInfo ci) {
Config.TEMP_CONFIG.setWorldName(worldName);
Path backupDir = Path.of(QbmConstant.gameDir + "/QuickBackupMulti/").resolve(worldName);
createBackupDir(backupDir);
setDataBase(worldName);
startSchedule();
}

@Inject(method = "shutdown", at = @At("HEAD"))
private void stopSchedule(CallbackInfo ci) {
shutdownSchedule();
getDataBase().stopInternalMongoServer();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.nio.file.Path;

import static dev.skydynamic.quickbackupmulti.QuickBackupMulti.setDataBase;
import static dev.skydynamic.quickbackupmulti.utils.QbmManager.createBackupDir;
import static dev.skydynamic.quickbackupmulti.utils.schedule.ScheduleUtils.startSchedule;

Expand All @@ -28,6 +29,7 @@ private void setServer(CallbackInfo ci) {
private void initQuickBackupMulti(CallbackInfo ci) {
Path backupDir = Path.of(QbmConstant.gameDir + "/QuickBackupMulti/");
createBackupDir(backupDir);
setDataBase("server");
startSchedule();
}

Expand Down
22 changes: 13 additions & 9 deletions src/main/java/dev/skydynamic/quickbackupmulti/utils/DataBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import dev.morphia.query.Query;
import dev.morphia.query.filters.Filters;
import dev.skydynamic.quickbackupmulti.QbmConstant;
import dev.skydynamic.quickbackupmulti.utils.config.Config;
import dev.skydynamic.quickbackupmulti.utils.storage.BackupInfo;
import dev.skydynamic.quickbackupmulti.utils.storage.IndexFile;
import dev.skydynamic.quickbackupmulti.utils.storage.codec.DimensionFormatCodec;
Expand All @@ -35,16 +36,19 @@
public final class DataBase {
private MongoServer server;
private Datastore datastore;
private static final DataBase dataBase = new DataBase();

private static final InsertOneOptions INSERT_OPTIONS = new InsertOneOptions();
private static final DeleteOptions DELETE_OPTIONS = new DeleteOptions();

public DataBase() {
public DataBase(String worldName) {
File qbmDir = new File(QbmConstant.gameDir + "/QuickBackupMulti/");
if (!qbmDir.exists()) qbmDir.mkdirs();
String connectionString = startInternalMongoServer();
LOGGER.info("Started local MongoDB server at " + server.getConnectionString());

String connectionString = Config.INSTANCE.getMongoDBUri();
if (Config.INSTANCE.getUseInternalDataBase()) {
connectionString = startInternalMongoServer();
LOGGER.info("Started local MongoDB server at " + server.getConnectionString());
}

MongoClient mongoClient = MongoClients.create(connectionString);

Expand All @@ -58,7 +62,7 @@ public DataBase() {
.codecProvider(codecProvider)
.build();

datastore = Morphia.createDatastore(mongoClient, "QuickBackupMulti", mapperOptions);
datastore = Morphia.createDatastore(mongoClient, "QuickBackupMulti-" + worldName.replace(" ", ""), mapperOptions);
// database = mongoClient.getDatabase("QuickBackupMulti");
}

Expand Down Expand Up @@ -135,11 +139,11 @@ private String startInternalMongoServer() {
return server.getConnectionString();
}

public Datastore getDatastore() {
return datastore;
public void stopInternalMongoServer() {
server.shutdownNow();
}

public static DataBase getDatabase() {
return dataBase;
public Datastore getDatastore() {
return datastore;
}
}
Loading

0 comments on commit bbcee56

Please sign in to comment.