mirror of
https://github.com/Shiewk/SModeration.git
synced 2026-04-28 05:54:16 +02:00
Config rework
This commit is contained in:
@@ -2,7 +2,6 @@ package de.shiewk.smoderation.paper;
|
|||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import de.shiewk.smoderation.paper.command.*;
|
import de.shiewk.smoderation.paper.command.*;
|
||||||
import de.shiewk.smoderation.paper.config.SModerationConfig;
|
|
||||||
import de.shiewk.smoderation.paper.input.ChatInput;
|
import de.shiewk.smoderation.paper.input.ChatInput;
|
||||||
import de.shiewk.smoderation.paper.input.ChatInputListener;
|
import de.shiewk.smoderation.paper.input.ChatInputListener;
|
||||||
import de.shiewk.smoderation.paper.listener.*;
|
import de.shiewk.smoderation.paper.listener.*;
|
||||||
@@ -20,10 +19,14 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
|
|||||||
import net.kyori.adventure.text.minimessage.tag.Tag;
|
import net.kyori.adventure.text.minimessage.tag.Tag;
|
||||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import static de.shiewk.smoderation.paper.command.VanishCommand.isVanished;
|
import static de.shiewk.smoderation.paper.command.VanishCommand.isVanished;
|
||||||
@@ -37,7 +40,6 @@ public final class SModerationPaper extends JavaPlugin {
|
|||||||
public static final PunishmentContainer container = new PunishmentContainer();
|
public static final PunishmentContainer container = new PunishmentContainer();
|
||||||
public static ComponentLogger LOGGER = null;
|
public static ComponentLogger LOGGER = null;
|
||||||
public static SModerationPaper PLUGIN = null;
|
public static SModerationPaper PLUGIN = null;
|
||||||
public static SModerationConfig CONFIG = null;
|
|
||||||
public static File SAVE_FILE = null;
|
public static File SAVE_FILE = null;
|
||||||
private static SkinTextureProvider textureProvider = null;
|
private static SkinTextureProvider textureProvider = null;
|
||||||
|
|
||||||
@@ -55,20 +57,22 @@ public final class SModerationPaper extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
public static FileConfiguration config() {
|
||||||
|
return PLUGIN.getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
LOGGER = getComponentLogger();
|
LOGGER = getComponentLogger();
|
||||||
PLUGIN = this;
|
PLUGIN = this;
|
||||||
CONFIG = new SModerationConfig(this.getConfig(), this);
|
|
||||||
SAVE_FILE = new File(this.getDataFolder().getAbsolutePath() + "/container.gz");
|
SAVE_FILE = new File(this.getDataFolder().getAbsolutePath() + "/container.gz");
|
||||||
LOGGER.info("Loading translations");
|
LOGGER.info("Loading translations");
|
||||||
translatorManager.load();
|
translatorManager.load();
|
||||||
|
updateConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
CONFIG.reload();
|
|
||||||
|
|
||||||
getPluginManager().registerEvents(new PunishmentListener(), this);
|
getPluginManager().registerEvents(new PunishmentListener(), this);
|
||||||
getPluginManager().registerEvents(new CustomInventoryListener(), this);
|
getPluginManager().registerEvents(new CustomInventoryListener(), this);
|
||||||
getPluginManager().registerEvents(new InvSeeListener(), this);
|
getPluginManager().registerEvents(new InvSeeListener(), this);
|
||||||
@@ -140,4 +144,32 @@ public final class SModerationPaper extends JavaPlugin {
|
|||||||
)
|
)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updateConfig() {
|
||||||
|
LOGGER.info("Updating config");
|
||||||
|
try {
|
||||||
|
FileConfiguration config = getConfig();
|
||||||
|
|
||||||
|
InputStream defaultConfigStream = getResource("default-config.yml");
|
||||||
|
if (defaultConfigStream == null) {
|
||||||
|
throw new IllegalStateException("Default config not found in JAR; could not load");
|
||||||
|
}
|
||||||
|
|
||||||
|
YamlConfiguration defaultConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultConfigStream));
|
||||||
|
|
||||||
|
boolean changedSomething = false;
|
||||||
|
for (String key : defaultConfig.getKeys(true)) {
|
||||||
|
if (!config.contains(key)) { // There's a new key in the default config
|
||||||
|
config.set(key, defaultConfig.get(key));
|
||||||
|
changedSomething = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save the updated configuration file
|
||||||
|
if (changedSomething) saveConfig();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Could not update config", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public final class BanCommand implements CommandProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int banWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
private int banWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
||||||
if (SModerationPaper.CONFIG.shouldForceReason()){
|
if (SModerationPaper.config().getBoolean("force-reason", false)){
|
||||||
CommandUtil.errorTranslatable("smod.command.ban.fail.forceReason");
|
CommandUtil.errorTranslatable("smod.command.ban.fail.forceReason");
|
||||||
}
|
}
|
||||||
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public final class KickCommand implements CommandProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int kickWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
private int kickWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
||||||
if (SModerationPaper.CONFIG.shouldForceReason()){
|
if (SModerationPaper.config().getBoolean("force-reason", false)){
|
||||||
CommandUtil.errorTranslatable("smod.command.kick.fail.forceReason");
|
CommandUtil.errorTranslatable("smod.command.kick.fail.forceReason");
|
||||||
}
|
}
|
||||||
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ public final class MuteCommand implements CommandProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int muteWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
private int muteWithoutReason(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
|
||||||
if (SModerationPaper.CONFIG.shouldForceReason()){
|
if (SModerationPaper.config().getBoolean("force-reason", false)){
|
||||||
CommandUtil.errorTranslatable("smod.command.mute.fail.forceReason");
|
CommandUtil.errorTranslatable("smod.command.mute.fail.forceReason");
|
||||||
}
|
}
|
||||||
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
UUID sender = CommandUtil.getSenderUUID(context.getSource());
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
package de.shiewk.smoderation.paper.config;
|
|
||||||
|
|
||||||
import de.shiewk.smoderation.paper.SModerationPaper;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class SModerationConfig {
|
|
||||||
|
|
||||||
private final FileConfiguration config;
|
|
||||||
private final SModerationPaper plugin;
|
|
||||||
|
|
||||||
public SModerationConfig(FileConfiguration config, SModerationPaper plugin) {
|
|
||||||
this.config = config;
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> socialSpyCommands = List.of(
|
|
||||||
"w",
|
|
||||||
"tell",
|
|
||||||
"msg",
|
|
||||||
"teammsg",
|
|
||||||
"tm",
|
|
||||||
"minecraft:w",
|
|
||||||
"minecraft:tell",
|
|
||||||
"minecraft:msg",
|
|
||||||
"minecraft:teammsg",
|
|
||||||
"minecraft:tm"
|
|
||||||
);
|
|
||||||
private boolean forceReason = false;
|
|
||||||
|
|
||||||
public void reload(){
|
|
||||||
socialSpyCommands = loadOrSetStringList("socialspy-commands", socialSpyCommands);
|
|
||||||
forceReason = loadOrSetBoolean("force-reason", forceReason);
|
|
||||||
}
|
|
||||||
|
|
||||||
private List<String> loadOrSetStringList(String path, List<String> defaultValue) {
|
|
||||||
if (!config.contains(path)){
|
|
||||||
config.set(path, defaultValue);
|
|
||||||
plugin.saveConfig();
|
|
||||||
}
|
|
||||||
return config.getStringList(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean loadOrSetBoolean(String path, boolean defaultValue) {
|
|
||||||
if (!config.contains(path)){
|
|
||||||
config.set(path, defaultValue);
|
|
||||||
plugin.saveConfig();
|
|
||||||
}
|
|
||||||
return config.getBoolean(path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<String> getSocialSpyCommands(){
|
|
||||||
return socialSpyCommands;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean shouldForceReason(){
|
|
||||||
return forceReason;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -58,7 +58,7 @@ public class SocialSpyListener implements Listener {
|
|||||||
|
|
||||||
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
|
@EventHandler(ignoreCancelled = true, priority = EventPriority.NORMAL)
|
||||||
public void onPlayerSendCommand(PlayerCommandPreprocessEvent event){
|
public void onPlayerSendCommand(PlayerCommandPreprocessEvent event){
|
||||||
List<String> socialSpyCommands = SModerationPaper.CONFIG.getSocialSpyCommands();
|
List<String> socialSpyCommands = SModerationPaper.config().getStringList("socialspy-commands");
|
||||||
final String message = event.getMessage();
|
final String message = event.getMessage();
|
||||||
if (socialSpyCommands.stream().anyMatch(str ->
|
if (socialSpyCommands.stream().anyMatch(str ->
|
||||||
message.startsWith("/"+str+" ")
|
message.startsWith("/"+str+" ")
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
socialspy-commands:
|
||||||
|
- w
|
||||||
|
- tell
|
||||||
|
- msg
|
||||||
|
- teammsg
|
||||||
|
- tm
|
||||||
|
- minecraft:w
|
||||||
|
- minecraft:tell
|
||||||
|
- minecraft:msg
|
||||||
|
- minecraft:teammsg
|
||||||
|
- minecraft:tm
|
||||||
|
force-reason: false
|
||||||
Reference in New Issue
Block a user