diff --git a/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java b/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java index cffc5be1..fb685132 100644 --- a/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java +++ b/src/de/steamwar/bungeecore/commands/LockCurrentLocaleCommand.java @@ -20,7 +20,7 @@ package de.steamwar.bungeecore.commands; import de.steamwar.bungeecore.sql.SteamwarUser; -import de.steamwar.bungeecore.sql.UserConfig; +import de.steamwar.messages.ChatSender; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -40,12 +40,12 @@ public class LockCurrentLocaleCommand extends BasicCommand { 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 Locale locale = proxiedPlayer.getLocale(); if (locale == null) { - // TODO: Fehlermeldung + ChatSender.of(proxiedPlayer).system("LOCK_LOCALE_ERROR"); return; } - UserConfig.updatePlayerConfig(steamwarUser.getId(), "language", proxiedPlayer.getLocale().getLanguage()); + steamwarUser.setLocale(locale, true); + ChatSender.of(proxiedPlayer).system("LOCK_LOCALE_CHANGED"); } } diff --git a/src/de/steamwar/bungeecore/comms/PacketIdManager.java b/src/de/steamwar/bungeecore/comms/PacketIdManager.java index 8689fc6a..94edde93 100644 --- a/src/de/steamwar/bungeecore/comms/PacketIdManager.java +++ b/src/de/steamwar/bungeecore/comms/PacketIdManager.java @@ -28,6 +28,8 @@ public class PacketIdManager { public static final byte BAUMEMBER_UPDATE = 0x04; public static final byte EXECUTE_COMMAND = 0x05; + public static final byte LOCALE_INVALIDATION = 0x06; + //0x1(X) Bungee Inventory public static final byte INVENTORY_PACKET = 0x10; public static final byte INVENTORY_CALLBACK_PACKET = 0x11; diff --git a/src/de/steamwar/bungeecore/comms/packets/LocaleInvalidationPacket.java b/src/de/steamwar/bungeecore/comms/packets/LocaleInvalidationPacket.java new file mode 100644 index 00000000..1a0f4f7d --- /dev/null +++ b/src/de/steamwar/bungeecore/comms/packets/LocaleInvalidationPacket.java @@ -0,0 +1,43 @@ +/* + * 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.comms.packets; + +import com.google.common.io.ByteArrayDataOutput; +import de.steamwar.bungeecore.comms.BungeePacket; +import de.steamwar.bungeecore.comms.PacketIdManager; + +public class LocaleInvalidationPacket extends BungeePacket { + + private int userId; + + public LocaleInvalidationPacket(int userId) { + this.userId = userId; + } + + @Override + public int getId() { + return PacketIdManager.LOCALE_INVALIDATION; + } + + @Override + public void writeVars(ByteArrayDataOutput byteArrayDataOutput) { + byteArrayDataOutput.writeInt(userId); + } +} diff --git a/src/de/steamwar/bungeecore/sql/SteamwarUser.java b/src/de/steamwar/bungeecore/sql/SteamwarUser.java index 1cd6d8aa..6bb88e53 100644 --- a/src/de/steamwar/bungeecore/sql/SteamwarUser.java +++ b/src/de/steamwar/bungeecore/sql/SteamwarUser.java @@ -23,6 +23,7 @@ import com.google.gson.JsonParser; import de.steamwar.bungeecore.BungeeCore; import de.steamwar.bungeecore.Message; import de.steamwar.bungeecore.commands.WebregisterCommand; +import de.steamwar.bungeecore.comms.packets.LocaleInvalidationPacket; import de.steamwar.bungeecore.listeners.ConnectionListener; import de.steamwar.messages.ChatSender; import net.md_5.bungee.api.ProxyServer; @@ -45,6 +46,7 @@ public class SteamwarUser { private static final Statement updateName = new Statement("UPDATE UserData SET UserName = ? WHERE id = ?"); private static final Statement updateBedrock = new Statement("UPDATE UserData SET Bedrock = ? WHERE id = ?"); + private static final Statement updateLocale = new Statement("UPDATE UserData SET Locale = ?, ManualLocale = ? WHERE id = ?"); private static final Statement insert = new Statement("INSERT INTO UserData (UUID, UserName, UserGroup) VALUES (?, ?, 'Member')"); private static final Statement byUUID = new Statement("SELECT * FROM UserData WHERE UUID = ?"); private static final Statement byName = new Statement("SELECT * FROM UserData WHERE lower(UserName) = ?"); @@ -74,6 +76,9 @@ public class SteamwarUser { private final Map punishments; private Long discordId; + private Locale locale; + private boolean manualLocale; + static { try { LIXFEL_DE = InetAddress.getByAddress(new byte[]{(byte) 195, (byte) 201, (byte) 242, 43}); @@ -94,6 +99,15 @@ public class SteamwarUser { if(rs.wasNull()) { discordId = null; } + + String dbLocale = rs.getString("Locale"); + if (dbLocale == null) { + locale = null; + } else { + locale = new Locale(dbLocale); + } + manualLocale = rs.getBoolean("ManualLocale"); + usersById.put(id, this); usersByName.put(userName.toLowerCase(), this); usersByUUID.put(uuid, this); @@ -101,6 +115,7 @@ public class SteamwarUser { usersByDiscord.put(discordId, this); } punishments = Punishment.getPunishmentsOfPlayer(id); + } public static SteamwarUser getOrCreate(PendingConnection connection) { @@ -364,4 +379,19 @@ public class SteamwarUser { this.leader = leader; updateLeader.update(leader, id); } + + public Locale getLocale() { + return locale; + } + + public boolean isManualLocale() { + return manualLocale; + } + + public void setLocale(Locale locale, boolean manualLocale) { + this.locale = locale; + this.manualLocale = manualLocale; + updateLocale.update(locale.getLanguage(), manualLocale, id); + new LocaleInvalidationPacket(id).send(getPlayer()); + } } diff --git a/src/de/steamwar/bungeecore/sql/UserConfig.java b/src/de/steamwar/bungeecore/sql/UserConfig.java deleted file mode 100644 index c62da9dc..00000000 --- a/src/de/steamwar/bungeecore/sql/UserConfig.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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/BungeeCore.properties b/src/de/steamwar/messages/BungeeCore.properties index a62aa000..8e13950f 100644 --- a/src/de/steamwar/messages/BungeeCore.properties +++ b/src/de/steamwar/messages/BungeeCore.properties @@ -619,4 +619,8 @@ FIGHT_MERGE_DECLINE=Launch new arena FIGHT_MERGE_ACCEPT=Join fight FIGHT_MERGE_INFO_LORE_1=§8By: §e{0} FIGHT_MERGE_OFFLINE=§7The proposed arena has been terminated in the meantime, a new arena will be started. -FIGHT_MERGE_INFO=§e{0}§8: §e{1} \ No newline at end of file +FIGHT_MERGE_INFO=§e{0}§8: §e{1} + +#Locale Locking +LOCK_LOCALE_ERROR=§cError while locking your language +LOCK_LOCALE_CHANGED=§aLanguage locked to your current minecraft language \ No newline at end of file diff --git a/src/de/steamwar/messages/ChatSender.java b/src/de/steamwar/messages/ChatSender.java index 674290a9..4c8f6118 100644 --- a/src/de/steamwar/messages/ChatSender.java +++ b/src/de/steamwar/messages/ChatSender.java @@ -22,7 +22,6 @@ 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; @@ -47,14 +46,7 @@ import java.util.stream.Stream; public interface ChatSender { static Locale getLocale(ProxiedPlayer player) { - String dbLocale = UserConfig.getConfig(SteamwarUser.get(player).getId(), "language"); - if (dbLocale != null) { - return new Locale(dbLocale); - } - Locale locale = player.getLocale(); - if(locale == null) - locale = Locale.getDefault(); - return locale; + return SteamwarUser.get(player).getLocale(); } static Stream all() {