Skip to content

Commit

Permalink
refactor: ChatStyleModule -> ChatModule + refactor: disable optimizat…
Browse files Browse the repository at this point in the history
…ion modules by default + refactor: a simple log for ModuleManager + refactor: DynamicMotdModule -> MotdModule
  • Loading branch information
sakurawald committed Oct 23, 2023
1 parent 6cedb78 commit 21ee8db
Show file tree
Hide file tree
Showing 20 changed files with 45 additions and 39 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ provides a command to toggle the pvp status. (/pvp [on/off/status/list])
create and manage auto-reset resource world for overworld, the_nether and the_end. (/rw [tp/delete/reset])
<img src="https://github.com/SakuraWald/sakurawald-fabric/raw/master/.github/images/resource-world.gif" alt="module presentation gif">

#### ChatStyleModule
A simple chat style system.
#### ChatModule
A simple chat system.

> - Support mini-message based parser
> - Support mention players
Expand Down Expand Up @@ -97,7 +97,7 @@ This module broadcasts a welcome-message and random teleport the new player and
#### CommandCooldownModule
Yeah, you know what this module does. (Use this module to avoid some heavy-command abuse)

#### DynamicMOTDModule
#### MotdModule
A simple MOTD that supports fancy and random motd, and supports some placeholders like MainStats

#### HeadModule
Expand Down Expand Up @@ -154,6 +154,12 @@ an optimization for iterating chunks, use event-based chunk-list constructor to
#### SchedulerModule
where you can add schedule jobs by cron expression, set the random command-list to be executed.

#### ConfigModule
provides `/sakurawald reload` to reload configs.

