From 22e691dcb85df63fb004ecea432cda30b1da88a1 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 21 May 2022 13:21:20 +0200 Subject: [PATCH] Add LocaleInvalidationHandler --- .../src/de/steamwar/comms/BungeeReceiver.java | 2 ++ .../de/steamwar/comms/PacketIdManager.java | 2 ++ .../handlers/LocaleInvalidationHandler.java | 31 +++++++++++++++++++ .../src/de/steamwar/message/Message.java | 13 ++++---- .../src/de/steamwar/sql/SQLProvider.java | 7 ++++- .../de/steamwar/sql/StandaloneProvider.java | 5 +-- .../src/de/steamwar/sql/SteamwarUser.java | 22 ++++++++++--- 7 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 021414e..e7a0b8c 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -23,6 +23,7 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import de.steamwar.comms.handlers.BungeeHandler; import de.steamwar.comms.handlers.InventoryHandler; +import de.steamwar.comms.handlers.LocaleInvalidationHandler; import de.steamwar.core.BountifulWrapper; import de.steamwar.sql.BauweltMember; import de.steamwar.sql.SteamwarUser; @@ -57,6 +58,7 @@ public class BungeeReceiver implements PluginMessageListener { Player player = Bukkit.getPlayer(SteamwarUser.get(byteArrayDataInput.readInt()).getUUID()); player.closeInventory(); }); + registerHandler(PacketIdManager.LOCALE_INVALIDATION, new LocaleInvalidationHandler()); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java index 6eb462b..7020c68 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java +++ b/SpigotCore_Main/src/de/steamwar/comms/PacketIdManager.java @@ -28,6 +28,8 @@ public class PacketIdManager { public static final byte PREPARE_SCHEM = 0x03; 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/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java b/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java new file mode 100644 index 0000000..3ebaaa1 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/comms/handlers/LocaleInvalidationHandler.java @@ -0,0 +1,31 @@ +/* + * 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.comms.handlers; + +import com.google.common.io.ByteArrayDataInput; +import de.steamwar.sql.SteamwarUser; + +public class LocaleInvalidationHandler implements BungeeHandler { + + @Override + public void handle(ByteArrayDataInput byteArrayDataInput) { + SteamwarUser.invalidate(byteArrayDataInput.readInt()); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/message/Message.java b/SpigotCore_Main/src/de/steamwar/message/Message.java index 21977e6..9cb9283 100644 --- a/SpigotCore_Main/src/de/steamwar/message/Message.java +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -21,7 +21,7 @@ package de.steamwar.message; import de.steamwar.core.BountifulWrapper; import de.steamwar.core.WorldOfColorWrapper; -import de.steamwar.sql.UserConfig; +import de.steamwar.sql.SteamwarUser; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.HoverEvent; @@ -75,11 +75,12 @@ public class Message { } private Locale getLocale(Player player){ - String dbLocale = UserConfig.getConfig(player.getUniqueId(), "language"); - if (dbLocale != null) { - return new Locale(dbLocale); - } - return WorldOfColorWrapper.impl.getLocale(player); + Locale locale = SteamwarUser.get(player).getLocale(); + if (locale == null) + locale = WorldOfColorWrapper.impl.getLocale(player); + if (locale == null) + locale = Locale.getDefault(); + return locale; } /* Send a message to one player */ diff --git a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java index f84865c..6e71127 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SQLProvider.java @@ -331,7 +331,12 @@ public class SQLProvider implements Provider { } private SteamwarUser newSteamwarUser(ResultSet rs) throws SQLException { - return new SteamwarUser(rs.getInt("id"), UUID.fromString(rs.getString("UUID")), rs.getString("UserName"), UserGroup.getUsergroup(rs.getString("UserGroup")), rs.getInt("Team"), rs.getBoolean("Bedrock")); + String dbLocale = rs.getString("Locale"); + Locale locale = null; + if (dbLocale != null) { + locale = new Locale(dbLocale); + } + return new SteamwarUser(rs.getInt("id"), UUID.fromString(rs.getString("UUID")), rs.getString("UserName"), UserGroup.getUsergroup(rs.getString("UserGroup")), rs.getInt("Team"), rs.getBoolean("Bedrock"), locale); } private static final Statement insert = new Statement("INSERT INTO Exception (server, message, stacktrace) VALUES (?, ?, ?)"); diff --git a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java index 29b5f95..c70cd1c 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java +++ b/SpigotCore_Main/src/de/steamwar/sql/StandaloneProvider.java @@ -20,6 +20,7 @@ package de.steamwar.sql; import de.steamwar.core.WorldEditWrapper; +import de.steamwar.core.WorldOfColorWrapper; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -168,12 +169,12 @@ public class StandaloneProvider implements Provider { Player player = Bukkit.getPlayer(userName); if(player == null) return null; - return usersByUUID.computeIfAbsent(player.getUniqueId(), uuid -> new SteamwarUser(userId++, uuid, userName, UserGroup.Member, 0, false)); + return usersByUUID.computeIfAbsent(player.getUniqueId(), uuid -> new SteamwarUser(userId++, uuid, userName, UserGroup.Member, 0, false, WorldOfColorWrapper.impl.getLocale(player))); } @Override public SteamwarUser getUserByUUID(UUID uuid) { - return usersByUUID.computeIfAbsent(uuid, uuid1 -> new SteamwarUser(userId++, uuid1, Objects.requireNonNull(Objects.requireNonNull(Bukkit.getOfflinePlayer(uuid1)).getName()), UserGroup.Member, 0, false)); + return usersByUUID.computeIfAbsent(uuid, uuid1 -> new SteamwarUser(userId++, uuid1, Objects.requireNonNull(Objects.requireNonNull(Bukkit.getOfflinePlayer(uuid1)).getName()), UserGroup.Member, 0, false, Locale.getDefault())); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index 6bac625..9de9c2d 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -23,10 +23,7 @@ import de.steamwar.core.Core; import org.bukkit.Bukkit; import org.bukkit.entity.Player; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; public class SteamwarUser { @@ -43,20 +40,31 @@ public class SteamwarUser { }, 72000, 72000); } + public static void invalidate(int userId) { + if (!byId.containsKey(userId)) + return; + SteamwarUser user = byId.get(userId); + byId.remove(userId); + byName.remove(user.getUserName()); + byUUID.remove(user.getUUID()); + } + private final int id; private final UUID uuid; private final String userName; private final UserGroup userGroup; private final int team; private final boolean bedrock; + private final Locale locale; - public SteamwarUser(int id, UUID uuid, String userName, UserGroup userGroup, int team, boolean bedrock) { + public SteamwarUser(int id, UUID uuid, String userName, UserGroup userGroup, int team, boolean bedrock, Locale locale) { this.id = id; this.uuid = uuid; this.userName = userName; this.userGroup = userGroup; this.team = team; this.bedrock = bedrock; + this.locale = locale; byUUID.put(uuid, this); byName.put(userName.toLowerCase(), this); @@ -87,6 +95,10 @@ public class SteamwarUser { return bedrock; } + public Locale getLocale() { + return locale; + } + public static SteamwarUser get(String userName){ SteamwarUser user = byName.get(userName.toLowerCase()); if(user == null)