From 779199f05850505e840722b7d2ff8e6797f3b1e0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 9 Dec 2021 17:27:27 +0100 Subject: [PATCH] Update SWCommandUtils SteamwarUser mapper --- .../de/steamwar/command/SWCommandUtils.java | 6 +--- .../src/de/steamwar/sql/SteamwarUser.java | 31 ++++++++++++++----- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 0d95f8c..aa5d27b 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -23,15 +23,11 @@ import de.steamwar.sql.SchematicNode; import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.GameMode; -import org.bukkit.command.Command; -import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; -import org.bukkit.command.SimpleCommandMap; import org.bukkit.entity.Player; import java.lang.annotation.Annotation; import java.lang.reflect.Array; -import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.*; import java.util.function.BiFunction; @@ -76,7 +72,7 @@ public class SWCommandUtils { if (s.length() < 1) { return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); } - List steamwarUsers = SteamwarUser.startsWithName(s); + List steamwarUsers = SteamwarUser.getByPrefix(s); return steamwarUsers.stream().map(SteamwarUser::getUserName).limit(40).collect(Collectors.toList()); })); MAPPER_FUNCTIONS.put(SchematicNode.class.getTypeName(), new TypeMapper() { diff --git a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java index cc958cf..9c3115b 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java +++ b/SpigotCore_Main/src/de/steamwar/sql/SteamwarUser.java @@ -26,6 +26,7 @@ import org.bukkit.entity.Player; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; +import java.util.stream.Collectors; public class SteamwarUser { @@ -37,12 +38,14 @@ public class SteamwarUser { private static final Map byUUID = new HashMap<>(); private static final Map byName = new HashMap<>(); private static final Map byId = new HashMap<>(); + private static final Map> byPrefix = new HashMap<>(); static{ Bukkit.getScheduler().runTaskTimer(Core.getInstance(), () -> { byId.clear(); byName.clear(); byUUID.clear(); + byPrefix.clear(); }, 72000, 72000); } @@ -94,14 +97,26 @@ public class SteamwarUser { return bedrock; } - public static List startsWithName(String userName){ - return startWithName.select(rs -> { - List steamwarUsers = new ArrayList<>(); - while (rs.next()) { - steamwarUsers.add(new SteamwarUser(rs)); - } - return steamwarUsers; - }, userName.toLowerCase() + "%"); + public static List getByPrefix(String userName){ + if (userName.length() < 2) { + return new ArrayList<>(); + } + List users = byPrefix.get(userName.toLowerCase().substring(0, 1)); + if (users == null) { + users = startWithName.select(rs -> { + List steamwarUsers = new ArrayList<>(); + while (rs.next()) { + String name = rs.getString("UserName"); + if (byName.containsKey(name)) { + steamwarUsers.add(byName.get(name)); + } else { + steamwarUsers.add(new SteamwarUser(rs)); + } + } + return steamwarUsers; + }, userName.toLowerCase() + "%"); + } + return users.stream().filter(user -> user.getUserName().toLowerCase().startsWith(userName.toLowerCase())).collect(Collectors.toList()); } public static SteamwarUser get(String userName){