#### TestModule
provides `/test` command only for test purpose. (Disable this by default, and you don't need to enable this unless you know what you are doing)

# Commands
Different modules provide different commands, but it's easy to guess what commands the module provides.

Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/github/sakurawald/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

@Slf4j
// TODO: server language system
// TODO: /rtp
public class ServerMain implements ModInitializer {
public static final Path CONFIG_PATH = Path.of(FabricLoader.getInstance().getConfigDir().resolve("sakurawald").toString());
public static final String MOD_ID = "sakurawald";
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/github/sakurawald/config/ConfigGSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Modules {
public BetterInfo better_info = new BetterInfo();
public CommandCooldown command_cooldown = new CommandCooldown();
public TopChunks top_chunks = new TopChunks();
public ChatStyle chat_style = new ChatStyle();
public Chat chat = new Chat();
public Skin skin = new Skin();
public Back back = new Back();
public Tpa tpa = new Tpa();
Expand Down Expand Up @@ -187,7 +187,7 @@ public class TopChunks {
};
}

public class ChatStyle {
public class Chat {
public boolean enable = true;
public String format = "<#B1B2FF>[%playtime%\uD83D\uDD25 %mined%⛏ %placed%\uD83D\uDD33 %killed%\uD83D\uDDE1 %moved%\uD83C\uDF0D]<reset> <<dark_green><click:suggest_command:/msg %player% ><hover:show_text:\"Time: <date:'yyyy-MM-dd HH:mm:ss'><newline><italic>Click to Message\">%player%</hover></click></dark_green>> %message%";
public MentionPlayer mention_player = new MentionPlayer();
Expand Down Expand Up @@ -339,11 +339,11 @@ public class Scheduler {
}

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

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

public class Config {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sakurawald.mixin.chat_style;
package io.github.sakurawald.mixin.chat;

import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.chat_style.ChatStyleModule;
import io.github.sakurawald.module.chat.ChatStyleModule;
import io.github.sakurawald.util.CarpetUtil;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.sakurawald.mixin.chat_style;
package io.github.sakurawald.mixin.chat;

import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.chat_style.ChatStyleModule;
import io.github.sakurawald.module.chat.ChatStyleModule;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import io.github.sakurawald.ServerMain;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.motd.DynamicMotdModule;
import io.github.sakurawald.module.motd.MotdModule;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.network.protocol.status.ServerStatus;
import net.minecraft.server.network.ServerStatusPacketListenerImpl;
Expand All @@ -41,7 +41,7 @@
abstract class ServerStatusPacketListenerImplMixin {

@Unique
private static final DynamicMotdModule module = ModuleManager.getOrNewInstance(DynamicMotdModule.class);
private static final MotdModule module = ModuleManager.getOrNewInstance(MotdModule.class);

@Redirect(method = "handleStatusRequest", at = @At(value = "FIELD", target = "Lnet/minecraft/server/network/ServerStatusPacketListenerImpl;status:Lnet/minecraft/network/protocol/status/ServerStatus;"))
public ServerStatus $handleStatusRequest(final ServerStatusPacketListenerImpl instance) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/io/github/sakurawald/module/ModuleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ public static void reloadModules() {
*/
public static <T extends AbstractModule> T getOrNewInstance(Class<T> clazz) {
JsonElement config = ConfigManager.configWrapper.toJsonElement();

if (!instances.containsKey(clazz)) {
String basePackageName = calculateBasePackageName(ModuleManager.class, clazz.getName());
String moduleName = clazz.getSimpleName();
if (enableModule(config, basePackageName)) {
log.info("Enable module -> {}", moduleName);
log.info("+ {}", moduleName);
try {
AbstractModule abstractModule = clazz.getDeclaredConstructor().newInstance();
abstractModule.onInitialize();
Expand All @@ -46,7 +45,7 @@ public static <T extends AbstractModule> T getOrNewInstance(Class<T> clazz) {
log.error(e.toString());
}
} else {
log.info("Disable module {}", moduleName);
log.info("- {}", moduleName);

}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style;
package io.github.sakurawald.module.chat;

import com.google.common.collect.EvictingQueue;
import com.mojang.brigadier.Command;
Expand All @@ -9,8 +9,8 @@
import io.github.sakurawald.config.ConfigManager;
import io.github.sakurawald.module.AbstractModule;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.chat_style.display.DisplayHelper;
import io.github.sakurawald.module.chat_style.mention.MentionPlayersJob;
import io.github.sakurawald.module.chat.display.DisplayHelper;
import io.github.sakurawald.module.chat.mention.MentionPlayersJob;
import io.github.sakurawald.module.main_stats.MainStats;
import io.github.sakurawald.module.main_stats.MainStatsModule;
import io.github.sakurawald.util.MessageUtil;
Expand Down Expand Up @@ -56,7 +56,7 @@ public class ChatStyleModule extends AbstractModule {
public void onInitialize() {
ConfigManager.chatWrapper.loadFromDisk();

chatHistory = EvictingQueue.create(ConfigManager.configWrapper.instance().modules.chat_style.history.cache_size);
chatHistory = EvictingQueue.create(ConfigManager.configWrapper.instance().modules.chat.history.cache_size);
CommandRegistrationCallback.EVENT.register(this::registerCommand);
}

Expand All @@ -65,7 +65,7 @@ public void onInitialize() {
public void onReload() {
ConfigManager.chatWrapper.loadFromDisk();

EvictingQueue<Component> newQueue = EvictingQueue.create(ConfigManager.configWrapper.instance().modules.chat_style.history.cache_size);
EvictingQueue<Component> newQueue = EvictingQueue.create(ConfigManager.configWrapper.instance().modules.chat.history.cache_size);
newQueue.addAll(chatHistory);
chatHistory.clear();
chatHistory = newQueue;
Expand Down Expand Up @@ -132,7 +132,7 @@ private ClickEvent displayCallback(String displayUUID) {
if (audience instanceof CommandSourceStack css && css.getPlayer() != null) {
DisplayHelper.viewDisplay(css.getPlayer(), displayUUID);
}
}, ClickCallback.Options.builder().lifetime(Duration.of(ConfigManager.configWrapper.instance().modules.chat_style.display.expiration_duration_s, ChronoUnit.SECONDS))
}, ClickCallback.Options.builder().lifetime(Duration.of(ConfigManager.configWrapper.instance().modules.chat.display.expiration_duration_s, ChronoUnit.SECONDS))
.uses(Integer.MAX_VALUE).build());
}

Expand Down Expand Up @@ -164,7 +164,7 @@ public void broadcastChatMessage(ServerPlayer player, String message) {
message = ConfigManager.chatWrapper.instance().format.player2format.getOrDefault(player.getGameProfile().getName(), message)
.replace("%message%", message);
message = resolveMentionTag(player, message);
String format = ConfigManager.configWrapper.instance().modules.chat_style.format;
String format = ConfigManager.configWrapper.instance().modules.chat.format;
format = format.replace("%message%", message);
format = format.replace("%player%", player.getGameProfile().getName());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.sakurawald.module.chat_style.display;
package io.github.sakurawald.module.chat.display;

import io.github.sakurawald.module.chat_style.display.gui.*;
import io.github.sakurawald.module.chat.display.gui.*;
import io.github.sakurawald.util.MessageUtil;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display;
package io.github.sakurawald.module.chat.display;

import java.lang.ref.SoftReference;
import java.util.HashMap;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display.gui;
package io.github.sakurawald.module.chat.display.gui;

import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display.gui;
package io.github.sakurawald.module.chat.display.gui;

import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display.gui;
package io.github.sakurawald.module.chat.display.gui;

import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display.gui;
package io.github.sakurawald.module.chat.display.gui;

import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.display.gui;
package io.github.sakurawald.module.chat.display.gui;

import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.SimpleGui;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.sakurawald.module.chat_style.mention;
package io.github.sakurawald.module.chat.mention;

import io.github.sakurawald.config.ConfigGSON;
import io.github.sakurawald.config.ConfigManager;
Expand All @@ -17,7 +17,7 @@
public class MentionPlayersJob implements Job {

public static void scheduleJob(ArrayList<ServerPlayer> players) {
ConfigGSON.Modules.ChatStyle.MentionPlayer mentionPlayer = ConfigManager.configWrapper.instance().modules.chat_style.mention_player;
ConfigGSON.Modules.Chat.MentionPlayer mentionPlayer = ConfigManager.configWrapper.instance().modules.chat.mention_player;
int intervalMs = mentionPlayer.interval_ms;
int repeatCount = mentionPlayer.repeat_count;
Sound sound = Sound.sound(Key.key(mentionPlayer.sound), Sound.Source.MUSIC, mentionPlayer.volume, mentionPlayer.pitch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.sakurawald.config.ConfigManager;
import io.github.sakurawald.module.AbstractModule;
import io.github.sakurawald.module.ModuleManager;
import io.github.sakurawald.module.motd.DynamicMotdModule;
import io.github.sakurawald.module.motd.MotdModule;
import io.github.sakurawald.util.ScheduleUtil;
import lombok.extern.slf4j.Slf4j;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand All @@ -21,7 +21,7 @@
public class MainStatsModule extends AbstractModule {

private final List<Character> colors = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
private final DynamicMotdModule dynamic_motd_module = ModuleManager.getOrNewInstance(DynamicMotdModule.class);
private final MotdModule dynamic_motd_module = ModuleManager.getOrNewInstance(MotdModule.class);

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.Random;

@Slf4j
public class DynamicMotdModule extends AbstractModule {
public class MotdModule extends AbstractModule {
private final File ICON_FOLDER = ServerMain.CONFIG_PATH.resolve("icon").toFile();

private List<String> descriptions = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import io.github.sakurawald.module.AbstractModule;
import io.github.sakurawald.module.chat_style.mention.MentionPlayersJob;
import io.github.sakurawald.module.chat.mention.MentionPlayersJob;
import io.github.sakurawald.util.MessageUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/sakurawald.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"bypass_chat_speed.ServerGamePacketListenerImplMixin",
"bypass_max_player_limit.DedicatedPlayerManagerMixin",
"bypass_move_speed.ServerGamePacketListenerImplMixin",
"chat_style.PlayerListMixin",
"chat_style.ServerGamePacketListenerImplMixin",
"chat.PlayerListMixin",
"chat.ServerGamePacketListenerImplMixin",
"command_cooldown.CommandsMixin",
"command_spy.CommandsMixin",
"config.ServerPlayerMixin",
Expand Down

0 comments on commit 21ee8db

Please sign in to comment.