Skip to content

Commit

Permalink
feat: add logger level for quartz + add: LanguageModule
Browse files Browse the repository at this point in the history
  • Loading branch information
sakurawald committed Oct 25, 2023
1 parent 104ff96 commit b3f841a
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 20 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ dependencies {
include(implementation "net.kyori:adventure-text-minimessage:${project.adventure_api_version}")
include(implementation "net.kyori:adventure-text-serializer-plain:${project.adventure_api_version}")

include(implementation group: 'org.apache.commons', name: 'commons-compress', version: "${project.apache_commons_version}")
include(implementation group: 'org.apache.commons', name: 'commons-compress', version: "${project.apache_commons_compression_version}")

include(implementation group: 'commons-io', name: 'commons-io', version: "${project.apache_commons_io_version}")

include(implementation group: 'org.quartz-scheduler', name: 'quartz', version: "${project.quartz_version}")

Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ lombok_version=1.18.30
adventure_api_version=4.14.0
adventure_platform_fabric_version=5.9.0
sgui_version=1.2.2+1.20
apache_commons_version=1.24.0
apache_commons_compression_version=1.24.0
apache_commons_io_version=2.14.0
spark_version=0.1-SNAPSHOT
quartz_version=2.5.0-rc1
reflections_version=0.10.2
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/assets/sakurawald/ResourceLoader.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/io/github/sakurawald/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public void onInitialize() {
/* modules */
ModuleManager.initializeModules();

/* server instance */
ServerLifecycleEvents.SERVER_STARTED.register(server -> SERVER = server);

/* scheduler */
ServerLifecycleEvents.SERVER_STARTED.register(server -> ScheduleUtil.startScheduler());
ServerLifecycleEvents.SERVER_STOPPING.register((server) -> ScheduleUtil.shutdownScheduler());
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/io/github/sakurawald/config/ConfigGSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@
@SuppressWarnings("ALL")
public class ConfigGSON {

public Common common = new Common();
public Modules modules = new Modules();

public class Common {

public Quartz quartz = new Quartz();

public class Quartz {
public String logger_level = "WARN";
}

}

public class Modules {
public ResourceWorld resource_world = new ResourceWorld();
public NewbieWelcome newbie_welcome = new NewbieWelcome();
Expand Down Expand Up @@ -350,7 +361,7 @@ public class TickChunkCache {
}

public class Config {
public boolean enable = true;
public boolean enable = false;
}

public class Test {
Expand All @@ -369,5 +380,10 @@ public class Fly {
public class God {
public boolean enable = false;
}

public Language language = new Language();
public class Language {
public boolean enable = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.mixin.config;
package io.github.sakurawald.mixin.language;

import io.github.sakurawald.module.teleport_warmup.ServerPlayerAccessor;
import io.github.sakurawald.util.MessageUtil;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.sakurawald.module.language;

import io.github.sakurawald.ServerMain;
import io.github.sakurawald.module.AbstractModule;
import lombok.extern.slf4j.Slf4j;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.commons.io.FileUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@Slf4j
public class LanguageModule extends AbstractModule {

private final Path STORAGE_PATH = ServerMain.CONFIG_PATH.resolve("language").toAbsolutePath();

@Override
public void onInitialize() {
loadLanguages();
}

public void loadLanguages() {
if (!Files.exists(STORAGE_PATH)) {
log.info("Create language folder.");
try {
Files.createDirectories(STORAGE_PATH);
FileUtils.copyDirectory(FabricLoader.getInstance().getModContainer(ServerMain.MOD_ID).flatMap(modContainer -> modContainer.findPath("assets/sakurawald/lang")).get().toFile(), STORAGE_PATH.toFile());
} catch (IOException e) {
log.warn("Failed to create language folder -> {}", e.getMessage());
}
}
}
}
38 changes: 28 additions & 10 deletions src/main/java/io/github/sakurawald/util/MessageUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.sakurawald.util;

import assets.sakurawald.ResourceLoader;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.github.sakurawald.ServerMain;
Expand All @@ -14,9 +13,13 @@
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.level.ServerPlayer;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,25 +28,40 @@
@Slf4j
public class MessageUtil {
private static final FabricServerAudiences adventure = FabricServerAudiences.of(ServerMain.SERVER);
private static final HashMap<String, JsonObject> lang2json = new HashMap<>();
@Getter
private static final HashMap<String, String> player2lang = new HashMap<>();
private static final HashMap<String, JsonObject> lang2json = new HashMap<>();
private static final String DEFAULT_LANG = "en_us";
private static final MiniMessage miniMessage = MiniMessage.builder().build();
private static final Path LANGUAGE_PATH = ServerMain.CONFIG_PATH.resolve("language");

static {
addLanguage("en_us");
addLanguage("zh_cn");
loadLanguages();
}

private static void addLanguage(String lang) {
InputStream input = ResourceLoader.class.getResourceAsStream("lang/%s.json".formatted(lang));
if (input == null) {
log.warn("Language File Not Found: %s.json".formatted(lang));
public static void loadLanguages() {
File[] languages = LANGUAGE_PATH.toFile().listFiles();
if (languages == null) {
log.error("Failed to load languages");
return;
}
JsonObject jsonObject = JsonParser.parseReader(new InputStreamReader(input)).getAsJsonObject();
lang2json.put(lang, jsonObject);
for (File language : languages) {
log.info("Loading Language File: %s".formatted(language.getName()));
addLanguage(language.getName());
}

}

private static void addLanguage(String fileName) {
InputStream input;
try {
input = FileUtils.openInputStream(LANGUAGE_PATH.resolve(fileName).toFile());
JsonObject jsonObject = JsonParser.parseReader(new InputStreamReader(input)).getAsJsonObject();
lang2json.put(fileName.replace(".json", ""), jsonObject);
} catch (IOException e) {
throw new RuntimeException(e);
}

}

public static String ofString(Audience audience, String key, Object... args) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/github/sakurawald/util/ScheduleUtil.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.github.sakurawald.util;


import io.github.sakurawald.config.ConfigManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

Expand All @@ -10,6 +13,11 @@ public class ScheduleUtil {
private static final Scheduler scheduler;

static {
/* set logger level for quartz */
Level level = Level.getLevel(ConfigManager.configWrapper.instance().common.quartz.logger_level);
Configurator.setAllLevels("org.quartz", level);

/* new scheduler */
try {
scheduler = new StdSchedulerFactory().getScheduler();
} catch (SchedulerException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/sakurawald.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"chat.ServerGamePacketListenerImplMixin",
"command_cooldown.CommandsMixin",
"command_spy.CommandsMixin",
"config.ServerPlayerMixin",
"deathlog.ServerPlayerMixin",
"language.ServerPlayerMixin",
"main_stats.BlockMixin",
"main_stats.PlayerListMixin",
"main_stats.ServerPlayNetworkHandlerMixin",
Expand Down

0 comments on commit b3f841a

Please sign in to comment.