From 092efd7063c1b2b31ece2e21a9278d4a3020512c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 21 May 2022 11:00:08 +0200 Subject: [PATCH] Add locale locking --- .../commands/LockCurrentLocaleCommand.java | 44 +++++++++++++ .../steamwar/bungeecore/sql/UserConfig.java | 62 +++++++++++++++++++ src/de/steamwar/messages/ChatSender.java | 4 ++ 3 files changed, 110 insertions(+) create mode 100644 src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java create mode 100644 src/de/steamwar/bungeecore/sql/UserConfig.java diff --git a/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java b/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java new file mode 100644 index 0000000..60daf4c --- /dev/null +++ b/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java @@ -0,0 +1,44 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.commands; + +import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.UserConfig; +import net.md_5.bungee.api.CommandSender; +import net.md_5.bungee.api.connection.ProxiedPlayer; + +public class LockCurrentLocaleCommand extends BasicCommand { + + public LockCurrentLocaleCommand() { + super("lockcurrentlocale", "", "locklocale", "locale"); + } + + @Override + public void execute(CommandSender commandSender, String[] strings) { + if (!(commandSender instanceof ProxiedPlayer)) { + return; + } + ProxiedPlayer proxiedPlayer = (ProxiedPlayer) commandSender; + SteamwarUser steamwarUser = SteamwarUser.get(proxiedPlayer); + + // TODO: Hier fehlt noch eine Nachricht an den User. Sollte deswegen erst gemerged werden, wenn der branch 'english' germerged ist + UserConfig.updatePlayerConfig(steamwarUser.getId(), "language", proxiedPlayer.getLocale().getLanguage()); + } +} diff --git a/src/de/steamwar/bungeecore/sql/UserConfig.java b/src/de/steamwar/bungeecore/sql/UserConfig.java new file mode 100644 index 0000000..c62da9d --- /dev/null +++ b/src/de/steamwar/bungeecore/sql/UserConfig.java @@ -0,0 +1,62 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bungeecore.sql; + +import java.util.UUID; + +public class UserConfig { + private UserConfig() {} + + private static final Statement getConfig = new Statement("SELECT Value FROM UserConfig WHERE User = ? AND Config = ?"); + private static final Statement setConfig = new Statement("INSERT INTO UserConfig (User, Config, Value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE Value = VALUES(Value)"); + private static final Statement deleteConfig = new Statement("DELETE FROM UserConfig WHERE User = ? AND Config = ?"); + + public static String getConfig(UUID player, String config) { + return getConfig(SteamwarUser.get(player).getId(), config); + } + + public static String getConfig(int player, String config) { + return getConfig.select(rs -> { + if(rs.next()) + return rs.getString("Value"); + return null; + }, player, config); + } + + public static void updatePlayerConfig(UUID uuid, String config, String value) { + updatePlayerConfig(SteamwarUser.get(uuid).getId(), config, value); + } + + public static void updatePlayerConfig(int id, String config, String value) { + if (value == null) { + removePlayerConfig(id, config); + return; + } + setConfig.update(id, config, value); + } + + public static void removePlayerConfig(UUID uuid, String config) { + removePlayerConfig(SteamwarUser.get(uuid).getId(), config); + } + + public static void removePlayerConfig(int id, String config) { + deleteConfig.update(id, config); + } +} diff --git a/src/de/steamwar/messages/ChatSender.java b/src/de/steamwar/messages/ChatSender.java index de80541..a5a3384 100644 --- a/src/de/steamwar/messages/ChatSender.java +++ b/src/de/steamwar/messages/ChatSender.java @@ -22,6 +22,7 @@ package de.steamwar.messages; import de.steamwar.bungeecore.*; import de.steamwar.bungeecore.bot.listeners.DiscordChatListener; import de.steamwar.bungeecore.sql.SteamwarUser; +import de.steamwar.bungeecore.sql.UserConfig; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; @@ -150,6 +151,9 @@ public interface ChatSender { @Override public Locale getLocale() { + String dbLocale = UserConfig.getConfig(user().getId(), "language"); + if (dbLocale != null) + return new Locale(dbLocale); Locale locale = player.getLocale(); if(locale == null) locale = Locale.getDefault();