From b6104a76c15b9c12355011c8bfc3f18792a42929 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 10 Jul 2022 00:55:56 +0200 Subject: [PATCH] Fixing a shit ton of warning / code style and stuff (code inspector from IDEA) --- .../lib/bungee/commands/BrigadierCommand.java | 24 +-- .../bungee/commands/BrigadierDispatcher.java | 14 +- .../bungee/util/PluginMessagePassthrough.java | 2 +- .../pandacube/lib/cli/BrigadierCommand.java | 15 +- .../lib/cli/BrigadierDispatcher.java | 8 +- .../main/java/fr/pandacube/lib/cli/CLI.java | 8 +- .../lib/cli/DailyLogRotateFileHandler.java | 11 +- Core/pom.xml | 8 +- .../java/fr/pandacube/lib/core/chat/Chat.java | 25 +-- .../lib/core/chat/ChatColorUtil.java | 20 +- .../pandacube/lib/core/chat/ChatStatic.java | 13 +- .../fr/pandacube/lib/core/chat/ChatUtil.java | 46 ++--- .../lib/core/commands/AbstractCommand.java | 3 - .../lib/core/commands/BadCommandUsage.java | 1 - .../core/commands/SuggestionsSupplier.java | 59 +++--- .../lib/core/config/AbstractConfig.java | 29 ++- .../core/config/AbstractConfigManager.java | 2 - .../java/fr/pandacube/lib/core/db/DB.java | 30 ++- .../pandacube/lib/core/db/DBConnection.java | 13 +- .../fr/pandacube/lib/core/db/DBException.java | 1 - .../lib/core/db/DBInitTableException.java | 5 - .../lib/core/db/ParameterizedSQLString.java | 12 +- .../fr/pandacube/lib/core/db/SQLElement.java | 75 ++++--- .../pandacube/lib/core/db/SQLElementList.java | 17 +- .../fr/pandacube/lib/core/db/SQLField.java | 11 +- .../fr/pandacube/lib/core/db/SQLOrderBy.java | 16 +- .../fr/pandacube/lib/core/db/SQLType.java | 9 +- .../fr/pandacube/lib/core/db/SQLUpdate.java | 12 +- .../fr/pandacube/lib/core/db/SQLWhere.java | 41 ++-- .../fr/pandacube/lib/core/net/Array8Bit.java | 4 - .../fr/pandacube/lib/core/net/ByteBuffer.java | 7 +- .../fr/pandacube/lib/core/net/PPacket.java | 4 +- .../pandacube/lib/core/net/PPacketAnswer.java | 2 +- .../lib/core/net/PPacketListener.java | 2 +- .../fr/pandacube/lib/core/net/PServer.java | 56 +++-- .../fr/pandacube/lib/core/net/PSocket.java | 24 +-- .../core/net/PSocketConnectionListener.java | 4 +- .../network_api/client/AbstractRequest.java | 2 +- .../network_api/client/ResponseAnalyser.java | 4 +- .../server/NetworkAPIListener.java | 19 +- .../network_api/server/PacketExecutor.java | 10 +- .../network_api/server/RequestAnalyser.java | 15 +- .../lib/core/permissions/PermEntity.java | 16 +- .../lib/core/permissions/PermPlayer.java | 3 - .../lib/core/permissions/Permissions.java | 6 +- .../permissions/PermissionsBackendWriter.java | 78 ++++--- .../PermissionsCachedBackendReader.java | 15 +- .../core/permissions/PermissionsResolver.java | 60 +++--- .../lib/core/permissions/ServerWorldKey.java | 6 +- .../lib/core/players/IOffPlayer.java | 75 ++++--- .../lib/core/players/IOnlinePlayer.java | 91 ++++---- .../lib/core/players/IPlayerManager.java | 194 +++--------------- .../lib/core/players/PlayerFinder.java | 16 +- .../lib/core/players/SQLPlayerIgnore.java | 1 - .../lib/core/search/SearchEngine.java | 32 ++- .../lib/core/search/SearchResult.java | 4 +- .../lib/core/util/AmountPerTimeLimiter.java | 2 +- .../fr/pandacube/lib/core/util/BiMap.java | 4 +- .../lib/core/util/CronExpression.java | 52 ++--- .../pandacube/lib/core/util/DistanceUtil.java | 13 +- .../fr/pandacube/lib/core/util/EnumUtil.java | 17 +- .../fr/pandacube/lib/core/util/FileUtils.java | 16 +- .../pandacube/lib/core/util/GifDecoder.java | 46 ++--- .../lib/core/util/IteratorIterator.java | 19 +- .../lib/core/util/JArithmeticInterpreter.java | 78 +++---- .../java/fr/pandacube/lib/core/util/Json.java | 16 +- .../lib/core/util/LazyOrException.java | 8 +- .../lib/core/util/LevenshteinDistance.java | 8 +- .../java/fr/pandacube/lib/core/util/Log.java | 2 +- .../lib/core/util/MappedListView.java | 151 +++++++++++++- .../pandacube/lib/core/util/MemoryUtil.java | 2 +- .../lib/core/util/MinecraftVersion.java | 6 +- .../lib/core/util/MinecraftWebUtil.java | 32 ++- .../pandacube/lib/core/util/OfflineUUID.java | 4 +- .../pandacube/lib/core/util/RandomUtil.java | 40 ++-- .../fr/pandacube/lib/core/util/Reflect.java | 147 +++++++------ .../lib/core/util/ServerPropertyFile.java | 8 +- .../lib/core/util/ThrowableUtil.java | 13 +- .../java/fr/pandacube/lib/core/util/Tick.java | 2 +- .../fr/pandacube/lib/core/util/TimeUtil.java | 118 ++++------- .../lib/core/util/TypeConverter.java | 10 +- .../fr/pandacube/lib/paper/gui/GUIHotBar.java | 21 +- .../pandacube/lib/paper/gui/GUIInventory.java | 6 +- .../lib/paper/reflect/NMSReflect.java | 36 ++-- .../fr/pandacube/lib/paper/reflect/Type.java | 5 - .../reflect/wrapper/ReflectListWrapper.java | 153 +------------- .../paper/reflect/wrapper/ReflectWrapper.java | 3 + .../reflect/wrapper/WrapperRegistry.java | 21 +- .../wrapper/brigadier/CommandNode.java | 1 - .../wrapper/minecraft/WorldVersion.java | 4 +- .../minecraft/commands/BlockPosArgument.java | 5 +- .../minecraft/commands/ComponentArgument.java | 5 +- .../minecraft/commands/Coordinates.java | 12 +- .../minecraft/commands/EntityArgument.java | 20 +- .../commands/GameProfileArgument.java | 5 +- .../commands/ResourceLocationArgument.java | 5 +- .../minecraft/commands/Vec3Argument.java | 5 +- .../wrapper/minecraft/core/BlockPos.java | 2 - .../reflect/wrapper/minecraft/nbt/Tag.java | 8 +- .../minecraft/network/chat/Component.java | 4 +- .../minecraft/network/protocol/Packet.java | 4 +- .../minecraft/server/ServerPlayer.java | 7 + .../minecraft/util/ProgressListener.java | 4 +- .../wrapper/minecraft/world/SavedData.java | 6 +- .../paper/scheduler/AutoUpdatedObject.java | 6 +- .../pandacube/lib/paper/util/AABBBlock.java | 33 ++- .../lib/paper/util/AABBBlockGroup.java | 11 +- .../lib/paper/util/AutoUpdatedBossBar.java | 28 +-- .../lib/paper/util/BukkitChatColorUtil.java | 53 ++--- .../pandacube/lib/paper/util/BukkitEvent.java | 32 ++- .../pandacube/lib/paper/util/ColorUtil.java | 3 +- .../lib/paper/util/ExperienceUtil.java | 15 +- .../lib/paper/util/GameWorldUtils.java | 2 +- .../lib/paper/util/GeometryUtil.java | 28 +-- .../lib/paper/util/ItemStackBuilder.java | 10 +- .../lib/paper/util/LocationUtil.java | 18 +- .../fr/pandacube/lib/paper/util/Skull.java | 12 +- pom.xml | 3 +- 118 files changed, 1116 insertions(+), 1574 deletions(-) diff --git a/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierCommand.java b/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierCommand.java index b4e41af..cc1bae5 100644 --- a/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierCommand.java +++ b/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierCommand.java @@ -33,9 +33,7 @@ import net.md_5.bungee.api.plugin.TabExecutor; import net.md_5.bungee.command.ConsoleCommandSender; public abstract class BrigadierCommand extends ChatStatic { - - private LiteralCommandNode commandNode; - + protected BrigadierDispatcher dispatcher; public BrigadierCommand() { @@ -56,8 +54,8 @@ public abstract class BrigadierCommand extends ChatStatic { } if (aliases == null) aliases = new String[0]; - - commandNode = dispatcher.register(builder); + + LiteralCommandNode commandNode = dispatcher.register(builder); // still have to be registered for console BungeeCord.getInstance().getPluginManager().registerCommand(dispatcher.plugin, new CommandRelay(commandNode.getLiteral())); @@ -75,7 +73,7 @@ public abstract class BrigadierCommand extends ChatStatic { } private class CommandRelay extends Command implements TabExecutor { - private String alias; + private final String alias; public CommandRelay(String alias) { super(alias); this.alias = alias; @@ -98,7 +96,7 @@ public abstract class BrigadierCommand extends ChatStatic { return suggestions.getList() .stream() .filter(s -> s.getRange().equals(supportedRange)) - .map(s -> s.getText()) + .map(Suggestion::getText) .collect(Collectors.toList()); } } @@ -125,7 +123,7 @@ public abstract class BrigadierCommand extends ChatStatic { } public static Predicate isPlayer() { - return sender -> isPlayer(sender); + return BrigadierCommand::isPlayer; } public static boolean isPlayer(CommandSender sender) { @@ -133,7 +131,7 @@ public abstract class BrigadierCommand extends ChatStatic { } public static Predicate isConsole() { - return sender -> isConsole(sender); + return BrigadierCommand::isConsole; } public static boolean isConsole(CommandSender sender) { @@ -172,16 +170,14 @@ public abstract class BrigadierCommand extends ChatStatic { String message = builder.getInput(); try { int tokenStartPos = builder.getStart(); - - List results = Collections.emptyList(); - + int firstSpacePos = message.indexOf(" "); String[] args = (firstSpacePos + 1 > tokenStartPos - 1) ? new String[0] : message.substring(firstSpacePos + 1, tokenStartPos - 1).split(" ", -1); args = Arrays.copyOf(args, args.length + 1); args[args.length - 1] = message.substring(tokenStartPos); - - results = suggestions.getSuggestions(sender, args.length - 1, args[args.length - 1], args); + + List results = suggestions.getSuggestions(sender, args.length - 1, args[args.length - 1], args); for (String s : results) { if (s != null) diff --git a/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierDispatcher.java b/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierDispatcher.java index 30e60fe..f4bb3c7 100644 --- a/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierDispatcher.java +++ b/Bungee/src/main/java/fr/pandacube/lib/bungee/commands/BrigadierDispatcher.java @@ -46,8 +46,8 @@ public class BrigadierDispatcher implements Listener { - private CommandDispatcher dispatcher; - /* package */ Plugin plugin; + private final CommandDispatcher dispatcher; + /* package */ final Plugin plugin; private BrigadierDispatcher(Plugin pl) { plugin = pl; @@ -114,27 +114,23 @@ public class BrigadierDispatcher implements Listener { event.setCancelled(true); - ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> { - execute((ProxiedPlayer) event.getSender(), commandLine); - }); + ProxyServer.getInstance().getScheduler().runAsync(plugin, () -> execute((ProxiedPlayer) event.getSender(), commandLine)); } - /* package */ int execute(CommandSender sender, String commandWithoutSlash) { + /* package */ void execute(CommandSender sender, String commandWithoutSlash) { ParseResults parsed = dispatcher.parse(commandWithoutSlash, sender); try { - return dispatcher.execute(parsed); + dispatcher.execute(parsed); } catch (CommandSyntaxException e) { sender.sendMessage(Chat.failureText("Erreur d'utilisation de la commande : " + e.getMessage()).get()); - return 0; } catch (Throwable e) { sender.sendMessage(Chat.failureText("Erreur lors de l'exécution de la commande : " + e.getMessage()).get()); Log.severe(e); - return 0; } } diff --git a/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java b/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java index 5652f31..f7ae1a0 100644 --- a/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java +++ b/Bungee/src/main/java/fr/pandacube/lib/bungee/util/PluginMessagePassthrough.java @@ -25,7 +25,7 @@ public class PluginMessagePassthrough implements Listener { public static void clear() { synchronized (channels) { - new ArrayList<>(channels).forEach(c -> unregister(c)); + unregisterAll(channels.toArray(new String[0])); } } diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java b/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java index 0917f1a..b857351 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierCommand.java @@ -1,7 +1,6 @@ package fr.pandacube.lib.cli; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -22,16 +21,14 @@ import fr.pandacube.lib.core.util.Log; import fr.pandacube.lib.core.util.Reflect; public abstract class BrigadierCommand extends ChatStatic { - - private LiteralCommandNode commandNode; - + public BrigadierCommand() { LiteralArgumentBuilder builder = buildCommand(); String[] aliases = getAliases(); if (aliases == null) aliases = new String[0]; - - commandNode = BrigadierDispatcher.instance.register(builder); + + LiteralCommandNode commandNode = BrigadierDispatcher.instance.register(builder); for (String alias : aliases) { @@ -94,16 +91,14 @@ public abstract class BrigadierCommand extends ChatStatic { String message = builder.getInput(); try { int tokenStartPos = builder.getStart(); - - List results = Collections.emptyList(); int firstSpacePos = message.indexOf(" "); String[] args = (firstSpacePos + 1 > tokenStartPos - 1) ? new String[0] : message.substring(firstSpacePos + 1, tokenStartPos - 1).split(" ", -1); args = Arrays.copyOf(args, args.length + 1); args[args.length - 1] = message.substring(tokenStartPos); - - results = suggestions.getSuggestions(sender, args.length - 1, args[args.length - 1], args); + + List results = suggestions.getSuggestions(sender, args.length - 1, args[args.length - 1], args); for (String s : results) { if (s != null) diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierDispatcher.java b/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierDispatcher.java index c5a7ef6..51930de 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierDispatcher.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/BrigadierDispatcher.java @@ -31,9 +31,9 @@ public class BrigadierDispatcher implements Completer { - private CommandDispatcher dispatcher; + private final CommandDispatcher dispatcher; - private Object sender = new Object(); + private final Object sender = new Object(); public BrigadierDispatcher() { dispatcher = new CommandDispatcher<>(); @@ -69,7 +69,9 @@ public class BrigadierDispatcher implements Completer { Suggestions completeResult = getSuggestions(bufferBeforeCursor); - completeResult.getList().stream().map(s -> s.getText()).forEach(candidates::add); + completeResult.getList().stream() + .map(Suggestion::getText) + .forEach(candidates::add); return completeResult.getRange().getStart(); } diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java index 813579d..63f6828 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/CLI.java @@ -38,8 +38,8 @@ public class CLI { - private ConsoleReader reader; - private Logger logger; + private final ConsoleReader reader; + private final Logger logger; public CLI() throws IOException { @@ -78,9 +78,7 @@ public class CLI { if (line.trim().equals("")) continue; String cmdLine = line; - new Thread(() -> { - BrigadierDispatcher.instance.execute(cmdLine); - }, "CLICmdThread #"+(i++)).start(); + new Thread(() -> BrigadierDispatcher.instance.execute(cmdLine), "CLICmdThread #"+(i++)).start(); } } catch (IOException e) { diff --git a/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java b/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java index 5f1d01a..ad65d20 100644 --- a/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java +++ b/CLI/src/main/java/fr/pandacube/lib/cli/DailyLogRotateFileHandler.java @@ -12,9 +12,9 @@ import java.util.logging.Handler; import java.util.logging.LogRecord; class DailyLogRotateFileHandler extends Handler { + private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); private BufferedWriter currentFile = null; - private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); private String currentFileDate = getCurrentDay(); private boolean closed = false; @@ -24,7 +24,7 @@ class DailyLogRotateFileHandler extends Handler { closed = true; if (currentFile != null) try { currentFile.close(); - } catch (IOException e) {} + } catch (IOException ignored) {} } @Override @@ -33,7 +33,7 @@ class DailyLogRotateFileHandler extends Handler { if (currentFile == null) return; try { currentFile.flush(); - } catch (IOException e) {} + } catch (IOException ignored) {} } @Override @@ -68,7 +68,7 @@ class DailyLogRotateFileHandler extends Handler { try { currentFile.flush(); currentFile.close(); - } catch (IOException e) {} + } catch (IOException ignored) {} new File("logs/latest.log").renameTo(new File("logs/" + currentFileDate + ".log")); } @@ -76,11 +76,10 @@ class DailyLogRotateFileHandler extends Handler { try { File logDir = new File("logs"); logDir.mkdir(); - currentFile = new BufferedWriter(new FileWriter(new File("logs/latest.log"), true)); + currentFile = new BufferedWriter(new FileWriter("logs/latest.log", true)); } catch (SecurityException | IOException e) { reportError("Erreur lors de l'initialisation d'un fichier log", e, ErrorManager.OPEN_FAILURE); currentFile = null; - return; } } diff --git a/Core/pom.xml b/Core/pom.xml index 535bd52..b5bd1ec 100644 --- a/Core/pom.xml +++ b/Core/pom.xml @@ -36,24 +36,24 @@ net.kyori adventure-api - 4.8.1 + 4.11.0 net.kyori adventure-platform-bungeecord - 4.0.1-SNAPSHOT + 4.1.1 net.kyori adventure-text-serializer-plain - 4.8.1 + 4.11.0 io.github.classgraph classgraph - 4.8.108 + 4.8.147 diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java b/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java index d9e663d..749ee83 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/Chat.java @@ -36,7 +36,7 @@ import net.md_5.bungee.api.chat.BaseComponent; public abstract sealed class Chat extends ChatStatic implements HoverEventSource, ComponentLike { - protected ComponentBuilder builder; + protected final ComponentBuilder builder; protected boolean console = false; /* package */ Chat(ComponentBuilder b) { @@ -76,12 +76,11 @@ public abstract sealed class Chat extends ChatStatic implements HoverEventSource public Chat then(Component comp) { - if (comp instanceof TextComponent) { - TextComponent txtComp = (TextComponent) comp; - if (!txtComp.hasStyling() && (txtComp.content() == null || txtComp.content().isEmpty())) { + if (comp instanceof TextComponent txtComp) { + if (!txtComp.hasStyling() && (txtComp.content().isEmpty())) { // no need to add the provided component to the current component. // but eventual child component must be added - if (txtComp.children() != null && !txtComp.children().isEmpty()) { + if (!txtComp.children().isEmpty()) { for (Component child : txtComp.children()) then(child); } @@ -313,9 +312,8 @@ public abstract sealed class Chat extends ChatStatic implements HoverEventSource @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof Chat)) - return false; - return getAdv().equals(((Chat)obj).getAdv()); + return obj instanceof Chat c + && getAdv().equals(c.getAdv()); } @Override @@ -330,18 +328,7 @@ public abstract sealed class Chat extends ChatStatic implements HoverEventSource - - /* package */ static Object[] filterChatToBaseComponent(Object[] values) { - if (values == null) - return null; - for (int i = 0; i < values.length; i++) { - Object v = values[i]; - if (v instanceof Chat) - values[i] = ((Chat) v).get(); - } - return values; - } /* package */ static ComponentLike[] filterObjToComponentLike(Object[] values) { if (values == null) diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java index e19f4fb..841b0ca 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatColorUtil.java @@ -18,18 +18,16 @@ public class ChatColorUtil { public static final String ALL_COLORS = "0123456789AaBbCcDdEeFf"; - private static Pattern HEX_COLOR_PATTERN = Pattern.compile("§x(?>§[0-9a-f]){6}", Pattern.CASE_INSENSITIVE); - private static Pattern ESS_COLOR_PATTERN = Pattern.compile("§#[0-9a-f]{6}", Pattern.CASE_INSENSITIVE); + private static final Pattern HEX_COLOR_PATTERN = Pattern.compile("§x(?>§[\\da-f]){6}", Pattern.CASE_INSENSITIVE); + private static final Pattern ESS_COLOR_PATTERN = Pattern.compile("§#[\\da-f]{6}", Pattern.CASE_INSENSITIVE); /** * Return the legacy format needed to reproduce the format at the end of the provided legacy text. * Supports standard chat colors and formats, BungeeCord Chat rgb format and EssentialsX rgb format. * The RGB value from EssentialsX format is converted to BungeeCord Chat when included in the returned value. - * @param legacyText - * @return */ public static String getLastColors(String legacyText) { - String result = ""; + StringBuilder result = new StringBuilder(); int length = legacyText.length(); for (int index = length - 2; index >= 0; index--) { @@ -42,7 +40,7 @@ public class ChatColorUtil { && (legacyText.charAt(index - 11) == 'x' || legacyText.charAt(index - 11) == 'X') && HEX_COLOR_PATTERN.matcher(rgb = legacyText.substring(index - 12, index + 2)).matches()) { - result = rgb + result; + result.insert(0, rgb); break; } @@ -53,7 +51,7 @@ public class ChatColorUtil { rgb = "§x§" + rgb.charAt(2) + "§" + rgb.charAt(3) + "§" + rgb.charAt(4) + "§" + rgb.charAt(5) + "§" + rgb.charAt(6) + "§" + rgb.charAt(7); - result = rgb + result; + result.insert(0, rgb); break; } @@ -62,7 +60,7 @@ public class ChatColorUtil { ChatColor legacyColor = getChatColorByChar(colorChar); if (legacyColor != null) { - result = legacyColor.toString() + result; + result.insert(0, legacyColor); // Once we find a color or reset we can stop searching char col = legacyColor.toString().charAt(1); @@ -75,7 +73,7 @@ public class ChatColorUtil { } } - return result; + return result.toString(); } public static ChatColor getChatColorByChar(char code) { @@ -215,7 +213,7 @@ public class ChatColorUtil { private static String forceFormat(String legacyText, ChatColor format) { return format + legacyText .replace(format.toString(), "") // remove previous tag to make the result cleaner - .replaceAll("§([a-frA-FR0-9])", "§$1" + format); + .replaceAll("§([a-frA-FR\\d])", "§$1" + format); } @@ -270,7 +268,7 @@ public class ChatColorUtil { public static class ChatValueGradient { private record GradientValueColor(float value, TextColor color) { } // Java 16 - List colors = new ArrayList<>(); + final List colors = new ArrayList<>(); public synchronized ChatValueGradient add(float v, TextColor col) { colors.add(new GradientValueColor(v, col)); diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java index fe4eb67..9ba52f5 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatStatic.java @@ -114,16 +114,5 @@ public abstract class ChatStatic { } - - - -// public static void main(String[] args) { -// for (Chat c : new Chat[] { -// chat(), -// text("toto").color(NamedTextColor.GRAY), -// legacyText("tot§9o").color(NamedTextColor.GRAY), -// }) { -// System.out.println(GsonComponentSerializer.gson().serialize(c.getAdv())); -// } -// } + } diff --git a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java index da4b120..821fe7f 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/chat/ChatUtil.java @@ -1,9 +1,5 @@ package fr.pandacube.lib.core.chat; -import static fr.pandacube.lib.core.chat.ChatStatic.chat; -import static fr.pandacube.lib.core.chat.ChatStatic.legacyText; -import static fr.pandacube.lib.core.chat.ChatStatic.text; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -13,8 +9,6 @@ import java.util.TreeSet; import java.util.stream.Collectors; import com.google.common.collect.ImmutableMap; - -import fr.pandacube.lib.core.chat.Chat.FormatableChat; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; @@ -24,6 +18,12 @@ import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.format.TextDecoration.State; import net.md_5.bungee.api.ChatColor; +import fr.pandacube.lib.core.chat.Chat.FormatableChat; + +import static fr.pandacube.lib.core.chat.ChatStatic.chat; +import static fr.pandacube.lib.core.chat.ChatStatic.legacyText; +import static fr.pandacube.lib.core.chat.ChatStatic.text; + public class ChatUtil { public static final int DEFAULT_CHAR_SIZE = 6; @@ -328,12 +328,10 @@ public class ChatUtil { for (Component c : ((TranslatableComponent)component).args()) count += componentWidth(c, console, actuallyBold); } - - if (component.children() != null) { - for (Component c : component.children()) - count += componentWidth(c, console, actuallyBold); - } - + + for (Component c : component.children()) + count += componentWidth(c, console, actuallyBold); + return count; } @@ -345,7 +343,7 @@ public class ChatUtil { int count = 0; for (char c : str.toCharArray()) count += charW(c, console, bold); - return (count < 0) ? 0 : count; + return Math.max(count, 0); } public static int charW(char c, boolean console, boolean bold) { @@ -364,7 +362,7 @@ public class ChatUtil { public static List wrapInLimitedPixelsToChat(String legacyText, int pixelWidth) { return wrapInLimitedPixels(legacyText, pixelWidth).stream() - .map(t -> legacyText(t)) + .map(ChatStatic::legacyText) .collect(Collectors.toList()); } @@ -377,7 +375,7 @@ public class ChatUtil { int currentLineSize = 0; int index = 0; - String currentWord = ""; + StringBuilder currentWord = new StringBuilder(); int currentWordSize = 0; boolean bold = false; boolean firstCharCurrentWordBold = false; @@ -385,9 +383,9 @@ public class ChatUtil { do { char c = legacyText.charAt(index); if (c == ChatColor.COLOR_CHAR && index < legacyText.length() - 1) { - currentWord += c; + currentWord.append(c); c = legacyText.charAt(++index); - currentWord += c; + currentWord.append(c); if (c == 'l' || c == 'L') // bold bold = true; @@ -404,7 +402,7 @@ public class ChatUtil { lines.add(currentLine); String lastStyle = ChatColorUtil.getLastColors(currentLine); if (currentWord.charAt(0) == ' ') { - currentWord = currentWord.substring(1); + currentWord = new StringBuilder(currentWord.substring(1)); currentWordSize -= charW(' ', false, firstCharCurrentWordBold); } currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord; @@ -414,7 +412,7 @@ public class ChatUtil { currentLine += currentWord; currentLineSize += currentWordSize; } - currentWord = ""+c; + currentWord = new StringBuilder("" + c); currentWordSize = charW(c, false, bold); firstCharCurrentWordBold = bold; } @@ -423,15 +421,12 @@ public class ChatUtil { lines.add(currentLine); String lastStyle = ChatColorUtil.getLastColors(currentLine); if (currentWord.charAt(0) == ' ') { - currentWord = currentWord.substring(1); - currentWordSize -= charW(' ', false, firstCharCurrentWordBold); + currentWord = new StringBuilder(currentWord.substring(1)); } currentLine = (lastStyle.equals("§r") ? "" : lastStyle) + currentWord; - currentLineSize = currentWordSize; } else { currentLine += currentWord; - currentLineSize += currentWordSize; } // wrap after lines.add(currentLine); @@ -439,12 +434,12 @@ public class ChatUtil { currentLine = lastStyle.equals("§r") ? "" : lastStyle; currentLineSize = 0; - currentWord = ""; + currentWord = new StringBuilder(); currentWordSize = 0; firstCharCurrentWordBold = bold; } else { - currentWord += c; + currentWord.append(c); currentWordSize += charW(c, false, bold); } @@ -674,7 +669,6 @@ public class ChatUtil { * * Each element in the returned list represent 1 line of the tree view. * Thus, the caller may send each line separately or at once depending of the quantity of data. - * @param node * @return A array of component, each element being a single line. */ public static List treeView(DisplayTreeNode node, boolean console) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java b/Core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java index da80c7f..21ab506 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java +++ b/Core/src/main/java/fr/pandacube/lib/core/commands/AbstractCommand.java @@ -30,11 +30,8 @@ public class AbstractCommand extends ChatStatic { * Le premier élément est l'argument qui suit le nom de la commande. * Usuellement, ce paramètre correspond au paramètre * args de la méthode onCommand - * @param index - * @return */ public static String getLastParams(String[] args, int index) { - if (index < 0 || index >= args.length) return null; return String.join(" ", Arrays.copyOfRange(args, index, args.length)); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java b/Core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java index 888d290..5ac04e1 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java +++ b/Core/src/main/java/fr/pandacube/lib/core/commands/BadCommandUsage.java @@ -11,7 +11,6 @@ import java.util.logging.Logger; * */ public class BadCommandUsage extends RuntimeException { - private static final long serialVersionUID = 1L; public BadCommandUsage() { super(); diff --git a/Core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java b/Core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java index 83958f3..66033a6 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java +++ b/Core/src/main/java/fr/pandacube/lib/core/commands/SuggestionsSupplier.java @@ -19,17 +19,17 @@ public interface SuggestionsSupplier { /** * Number of suggestion visible at once without having to scroll */ - public static int VISIBLE_SUGGESTION_COUNT = 10; + int VISIBLE_SUGGESTION_COUNT = 10; - public abstract List getSuggestions(S sender, int tokenIndex, String token, String[] args); + List getSuggestions(S sender, int tokenIndex, String token, String[] args); - public static Predicate filter(String token) { + static Predicate filter(String token) { return suggestion -> suggestion != null && suggestion.toLowerCase().startsWith(token.toLowerCase()); } @@ -39,7 +39,7 @@ public interface SuggestionsSupplier { * * This methods consume the provided stream, so will not be usable anymore. */ - public static List collectFilteredStream(Stream stream, String token) { + static List collectFilteredStream(Stream stream, String token) { return stream.filter(filter(token)).sorted().collect(Collectors.toList()); } @@ -47,40 +47,40 @@ public interface SuggestionsSupplier { - public static SuggestionsSupplier empty() { return (s, ti, t, a) -> Collections.emptyList(); } + static SuggestionsSupplier empty() { return (s, ti, t, a) -> Collections.emptyList(); } - public static SuggestionsSupplier fromCollectionsSupplier(Supplier> streamSupplier) { + static SuggestionsSupplier fromCollectionsSupplier(Supplier> streamSupplier) { return (s, ti, token, a) -> collectFilteredStream(streamSupplier.get().stream(), token); } - public static SuggestionsSupplier fromStreamSupplier(Supplier> streamSupplier) { + static SuggestionsSupplier fromStreamSupplier(Supplier> streamSupplier) { return (s, ti, token, a) -> collectFilteredStream(streamSupplier.get(), token); } - public static SuggestionsSupplier fromCollection(Collection suggestions) { + static SuggestionsSupplier fromCollection(Collection suggestions) { return fromStreamSupplier(suggestions::stream); } - public static SuggestionsSupplier fromArray(String... suggestions) { + static SuggestionsSupplier fromArray(String... suggestions) { return fromStreamSupplier(() -> Arrays.stream(suggestions)); } - public static , S> SuggestionsSupplier fromEnum(Class enumClass) { + static , S> SuggestionsSupplier fromEnum(Class enumClass) { return fromEnumValues(enumClass.getEnumConstants()); } - public static , S> SuggestionsSupplier fromEnum(Class enumClass, boolean lowerCase) { + static , S> SuggestionsSupplier fromEnum(Class enumClass, boolean lowerCase) { return fromEnumValues(lowerCase, enumClass.getEnumConstants()); } @SafeVarargs - public static , S> SuggestionsSupplier fromEnumValues(E... enumValues) { + static , S> SuggestionsSupplier fromEnumValues(E... enumValues) { return fromEnumValues(false, enumValues); } @SafeVarargs - public static , S> SuggestionsSupplier fromEnumValues(boolean lowerCase, E... enumValues) { + static , S> SuggestionsSupplier fromEnumValues(boolean lowerCase, E... enumValues) { return (s, ti, token, a) -> { Stream st = Arrays.stream(enumValues).map(Enum::name); if (lowerCase) @@ -91,7 +91,7 @@ public interface SuggestionsSupplier { - public static SuggestionsSupplier booleanValues() { + static SuggestionsSupplier booleanValues() { return fromCollection(Arrays.asList("true", "false")); } @@ -103,11 +103,8 @@ public interface SuggestionsSupplier { * Create a {@link SuggestionsSupplier} that suggest numbers according to the provided range. * * The current implementation only support range that include either -1 or 1. - * @param min - * @param max - * @return */ - public static SuggestionsSupplier fromIntRange(int min, int max, boolean compact) { + static SuggestionsSupplier fromIntRange(int min, int max, boolean compact) { return fromLongRange(min, max, compact); } @@ -118,11 +115,8 @@ public interface SuggestionsSupplier { * Create a {@link SuggestionsSupplier} that suggest numbers according to the provided range. * * The current implementation only support range that include either -1 or 1. - * @param min - * @param max - * @return */ - public static SuggestionsSupplier fromLongRange(long min, long max, boolean compact) { + static SuggestionsSupplier fromLongRange(long min, long max, boolean compact) { if (max < min) { throw new IllegalArgumentException("min should be less or equals than max"); } @@ -179,7 +173,7 @@ public interface SuggestionsSupplier { } } - return collectFilteredStream(proposedValues.stream().map(i -> i.toString()), token); + return collectFilteredStream(proposedValues.stream().map(Object::toString), token); } catch (NumberFormatException e) { return Collections.emptyList(); } @@ -196,9 +190,8 @@ public interface SuggestionsSupplier { /** * Create a {@link SuggestionsSupplier} that support greedy strings argument using the suggestion from this {@link SuggestionsSupplier}. * @param index the index of the first argument of the greedy string argument - * @return */ - public default SuggestionsSupplier greedyString(int index) { + default SuggestionsSupplier greedyString(int index) { return (s, ti, token, args) -> { @@ -233,7 +226,7 @@ public interface SuggestionsSupplier { - public default SuggestionsSupplier quotableString() { + default SuggestionsSupplier quotableString() { return (s, ti, token, a) -> { boolean startWithQuote = token.length() > 0 && (token.charAt(0) == '"' || token.charAt(0) == '\''); String realToken = startWithQuote ? unescapeBrigadierQuotable(token.substring(1), token.charAt(0)) : token; @@ -309,10 +302,8 @@ public interface SuggestionsSupplier { - public default SuggestionsSupplier requires(Predicate check) { - return (s, ti, to, a) -> { - return check.test(s) ? getSuggestions(s, ti, to, a) : Collections.emptyList(); - }; + default SuggestionsSupplier requires(Predicate check) { + return (s, ti, to, a) -> check.test(s) ? getSuggestions(s, ti, to, a) : Collections.emptyList(); } @@ -320,10 +311,8 @@ public interface SuggestionsSupplier { /** * Returns a new {@link SuggestionsSupplier} containing all the element of this instance then the element of the provided one, * with all duplicated values removed using {@link Stream#distinct()}. - * @param other - * @return */ - public default SuggestionsSupplier merge(SuggestionsSupplier other) { + default SuggestionsSupplier merge(SuggestionsSupplier other) { return (s, ti, to, a) -> { List l1 = getSuggestions(s, ti, to, a); List l2 = other.getSuggestions(s, ti, to, a); @@ -337,10 +326,8 @@ public interface SuggestionsSupplier { /** * Returns a new {@link SuggestionsSupplier} containing all the suggestions of this instance, * but if this list is still empty, returns the suggestions from the provided one. - * @param other - * @return */ - public default SuggestionsSupplier orIfEmpty(SuggestionsSupplier other) { + default SuggestionsSupplier orIfEmpty(SuggestionsSupplier other) { return (s, ti, to, a) -> { List l1 = getSuggestions(s, ti, to, a); return !l1.isEmpty() ? l1 : other.getSuggestions(s, ti, to, a); diff --git a/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java b/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java index 0bcbacd..7078441 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java +++ b/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfig.java @@ -6,14 +6,12 @@ import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import fr.pandacube.lib.core.chat.ChatColorUtil; import fr.pandacube.lib.core.util.Log; /** - * Classe chargeant en mémoire un fichier de configuration ou un dossier donné - * @author Marc Baloup + * Class tht loads a specific config file or directory * */ public abstract class AbstractConfig { @@ -22,12 +20,13 @@ public abstract class AbstractConfig { * Correspond au dossier ou au fichier de configuration traité par la sous-classe * courante de {@link AbstractConfig} */ - protected File configFile; + protected final File configFile; /** - * @param fileOrDirName le nom du fichier ou du dossier correspondant à la sous-classe de {@link AbstractConfig} - * @param isDir true si il s'agit d'un dossier, false sinon - * @throws IOException si le fichier est impossible à créer + * @param configDir the parent directory + * @param fileOrDirName The name of the config file or folder + * @param type if the provided name is a file or a directory + * @throws IOException if we cannot create the file */ public AbstractConfig(File configDir, String fileOrDirName, FileType type) throws IOException { configFile = new File(configDir, fileOrDirName); @@ -38,13 +37,12 @@ public abstract class AbstractConfig { } /** - * Retourne toutes les lignes d'un fichier donné - * @param ignoreEmpty true si on doit ignorer les lignes vides - * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #) - * @param trimOutput true si on doit appeller la méthode String.trim() sur chaque ligne retournée - * @param f le fichier à lire - * @return la liste des lignes utiles - * @throws IOException + * Gets the lines from the config file + * @param ignoreEmpty true if we ignore the empty lines + * @param ignoreHashtagComment true if we ignore the comment lines (starting with {@code #}) + * @param trimOutput true if we want to trim all lines using {@link String#trim()} + * @param f the file to read + * @return the list of lines, filtered according to the parameters */ protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput, File f) throws IOException { if (!f.isFile()) @@ -83,7 +81,6 @@ public abstract class AbstractConfig { * @param ignoreHashtagComment true si on doit ignorer les lignes commentés (commençant par un #) * @param trimOutput true si on doit appeller la méthode String.trim() sur chaque ligne retournée * @return la liste des lignes utiles - * @throws IOException */ protected List getFileLines(boolean ignoreEmpty, boolean ignoreHashtagComment, boolean trimOutput) throws IOException { return getFileLines(ignoreEmpty, ignoreHashtagComment, trimOutput, configFile); @@ -119,7 +116,7 @@ public abstract class AbstractConfig { public static List getSplittedString(String value, String split) { - return Collections.unmodifiableList(Arrays.asList(value.split(split))); + return List.of(value.split(split)); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java b/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java index bdf138d..b379b66 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java +++ b/Core/src/main/java/fr/pandacube/lib/core/config/AbstractConfigManager.java @@ -17,13 +17,11 @@ public abstract class AbstractConfigManager { /** * Implementation must close all closeable configuration (saving for example) - * @throws IOException */ public abstract void close() throws IOException; /** * Implementation must init all config data - * @throws IOException */ public abstract void init() throws IOException; diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/DB.java b/Core/src/main/java/fr/pandacube/lib/core/db/DB.java index 694cdc1..3d40fba 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/DB.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/DB.java @@ -18,15 +18,15 @@ import fr.pandacube.lib.core.util.Log; /** * Static class to handle most of the database operations. * - * To use this database library, first call {@link #init(DBConnection)} with an appropriate {@link DBConnection}, + * To use this database library, first call {@link #init(DBConnection, String)} with an appropriate {@link DBConnection}, * they you can initialize every table you need for your application, using {@link #initTable(Class)}. * * @author Marc Baloup */ public final class DB { - private static List>> tables = new ArrayList<>(); - private static Map>, String> tableNames = new HashMap<>(); + private static final List>> tables = new ArrayList<>(); + private static final Map>, String> tableNames = new HashMap<>(); private static DBConnection connection; /* package */ static String tablePrefix = ""; @@ -35,7 +35,7 @@ public final class DB { return connection; } - public synchronized static > void init(DBConnection conn, String tablePrefix) { + public synchronized static void init(DBConnection conn, String tablePrefix) { connection = conn; DB.tablePrefix = Objects.requireNonNull(tablePrefix); } @@ -62,7 +62,7 @@ public final class DB { String tableName = tablePrefix + elem.tableName(); - String sql = "CREATE TABLE IF NOT EXISTS " + tableName + " ("; + StringBuilder sql = new StringBuilder("CREATE TABLE IF NOT EXISTS " + tableName + " ("); List params = new ArrayList<>(); Collection> tableFields = elem.getFields().values(); @@ -71,14 +71,15 @@ public final class DB { ParameterizedSQLString statementPart = f.forSQLPreparedStatement(); params.addAll(statementPart.parameters()); - if (!first) sql += ", "; + if (!first) + sql.append(", "); first = false; - sql += statementPart.sqlString(); + sql.append(statementPart.sqlString()); } - sql += ", PRIMARY KEY id(id))"; + sql.append(", PRIMARY KEY id(id))"); - try (PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql)) { + try (PreparedStatement ps = connection.getNativeConnection().prepareStatement(sql.toString())) { int i = 1; for (Object val : params) ps.setObject(i++, val); @@ -93,11 +94,9 @@ public final class DB { } private static boolean tableExistInDB(String tableName) throws SQLException { - boolean exist = false; try (ResultSet set = connection.getNativeConnection().getMetaData().getTables(null, null, tableName, null)) { - exist = set.next(); + return set.next(); } - return exist; } @SuppressWarnings("unchecked") @@ -277,7 +276,7 @@ public final class DB { - public static > SQLUpdate update(Class elemClass, SQLWhere where) throws DBException { + public static > SQLUpdate update(Class elemClass, SQLWhere where) { return new SQLUpdate<>(elemClass, where); } @@ -291,7 +290,6 @@ public final class DB { * @param elemClass the SQLElement representing the table. * @param where the condition to meet for an element to be deleted from the table. If null, the table is truncated using {@link #truncateTable(Class)}. * @return The return value of {@link PreparedStatement#executeUpdate()}, for an SQL query {@code DELETE}. - * @throws DBException */ public static > int delete(Class elemClass, SQLWhere where) throws DBException { initTable(elemClass); @@ -369,7 +367,7 @@ public final class DB { val = ((SQLCustomType)sqlField.type).dbToJavaConv.apply(val); } catch (Exception e) { throw new DBException("Error while converting value of field '"+sqlField.getName()+"' with SQLCustomType from "+((SQLCustomType)sqlField.type).intermediateJavaType - +"(jdbc source) to "+sqlField.type.getJavaType()+"(java destination). The original value is '"+val.toString()+"'", e); + +"(jdbc source) to "+sqlField.type.getJavaType()+"(java destination). The original value is '"+ val +"'", e); } } @@ -383,7 +381,7 @@ public final class DB { } if (!instance.isValidForSave()) throw new DBException( - "This SQLElement representing a database entry is not valid for save : " + instance.toString()); + "This SQLElement representing a database entry is not valid for save : " + instance); return instance; } catch (ReflectiveOperationException | IllegalArgumentException | SecurityException | SQLException e) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/DBConnection.java b/Core/src/main/java/fr/pandacube/lib/core/db/DBConnection.java index 437419e..493a519 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/DBConnection.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/DBConnection.java @@ -11,15 +11,14 @@ public class DBConnection { private static final long CONNECTION_CHECK_TIMEOUT = 30000; // in ms private Connection conn; - private String url; - private String login; - private String pass; + private final String url; + private final String login; + private final String pass; private long timeOfLastCheck = 0; public DBConnection(String host, int port, String dbname, String l, String p) - throws ClassNotFoundException, SQLException { - //Class.forName("com.mysql.jdbc.Driver"); // apparently this is deprecated now + throws SQLException { url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?autoReconnect=true" + "&useUnicode=true" @@ -58,7 +57,7 @@ public class DBConnection { return true; try (ResultSet rs = conn.createStatement().executeQuery("SELECT 1;")) { - return rs == null ? false : rs.next(); + return rs != null && rs.next(); } } catch (Exception e) { return false; @@ -78,7 +77,7 @@ public class DBConnection { public void close() { try { conn.close(); - } catch (Exception e) {} + } catch (Exception ignored) {} } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/DBException.java b/Core/src/main/java/fr/pandacube/lib/core/db/DBException.java index b536da1..dff4503 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/DBException.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/DBException.java @@ -1,7 +1,6 @@ package fr.pandacube.lib.core.db; public class DBException extends Exception { - private static final long serialVersionUID = 1L; public DBException(Throwable initCause) { super(initCause); diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/DBInitTableException.java b/Core/src/main/java/fr/pandacube/lib/core/db/DBInitTableException.java index 18fcb61..d9f426f 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/DBInitTableException.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/DBInitTableException.java @@ -1,11 +1,6 @@ package fr.pandacube.lib.core.db; public class DBInitTableException extends DBException { - private static final long serialVersionUID = 1L; - - /* package */ > DBInitTableException(Class tableElem) { - super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null")); - } /* package */ > DBInitTableException(Class tableElem, Throwable t) { super("Error while initializing table " + ((tableElem != null) ? tableElem.getName() : "null"), t); diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java b/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java index 81b8a64..eb8f4a6 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/ParameterizedSQLString.java @@ -2,13 +2,7 @@ package fr.pandacube.lib.core.db; import java.util.List; -//public record ParameterizedSQLString(String sqlString, List parameters) { } // Java 16 -public class ParameterizedSQLString { - private final String sqlString; - private final List parameters; - public ParameterizedSQLString(String sqlString, List parameters) { - this.sqlString = sqlString; this.parameters = parameters; - } - public String sqlString() { return sqlString; } - public List parameters() { return parameters; } +public record ParameterizedSQLString(String sqlString, List parameters) { + + } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLElement.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLElement.java index 0c7f7eb..393ca1c 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLElement.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLElement.java @@ -1,5 +1,12 @@ package fr.pandacube.lib.core.db; +import com.google.common.base.MoreObjects; +import com.google.common.base.MoreObjects.ToStringHelper; +import com.google.gson.JsonObject; +import fr.pandacube.lib.core.util.EnumUtil; +import fr.pandacube.lib.core.util.Json; +import fr.pandacube.lib.core.util.Log; + import java.lang.reflect.Modifier; import java.sql.Date; import java.sql.PreparedStatement; @@ -17,19 +24,11 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; -import com.google.common.base.MoreObjects; -import com.google.common.base.MoreObjects.ToStringHelper; -import com.google.gson.JsonObject; - -import fr.pandacube.lib.core.util.EnumUtil; -import fr.pandacube.lib.core.util.Json; -import fr.pandacube.lib.core.util.Log; - public abstract class SQLElement> { /** cache for fields for each subclass of SQLElement */ /* package */ static final Map>, SQLFieldMap>> fieldsCache = new HashMap<>(); - DBConnection db = DB.getConnection(); + private final DBConnection db = DB.getConnection(); private boolean stored = false; private int id; @@ -109,7 +108,7 @@ public abstract class SQLElement> { field.setAccessible(true); try { Object val = field.get(null); - if (val == null || !(val instanceof SQLField)) { + if (!(val instanceof SQLField)) { Log.severe("[ORM] The field " + field.getDeclaringClass().getName() + "." + field.getName() + " can't be initialized because its value is null."); continue; } @@ -122,7 +121,7 @@ public abstract class SQLElement> { checkedF.setSQLElementType((Class) getClass()); listToFill.addField((SQLField) val); } catch (IllegalArgumentException | IllegalAccessException e) { - Log.severe("Can't get value of static field " + field.toString(), e); + Log.severe("Can't get value of static field " + field, e); } } @@ -147,20 +146,18 @@ public abstract class SQLElement> { if (!fields.containsValue(sqlField)) // should not append at runtime because of generic type check at compilation throw new IllegalStateException("In the table "+getClass().getName()+ ": the field asked for modification is not initialized properly."); - boolean modify = false; if (value == null) { - if (sqlField.canBeNull || (sqlField.autoIncrement && !stored)) modify = true; - else + if (!sqlField.canBeNull && (!sqlField.autoIncrement || stored)) throw new IllegalArgumentException( "SQLField '" + sqlField.getName() + "' of " + getClass().getName() + " is a NOT NULL field"); } - else if (sqlField.type.isAssignableFrom(value)) modify = true; - else + else if (!sqlField.type.isInstance(value)) { throw new IllegalArgumentException("SQLField '" + sqlField.getName() + "' of " + getClass().getName() - + " type is '" + sqlField.type.toString() + "' and can't accept values of type " + + " type is '" + sqlField.type + "' and can't accept values of type " + value.getClass().getName()); + } - if (modify) if (!values.containsKey(sqlField)) { + if (!values.containsKey(sqlField)) { values.put(sqlField, value); if (setModified) modifiedSinceLastSave.add(sqlField.getName()); } @@ -227,7 +224,7 @@ public abstract class SQLElement> { @SuppressWarnings("unchecked") public E save() throws DBException { if (!isValidForSave()) - throw new IllegalStateException(toString() + " has at least one undefined value and can't be saved."); + throw new IllegalStateException(this + " has at least one undefined value and can't be saved."); DB.initTable((Class)getClass()); try { @@ -251,24 +248,24 @@ public abstract class SQLElement> { // values values.put(fields.get("id"), null); - String concat_vals = ""; - String concat_fields = ""; + StringBuilder concatValues = new StringBuilder(); + StringBuilder concatFields = new StringBuilder(); List psValues = new ArrayList<>(); boolean first = true; for (Map.Entry, Object> entry : values.entrySet()) { if (!first) { - concat_vals += ","; - concat_fields += ","; + concatValues.append(","); + concatFields.append(","); } first = false; - concat_vals += " ? "; - concat_fields += "`" + entry.getKey().getName() + "`"; + concatValues.append(" ? "); + concatFields.append("`").append(entry.getKey().getName()).append("`"); addValueToSQLObjectList(psValues, entry.getKey(), entry.getValue()); } try (PreparedStatement ps = db.getNativeConnection().prepareStatement( - "INSERT INTO " + DB.tablePrefix + tableName() + " (" + concat_fields + ") VALUES (" + concat_vals + ")", + "INSERT INTO " + DB.tablePrefix + tableName() + " (" + concatFields + ") VALUES (" + concatValues + ")", Statement.RETURN_GENERATED_KEYS)) { int i = 1; @@ -300,7 +297,7 @@ public abstract class SQLElement> { jValue = ((SQLCustomType)field.type).javaToDbConv.apply(jValue); } catch (Exception e) { throw new DBException("Error while converting value of field '"+field.getName()+"' with SQLCustomType from "+field.type.getJavaType() - +"(java source) to "+((SQLCustomType)field.type).intermediateJavaType+"(jdbc destination). The original value is '"+jValue.toString()+"'", e); + +"(java source) to "+((SQLCustomType)field.type).intermediateJavaType+"(jdbc destination). The original value is '"+jValue+"'", e); } } list.add(jValue); @@ -346,8 +343,6 @@ public abstract class SQLElement> { } protected static class SQLFieldMap> extends LinkedHashMap> { - private static final long serialVersionUID = 1L; - private final Class sqlElemClass; private SQLFieldMap(Class elemClass) { @@ -382,7 +377,7 @@ public abstract class SQLElement> { @Override public boolean equals(Object o) { - if (o == null || !(getClass().isInstance(o))) return false; + if (!(getClass().isInstance(o))) return false; SQLElement oEl = (SQLElement) o; if (oEl.getId() == null) return false; return oEl.getId().equals(getId()); @@ -390,7 +385,7 @@ public abstract class SQLElement> { @Override public int hashCode() { - return super.hashCode(); + return getClass().hashCode() ^ Objects.hashCode(getId()); } @@ -466,12 +461,12 @@ public abstract class SQLElement> { public static final SQLType DOUBLE = new SQLType<>("DOUBLE", Double.class); - public static final SQLType CHAR(int charCount) { + public static SQLType CHAR(int charCount) { if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive."); return new SQLType<>("CHAR(" + charCount + ")", String.class); } - public static final SQLType VARCHAR(int charCount) { + public static SQLType VARCHAR(int charCount) { if (charCount <= 0) throw new IllegalArgumentException("charCount must be positive."); return new SQLType<>("VARCHAR(" + charCount + ")", String.class); } @@ -479,29 +474,29 @@ public abstract class SQLElement> { public static final SQLType TEXT = new SQLType<>("TEXT", String.class); public static final SQLType STRING = TEXT; - public static final SQLType BINARY(int byteCount) { + public static SQLType BINARY(int byteCount) { if (byteCount <= 0) throw new IllegalArgumentException("byteCount must be positive."); return new SQLType<>("BINARY(" + byteCount + ")", byte[].class); } - public static final SQLType VARBINARY(int byteCount) { + public static SQLType VARBINARY(int byteCount) { if (byteCount <= 0) throw new IllegalArgumentException("byteCount must be positive."); return new SQLType<>("VARBINARY(" + byteCount + ")", byte[].class); } public static final SQLType BLOB = new SQLType<>("BLOB", byte[].class); - public static final > SQLType ENUM(Class enumType) { + public static > SQLType ENUM(Class enumType) { if (enumType == null) throw new IllegalArgumentException("enumType can't be null."); - String enumStr = "'"; + StringBuilder enumStr = new StringBuilder("'"); boolean first = true; for (T el : enumType.getEnumConstants()) { - if (!first) enumStr += "', '"; + if (!first) enumStr.append("', '"); first = false; - enumStr += el.name(); + enumStr.append(el.name()); } - enumStr += "'"; + enumStr.append("'"); return new SQLCustomType<>("VARCHAR(" + enumStr + ")", String.class, enumType, s -> EnumUtil.searchEnum(enumType, s), Enum::name); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLElementList.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLElementList.java index 0d32a9c..d6b4ff6 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLElementList.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLElementList.java @@ -1,25 +1,23 @@ package fr.pandacube.lib.core.db; -import java.sql.SQLException; +import com.google.gson.JsonArray; +import fr.pandacube.lib.core.util.Log; + import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.stream.Collectors; -import com.google.gson.JsonArray; - -import fr.pandacube.lib.core.util.Log; - /** * * @param */ public class SQLElementList> extends ArrayList { - private static final long serialVersionUID = 1L; private final Map, Object> modifiedValues = new LinkedHashMap<>(); @@ -38,14 +36,13 @@ public class SQLElementList> extends ArrayList { * que lors de * l'appel à {@link #saveCommon()} * - * @param * @param field le champs à modifier * @param value la valeur à lui appliquer */ public synchronized void setCommon(SQLField field, T value) { if (field == null) throw new IllegalArgumentException("field can't be null"); - if (field.getName() == "id") + if (Objects.equals(field.getName(), "id")) throw new IllegalArgumentException("Can't modify id field in a SQLElementList"); Class elemClass = field.getSQLElementType(); @@ -72,8 +69,6 @@ public class SQLElementList> extends ArrayList { * {@link SQLElement#set(SQLField, Object)}.
* Les objets de cette liste qui n'ont pas leur données en base de données * sont ignorées. - * - * @throws SQLException */ public synchronized int saveCommon() throws DBException { List storedEl = getStoredEl(); @@ -102,7 +97,7 @@ public class SQLElementList> extends ArrayList { } private List getStoredEl() { - return stream().filter(SQLElement::isStored).collect(Collectors.toCollection(() -> new ArrayList<>())); + return stream().filter(SQLElement::isStored).collect(Collectors.toCollection(ArrayList::new)); } /** diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java index 3102689..e3f9033 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLField.java @@ -63,7 +63,7 @@ public class SQLField, T> { } /** - * Don't use this {@link #toString()} method in a SQL query, because + * Don't use this {@code toString()} method in a SQL query, because * the default value is not escaped correctly * * @see java.lang.Object#toString() @@ -76,12 +76,9 @@ public class SQLField, T> { @Override public boolean equals(Object obj) { - if (obj == null) return false; - if (!(obj instanceof SQLField)) return false; - SQLField f = (SQLField) obj; - if (!f.getName().equals(getName())) return false; - if (!f.sqlElemClass.equals(sqlElemClass)) return false; - return true; + return obj instanceof SQLField f + && f.getName().equals(getName()) + && f.sqlElemClass.equals(sqlElemClass); } @Override diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLOrderBy.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLOrderBy.java index 3f1919d..84fa916 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLOrderBy.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLOrderBy.java @@ -2,10 +2,11 @@ package fr.pandacube.lib.core.db; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; public class SQLOrderBy> { - private List orderByFields = new ArrayList<>(); + private final List orderByFields = new ArrayList<>(); /** * Construit une nouvelle clause ORDER BY @@ -45,14 +46,9 @@ public class SQLOrderBy> { } /* package */ String toSQL() { - String ret = ""; - boolean first = true; - for (OBField f : orderByFields) { - if (!first) ret += ", "; - first = false; - ret += "`" + f.field.getName() + "` " + f.direction.name(); - } - return ret; + return orderByFields.stream() + .map(f -> "`" + f.field.getName() + "` " + f.direction.name()) + .collect(Collectors.joining(", ")); } @Override @@ -72,7 +68,7 @@ public class SQLOrderBy> { } private enum Direction { - ASC, DESC; + ASC, DESC } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLType.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLType.java index 8860a3a..4b58ad7 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLType.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLType.java @@ -15,9 +15,8 @@ public class SQLType { return sqlDeclaration; } - public boolean isAssignableFrom(Object val) { - if (javaTypes.isInstance(val)) return true; - return false; + public boolean isInstance(Object val) { + return javaTypes.isInstance(val); } @Override @@ -27,8 +26,8 @@ public class SQLType { @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof SQLType)) return false; - return toString().equals(((SQLType) obj).toString()); + return obj instanceof SQLType o + && toString().equals(o.toString()); } public Class getJavaType() { diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java index ee57494..6c51546 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLUpdate.java @@ -42,27 +42,27 @@ public class SQLUpdate> { return 0; } - String sql = "UPDATE " + DB.getTableName(elemClass) + " SET "; + StringBuilder sql = new StringBuilder("UPDATE " + DB.getTableName(elemClass) + " SET "); List params = new ArrayList<>(); boolean first = true; for (Map.Entry, Object> entry : values.entrySet()) { if (!first) - sql += ", "; - sql += "`" + entry.getKey().getName() + "` = ? "; + sql.append(", "); + sql.append("`").append(entry.getKey().getName()).append("` = ? "); SQLElement.addValueToSQLObjectList(params, entry.getKey(), entry.getValue()); first = false; } if (where != null) { ParameterizedSQLString ret = where.toSQL(); - sql += " WHERE " + ret.sqlString(); + sql.append(" WHERE ").append(ret.sqlString()); params.addAll(ret.parameters()); } - sql += ";"; + sql.append(";"); - return DB.customUpdateStatement(sql, params); + return DB.customUpdateStatement(sql.toString(), params); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java b/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java index c428999..2f8ec1a 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java +++ b/Core/src/main/java/fr/pandacube/lib/core/db/SQLWhere.java @@ -50,8 +50,8 @@ public abstract class SQLWhere> { public static abstract class SQLWhereChain> extends SQLWhere { - private SQLBoolOp operator; - protected List> conditions = new ArrayList<>(); + private final SQLBoolOp operator; + private final List> conditions = new ArrayList<>(); private SQLWhereChain(SQLBoolOp op) { if (op == null) throw new IllegalArgumentException("op can't be null"); @@ -73,20 +73,21 @@ public abstract class SQLWhere> { throw new DBException("SQLWhereChain needs at least one element inside !"); } - String sql = ""; + StringBuilder sql = new StringBuilder(); List params = new ArrayList<>(); boolean first = true; for (SQLWhere w : conditions) { - if (!first) sql += " " + operator.sql + " "; + if (!first) + sql.append(" ").append(operator.sql).append(" "); first = false; ParameterizedSQLString ret = w.toSQL(); - sql += "(" + ret.sqlString() + ")"; + sql.append("(").append(ret.sqlString()).append(")"); params.addAll(ret.parameters()); } - return new ParameterizedSQLString(sql, params); + return new ParameterizedSQLString(sql.toString(), params); } protected enum SQLBoolOp { @@ -96,7 +97,7 @@ public abstract class SQLWhere> { OR("OR"); /* package */ final String sql; - private SQLBoolOp(String s) { + SQLBoolOp(String s) { sql = s; } @@ -151,9 +152,9 @@ public abstract class SQLWhere> { /* package */ static class SQLWhereComp> extends SQLWhere { - private SQLField left; - private SQLComparator comp; - private Object right; + private final SQLField left; + private final SQLComparator comp; + private final Object right; /** * Compare a field with a value @@ -193,7 +194,7 @@ public abstract class SQLWhere> { /* package */ final String sql; - private SQLComparator(String s) { + SQLComparator(String s) { sql = s; } @@ -208,8 +209,8 @@ public abstract class SQLWhere> { /* package */ static class SQLWhereIn> extends SQLWhere { - private SQLField field; - private Collection values; + private final SQLField field; + private final Collection values; /* package */ SQLWhereIn(SQLField f, Collection v) { if (f == null || v == null) @@ -246,8 +247,8 @@ public abstract class SQLWhere> { /* package */ static class SQLWhereLike> extends SQLWhere { - private SQLField field; - private String likeExpr; + private final SQLField field; + private final String likeExpr; /** * Compare a field with a value @@ -279,8 +280,8 @@ public abstract class SQLWhere> { /* package */ static class SQLWhereNull> extends SQLWhere { - private SQLField fild; - private boolean nulll; + private final SQLField field; + private final boolean isNull; /** * Init a IS NULL / IS NOT NULL expression for a SQL WHERE condition. @@ -294,13 +295,13 @@ public abstract class SQLWhere> { if (!field.canBeNull) Log.getLogger().log(Level.WARNING, "Useless : Trying to check IS [NOT] NULL on the field " + field.getSQLElementType().getName() + "#" + field.getName() + " which is declared in the ORM as 'can't be null'"); - fild = field; - nulll = isNull; + this.field = field; + this.isNull = isNull; } @Override public ParameterizedSQLString toSQL() { - return new ParameterizedSQLString("`" + fild.getName() + "` IS " + ((nulll) ? "NULL" : "NOT NULL"), new ArrayList<>()); + return new ParameterizedSQLString("`" + field.getName() + "` IS " + ((isNull) ? "NULL" : "NOT NULL"), new ArrayList<>()); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/Array8Bit.java b/Core/src/main/java/fr/pandacube/lib/core/net/Array8Bit.java index 10f45a9..798ff21 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/Array8Bit.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/Array8Bit.java @@ -23,8 +23,6 @@ public class Array8Bit { /** * i = 0 is the lowest significant bit - * @param i - * @return */ public boolean getBit(int i) { return values[i]; @@ -32,8 +30,6 @@ public class Array8Bit { /** * i = 0 is the lowest significant bit - * @param i - * @param b */ public void setBit(int i, boolean b) { values[i] = b; diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/ByteBuffer.java b/Core/src/main/java/fr/pandacube/lib/core/net/ByteBuffer.java index 0af8b1b..3638a35 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/ByteBuffer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/ByteBuffer.java @@ -1,13 +1,14 @@ package fr.pandacube.lib.core.net; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public final class ByteBuffer implements Cloneable { - public static final Charset NETWORK_CHARSET = Charset.forName("UTF-8"); + public static final Charset NETWORK_CHARSET = StandardCharsets.UTF_8; private java.nio.ByteBuffer buff; @@ -64,7 +65,6 @@ public final class ByteBuffer implements Cloneable { /** * Return the next byte array wich is preceded with his size as integer, * or null if the founded size is negative. - * @return */ public byte[] getSizedByteArray() { int size = getInt(); @@ -218,7 +218,6 @@ public final class ByteBuffer implements Cloneable { /** * * @param s null String are supported - * @return */ public ByteBuffer putString(String s) { if (s == null) { @@ -229,7 +228,6 @@ public final class ByteBuffer implements Cloneable { /** * returned string can be null - * @return */ public String getString() { byte[] binaryString = getSizedByteArray(); @@ -239,7 +237,6 @@ public final class ByteBuffer implements Cloneable { /** * * @param list The list can be null, and any String can be null too. - * @return */ public ByteBuffer putListOfString(List list) { if (list == null) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PPacket.java b/Core/src/main/java/fr/pandacube/lib/core/net/PPacket.java index fcaa093..382fa59 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PPacket.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PPacket.java @@ -3,9 +3,9 @@ package fr.pandacube.lib.core.net; import java.util.Arrays; public class PPacket { - public String name; + public final String name; /* package */ int id; - public byte[] content; + public final byte[] content; /** * Construct a new PPacket based on the content of the provided buffer before his position. diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PPacketAnswer.java b/Core/src/main/java/fr/pandacube/lib/core/net/PPacketAnswer.java index a2cf36e..803a8f3 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PPacketAnswer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PPacketAnswer.java @@ -3,7 +3,7 @@ package fr.pandacube.lib.core.net; import java.util.Arrays; public class PPacketAnswer extends PPacket { - /* package */ int answer; + /* package */ final int answer; /** * Construct a new PPacketAnswer based on the content of the provided buffer before his position. diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PPacketListener.java b/Core/src/main/java/fr/pandacube/lib/core/net/PPacketListener.java index 385845c..f265e6a 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PPacketListener.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PPacketListener.java @@ -8,6 +8,6 @@ public interface PPacketListener

{ * @param connection the connection from where the packet comes * @param packet the received packet */ - public void onPacketReceive(PSocket connection, P packet); + void onPacketReceive(PSocket connection, P packet); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PServer.java b/Core/src/main/java/fr/pandacube/lib/core/net/PServer.java index 33ac3a0..e77fd48 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PServer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PServer.java @@ -17,25 +17,25 @@ import com.google.common.base.MoreObjects; import fr.pandacube.lib.core.util.Log; public class PServer extends Thread implements Closeable { - private static AtomicInteger connectionCounterId = new AtomicInteger(0); + private static final AtomicInteger connectionCounterId = new AtomicInteger(0); - private int port; + private final int port; private ServerSocket socket; - private String socketName; + private final String socketName; - private List clients = Collections.synchronizedList(new ArrayList<>()); + private final List clients = Collections.synchronizedList(new ArrayList<>()); - private AtomicBoolean isClosed = new AtomicBoolean(false); + private final AtomicBoolean isClosed = new AtomicBoolean(false); - private List> globalPacketListeners = Collections.synchronizedList(new ArrayList<>()); - private List clientConnectionListeners = Collections.synchronizedList(new ArrayList<>()); + private final List> globalPacketListeners = Collections.synchronizedList(new ArrayList<>()); + private final List clientConnectionListeners = Collections.synchronizedList(new ArrayList<>()); - private String password; + private final String password; - public PServer(int port, String sckName, String password) throws IOException { + public PServer(int port, String sckName, String password) { super("PServer " + sckName); setDaemon(true); if (port <= 0 || port > 65535) throw new IllegalArgumentException("le numéro de port est invalide"); @@ -59,17 +59,11 @@ public class PServer extends Thread implements Closeable { socketClient.setSendBufferSize(PSocket.NETWORK_TCP_BUFFER_SIZE); socketClient.setSoTimeout(PSocket.NETWORK_TIMEOUT); - try { - @SuppressWarnings("resource") - TCPServerClientConnection co = new TCPServerClientConnection(socketClient, - connectionCounterId.getAndIncrement()); - co.start(); - } catch (IOException e) { - Log.severe("Connexion impossible avec " + socketClient.getInetAddress()); - } + TCPServerClientConnection co = new TCPServerClientConnection(socketClient, + connectionCounterId.getAndIncrement()); + co.start(); } - } catch(SocketException e) { - + } catch (SocketException ignored) { } catch (Exception e) { Log.warning("Plus aucune connexion ne peux être acceptée", e); } @@ -97,7 +91,7 @@ public class PServer extends Thread implements Closeable { boolean loggedIn; - private TCPServerClientConnection(Socket s, int coId) throws IOException { + private TCPServerClientConnection(Socket s, int coId) { super(s, "Conn#" + coId + " via TCPSv " + socketName, password); addConnectionListener(new PSocketConnectionListener() { @Override @@ -114,16 +108,16 @@ public class PServer extends Thread implements Closeable { clientConnectionListeners.forEach(l -> l.onConnect(connection)); } }); - addPacketListener((conn, packet) -> { - globalPacketListeners.forEach(l -> { - try { - l.onPacketReceive(conn, packet); - } catch (Exception e) { - Log.severe("Exception while calling PPacketListener.onPacketReceive().", e); - sendSilently(PPacketAnswer.buildExceptionPacket(packet, e.toString())); - } - }); - }); + addPacketListener((conn, packet) -> + globalPacketListeners.forEach(l -> { + try { + l.onPacketReceive(conn, packet); + } catch (Exception e) { + Log.severe("Exception while calling PPacketListener.onPacketReceive().", e); + sendSilently(PPacketAnswer.buildExceptionPacket(packet, e.toString())); + } + }) + ); } } @@ -137,7 +131,7 @@ public class PServer extends Thread implements Closeable { clients.forEach(PSocket::close); socket.close(); - } catch (IOException e) {} + } catch (IOException ignored) {} } public boolean isClosed() { diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PSocket.java b/Core/src/main/java/fr/pandacube/lib/core/net/PSocket.java index 29a11cd..7b373d8 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PSocket.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PSocket.java @@ -40,17 +40,17 @@ public class PSocket extends Thread implements Closeable { private boolean server = false; private Socket socket; - private SocketAddress addr; + private final SocketAddress addr; private DataInputStream in; private DataOutputStream out; - private Object outSynchronizer = new Object(); + private final Object outSynchronizer = new Object(); private String password; - private AtomicBoolean isClosed = new AtomicBoolean(false); + private final AtomicBoolean isClosed = new AtomicBoolean(false); - private List> packetListeners = Collections.synchronizedList(new ArrayList<>()); - private List connectionListeners = Collections.synchronizedList(new ArrayList<>()); - private Map> answersCallbacks = Collections.synchronizedMap(new HashMap<>()); + private final List> packetListeners = Collections.synchronizedList(new ArrayList<>()); + private final List connectionListeners = Collections.synchronizedList(new ArrayList<>()); + private final Map> answersCallbacks = Collections.synchronizedMap(new HashMap<>()); private int nextSendId = 0; @@ -58,7 +58,7 @@ public class PSocket extends Thread implements Closeable { * Create a new PSocket that will connect to the specified SocketAddress. * @param a The target server to connect to * @param connName the name of the connection, used to name the Thread used to receive the packet. - * @param the password to send to the server. + * @param pass the password to send to the server. */ public PSocket(SocketAddress a, String connName, String pass) { super("PSocket " + connName); @@ -115,7 +115,6 @@ public class PSocket extends Thread implements Closeable { } send(PPacketAnswer.buildLoginOkPacket(packet)); // login ok at this point - password = null; } else { send(PPacket.buildLoginPacket(password)); @@ -136,9 +135,9 @@ public class PSocket extends Thread implements Closeable { return; } // login ok at this point - password = null; } - + password = null; + socket.setSoTimeout(NETWORK_TIMEOUT); Log.info(getName() + " connected."); @@ -189,7 +188,6 @@ public class PSocket extends Thread implements Closeable { /** * Return the packet read in the socket, or null if the packet is in a bad format. * @return the packet - * @throws IOException * */ private PPacket readPacket() throws IOException { @@ -232,8 +230,6 @@ public class PSocket extends Thread implements Closeable { /** * Send the provided packet, without waiting for an answer. - * @param packet - * @throws IOException */ public void send(PPacket packet) throws IOException { if (packet == null) @@ -271,7 +267,7 @@ public class PSocket extends Thread implements Closeable { public void sendSilently(PPacket packet) { try { send(packet); - } catch (IOException e) {} + } catch (IOException ignored) {} } diff --git a/Core/src/main/java/fr/pandacube/lib/core/net/PSocketConnectionListener.java b/Core/src/main/java/fr/pandacube/lib/core/net/PSocketConnectionListener.java index 8eeeb96..62bec75 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/net/PSocketConnectionListener.java +++ b/Core/src/main/java/fr/pandacube/lib/core/net/PSocketConnectionListener.java @@ -6,12 +6,12 @@ public interface PSocketConnectionListener { * Called when a socket is connected * @param connection the connection */ - public void onConnect(PSocket connection); + void onConnect(PSocket connection); /** * Called just before a socket is disconnected * @param connection the connection */ - public void onDisconnect(PSocket connection); + void onDisconnect(PSocket connection); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/network_api/client/AbstractRequest.java b/Core/src/main/java/fr/pandacube/lib/core/network_api/client/AbstractRequest.java index 07fb39d..645576c 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/network_api/client/AbstractRequest.java +++ b/Core/src/main/java/fr/pandacube/lib/core/network_api/client/AbstractRequest.java @@ -6,7 +6,7 @@ import java.io.PrintStream; public abstract class AbstractRequest { private final String pass; - private String command; + private final String command; private String data; protected AbstractRequest(String cmd, String p) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/network_api/client/ResponseAnalyser.java b/Core/src/main/java/fr/pandacube/lib/core/network_api/client/ResponseAnalyser.java index d80a8f7..d2dab93 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/network_api/client/ResponseAnalyser.java +++ b/Core/src/main/java/fr/pandacube/lib/core/network_api/client/ResponseAnalyser.java @@ -35,7 +35,7 @@ public class ResponseAnalyser { if (line == null) throw new IOException("Not enough data to read second line of response."); - int data_size = 0; + int data_size; try { data_size = Integer.parseInt(line); } catch (NumberFormatException e) { @@ -45,7 +45,7 @@ public class ResponseAnalyser { // lecture du reste StringBuilder sB_data = new StringBuilder(); char[] c = new char[100]; - int nbC = 0; + int nbC; while ((nbC = in.read(c)) != -1) sB_data.append(c, 0, nbC); data = sB_data.toString(); diff --git a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/NetworkAPIListener.java b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/NetworkAPIListener.java index e391901..6fbb683 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/NetworkAPIListener.java +++ b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/NetworkAPIListener.java @@ -10,11 +10,11 @@ import fr.pandacube.lib.core.util.Log; @Deprecated public class NetworkAPIListener implements Runnable { - private int port = 0; - String pass; + private final int port; + final String pass; private ServerSocket serverSocket; - private HashMap requestExecutors = new HashMap<>(); - private String name; + private final HashMap requestExecutors = new HashMap<>(); + private final String name; /** * Instencie le côté serveur du NetworkAPI @@ -22,8 +22,6 @@ public class NetworkAPIListener implements Runnable { * @param n nom du networkAPI (permet l'identification dans les logs) * @param p le port d'écoute * @param pa le mot de passe réseau - * @param peh PacketExecutionHandler permettant de prendre en charge - * l'exécution asynchrone d'une requête reçu pas un client */ public NetworkAPIListener(String n, int p, String pa) { port = p; @@ -51,12 +49,13 @@ public class NetworkAPIListener implements Runnable { t.setDaemon(true); t.start(); } - } catch (IOException e) {} + } catch (IOException ignored) {} synchronized (this) { try { - if (!serverSocket.isClosed()) serverSocket.close(); - } catch (IOException e) {} + if (!serverSocket.isClosed()) + serverSocket.close(); + } catch (IOException ignored) {} } Log.info("NetworkAPI '" + name + "' ferme le port " + port); @@ -70,7 +69,7 @@ public class NetworkAPIListener implements Runnable { public synchronized void closeServerSocket() { if (serverSocket != null) try { serverSocket.close(); - } catch (IOException e) {} + } catch (IOException ignored) {} } public int getPort() { diff --git a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/PacketExecutor.java b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/PacketExecutor.java index 25eb5c7..3fcd9f4 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/PacketExecutor.java +++ b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/PacketExecutor.java @@ -18,8 +18,8 @@ import fr.pandacube.lib.core.util.Log; */ @Deprecated public class PacketExecutor implements Runnable { - private Socket socket; - private NetworkAPIListener networkAPIListener; + private final Socket socket; + private final NetworkAPIListener networkAPIListener; public PacketExecutor(Socket s, NetworkAPIListener napiListener) { socket = s; @@ -43,9 +43,9 @@ public class PacketExecutor implements Runnable { rep.data = e.toString(); try { rep.sendPacket(new PrintStream(socket.getOutputStream())); - } catch (IOException e1) {} + } catch (IOException ignored) {} if (e instanceof IOException) - Log.warning("Unable to read packet from socket " + socket + ": " + e.toString()); + Log.warning("Unable to read packet from socket " + socket + ": " + e); else if(e instanceof BadRequestException) { if (e.getMessage().equals("wrong_password")) Log.warning("Wrong password received from socket " + socket); @@ -60,6 +60,6 @@ public class PacketExecutor implements Runnable { try { socket.close(); - } catch (Exception e) {} + } catch (Exception ignored) {} } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/RequestAnalyser.java b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/RequestAnalyser.java index 91db69a..908b94e 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/network_api/server/RequestAnalyser.java +++ b/Core/src/main/java/fr/pandacube/lib/core/network_api/server/RequestAnalyser.java @@ -8,7 +8,6 @@ import java.net.Socket; @Deprecated public class RequestAnalyser { - private NetworkAPIListener networkAPIListener; public final String command; public final String data; @@ -17,8 +16,6 @@ public class RequestAnalyser { throw new IllegalArgumentException( "le socket doit être non null et doit être ouvert sur le flux d'entrée et napiListener ne doit pas être null"); - networkAPIListener = napiListener; - // on lis la réponse BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); @@ -26,18 +23,18 @@ public class RequestAnalyser { // lecture de la première ligne line = in.readLine(); - if (line == null || !line.equals(networkAPIListener.pass)) throw new BadRequestException("wrong_password"); + if (line == null || !line.equals(napiListener.pass)) throw new BadRequestException("wrong_password"); // lecture de la deuxième ligne line = in.readLine(); - if (line == null || networkAPIListener.getRequestExecutor(line) == null) + if (line == null || napiListener.getRequestExecutor(line) == null) throw new BadRequestException("command_not_exists"); command = line; // lecture de la troisième ligne line = in.readLine(); - int data_size = 0; + int data_size; try { data_size = Integer.parseInt(line); } catch (NumberFormatException e) { @@ -47,7 +44,7 @@ public class RequestAnalyser { // lecture du reste StringBuilder sB_data = new StringBuilder(); char[] c = new char[100]; - int nbC = 0; + int nbC; while ((nbC = in.read(c)) != -1) sB_data.append(c, 0, nbC); @@ -58,9 +55,7 @@ public class RequestAnalyser { socket.shutdownInput(); } - public class BadRequestException extends Exception { - - private static final long serialVersionUID = 1L; + public static class BadRequestException extends Exception { public BadRequestException(String message) { super(message); diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermEntity.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermEntity.java index 39f9267..19c0b90 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermEntity.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermEntity.java @@ -33,12 +33,11 @@ public abstract class PermEntity { * Tells if the current entity inherits directly or indirectly from the specified group * @param group the group to search for * @param recursive true to search in the inheritance tree, or false to search only in the inheritance list of the current entity. - * @return */ public boolean inheritsFromGroup(String group, boolean recursive) { if (group == null) return false; - return getInheritances().stream().anyMatch(g -> g.name.equals(group) || (recursive && g.inheritsFromGroup(group, recursive))); + return getInheritances().stream().anyMatch(g -> g.name.equals(group) || (recursive && g.inheritsFromGroup(group, true))); } public String getPrefix() { @@ -105,8 +104,8 @@ public abstract class PermEntity { String prefixWithEndingDot = permissionPrefix.endsWith(".") ? permissionPrefix : (permissionPrefix + "."); int prefixLength = prefixWithEndingDot.length(); return listEffectivePermissions(server, world).entrySet().stream() - .filter(e -> e.getValue()) // permission must be positive - .map(e -> e.getKey()) // keep only the permission node (key), since the value is always true + .filter(Map.Entry::getValue) // permission must be positive + .map(Map.Entry::getKey) // keep only the permission node (key), since the value is always true .filter(p -> p.startsWith(prefixWithEndingDot)) // keep only relevant permissions .map(p -> p.substring(prefixLength)) // keep only what is after the prefix .map(suffix -> { // convert to long @@ -117,7 +116,7 @@ public abstract class PermEntity { return null; } }) - .filter(longSuffix -> longSuffix != null) + .filter(Objects::nonNull) .mapToLong(longSuffix -> longSuffix) .sorted(); } @@ -224,10 +223,9 @@ public abstract class PermEntity { @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof PermEntity)) - return false; - PermEntity o = (PermEntity) obj; - return Objects.equals(name, o.name) && type == o.type; + return obj instanceof PermEntity o + && Objects.equals(name, o.name) + && type == o.type; } @Override diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermPlayer.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermPlayer.java index 0a9d4e7..1432a1f 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermPlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermPlayer.java @@ -50,7 +50,6 @@ public class PermPlayer extends PermEntity { /** * Alias for {@link #getInheritances()}. - * @return */ public List getGroups() { return getInheritances(); @@ -58,7 +57,6 @@ public class PermPlayer extends PermEntity { /** * Alias for {@link #getInheritances()}. - * @return */ public List getGroupsString() { return getInheritancesString(); @@ -68,7 +66,6 @@ public class PermPlayer extends PermEntity { * Tells if the player is directly part of a group. * This is equivalent to {@link #inheritsFromGroup(String, boolean) inheritsFromGroup(group, false)} * @param group the group to search for - * @return */ public boolean isInGroup(String group) { return inheritsFromGroup(group, false); diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java index 4fcf6c2..5983b57 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/Permissions.java @@ -1,9 +1,11 @@ package fr.pandacube.lib.core.permissions; +import java.util.Arrays; import java.util.List; import java.util.UUID; import fr.pandacube.lib.core.db.DB; +import fr.pandacube.lib.core.db.DBConnection; import fr.pandacube.lib.core.db.DBException; import fr.pandacube.lib.core.util.Log; @@ -16,7 +18,6 @@ public class Permissions { /** * Initialize the permission system. * The connection to the database needs to be initialized first, using {@link DB#init(DBConnection, String)}. - * @throws DBException */ public static void init() throws DBException { if (backendReader != null) @@ -38,8 +39,7 @@ public class Permissions { checkInitialized(); if (specialPermissions == null) return; - for (SpecialPermission sp : specialPermissions) - resolver.specialPermissions.add(sp); + resolver.specialPermissions.addAll(Arrays.asList(specialPermissions)); } private static void checkInitialized() { diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsBackendWriter.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsBackendWriter.java index 6a35e9a..21b93d3 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsBackendWriter.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsBackendWriter.java @@ -57,22 +57,20 @@ import fr.pandacube.lib.core.permissions.SQLPermissions.EntityType; .and(SQLPermissions.type.eq(EntityType.Group.getCode())) .and(SQLPermissions.key.like("default")) ); - if (entry == null && !deflt) { - return; + if (entry != null) { + if (deflt) { + // update just in case + if ("true".equals(entry.get(SQLPermissions.value))) + return; + entry.set(SQLPermissions.value, "true"); + entry.save(); + } + else { + // delete + entry.delete(); + } } - else if (entry != null && deflt) { - // update - if ("true".equals(entry.get(SQLPermissions.value))) - return; - entry.set(SQLPermissions.value, "true"); - entry.save(); - return; - } - else if (entry != null && !deflt) { - // delete - entry.delete(); - } - else { + else if (deflt) { // insert addEntry(name, EntityType.Group, "default", "true", null, null); } @@ -97,20 +95,18 @@ import fr.pandacube.lib.core.permissions.SQLPermissions.EntityType; .and(SQLPermissions.type.eq(type.getCode())) .and(SQLPermissions.key.like("prefix")) ); - if (entry == null && prefix == null) { - return; + if (entry != null) { + if (prefix != null) { + // update + entry.set(SQLPermissions.value, prefix); + entry.save(); + } + else { + // delete + entry.delete(); + } } - else if (entry != null && prefix != null) { - // update - entry.set(SQLPermissions.value, prefix); - entry.save(); - return; - } - else if (entry != null && prefix == null) { - // delete - entry.delete(); - } - else { + else if (prefix != null) { // insert addEntry(name, type, "prefix", prefix, null, null); } @@ -130,20 +126,18 @@ import fr.pandacube.lib.core.permissions.SQLPermissions.EntityType; .and(SQLPermissions.type.eq(type.getCode())) .and(SQLPermissions.key.like("suffix")) ); - if (entry == null && suffix == null) { - return; + if (entry != null) { + if (suffix != null) { + // update + entry.set(SQLPermissions.value, suffix); + entry.save(); + } + else { + // delete + entry.delete(); + } } - else if (entry != null && suffix != null) { - // update - entry.set(SQLPermissions.value, suffix); - entry.save(); - return; - } - else if (entry != null && suffix == null) { - // delete - entry.delete(); - } - else { + else if (suffix != null) { // insert addEntry(name, type, "suffix", suffix, null, null); } @@ -175,7 +169,7 @@ import fr.pandacube.lib.core.permissions.SQLPermissions.EntityType; throw new IllegalStateException("Inheritance already set"); addEntry(name, type, key, inheritance, null, null); } catch (DBException e) { - new RuntimeException(e); + throw new RuntimeException(e); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsCachedBackendReader.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsCachedBackendReader.java index 98c2400..cbcdfcb 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsCachedBackendReader.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsCachedBackendReader.java @@ -29,10 +29,10 @@ import fr.pandacube.lib.core.util.Log; - private Cache usersCache = CacheBuilder.newBuilder() + private final Cache usersCache = CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .build(); - private Set fullPermissionsList = new TreeSet(); + private Set fullPermissionsList = new TreeSet<>(); /* package */ synchronized List getFullPermissionsList() { return new ArrayList<>(fullPermissionsList); @@ -105,14 +105,12 @@ import fr.pandacube.lib.core.util.Log; if (playerRawData.containsKey("groups")) { playerRawData.get("groups").stream() .map(e -> e.get(SQLPermissions.value)) - .forEach(g -> { - player.groups.add(getCachedGroup(g)); - }); + .forEach(g -> player.groups.add(getCachedGroup(g))); } if (player.groups.isEmpty()) { player.usingDefaultGroups = true; - getDefaultGroups().forEach(player.groups::add); + player.groups.addAll(getDefaultGroups()); } return player; @@ -121,7 +119,7 @@ import fr.pandacube.lib.core.util.Log; - private Map groupsCache = new LinkedHashMap<>(); + private final Map groupsCache = new LinkedHashMap<>(); private boolean cacheIsUpdating = false; @@ -207,8 +205,7 @@ import fr.pandacube.lib.core.util.Log; Map> groupRawData = groupsRawData.getOrDefault(groupName, new LinkedHashMap<>()); boolean groupDefault = groupRawData.containsKey("default") - ? "true".equals(groupRawData.get("default").get(0).get(SQLPermissions.value)) - : false; + && "true".equals(groupRawData.get("default").get(0).get(SQLPermissions.value)); String groupSelfPrefix = null; if (groupRawData.containsKey("prefix")) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java index a4016c7..828752f 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/PermissionsResolver.java @@ -29,7 +29,7 @@ import net.md_5.bungee.api.ChatColor; public class PermissionsResolver { - private PermissionsCachedBackendReader backendReader; + private final PermissionsCachedBackendReader backendReader; /* package */ PermissionsResolver(PermissionsCachedBackendReader b) { backendReader = b; @@ -74,7 +74,7 @@ public class PermissionsResolver { return debugData(name, type, DataType.SUFFIX); } - private Cache effectiveDataCache = CacheBuilder.newBuilder() + private final Cache effectiveDataCache = CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .build(); @@ -83,9 +83,7 @@ public class PermissionsResolver { Objects.requireNonNull(type, "type can’t be null"); try { - return effectiveDataCache.get(new DataCacheKey(name, type, dataType), () -> { - return resolveData(name, type, dataType); - }); + return effectiveDataCache.get(new DataCacheKey(name, type, dataType), () -> resolveData(name, type, dataType)); } catch (ExecutionException e) { Log.severe(e); return null; @@ -108,7 +106,7 @@ public class PermissionsResolver { if (resolutionResult.conflict) { Log.warning("For data " + dataType + ":\n" + ChatUtil.treeView(resolutionResult.toDisplayTreeNode(), true).stream() - .map(cmp -> cmp.getLegacyText()) + .map(Chat::getLegacyText) .collect(Collectors.joining(ChatColor.RESET + "\n"))); } @@ -140,7 +138,7 @@ public class PermissionsResolver { Set inheritedPermissions = inheritedResults.stream() .map(g -> g.result) - .filter(r -> r != null) + .filter(Objects::nonNull) .collect(Collectors.toSet()); if (inheritedPermissions.size() == 1) @@ -176,7 +174,7 @@ public class PermissionsResolver { c.thenFailure(" " + conflictMessage); DisplayTreeNode node = new DisplayTreeNode(c); - if (result == null && conflict == false && !inheritances.isEmpty()) { + if (result == null && !conflict && !inheritances.isEmpty()) { // there is nothing interesting to show on current or subnode node.children.add(new DisplayTreeNode(Chat.text("(Inheritances hidden for brevety)").darkGray().italic())); return node; @@ -197,10 +195,8 @@ public class PermissionsResolver { } @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof DataCacheKey)) - return false; - DataCacheKey o = (DataCacheKey) obj; - return Objects.equals(name, o.name) + return obj instanceof DataCacheKey o + && Objects.equals(name, o.name) && Objects.equals(type, o.type) && dataType == o.dataType; } @@ -211,7 +207,7 @@ public class PermissionsResolver { SUFFIX(CachedEntity::getSelfSuffix); private final CachedEntityGetter getter; - private DataType(CachedEntityGetter g) { + DataType(CachedEntityGetter g) { getter = g; } } @@ -233,7 +229,7 @@ public class PermissionsResolver { - private Cache> effectivePermissionsListCache = CacheBuilder.newBuilder() + private final Cache> effectivePermissionsListCache = CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .build(); @@ -265,7 +261,7 @@ public class PermissionsResolver { } - private Cache effectivePermissionsCache = CacheBuilder.newBuilder() + private final Cache effectivePermissionsCache = CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .build(); @@ -281,14 +277,14 @@ public class PermissionsResolver { reversed = true; } - String fPermission = permission == null ? null : permission.toLowerCase(); + String fPermission = permission.toLowerCase(); String fServer = server == null ? null : server.toLowerCase(); String fWorld = world == null ? null : world.toLowerCase(); try { - Boolean resolved = effectivePermissionsCache.get(new PermCacheKey(name, type, fPermission, fServer, fWorld), () -> { - return resolvePermission(name, type, fPermission, fServer, fWorld); - }).value; - return resolved == null ? null : (reversed != resolved.booleanValue()); + Boolean resolved = effectivePermissionsCache.get(new PermCacheKey(name, type, fPermission, fServer, fWorld), + () -> resolvePermission(name, type, fPermission, fServer, fWorld) + ).value; + return resolved == null ? null : (reversed != resolved); } catch (ExecutionException e) { Log.severe(e); return null; @@ -312,7 +308,7 @@ public class PermissionsResolver { if (resolutionResult.conflict) { Log.warning("For permission " + permission + ":\n" + ChatUtil.treeView(resolutionResult.toDisplayTreeNode(), true).stream() - .map(cmp -> cmp.getLegacyText()) + .map(Chat::getLegacyText) .collect(Collectors.joining(ChatColor.RESET + "\n"))); } @@ -374,7 +370,7 @@ public class PermissionsResolver { if (inheritancesGranted != inheritancesRevoqued) { resolutionNode.result = inheritancesGranted ? PermState.GRANTED : PermState.REVOQUED; } - else if (inheritancesGranted && inheritancesRevoqued) { + else if (inheritancesGranted) { resolutionNode.conflictMessage = (resolutionNode.conflictMessage == null ? "" : (resolutionNode.conflictMessage + " ; ")) + "Unsolvable conflict between inheritances"; resolutionNode.conflict = true; @@ -388,7 +384,7 @@ public class PermissionsResolver { - /* package */ List specialPermissions = new ArrayList<>(); + /* package */ final List specialPermissions = new ArrayList<>(); @@ -436,7 +432,7 @@ public class PermissionsResolver { resNode = new ParsedSelfPermission(p, false, PermType.WILDCARD); return resNode; }) - .filter(p -> p != null) + .filter(Objects::nonNull) .collect(Collectors.toList()); boolean explicitGranted = foundPerms.stream() @@ -459,13 +455,13 @@ public class PermissionsResolver { conflict = "Unnecessary explicit permission already granted by self wildcard permissions"; // redundent explicit perm } } - else if (explicitGranted && explicitRevoqued) { + else if (explicitGranted) { conflict = "Unsolvable conflit between explicit permissions"; } else if (wildcardGranted != wildcardRevoqued) { result = PermState.of(wildcardGranted); } - else if (wildcardGranted && wildcardRevoqued) { + else if (wildcardGranted) { conflict = "Unsolvable conflit between wildcard permissions"; } } @@ -505,7 +501,7 @@ public class PermissionsResolver { selfPermissions.forEach(p -> node.children.add(p.toDisplayTreeNode())); - if (result == PermState.UNDEFINED && conflict == false && !inheritances.isEmpty()) { + if (result == PermState.UNDEFINED && !conflict && !inheritances.isEmpty()) { // there is nothing interesting to show on current or subnode node.children.add(new DisplayTreeNode(Chat.text("(Inheritances hidden for brevety)").darkGray().italic())); return node; @@ -542,10 +538,8 @@ public class PermissionsResolver { } @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof PermCacheKey)) - return false; - PermCacheKey o = (PermCacheKey) obj; - return Objects.equals(name, o.name) + return obj instanceof PermCacheKey o + && Objects.equals(name, o.name) && Objects.equals(type, o.type) && Objects.equals(permission, o.permission) && Objects.equals(server, o.server) @@ -554,7 +548,7 @@ public class PermissionsResolver { } private enum PermType { - EXPLICIT, WILDCARD, SPECIAL; + EXPLICIT, WILDCARD, SPECIAL } private enum PermState { @@ -562,7 +556,7 @@ public class PermissionsResolver { REVOQUED(false), UNDEFINED(null); final Boolean value; - private PermState(Boolean v) { value = v; } + PermState(Boolean v) { value = v; } private static PermState of(Boolean v) { return v == null ? UNDEFINED : v ? GRANTED : REVOQUED; } diff --git a/Core/src/main/java/fr/pandacube/lib/core/permissions/ServerWorldKey.java b/Core/src/main/java/fr/pandacube/lib/core/permissions/ServerWorldKey.java index 3ebebd8..b98bb18 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/permissions/ServerWorldKey.java +++ b/Core/src/main/java/fr/pandacube/lib/core/permissions/ServerWorldKey.java @@ -10,10 +10,8 @@ public class ServerWorldKey implements Comparable { } @Override public boolean equals(Object obj) { - if (obj == null || !(obj instanceof ServerWorldKey)) - return false; - ServerWorldKey o = (ServerWorldKey) obj; - return Objects.equals(server, o.server) + return obj instanceof ServerWorldKey o + && Objects.equals(server, o.server) && Objects.equals(world, o.world); } @Override diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java index 788b212..bd17d50 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IOffPlayer.java @@ -20,7 +20,7 @@ import fr.pandacube.lib.core.util.Log; public interface IOffPlayer { /** From how long the last web activity should be before considering the user offline (in ms)? */ - public static final long TIMEOUT_WEB_SESSION = 10000; // msec + long TIMEOUT_WEB_SESSION = 10000; // msec @@ -34,7 +34,7 @@ public interface IOffPlayer { * * @return the id of the player */ - public abstract UUID getUniqueId(); + UUID getUniqueId(); /** @@ -42,7 +42,7 @@ public interface IOffPlayer { * * An alt account uses a specific bit in the UUID to distinguish themselves from the original account. */ - public default boolean isAltAccount() { + default boolean isAltAccount() { return (getUniqueId().getMostSignificantBits() & 0x8000L) == 0x8000L; } @@ -53,14 +53,14 @@ public interface IOffPlayer { * * This method will return undetermined value if {@link #isAltAccount()} is false. */ - public default int getAltIndex() { + default int getAltIndex() { return (int) (getUniqueId().getMostSignificantBits() >> 8) & 0xF; } /** * @return the last known player name of this player, or null if this player never joined the network. */ - public default String getName() { + default String getName() { return PlayerFinder.getLastKnownName(getUniqueId()); } @@ -68,7 +68,7 @@ public interface IOffPlayer { * Indicate if this player is connected to the current node (server or proxy, depending on interface implementation) * @return wether the player is online or not */ - public abstract boolean isOnline(); + boolean isOnline(); @@ -80,7 +80,7 @@ public interface IOffPlayer { * If the player is online in game, it provides the current server they are * connected. */ - public default PlayerStatusOnServer getPlayerStatus() { + default PlayerStatusOnServer getPlayerStatus() { IOnlinePlayer op = getOnlineInstance(); if (op != null && !op.isVanished()) @@ -103,7 +103,7 @@ public interface IOffPlayer { return new PlayerStatusOnServer(PlayerStatusOnServer.PlayerStatus.OFFLINE, null); } - public record PlayerStatusOnServer(PlayerStatus status, String server) { + record PlayerStatusOnServer(PlayerStatus status, String server) { public Chat toComponent() { if (status == PlayerStatus.ONLINE_IG) return successText("En ligne, " + server); @@ -130,12 +130,12 @@ public interface IOffPlayer { * Floodgate related stuff */ - public default boolean isBedrockAccount() { + default boolean isBedrockAccount() { int v = getUniqueId().version(); return v == 0 || v == 8; // also 8 if one day we supports alt accounts for floodgate players } - public default boolean isJavaAccount() { + default boolean isJavaAccount() { return !isBedrockAccount(); } @@ -149,13 +149,12 @@ public interface IOffPlayer { * Return the online instance of this player, if any exists. * May return itself if the current instance already represent an online player. */ - public abstract IOnlinePlayer getOnlineInstance(); + IOnlinePlayer getOnlineInstance(); /** * Get the database entry of this player, or null if the player never joined the network. - * @throws DBException */ - public default SQLPlayer getDbPlayer() throws DBException { + default SQLPlayer getDbPlayer() throws DBException { return SQLPlayer.getPlayerFromUUID(getUniqueId()); } @@ -163,7 +162,7 @@ public interface IOffPlayer { * Get the permission instance of this player. This will never return null. * @return the permission instance of this player */ - public default PermPlayer getPermissionUser() { + default PermPlayer getPermissionUser() { return Permissions.getPlayer(getUniqueId()); } @@ -181,15 +180,14 @@ public interface IOffPlayer { * (and team for bukkit implementation) * @return the display name of the player */ - public abstract String getDisplayName(); + String getDisplayName(); /** * Get an updated display name of the user, * generated using eventual permission’s prefix(es) and suffix(es) of the player, * and with color codes translated to Minecraft’s native {@code §}. - * @return */ - public default String getDisplayNameFromPermissionSystem() { + default String getDisplayNameFromPermissionSystem() { PermPlayer permU = getPermissionUser(); return ChatColorUtil.translateAlternateColorCodes('&', permU.getPrefix() + getName() + permU.getSuffix()); @@ -215,7 +213,7 @@ public interface IOffPlayer { * @param permission the permission node to test * @return whether this player has the provided permission */ - public default boolean hasPermission(String permission) { + default boolean hasPermission(String permission) { IOnlinePlayer online = getOnlineInstance(); if (online != null) @@ -233,10 +231,10 @@ public interface IOffPlayer { * loop. * If the player is offline, it just call the Pandacube * permission system. - * @param permission the permission node to test + * @param permissionExpression the permission node to test * @return whether this player has the provided permission */ - public default boolean hasPermissionExpression(String permissionExpression) { + default boolean hasPermissionExpression(String permissionExpression) { IOnlinePlayer online = getOnlineInstance(); if (online != null) @@ -259,7 +257,7 @@ public interface IOffPlayer { * @param permissionPrefix the permission prefix to search for. * @return a LongStream containing all the values found for the specified permission prefix. */ - public default LongStream getPermissionRangeValues(String permissionPrefix) { + default LongStream getPermissionRangeValues(String permissionPrefix) { IOnlinePlayer online = getOnlineInstance(); if (online != null) @@ -272,7 +270,7 @@ public interface IOffPlayer { /** * Returns the maximum value returned by {@link IOffPlayer#getPermissionRangeValues(String)}. */ - public default OptionalLong getPermissionRangeMax(String permissionPrefix) { + default OptionalLong getPermissionRangeMax(String permissionPrefix) { IOnlinePlayer online = getOnlineInstance(); if (online != null) @@ -289,14 +287,14 @@ public interface IOffPlayer { * @return true if this player is part of the group, * false otherwise */ - public default boolean isInGroup(String group) { + default boolean isInGroup(String group) { return getPermissionUser().isInGroup(group); } /** * Tells if this player is part of the staff, based on permission groups */ - public default boolean isInStaff() { + default boolean isInStaff() { return getPermissionUser().inheritsFromGroup("staff-base", true); } @@ -315,7 +313,7 @@ public interface IOffPlayer { * @param ignored the player that is potentially ignored by this player. * If this parameter is null, this method returns false. */ - public default boolean canIgnore(IOffPlayer ignored) { + default boolean canIgnore(IOffPlayer ignored) { if (ignored == null) return false; if (equals(ignored)) @@ -331,7 +329,7 @@ public interface IOffPlayer { * If this parameter is null, this method returns false. * @implNote the default implementation just calls {@link #canIgnore(IOffPlayer) ignorer.canIgnore(this)}. */ - public default boolean canBeIgnoredBy(IOffPlayer ignorer) { + default boolean canBeIgnoredBy(IOffPlayer ignorer) { if (ignorer == null) return false; return ignorer.canIgnore(this); @@ -343,7 +341,7 @@ public interface IOffPlayer { * If this parameter is null, this method returns false. * @return true if this player have to right to ignore the provided player and is actually ignoring him. */ - public default boolean isIgnoring(IOffPlayer ignored) { + default boolean isIgnoring(IOffPlayer ignored) { if (!canIgnore(ignored)) return false; @@ -363,7 +361,7 @@ public interface IOffPlayer { * @return true if the provided player have to right to ignore this player and is actually ignoring him. * @implNote the default implementation just calls {@link #isIgnoring(IOffPlayer) ignorer.isIgnoring(this)}. */ - public default boolean isIgnoredBy(IOffPlayer ignorer) { + default boolean isIgnoredBy(IOffPlayer ignorer) { return ignorer.isIgnoring(this); } @@ -379,7 +377,7 @@ public interface IOffPlayer { * Retrieve the time when the player will be unmuted, or null if the player is not muted. * @return the timestamp in millisecond of when the player will be unmuted */ - public default Long getMuteTimeout() { + default Long getMuteTimeout() { try { Long muteTimeout = getDbPlayer().get(SQLPlayer.muteTimeout); if (muteTimeout == null || muteTimeout <= System.currentTimeMillis()) @@ -394,9 +392,8 @@ public interface IOffPlayer { /** * Tells if the player is currently muted, meaning that they cannot communicate * through the chat or private messages. - * @return */ - public default boolean isMuted() { + default boolean isMuted() { return getMuteTimeout() != null; } @@ -408,7 +405,7 @@ public interface IOffPlayer { * Birthday */ - public default void setBirthday(int day, int month, Integer year) { + default void setBirthday(int day, int month, Integer year) { try { SQLPlayer dbPlayer = getDbPlayer(); dbPlayer.setBirthday(day, month, year); @@ -418,7 +415,7 @@ public interface IOffPlayer { } } - public default Calendar getBirthday() { + default Calendar getBirthday() { try { return getDbPlayer().getBirthday(); } catch (DBException e) { @@ -434,25 +431,25 @@ public interface IOffPlayer { * Player config */ - public default String getConfig(String key) throws DBException { + default String getConfig(String key) throws DBException { return SQLPlayerConfig.get(getUniqueId(), key); } - public default String getConfig(String key, String deflt) throws DBException { + default String getConfig(String key, String deflt) throws DBException { return SQLPlayerConfig.get(getUniqueId(), key, deflt); } - public default void setConfig(String key, String value) throws DBException { + default void setConfig(String key, String value) throws DBException { SQLPlayerConfig.set(getUniqueId(), key, value); } - public default void unsetConfig(String key) throws DBException { + default void unsetConfig(String key) throws DBException { SQLPlayerConfig.unset(getUniqueId(), key); } - public default boolean isWelcomeQuizzDone() { + default boolean isWelcomeQuizzDone() { try { - return Boolean.valueOf(getConfig("welcome.quizz.done", "false")); + return Boolean.parseBoolean(getConfig("welcome.quizz.done", "false")); } catch (DBException e) { Log.severe("Error knowing if player has already done the quizz. Assuming they did for now.", e); return true; diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java b/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java index 1aa1d4e..ea6ecc3 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IOnlinePlayer.java @@ -29,11 +29,11 @@ public interface IOnlinePlayer extends IOffPlayer { * @implSpec The implementation is expected to call the environment API * (Bukkit/Bungee) to get the name of the player. */ - public abstract String getName(); + String getName(); - public abstract String getServerName(); + String getServerName(); - public abstract String getWorldName(); + String getWorldName(); @@ -42,7 +42,7 @@ public interface IOnlinePlayer extends IOffPlayer { * Floodgate related */ - public default boolean isBedrockClient() { + default boolean isBedrockClient() { try { return FloodgateApi.getInstance().isFloodgatePlayer(getUniqueId()); } catch (NoClassDefFoundError e) { @@ -50,11 +50,11 @@ public interface IOnlinePlayer extends IOffPlayer { } } - public default FloodgatePlayer getBedrockClient() { + default FloodgatePlayer getBedrockClient() { return FloodgateApi.getInstance().getPlayer(getUniqueId()); } - public default boolean isJavaClient() { + default boolean isJavaClient() { return !isBedrockClient(); } @@ -70,7 +70,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @throws DBException if a database access error occurs */ @Override - public default SQLPlayer getDbPlayer() throws DBException { + default SQLPlayer getDbPlayer() throws DBException { SQLPlayer p = SQLPlayer.getPlayerFromUUID(getUniqueId()); if (p == null) throw new IllegalStateException("The player was not found in the database: " + getUniqueId()); @@ -93,7 +93,7 @@ public interface IOnlinePlayer extends IOffPlayer { * indirectly call the method {@link IOffPlayer#hasPermission(String)}, * or it may result in a {@link StackOverflowError}. */ - public abstract boolean hasPermission(String permission); + boolean hasPermission(String permission); /** * Tells if this online player has the permission resulted from the provided expression. @@ -101,7 +101,7 @@ public interface IOnlinePlayer extends IOffPlayer { * indirectly call the method {@link IOffPlayer#hasPermissionExpression(String)}, * or it may result in a {@link StackOverflowError}. */ - public abstract boolean hasPermissionExpression(String permission); + boolean hasPermissionExpression(String permission); /** * Lists all the values for a set of permission indicating an integer in a range. @@ -116,12 +116,12 @@ public interface IOnlinePlayer extends IOffPlayer { * @param permissionPrefix the permission prefix to search for. * @return a LongStream containing all the values found for the specified permission prefix. */ - public abstract LongStream getPermissionRangeValues(String permissionPrefix); + LongStream getPermissionRangeValues(String permissionPrefix); /** * Returns the maximum value returned by {@link IOffPlayer#getPermissionRangeValues(String)}. */ - public abstract OptionalLong getPermissionRangeMax(String permissionPrefix); + OptionalLong getPermissionRangeMax(String permissionPrefix); @@ -135,9 +135,9 @@ public interface IOnlinePlayer extends IOffPlayer { * Vanish */ - public abstract boolean isVanished(); + boolean isVanished(); - public default boolean isVanishedFor(IOffPlayer other) { + default boolean isVanishedFor(IOffPlayer other) { if (!isVanished()) return false; // can see unvanished @@ -168,14 +168,14 @@ public interface IOnlinePlayer extends IOffPlayer { * the chat is activated. * @param message the message to display. */ - public abstract void sendMessage(Component message); + void sendMessage(Component message); /** * Display the provided message in the player’s chat, if * the chat is activated. * @param message the message to display. */ - public default void sendMessage(ComponentLike message) { + default void sendMessage(ComponentLike message) { sendMessage(message.asComponent()); } @@ -184,7 +184,7 @@ public interface IOnlinePlayer extends IOffPlayer { * the chat is activated * @param message the message to display */ - public default void sendMessage(Chat message) { + default void sendMessage(Chat message) { sendMessage(message.getAdv()); } @@ -198,7 +198,7 @@ public interface IOnlinePlayer extends IOffPlayer { * the sender. This parameter is only there to be transmitted to the client, so client side filtering can * be processed. */ - public default void sendMessage(Component message, UUID sender) { + default void sendMessage(Component message, UUID sender) { sendMessage(message, () -> sender == null ? Identity.nil() : Identity.identity(sender)); } @@ -212,7 +212,7 @@ public interface IOnlinePlayer extends IOffPlayer { * the sender. This parameter is only there to be transmitted to the client, so client side filtering can * be processed. */ - public abstract void sendMessage(Component message, Identified sender); + void sendMessage(Component message, Identified sender); /** * Display the provided message in the player’s chat, if @@ -221,7 +221,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @param sender the player causing the send of this message. Client side filtering may occur. * May be null if we don’t want client filtering, but still consider the message as CHAT message. */ - public default void sendMessage(ComponentLike message, UUID sender) { + default void sendMessage(ComponentLike message, UUID sender) { sendMessage(message.asComponent(), sender); } @@ -232,7 +232,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @param sender the player causing the send of this message. Client side filtering may occur. * May be null if we don’t want client filtering, but still consider the message as CHAT message. */ - public default void sendMessage(Chat message, UUID sender) { + default void sendMessage(Chat message, UUID sender) { sendMessage(message.getAdv(), sender); } @@ -241,7 +241,7 @@ public interface IOnlinePlayer extends IOffPlayer { * activated, prepended with the server prefix. * @param message the message to display */ - public default void sendPrefixedMessage(Component message) { + default void sendPrefixedMessage(Component message) { sendMessage(IPlayerManager.prefixedAndColored(message)); } @@ -250,7 +250,7 @@ public interface IOnlinePlayer extends IOffPlayer { * activated, prepended with the server prefix. * @param message the message to display */ - public default void sendPrefixedMessage(Chat message) { + default void sendPrefixedMessage(Chat message) { sendPrefixedMessage(message.getAdv()); } @@ -262,7 +262,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @param stay Stay time in tick * @param fadeOut Fade out time in tick */ - public abstract void sendTitle(Component title, Component subtitle, int fadeIn, int stay, int fadeOut); + void sendTitle(Component title, Component subtitle, int fadeIn, int stay, int fadeOut); /** * Display a title in the middle of the screen. @@ -272,7 +272,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @param stay Stay time in tick * @param fadeOut Fade out time in tick */ - public default void sendTitle(Chat title, Chat subtitle, int fadeIn, int stay, int fadeOut) { + default void sendTitle(Chat title, Chat subtitle, int fadeIn, int stay, int fadeOut) { sendTitle(title.getAdv(), subtitle.getAdv(), fadeIn, stay, fadeOut); } @@ -282,7 +282,7 @@ public interface IOnlinePlayer extends IOffPlayer { * line break. * @param brand the server brand to send to the client. */ - public abstract void sendServerBrand(String brand); + void sendServerBrand(String brand); @@ -297,17 +297,17 @@ public interface IOnlinePlayer extends IOffPlayer { - public abstract ClientOptions getClientOptions(); + ClientOptions getClientOptions(); - public interface ClientOptions { + interface ClientOptions { - public Locale getLocale(); + Locale getLocale(); - public int getViewDistance(); + int getViewDistance(); - public boolean hasChatColorEnabled(); + boolean hasChatColorEnabled(); /** * Tells if the client is configured to completely hide the chat to the @@ -316,7 +316,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @implSpec if the value is unknown, it is assumed that the chat is * fully visible. */ - public boolean isChatHidden(); + boolean isChatHidden(); /** * Tells if the client is configured to display the chat normally. @@ -326,7 +326,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @implSpec if the value is unknown, it is assumed that the chat is * fully visible. */ - public boolean isChatFullyVisible(); + boolean isChatFullyVisible(); /** * Tells if the client is configured to only display system messages @@ -336,7 +336,7 @@ public interface IOnlinePlayer extends IOffPlayer { * @implSpec if the value is unknown, it is assumed that the chat is * fully visible. */ - public boolean isChatOnlyDisplayingSystemMessages(); + boolean isChatOnlyDisplayingSystemMessages(); @@ -345,44 +345,44 @@ public interface IOnlinePlayer extends IOffPlayer { * @implSpec if the value is unknown, it is assumed that the main hand * is on the right. */ - public boolean isLeftHanded(); + boolean isLeftHanded(); /** * Tells if the client has configured the main hand on the right. * @implSpec if the value is unknown, it is assumed that the main hand * is on the right. */ - public boolean isRightHanded(); + boolean isRightHanded(); /** * Tells if the client has enabled the filtering of texts on sign and book titles. * Always false as of MC 1.18. */ - public boolean isTextFilteringEnabled(); + boolean isTextFilteringEnabled(); /** * Tells if the client allows the server to list their player name in the * multiplayer menu. */ - public boolean allowsServerListing(); + boolean allowsServerListing(); - public boolean hasSkinCapeEnabled(); + boolean hasSkinCapeEnabled(); - public boolean hasSkinJacketEnabled(); + boolean hasSkinJacketEnabled(); - public boolean hasSkinLeftSleeveEnabled(); + boolean hasSkinLeftSleeveEnabled(); - public boolean hasSkinRightSleeveEnabled(); + boolean hasSkinRightSleeveEnabled(); - public boolean hasSkinLeftPantsEnabled(); + boolean hasSkinLeftPantsEnabled(); - public boolean hasSkinRightPantsEnabled(); + boolean hasSkinRightPantsEnabled(); - public boolean hasSkinHatsEnabled(); + boolean hasSkinHatsEnabled(); } @@ -393,9 +393,8 @@ public interface IOnlinePlayer extends IOffPlayer { * Chat messages represent public communication between players. By default, * it only include actual chat message. This method may be used in commands * like /me, /afk or the login/logout broadcasted messages - * @return */ - public default boolean canChat() { + default boolean canChat() { return getClientOptions().isChatFullyVisible(); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java b/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java index 6f37aeb..1ef191a 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/IPlayerManager.java @@ -19,6 +19,7 @@ import fr.pandacube.lib.core.db.DB; import fr.pandacube.lib.core.db.DBInitTableException; import fr.pandacube.lib.core.util.Log; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import net.md_5.bungee.api.chat.BaseComponent; public abstract class IPlayerManager { @@ -45,11 +46,11 @@ public abstract class IPlayerManager onlinePlayers = Collections.synchronizedMap(new HashMap<>()); + private final Map onlinePlayers = Collections.synchronizedMap(new HashMap<>()); - private LoadingCache offlinePlayers = CacheBuilder.newBuilder() + private final LoadingCache offlinePlayers = CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) - .build(CacheLoader.from(pId -> newOffPlayerInstance(pId))); + .build(CacheLoader.from(this::newOffPlayerInstance)); public IPlayerManager() throws DBInitTableException { @@ -90,7 +91,7 @@ public abstract class IPlayerManager getAllNotVanished() { List players = getAll(); - players.removeIf(op -> op.isVanished()); + players.removeIf(IOnlinePlayer::isVanished); return players; } @@ -183,8 +184,8 @@ public abstract class IPlayerManager * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, boolean, String, UUID)}. + * {@link #broadcast(ComponentLike, boolean, boolean, String, UUID)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. @@ -246,7 +247,7 @@ public abstract class IPlayerManager * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String, UUID)}. + * want to specify a permission, use {@link #broadcast(ComponentLike, boolean, boolean, String, UUID)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. @@ -265,7 +266,7 @@ public abstract class IPlayerManager * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String)}. + * want to specify a permission, use {@link #broadcast(ComponentLike, boolean, boolean, String)}. *

* This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, boolean, UUID)}. + * {@link #broadcast(ComponentLike, boolean, boolean, UUID)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. * @param console if the message must be displayed in the console. * @throws IllegalArgumentException if message is null. */ - @Deprecated - public static void broadcast(BaseComponent message, boolean prefix, boolean console) { - broadcast(Chat.toAdventure(message), prefix, console, null, null); - } - - /** - * Broadcast a message to all players, and eventually to the console. - *

- * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String)}. - *

- * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, boolean, UUID)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @param console if the message must be displayed in the console. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Component message, boolean prefix, boolean console) { + public static void broadcast(ComponentLike message, boolean prefix, boolean console) { broadcast(message, prefix, console, null, null); } @@ -310,11 +292,11 @@ public abstract class IPlayerManager * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, String, UUID)}. + * {@link #broadcast(ComponentLike, boolean, String, UUID)}. *

* This method decides to send the message to the console depending on whether {@code permission} * is null (will send to console) or not (will not send to console). To specify this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean, String)}. + * {@link #broadcast(ComponentLike, boolean, boolean, String)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. @@ -322,7 +304,7 @@ public abstract class IPlayerManager * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, String, UUID)}. + * want to specify a permission, use {@link #broadcast(ComponentLike, boolean, String, UUID)}. *

* This method sends the message to the console. To change this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean, UUID)}. + * {@link #broadcast(ComponentLike, boolean, boolean, UUID)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. @@ -343,162 +325,46 @@ public abstract class IPlayerManager - * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, UUID)}. - *

- * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, String)}. - *

* This method sends the message to the console. To change this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Component message, boolean prefix) { - broadcast(message, prefix, true, null, null); - } - - - - - /** - * Broadcast a message to some or all players, and eventually to the console. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @param console if the message must be displayed in the console. - * @param permission if not null, the message is only sent to player with this permission. - * @param sourcePlayer specifiy the eventual player that is the source of the message. - * If null, the message will be sent as a SYSTEM chat message. - * If not null, the message will be sent as a CHAT message, and will not be sent - * to players ignoring the provided player. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Chat message, boolean prefix, boolean console, String permission, UUID sourcePlayer) { - Objects.requireNonNull(message, "message cannot be null"); - broadcast(message.getAdv(), prefix, console, permission, sourcePlayer); - } - - /** - * Broadcast a message to some or all players, and eventually to the console. - *

- * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, boolean, String, UUID)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @param console if the message must be displayed in the console. - * @param permission if not null, the message is only sent to player with this permission. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Chat message, boolean prefix, boolean console, String permission) { - broadcast(message, prefix, console, permission, null); - } - - /** - * Broadcast a message to all players, and eventually to the console. - *

- * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String, UUID)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @param console if the message must be displayed in the console. - * @param sourcePlayer specifiy the eventual player that is the source of the message. - * If null, the message will be sent as a SYSTEM chat message. - * If not null, the message will be sent as a CHAT message, and will not be sent - * to players ignoring the provided player. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Chat message, boolean prefix, boolean console, UUID sourcePlayer) { - broadcast(message, prefix, console, null, sourcePlayer); - } - - /** - * Broadcast a message to all players, and eventually to the console. - *

- * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, boolean, String)}. - *

- * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, boolean, UUID)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. - * @param console if the message must be displayed in the console. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Chat message, boolean prefix, boolean console) { - broadcast(message, prefix, console, null, null); - } - - /** - * Broadcast a message to some or all players, and eventually to the console. - *

- * This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, String, UUID)}. - *

- * This method decides to send the message to the console depending on whether {@code permission} - * is null (will send to console) or not (will not send to console). To specify this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean, String)}. + * {@link #broadcast(ComponentLike, boolean, boolean, String, UUID)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. * @param permission if not null, the message is only sent to player with this permission (but not to console). * If null, the message will be sent to all players and to console. - * @throws IllegalArgumentException if message is null. - */ - public static void broadcast(Chat message, boolean prefix, String permission) { - broadcast(message, prefix, (permission == null), permission, null); - } - - /** - * Broadcast a message to all players, and to the console. - *

- * This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, String, UUID)}. - *

- * This method sends the message to the console. To change this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean, UUID)}. - * - * @param message the message to send. - * @param prefix if the server prefix will be prepended to the message. * @param sourcePlayer specifiy the eventual player that is the source of the message. * If null, the message will be sent as a SYSTEM chat message. * If not null, the message will be sent as a CHAT message, and will not be sent * to players ignoring the provided player. * @throws IllegalArgumentException if message is null. */ - public static void broadcast(Chat message, boolean prefix, UUID sourcePlayer) { - broadcast(message, prefix, true, null, sourcePlayer); + public static void broadcast(ComponentLike message, boolean prefix, String permission, UUID sourcePlayer) { + broadcast(message, prefix, true, permission, sourcePlayer); } /** * Broadcast a message to all players, and to the console. *

* This method assumes this message is not caused by a specific player. To specify the source player, use - * {@link #broadcast(BaseComponent, boolean, UUID)}. + * {@link #broadcast(ComponentLike, boolean, UUID)}. *

* This method does not restrict the reception of the message to a specific permission. If you - * want to specify a permission, use {@link #broadcast(BaseComponent, boolean, String)}. + * want to specify a permission, use {@link #broadcast(ComponentLike, boolean, String)}. *

* This method sends the message to the console. To change this behaviour, use - * {@link #broadcast(BaseComponent, boolean, boolean)}. + * {@link #broadcast(ComponentLike, boolean, boolean)}. * * @param message the message to send. * @param prefix if the server prefix will be prepended to the message. * @throws IllegalArgumentException if message is null. */ - public static void broadcast(Chat message, boolean prefix) { + public static void broadcast(ComponentLike message, boolean prefix) { broadcast(message, prefix, true, null, null); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java index 76e1f18..a8c9c92 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/PlayerFinder.java @@ -32,13 +32,13 @@ import fr.pandacube.lib.core.util.Log; */ public class PlayerFinder { - private static Cache playerLastKnownName = CacheBuilder.newBuilder() + private static final Cache playerLastKnownName = CacheBuilder.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000) .build(); record PlayerIdCacheKey(String pName, boolean old) { } - private static Cache playerId = CacheBuilder.newBuilder() + private static final Cache playerId = CacheBuilder.newBuilder() .expireAfterWrite(2, TimeUnit.MINUTES) .maximumSize(1000) .build(); @@ -142,7 +142,7 @@ public class PlayerFinder { public static boolean isValidPlayerName(String name) { if (name == null) return false; - return name.matches("[0-9a-zA-Z_.]{2,20}"); + return name.matches("[\\da-zA-Z_.]{2,20}"); } public static SQLPlayer getDBPlayer(UUID id) throws DBException { @@ -167,7 +167,7 @@ public class PlayerFinder { }; @SuppressWarnings("unchecked") - public static final SuggestionsSupplier TAB_PLAYER_OFFLINE() { + public static SuggestionsSupplier TAB_PLAYER_OFFLINE() { return (SuggestionsSupplier) TAB_PLAYER_OFFLINE; } @@ -188,12 +188,12 @@ public class PlayerFinder { public static int OLD_NICK_MULTIPLIER = 2; - private static List> CONFUSABLE_CHARACTERS = ImmutableList.of( + private static final List> CONFUSABLE_CHARACTERS = ImmutableList.of( ImmutableList.of('o', '0'), ImmutableList.of('i', '1', 'l'), ImmutableList.of('b', '8') ); - private static ToIntBiFunction CHAR_DISTANCE = (c1, c2) -> { + private static final ToIntBiFunction CHAR_DISTANCE = (c1, c2) -> { if (c1.equals(c2)) return 0; for (List charTab : CONFUSABLE_CHARACTERS) { @@ -205,7 +205,7 @@ public class PlayerFinder { record NamesCacheResult(String name, String lowercaseName, UUID id) { } // Java 16 - private static LoadingCache> namesCache = CacheBuilder.newBuilder() + private static final LoadingCache> namesCache = CacheBuilder.newBuilder() .expireAfterWrite(2, TimeUnit.MINUTES) .maximumSize(1) .build(CacheLoader.from((String k) -> { @@ -221,7 +221,7 @@ public class PlayerFinder { return cached; })); - private static LoadingCache searchCache = CacheBuilder.newBuilder() + private static final LoadingCache searchCache = CacheBuilder.newBuilder() .expireAfterWrite(2, TimeUnit.MINUTES) .maximumSize(100) .build(CacheLoader.from((String query) -> { diff --git a/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerIgnore.java b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerIgnore.java index 3065a9c..d2ac6af 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerIgnore.java +++ b/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerIgnore.java @@ -46,7 +46,6 @@ public class SQLPlayerIgnore extends SQLElement { } if (el != null && !newIgnoreState) { el.delete(); - return; } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/search/SearchEngine.java b/Core/src/main/java/fr/pandacube/lib/core/search/SearchEngine.java index 3d51132..2573980 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/search/SearchEngine.java +++ b/Core/src/main/java/fr/pandacube/lib/core/search/SearchEngine.java @@ -1,5 +1,9 @@ package fr.pandacube.lib.core.search; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import fr.pandacube.lib.core.util.Log; + import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -7,15 +11,9 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; -import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; - -import fr.pandacube.lib.core.util.Log; - /** * Utility class to manage searching among a set of * SearchResult instances, using case insensitive @@ -23,15 +21,15 @@ import fr.pandacube.lib.core.util.Log; */ public class SearchEngine { - Map> searchKeywordsResultMap = new HashMap<>(); - Map> resultsSearchKeywordsMap = new HashMap<>(); + private final Map> searchKeywordsResultMap = new HashMap<>(); + private final Map> resultsSearchKeywordsMap = new HashMap<>(); - Map> suggestionsKeywordsResultMap = new HashMap<>(); - Map> resultsSuggestionsKeywordsMap = new HashMap<>(); + private final Map> suggestionsKeywordsResultMap = new HashMap<>(); + private final Map> resultsSuggestionsKeywordsMap = new HashMap<>(); + + private final Set resultSet = new HashSet<>(); - Set resultSet = new HashSet<>(); - - private Cache, List> suggestionsCache; + private final Cache, List> suggestionsCache; public SearchEngine(int suggestionsCacheSize) { suggestionsCache = CacheBuilder.newBuilder() @@ -50,7 +48,7 @@ public class SearchEngine { searchKw = result.getSearchKeywords(); Objects.requireNonNull(searchKw, "SearchResult instance must provide a non null set of search keywords"); searchKw = searchKw.stream() - .filter(e -> e != null) + .filter(Objects::nonNull) .map(String::toLowerCase) .collect(Collectors.toSet()); } catch (Exception e) { @@ -63,7 +61,7 @@ public class SearchEngine { suggestsKw = result.getSuggestionKeywords(); Objects.requireNonNull(suggestsKw, "SearchResult instance must provide a non null set of suggestions keywords"); suggestsKw = new HashSet<>(suggestsKw); - suggestsKw.removeIf(e -> e == null); + suggestsKw.removeIf(Objects::isNull); } catch (Exception e) { Log.severe(e); return; @@ -123,7 +121,7 @@ public class SearchEngine { public synchronized Set search(Set searchTerms) { if (searchTerms == null) - searchTerms = new HashSet(); + searchTerms = new HashSet<>(); Set retainedResults = new HashSet<>(resultSet); for (String term : searchTerms) { @@ -157,7 +155,7 @@ public class SearchEngine { .collect(Collectors.toSet()); try { - return suggestionsCache.get(lowerCaseSearchTerm, (Callable>) () -> { + return suggestionsCache.get(lowerCaseSearchTerm, () -> { Set prevResults = search(lowerCaseSearchTerm); Set suggestions = new HashSet<>(); diff --git a/Core/src/main/java/fr/pandacube/lib/core/search/SearchResult.java b/Core/src/main/java/fr/pandacube/lib/core/search/SearchResult.java index 4ce6d7d..d38b1f4 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/search/SearchResult.java +++ b/Core/src/main/java/fr/pandacube/lib/core/search/SearchResult.java @@ -4,8 +4,8 @@ import java.util.Set; public interface SearchResult { - public Set getSearchKeywords(); + Set getSearchKeywords(); - public Set getSuggestionKeywords(); + Set getSuggestionKeywords(); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/AmountPerTimeLimiter.java b/Core/src/main/java/fr/pandacube/lib/core/util/AmountPerTimeLimiter.java index 7943bc3..e6d4772 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/AmountPerTimeLimiter.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/AmountPerTimeLimiter.java @@ -19,7 +19,7 @@ public class AmountPerTimeLimiter { - private class Entry { + private static class Entry { private final long time; private double amount; public Entry(long t, double a) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/BiMap.java b/Core/src/main/java/fr/pandacube/lib/core/util/BiMap.java index 13245be..e47f613 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/BiMap.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/BiMap.java @@ -11,8 +11,8 @@ import java.util.function.Supplier; public class BiMap implements Iterable> { - protected Map map; - protected Map inversedMap; + protected final Map map; + protected final Map inversedMap; private BiMap reversedView = null; diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/CronExpression.java b/Core/src/main/java/fr/pandacube/lib/core/util/CronExpression.java index f4c9247..9400220 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/CronExpression.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/CronExpression.java @@ -299,7 +299,7 @@ public class CronExpression { this.hourField = new SimpleField(CronFieldType.HOUR, parts[ix++]); this.dayOfMonthField = new DayOfMonthField(parts[ix++]); this.monthField = new SimpleField(CronFieldType.MONTH, parts[ix++]); - this.dayOfWeekField = new DayOfWeekField(parts[ix++]); + this.dayOfWeekField = new DayOfWeekField(parts[ix]); } public static CronExpression create(final String expr) { @@ -336,7 +336,7 @@ public class CronExpression { if (!monthField.nextMatch(nextDateTime)) { continue; } - if (!findDay(nextDateTime, dateTimeBarrier)) { + if (!findDay(nextDateTime)) { continue; } if (!hourField.nextMatch(nextDateTime)) { @@ -361,11 +361,10 @@ public class CronExpression { * to handle them together in the same method. * * @param dateTime Initial {@link ZonedDateTime} instance to start from - * @param dateTimeBarrier At which point stop searching for next execution time * @return {@code true} if a match was found for this field or {@code false} if the field overflowed - * @see {@link SimpleField#nextMatch(ZonedDateTime[])} + * @see SimpleField#nextMatch(ZonedDateTime[]) */ - private boolean findDay(ZonedDateTime[] dateTime, ZonedDateTime dateTimeBarrier) { + private boolean findDay(ZonedDateTime[] dateTime) { int month = dateTime[0].getMonthValue(); while (!(dayOfMonthField.matches(dateTime[0].toLocalDate()) @@ -400,16 +399,19 @@ public class CronExpression { } abstract static class BasicField { + @SuppressWarnings({"RegExpRepeatedSpace", "RegExpSimplifiable", "RegExpSingleCharAlternation", "RegExpRedundantEscape"}) private static final Pattern CRON_FIELD_REGEXP = Pattern - .compile("(?: # start of group 1\n" - + " (?:(?\\*)|(?\\?)|(?L)) # global flag (L, ?, *)\n" - + " | (?[0-9]{1,2}|[a-z]{3,3}) # or start number or symbol\n" - + " (?: # start of group 2\n" - + " (?L|W) # modifier (L,W)\n" - + " | -(?[0-9]{1,2}|[a-z]{3,3}) # or end nummer or symbol (in range)\n" - + " )? # end of group 2\n" - + ") # end of group 1\n" - + "(?:(?/|\\#)(?[0-9]{1,7}))? # increment and increment modifier (/ or \\#)\n", + .compile(""" + (?: # start of group 1 + (?:(?\\*)|(?\\?)|(?L)) # global flag (L, ?, *) + | (?[0-9]{1,2}|[a-z]{3,3}) # or start number or symbol + (?: # start of group 2 + (?L|W) # modifier (L,W) + | -(?[0-9]{1,2}|[a-z]{3,3}) # or end nummer or symbol (in range) + )? # end of group 2 + ) # end of group 1 + (?:(?/|\\#)(?[0-9]{1,7}))? # increment and increment modifier (/ or \\#) + """, Pattern.CASE_INSENSITIVE | Pattern.COMMENTS); final CronFieldType fieldType; @@ -500,10 +502,7 @@ public class CronExpression { } protected boolean matches(int val, FieldPart part) { - if (val >= part.from && val <= part.to && (val - part.from) % part.increment == 0) { - return true; - } - return false; + return val >= part.from && val <= part.to && (val - part.from) % part.increment == 0; } protected int nextMatch(int val, FieldPart part) { @@ -529,17 +528,6 @@ public class CronExpression { super(fieldType, fieldExpr); } - public boolean matches(int val) { - if (val >= fieldType.from && val <= fieldType.to) { - for (FieldPart part : parts) { - if (matches(val, part)) { - return true; - } - } - } - return false; - } - /** * Find the next match for this field. If a match cannot be found force an overflow and increase the next * greatest field. @@ -602,9 +590,9 @@ public class CronExpression { @Override protected void validatePart(FieldPart part) { - if (part.modifier != null && Arrays.asList("L", "?").indexOf(part.modifier) == -1) { + if (part.modifier != null && !Arrays.asList("L", "?").contains(part.modifier)) { throw new IllegalArgumentException(String.format("Invalid modifier [%s]", part.modifier)); - } else if (part.incrementModifier != null && Arrays.asList("/", "#").indexOf(part.incrementModifier) == -1) { + } else if (part.incrementModifier != null && !Arrays.asList("/", "#").contains(part.incrementModifier)) { throw new IllegalArgumentException(String.format("Invalid increment modifier [%s]", part.incrementModifier)); } } @@ -639,7 +627,7 @@ public class CronExpression { @Override protected void validatePart(FieldPart part) { - if (part.modifier != null && Arrays.asList("L", "W", "?").indexOf(part.modifier) == -1) { + if (part.modifier != null && !Arrays.asList("L", "W", "?").contains(part.modifier)) { throw new IllegalArgumentException(String.format("Invalid modifier [%s]", part.modifier)); } else if (part.incrementModifier != null && !"/".equals(part.incrementModifier)) { throw new IllegalArgumentException(String.format("Invalid increment modifier [%s]", part.incrementModifier)); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/DistanceUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/DistanceUtil.java index c800ca8..da3d462 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/DistanceUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/DistanceUtil.java @@ -19,8 +19,8 @@ public class DistanceUtil { String precisionFormat = "##0"; if (precision > 0) precisionFormat += "."; - for (int i = 0; i < precision; i++) - precisionFormat += "0"; + precisionFormat += "0".repeat(precision); + DecimalFormat df = new DecimalFormat(precisionFormat); double dist = meterDist / choosenUnit.multiplicator; @@ -33,12 +33,17 @@ public class DistanceUtil { } public enum DistanceUnit { - NM(0.000000001, "nm"), µM(0.000001, "µm"), MM(0.001, "mm"), CM(0.01, "cm"), M(1, "m"), KM(1000, "km"); + NM(0.000000001, "nm"), + UM(0.000001, "µm"), + MM(0.001, "mm"), + CM(0.01, "cm"), + M(1, "m"), + KM(1000, "km"); private final double multiplicator; private final String unitStr; - private DistanceUnit(double mult, String s) { + DistanceUnit(double mult, String s) { multiplicator = mult; unitStr = s; } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/EnumUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/EnumUtil.java index 4a2abe1..02954fb 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/EnumUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/EnumUtil.java @@ -1,5 +1,8 @@ package fr.pandacube.lib.core.util; +import java.util.Arrays; +import java.util.stream.Collectors; + public class EnumUtil { /** @@ -10,17 +13,9 @@ public class EnumUtil { * @return a string representation of the enum class. */ public static > String enumList(Class enumType, String separator) { - T[] elements = enumType.getEnumConstants(); - - String out = ""; - boolean first = true; - for (T el : elements) { - if (!first) out += separator; - first = false; - out += el.name(); - - } - return out; + return Arrays.stream(enumType.getEnumConstants()) + .map(Enum::name) + .collect(Collectors.joining(separator)); } /** diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/FileUtils.java b/Core/src/main/java/fr/pandacube/lib/core/util/FileUtils.java index 8010e44..d0ba161 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/FileUtils.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/FileUtils.java @@ -3,6 +3,7 @@ package fr.pandacube.lib.core.util; import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.attribute.BasicFileAttributes; public class FileUtils { @@ -20,12 +21,17 @@ public class FileUtils { if (target.exists() && !target.isDirectory()) { throw new IllegalStateException("target file already exists: " + target); } - if (source.isDirectory()) { - target.mkdir(); - for (String child : source.list()) - copy(new File(source, child), new File(target, child)); + BasicFileAttributes sourceAttr = Files.readAttributes(source.toPath(), BasicFileAttributes.class); + if (sourceAttr.isDirectory()) { + if (target.mkdir()) { + for (String child : source.list()) + copy(new File(source, child), new File(target, child)); + } + else { + throw new IOException("Cannot create directory " + target); + } } - else if (source.isFile()) { + else if (sourceAttr.isRegularFile()) { Files.copy(source.toPath(), target.toPath()); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/GifDecoder.java b/Core/src/main/java/fr/pandacube/lib/core/util/GifDecoder.java index eed54d6..ebf17bf 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/GifDecoder.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/GifDecoder.java @@ -103,13 +103,7 @@ public class GifDecoder { protected ArrayList frames; // frames read from current file protected int frameCount; - static class GifFrame { - public GifFrame(BufferedImage im, int del) { - image = im; - delay = del; - } - public BufferedImage image; - public int delay; + record GifFrame(BufferedImage image, int delay) { } /** @@ -184,12 +178,7 @@ public class GifDecoder { if (lastDispose == 2) { // fill last image rect area with background color Graphics2D g = image.createGraphics(); - Color c = null; - if (transparency) { - c = new Color(0, 0, 0, 0); // assume background is transparent - } else { - c = new Color(lastBgColor); // use given background color - } + Color c = transparency ? new Color(0, 0, 0, 0) : new Color(lastBgColor); g.setColor(c); g.setComposite(AlphaComposite.Src); // replace area g.fill(lastRect); @@ -270,7 +259,7 @@ public class GifDecoder { /** * Reads GIF image from stream * - * @param BufferedInputStream containing GIF file. + * @param is containing GIF file. * @return read status code (0 = no errors) */ public int read(BufferedInputStream is) { @@ -286,7 +275,7 @@ public class GifDecoder { } try { is.close(); - } catch (IOException e) { + } catch (IOException ignored) { } } else { status = STATUS_OPEN_ERROR; @@ -297,7 +286,7 @@ public class GifDecoder { /** * Reads GIF image from stream * - * @param InputStream containing GIF file. + * @param is containing GIF file. * @return read status code (0 = no errors) */ public int read(InputStream is) { @@ -315,7 +304,7 @@ public class GifDecoder { } try { is.close(); - } catch (IOException e) { + } catch (IOException ignored) { } } else { status = STATUS_OPEN_ERROR; @@ -334,7 +323,7 @@ public class GifDecoder { status = STATUS_OK; try { name = name.trim().toLowerCase(); - if ((name.indexOf("file:") >= 0) || + if ((name.contains("file:")) || (name.indexOf(":/") > 0)) { URL url = new URL(name); in = new BufferedInputStream(url.openStream()); @@ -524,14 +513,14 @@ public class GifDecoder { int n = 0; if (blockSize > 0) { try { - int count = 0; + int count; while (n < blockSize) { count = in.read(block, n, blockSize - n); if (count == -1) break; n += count; } - } catch (IOException e) { + } catch (IOException ignored) { } if (n < blockSize) { @@ -554,7 +543,7 @@ public class GifDecoder { int n = 0; try { n = in.read(c); - } catch (IOException e) { + } catch (IOException ignored) { } if (n < nbytes) { status = STATUS_FORMAT_ERROR; @@ -595,11 +584,11 @@ public class GifDecoder { case 0xff : // application extension readBlock(); - String app = ""; + StringBuilder app = new StringBuilder(); for (int i = 0; i < 11; i++) { - app += (char) block[i]; + app.append((char) block[i]); } - if (app.equals("NETSCAPE2.0")) { + if (app.toString().equals("NETSCAPE2.0")) { readNetscapeExt(); } else @@ -644,11 +633,11 @@ public class GifDecoder { * Reads GIF file header information. */ protected void readHeader() { - String id = ""; + StringBuilder id = new StringBuilder(); for (int i = 0; i < 6; i++) { - id += (char) read(); + id.append((char) read()); } - if (!id.startsWith("GIF")) { + if (!id.toString().startsWith("GIF")) { status = STATUS_FORMAT_ERROR; return; } @@ -769,9 +758,6 @@ public class GifDecoder { lastRect = new Rectangle(ix, iy, iw, ih); lastImage = image; lastBgColor = bgColor; - /*int dispose = 0; - boolean transparency = false; - int delay = 0;*/ lct = null; } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/IteratorIterator.java b/Core/src/main/java/fr/pandacube/lib/core/util/IteratorIterator.java index 70a620e..ba0fc24 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/IteratorIterator.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/IteratorIterator.java @@ -9,7 +9,7 @@ import java.util.NoSuchElementException; public class IteratorIterator implements Iterator { public static IteratorIterator ofCollectionOfIterable(Collection> coll) { - return new IteratorIterator<>(coll.stream().map(i -> i.iterator()).iterator()); + return new IteratorIterator<>(coll.stream().map(Iterable::iterator).iterator()); } public static IteratorIterator ofCollectionOfIterator(Collection> coll) { @@ -18,7 +18,7 @@ public class IteratorIterator implements Iterator { @SafeVarargs public static IteratorIterator ofArrayOfIterable(Iterable... arr) { - return new IteratorIterator<>(Arrays.stream(arr).map(i -> i.iterator()).iterator()); + return new IteratorIterator<>(Arrays.stream(arr).map(Iterable::iterator).iterator()); } @SafeVarargs @@ -26,7 +26,7 @@ public class IteratorIterator implements Iterator { return new IteratorIterator<>(Arrays.asList(arr).iterator()); } - private Iterator> iterators; + private final Iterator> iterators; private Iterator currentIterator = null; @@ -59,5 +59,16 @@ public class IteratorIterator implements Iterator { throw new NoSuchElementException("No next value found in iterator."); return currentIterator.next(); } - + + /** + * @implNote The current implementation of {@link IteratorIterator} may not support + * running this method if the current position is the last value of one of + * the underlying iterable, and if the {@link #hasNext()} method has been called before this one. + */ + @Override + public void remove() { + // TODO change code to avoid currentIterator being null because we are about to change currentIterator + if (currentIterator != null) + currentIterator.remove(); + } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/JArithmeticInterpreter.java b/Core/src/main/java/fr/pandacube/lib/core/util/JArithmeticInterpreter.java index e38123b..bd13ecb 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/JArithmeticInterpreter.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/JArithmeticInterpreter.java @@ -53,7 +53,7 @@ pour les calculs (division par zero, etc...). */ -// Classe servant � palier l'absence de passage par variables ou reference +// Classe servant à palier l'absence de passage par variables ou reference class VariableInt { public int mValue; @@ -65,8 +65,8 @@ public class JArithmeticInterpreter { // Variables - int mOperator; - double mValue; + final int mOperator; + final double mValue; JArithmeticInterpreter fg, fd; // Methods @@ -75,27 +75,21 @@ public class JArithmeticInterpreter { // Node private JArithmeticInterpreter() { - this(0, 0, null, null); + this(0, 0); } // .................................................................................... // Node - private JArithmeticInterpreter(int Operator, double Value, JArithmeticInterpreter Fg, JArithmeticInterpreter Fd) { + private JArithmeticInterpreter(int Operator, double Value) { mOperator = Operator; mValue = Value; - fg = Fg; - fd = Fd; - } - - private JArithmeticInterpreter(int Operator, double Value) { - this(Operator, Value, null, null); } // .................................................................................... // Construct_Tree - private static JArithmeticInterpreter constructTree(StringBuffer string, int length, int error) { + private static JArithmeticInterpreter constructTree(StringBuffer string, int length) { int imbric, Bimbric; int priorite, ope; int position, positionv, i, j; @@ -108,7 +102,6 @@ public class JArithmeticInterpreter { // Initialisation des variables if (length <= 0) { - error = 3; return null; } @@ -180,7 +173,6 @@ public class JArithmeticInterpreter { i++; } else { - error = 2; // symbole non reconnu return null; } } @@ -202,7 +194,7 @@ public class JArithmeticInterpreter { position = i; caspp = 0; } - else if ((imbric == Bimbric) && (priorite >= 1)) { + else if (imbric == Bimbric) { priorite = 1; ope = 1; position = i; @@ -238,7 +230,7 @@ public class JArithmeticInterpreter { caspp = 0; } } - else if ((imbric == Bimbric) && (priorite >= 1)) if ((i - 1) < 0) { + else if (imbric == Bimbric) if ((i - 1) < 0) { if (priorite > 5) { priorite = 1; position = i; @@ -308,12 +300,10 @@ public class JArithmeticInterpreter { i++; break; default: - error = 2; // symbole non reconnu return null; } if (imbric != 0) { - error = 1; // erreur de "parenthesage" return null; } @@ -330,46 +320,42 @@ public class JArithmeticInterpreter { node.fd = new JArithmeticInterpreter(); if ((length - position - 1 - Bimbric) == 0) { // argument absent - error = 3; return null; } StringBuffer temp = CopyPartialString(string, (position + 1), (length - 1 - Bimbric)); - node.fd = constructTree(temp, (length - position - 1 - Bimbric), error); + node.fd = constructTree(temp, (length - position - 1 - Bimbric)); return node; } else if (priorite == 5) { - node = new JArithmeticInterpreter(0, calc_const(string, positionv), null, null); + node = new JArithmeticInterpreter(0, calc_const(string, positionv)); return node; } else if (ope > 5) { - node = new JArithmeticInterpreter(ope, 0, null, null); + node = new JArithmeticInterpreter(ope, 0); if ((length - position - espa - Bimbric) == 0) { // argument absent - error = 3; return null; } StringBuffer temp = CopyPartialString(string, (position + espa), (length - 1)); - node.fg = constructTree(temp, (length - position - espa - Bimbric), error); + node.fg = constructTree(temp, (length - position - espa - Bimbric)); return node; } else { - node = new JArithmeticInterpreter(ope, 0, null, null); + node = new JArithmeticInterpreter(ope, 0); if ((position - Bimbric) == 0) { // argument absent - error = 3; return null; } StringBuffer temp = CopyPartialString(string, Bimbric, (position - 1)); - node.fg = constructTree(temp, (position - Bimbric), error); + node.fg = constructTree(temp, (position - Bimbric)); if ((length - position - 1 - Bimbric) == 0) { // argument absent - error = 3; return null; } temp = CopyPartialString(string, (position + 1), (length - 1 - Bimbric)); - node.fd = constructTree(temp, (length - position - 1 - Bimbric), error); + node.fd = constructTree(temp, (length - position - 1 - Bimbric)); return node; } } @@ -378,15 +364,12 @@ public class JArithmeticInterpreter { private double computeTree() { if (mOperator == 0) return mValue; - int error = 0; double valueL = fg.computeTree(); - if (error != 0) return 0; double valueR = 0; if (fd != null) valueR = fd.computeTree(); - if (error != 0) return 0; switch (mOperator) { case 1: // + @@ -397,7 +380,6 @@ public class JArithmeticInterpreter { return (valueL * valueR); case 4: // - if (valueR == 0) { - error = 1; return 0; } return (valueL / valueR); @@ -407,23 +389,16 @@ public class JArithmeticInterpreter { return Math.exp(valueL); case 7: // ln if (valueL <= 0) { - if (valueL < 0) error = 2; - else - error = 1; return 0; } return (Math.log(valueL) / Math.log(2)); case 8: // log if (valueL <= 0) { - if (valueL < 0) error = 2; - else - error = 1; return 0; } return Math.log(valueL); case 9: // sqrt if (valueL < 0) { - error = 2; return 0; } return Math.sqrt(valueL); @@ -462,8 +437,7 @@ public class JArithmeticInterpreter { fd.writeTree(string); break; case 2: - if ((fg.mOperator == 0) && (fg.mValue == 0)) ; - else + if (fg.mOperator != 0 || fg.mValue != 0) fg.writeTree(string); string.append('-'); if ((fd.mOperator == 1) || (fd.mOperator == 2)) { @@ -471,7 +445,7 @@ public class JArithmeticInterpreter { string.append('('); } fd.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); break; case 3: if ((fg.mOperator == 1) || (fg.mOperator == 2)) { @@ -479,7 +453,7 @@ public class JArithmeticInterpreter { string.append('('); } fg.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); parenthese = false; string.append('*'); if ((fd.mOperator == 1) || (fd.mOperator == 2)) { @@ -487,7 +461,7 @@ public class JArithmeticInterpreter { string.append('('); } fd.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); break; case 4: if ((fg.mOperator == 1) || (fg.mOperator == 2)) { @@ -495,7 +469,7 @@ public class JArithmeticInterpreter { string.append('('); } fg.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); parenthese = false; string.append('/'); if ((fd.mOperator > 0) && (fd.mOperator < 5)) { @@ -503,7 +477,7 @@ public class JArithmeticInterpreter { string.append('('); } fd.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); break; case 5: if ((fg.mOperator > 0) && (fg.mOperator < 5)) { @@ -511,7 +485,7 @@ public class JArithmeticInterpreter { string.append('('); } fg.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); parenthese = false; string.append('^'); if ((fd.mOperator > 0) && (fd.mOperator < 5)) { @@ -519,7 +493,7 @@ public class JArithmeticInterpreter { string.append('('); } fd.writeTree(string); - if (parenthese == true) string.append(')'); + if (parenthese) string.append(')'); break; case 6: string.append("exp("); @@ -732,8 +706,8 @@ public class JArithmeticInterpreter { JArithmeticInterpreter jai = null; try { - jai = JArithmeticInterpreter.constructTree(input, input.length(), 0); - } catch (Exception e) {} + jai = JArithmeticInterpreter.constructTree(input, input.length()); + } catch (Exception ignored) {} if (jai == null) throw new IllegalArgumentException("Le calcul passé en paramètre est invalide"); @@ -749,7 +723,7 @@ public class JArithmeticInterpreter { return getResultFromExpression(expr, null); } - public static void main(String args[]) { + public static void main(String[] args) { StringBuffer b = new StringBuffer(0); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/Json.java b/Core/src/main/java/fr/pandacube/lib/core/util/Json.java index 1cef161..d3237d1 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/Json.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/Json.java @@ -18,9 +18,9 @@ import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; public class Json { - public static final Gson gson = build(b -> b); - public static final Gson gsonPrettyPrinting = build(b -> b.setPrettyPrinting()); - public static final Gson gsonSerializeNulls = build(b -> b.serializeNulls()); + public static final Gson gson = build(Function.identity()); + public static final Gson gsonPrettyPrinting = build(GsonBuilder::setPrettyPrinting); + public static final Gson gsonSerializeNulls = build(GsonBuilder::serializeNulls); public static final Gson gsonSerializeNullsPrettyPrinting = build(b -> b.serializeNulls().setPrettyPrinting()); @@ -46,9 +46,9 @@ public class Json { } private static class RecordTypeAdapter extends TypeAdapter { - private Gson gson; - private TypeAdapterFactory factory; - private TypeToken type; + private final Gson gson; + private final TypeAdapterFactory factory; + private final TypeToken type; public RecordTypeAdapter(Gson gson, TypeAdapterFactory factory, TypeToken type) { this.gson = gson; @@ -72,8 +72,8 @@ public class Json { RecordComponent[] recordComponents = clazz.getRecordComponents(); Map> typeMap = new HashMap<>(); - for (int i = 0; i < recordComponents.length; i++) { - typeMap.put(recordComponents[i].getName(), TypeToken.get(recordComponents[i].getGenericType())); + for (RecordComponent recordComponent : recordComponents) { + typeMap.put(recordComponent.getName(), TypeToken.get(recordComponent.getGenericType())); } var argsMap = new HashMap(); reader.beginObject(); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/LazyOrException.java b/Core/src/main/java/fr/pandacube/lib/core/util/LazyOrException.java index e9bf467..b699809 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/LazyOrException.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/LazyOrException.java @@ -2,6 +2,8 @@ package fr.pandacube.lib.core.util; import java.util.Objects; +import fr.pandacube.lib.core.util.ThrowableUtil.SupplierException; + /** * Represents a lazy loaded value. * @@ -42,11 +44,5 @@ public class LazyOrException { cachedValue = value; cached = true; } - - - @FunctionalInterface - public interface SupplierException { - public T get() throws Exception; - } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/LevenshteinDistance.java b/Core/src/main/java/fr/pandacube/lib/core/util/LevenshteinDistance.java index 17fef77..a69c9bc 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/LevenshteinDistance.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/LevenshteinDistance.java @@ -5,12 +5,12 @@ import java.util.function.ToIntBiFunction; public class LevenshteinDistance { - private String initialList; + private final String initialList; - private int elementAdditionScore; - private int elementDeletionScore; + private final int elementAdditionScore; + private final int elementDeletionScore; - private ToIntBiFunction elementDistanceFunction; + private final ToIntBiFunction elementDistanceFunction; private int[] prev, curr; diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/Log.java b/Core/src/main/java/fr/pandacube/lib/core/util/Log.java index 73556ed..d84d91a 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/Log.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/Log.java @@ -7,7 +7,7 @@ import java.util.logging.Logger; public class Log { private static Logger logger = Logger.getGlobal(); - private static AtomicBoolean logDebug = new AtomicBoolean(false); + private static final AtomicBoolean logDebug = new AtomicBoolean(false); public static void setDebug(boolean newVal) { logDebug.set(newVal); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/MappedListView.java b/Core/src/main/java/fr/pandacube/lib/core/util/MappedListView.java index b143f83..34d08af 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/MappedListView.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/MappedListView.java @@ -1,12 +1,23 @@ package fr.pandacube.lib.core.util; import java.util.AbstractList; +import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.function.Function; +/** + * A Wrapper list that provides a mapped view of the backend list. + * Every modification of this list will modify the bakend list. + * For each time a value is accessed or modified, the appropriate + * setter or getter is used to convert the value between the source {@code S} + * and the visible {@code T} type. + * @param the source (backend) type + * @param the visible (mapped) type + */ public class MappedListView extends AbstractList { - private final List backend; + protected final List backend; private final Function getter; private final Function setter; @@ -28,25 +39,149 @@ public class MappedListView extends AbstractList { @Override - public int size() { return backend.size(); } + public int size() { + return backend.size(); + } + @Override - public T get(int index) { return getter.apply(backend.get(index)); } + public T get(int index) { + return getter.apply(backend.get(index)); + } + @Override - public T set(int index, T element) { return getter.apply(backend.set(index, setter.apply(element))); } + public T set(int index, T element) { + return getter.apply(backend.set(index, setter.apply(element))); + } + @Override - public void add(int index, T element) { backend.add(index, setter.apply(element)); } + public boolean add(T element) { + return backend.add(setter.apply(element)); + } + @Override - public T remove(int index) { return getter.apply(backend.remove(index)); } + public void add(int index, T element) { + backend.add(index, setter.apply(element)); + } + @Override - public void clear() { backend.clear(); } + public T remove(int index) { + return getter.apply(backend.remove(index)); + } + + @Override + public void clear() { + backend.clear(); + } + @Override public List subList(int fromIndex, int toIndex) { - return new MappedListView(backend.subList(fromIndex, toIndex), getter, setter); + return new MappedListView<>(backend.subList(fromIndex, toIndex), getter, setter); } + @Override protected void removeRange(int fromIndex, int toIndex) { backend.subList(fromIndex, toIndex).clear(); } + + @Override + public boolean equals(Object o) { + return backend.equals(o); + } + + @Override + public int hashCode() { + return backend.hashCode(); + } + + + @SuppressWarnings("unchecked") + @Override + public int indexOf(Object o) { + return backend.indexOf(setter.apply((T) o)); + } + + @SuppressWarnings("unchecked") + @Override + public int lastIndexOf(Object o) { + return backend.lastIndexOf(setter.apply((T) o)); + } + + @Override + public Iterator iterator() { + return new Iterator<>() { + final Iterator wrappedIt = backend.iterator(); + @Override + public boolean hasNext() { + return wrappedIt.hasNext(); + } + + @Override + public T next() { + return getter.apply(wrappedIt.next()); + } + + @Override + public void remove() { + wrappedIt.remove(); + } + }; + } + + @Override + public ListIterator listIterator() { + return listIterator(0); + } + + @Override + public ListIterator listIterator(int index) { + return new ListIterator<>() { + final ListIterator wrappedIt = backend.listIterator(index); + @Override + public boolean hasNext() { + return wrappedIt.hasNext(); + } + + @Override + public T next() { + return getter.apply(wrappedIt.next()); + } + + @Override + public boolean hasPrevious() { + return wrappedIt.hasPrevious(); + } + + @Override + public T previous() { + return getter.apply(wrappedIt.previous()); + } + + @Override + public int nextIndex() { + return wrappedIt.nextIndex(); + } + + @Override + public int previousIndex() { + return wrappedIt.previousIndex(); + } + + @Override + public void remove() { + wrappedIt.remove(); + } + + @Override + public void set(T w) { + wrappedIt.set(setter.apply(w)); + } + + @Override + public void add(T w) { + wrappedIt.add(setter.apply(w)); + } + }; + } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/MemoryUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/MemoryUtil.java index e17587c..52abf37 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/MemoryUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/MemoryUtil.java @@ -30,7 +30,7 @@ public class MemoryUtil { return unitMultiplier == null ? "o" : (unitMultiplier + (si ? "o" : "io")); } - private MemoryUnit(long vTrad, long vSI, String uMult) { + MemoryUnit(long vTrad, long vSI, String uMult) { valueTrad = vTrad; valueSI = vSI; unitMultiplier = uMult; diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftVersion.java b/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftVersion.java index 987692c..d157b2c 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftVersion.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftVersion.java @@ -59,7 +59,7 @@ public enum MinecraftVersion { v1_19(759, "1.19"); // IMPORTANT: don't forget to update the versionMergeDisplay value when adding a new version; - private static Map, List> versionMergeDisplay; + private static final Map, List> versionMergeDisplay; static { versionMergeDisplay = new HashMap<>(); @@ -157,7 +157,7 @@ public enum MinecraftVersion { public final int id; public final List versionDisplay; - private MinecraftVersion(int v, String... d) { + MinecraftVersion(int v, String... d) { id = v; versionDisplay = Arrays.asList(d); } @@ -193,7 +193,7 @@ public enum MinecraftVersion { } - public static final List getVersionsDisplayList(List vList) { + public static List getVersionsDisplayList(List vList) { if (vList == null) return new ArrayList<>(); Set vSet = EnumSet.copyOf(vList); diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftWebUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftWebUtil.java index 1a10f06..5e03417 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftWebUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/MinecraftWebUtil.java @@ -7,8 +7,6 @@ public class MinecraftWebUtil { /** Convert a legacy Minecraft color coded String into HTML Format. - - @param */ // TODO update to support RGB colors (Bungee and Essentials notation). // See JavaScript implementation at https://www.pandacube.fr/assets/js/global.js @@ -16,7 +14,7 @@ public class MinecraftWebUtil { { String color_char = "0123456789abcdefr"; - String builder = ""; + StringBuilder builder = new StringBuilder(); char currentColor = 'r'; boolean bold = false, italic = false, underlined = false, strikethrough = false; @@ -26,67 +24,67 @@ public class MinecraftWebUtil { if (c == code_prefix && (i"); bold = false; } if (italic) { - builder += ""; + builder.append(""); italic = false; } if (underlined) { - builder += ""; + builder.append(""); underlined = false; } if (strikethrough) { - builder += ""; + builder.append(""); strikethrough = false; } if (Character.toLowerCase(c) != currentColor) { if (currentColor != 'r') - builder += ""; + builder.append(""); if (Character.toLowerCase(c) != 'r') - builder += ""; + builder.append(""); currentColor = Character.toLowerCase(c); } } else if (Character.toLowerCase(c) == 'l') { if (!bold) { - builder += ""; + builder.append(""); bold = true; } } else if (Character.toLowerCase(c) == 'm') { if (!strikethrough) { - builder += ""; + builder.append(""); strikethrough = true; } } else if (Character.toLowerCase(c) == 'n') { if (!underlined) { - builder += ""; + builder.append(""); underlined = true; } } else if (Character.toLowerCase(c) == 'o') { if (!italic) { - builder += ""; + builder.append(""); italic = true; } } else { - builder += code_prefix + c; + builder.append(code_prefix + c); } } else - builder += c; + builder.append(c); } - return builder; + return builder.toString(); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/OfflineUUID.java b/Core/src/main/java/fr/pandacube/lib/core/util/OfflineUUID.java index 91852da..d515d0d 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/OfflineUUID.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/OfflineUUID.java @@ -1,6 +1,6 @@ package fr.pandacube.lib.core.util; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Objects; import java.util.Scanner; import java.util.UUID; @@ -8,7 +8,7 @@ import java.util.UUID; public class OfflineUUID { public static UUID getFromNickName(String nickname) { - byte[] from_str = ("OfflinePlayer:" + nickname).getBytes(Charset.forName("UTF-8")); + byte[] from_str = ("OfflinePlayer:" + nickname).getBytes(StandardCharsets.UTF_8); return UUID.nameUUIDFromBytes(from_str); } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java index 7dd5552..f1283e1 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/RandomUtil.java @@ -6,7 +6,7 @@ import java.util.Set; public class RandomUtil { - public static Random rand = new Random(); + public static final Random rand = new Random(); public static int nextIntBetween(int minInclu, int maxExclu) { return rand.nextInt(maxExclu - minInclu) + minInclu; @@ -32,8 +32,8 @@ public class RandomUtil { * Returns a random value from a set. * * May not be optimized (Actually O(n) ) - * @param arr - * @return + * @param set the Set from which to pick a random value + * @return a random value from the set */ public static T setElement(Set set) { if (set.isEmpty()) @@ -51,26 +51,26 @@ public class RandomUtil { * Return a value between 0 and the number of parameter minus 1, using the provided frequencies. * * The probability of each value to be returned depends of the frequencies provided. - * @param frequencies the frequencies of each entries + * @param f the frequencies of each entries * @return the index of an entry, or -1 if it is unable to pick anything (all the frequencies are 0 or there is not provided frequency) */ - public static int randomIndexOfFrequencies(double... frequencies) { - if (frequencies == null) - return -1; + public static int randomIndexOfFrequencies(double... f) { + if (f == null) + throw new IllegalArgumentException("f cannot be null"); + int n = f.length; + double[] fSums = new double[n]; double sum = 0; - for (double f : frequencies) - sum += f; - if (sum == 0) - return -1; - double r = rand.nextDouble() * sum; - int i = -1; - double limit = frequencies[++i]; - while (i < frequencies.length) { - if (r < limit) - return i; - limit += frequencies[++i]; + for (int i = 0; i < n; i++) { + if (f[i] < 0) + throw new IllegalArgumentException("f[" + i + "] cannot be negative."); + fSums[i] = (sum += f[i]); } - return frequencies.length - 1; + double r = rand.nextDouble() * sum; + for (int i = 0; i < n; i++) { + if (fSums[i] > r) + return i; + } + return n - 1; } @@ -81,7 +81,7 @@ public class RandomUtil { public static final String PASSWORD_CHARSET_LATIN_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"; public static final String PASSWORD_CHARSET_LATIN_UPPERCASE = PASSWORD_CHARSET_LATIN_LOWERCASE.toUpperCase(); public static final String PASSWORD_CHARSET_DIGIT = "0123456789"; - public static final String PASSWORD_CHARSET_SPECIAL = "@#+*/-;:,.?!='()[]{}&\"\\"; + public static final String PASSWORD_CHARSET_SPECIAL = "@#+*/-;:,.?!='()[]{}&"; public static final String PASSWORD_CHARSET_NO_ANBIGUITY = "abcdefghkmnpqrstwxyzACDEFGHKLMNPQRSTWXYZ2345679"; public static String randomPassword(int length) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/Reflect.java b/Core/src/main/java/fr/pandacube/lib/core/util/Reflect.java index 52dcabf..f80d862 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/Reflect.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/Reflect.java @@ -4,6 +4,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; @@ -73,7 +74,7 @@ public class Reflect { } @Override public boolean equals(Object other) { - return other != null && other instanceof MethodIdentifier o + return other instanceof MethodIdentifier o && o.methodName.equals(methodName) && Arrays.equals(o.parameters, parameters); } @@ -89,7 +90,7 @@ public class Reflect { } @Override public boolean equals(Object other) { - return other != null && other instanceof ConstructorIdentifier o + return other instanceof ConstructorIdentifier o && Arrays.equals(o.parameters, parameters); } @Override @@ -196,10 +197,10 @@ public class Reflect { public static abstract class ReflectMember { - ReflectClass reflectClass; - protected ID identifier; + protected final ReflectClass reflectClass; + protected final ID identifier; - protected EL cached; + protected final EL cached; protected ReflectMember(ReflectClass c, ID id, boolean bypassFilter) throws EX { reflectClass = c; @@ -210,78 +211,71 @@ public class Reflect { protected EL fetch() throws EX { - EX ex = null; // get element in current class try { EL el = fetchFromClass(reflectClass.clazz); setAccessible(el); return el; - } catch (ReflectiveOperationException e) { + } catch (ReflectiveOperationException e1) { @SuppressWarnings("unchecked") - EX ee = (EX) e; - ex = ee; + EX ex = (EX) e1; + + // get parent class + Class superClass = reflectClass.clazz.getSuperclass(); + if (superClass == null) + throw ex; + + // get element in parent class (will do recursion) + try { + EL el = fetchFromReflectClass(ofClass(superClass)); + setAccessible(el); + return el; + } catch (ReflectiveOperationException e2) { + ex.addSuppressed(e2); + throw ex; + } } - - // get parent class - Class superClass = reflectClass.clazz.getSuperclass(); - if (superClass == null) - throw ex; - - // get element in parent class (will do recursion) - try { - EL el = fetchFromReflectClass(ofClass(superClass)); - setAccessible(el); - return el; - } catch (ReflectiveOperationException e) { - ex.addSuppressed(e); - throw ex; - } - } protected EL fetchFiltered() throws EX { - EX ex = null; // get element in current class try { EL el = fetchFromClass(reflectClass.clazz); setAccessible(el); return el; - } catch (ReflectiveOperationException e) { + } catch (ReflectiveOperationException e1) { @SuppressWarnings("unchecked") - EX ee = (EX) e; - ex = ee; + EX ex = (EX) e1; + + // trying to bypass filtered member + try { + @SuppressWarnings("unchecked") + EL[] elements = (EL[]) Reflect.ofClassOfInstance(reflectClass.clazz) + .method(internalMethodNameElementArray(), boolean.class) + .invoke(reflectClass.clazz, false); + for (EL element : elements) { + if (isEqualOurElement(element)) { + // the values in the elements array have to be copied + // (using special private methods in reflection api) before using it + Object reflectionFactoryOfClazz = Reflect.ofClassOfInstance(reflectClass.clazz) + .method("getReflectionFactory") + .invoke(reflectClass.clazz); + @SuppressWarnings("unchecked") + EL copiedElement = (EL) Reflect.ofClassOfInstance(reflectionFactoryOfClazz) + .method(internalMethodNameCopyElement(), element.getClass()) + .invoke(reflectionFactoryOfClazz, element); + setAccessible(copiedElement); + return copiedElement; + } + } + } catch (ReflectiveOperationException e2) { + ex.addSuppressed(e2); + } + + throw ex; } - - // trying to bypass filtered member - try { - @SuppressWarnings("unchecked") - EL[] elements = (EL[]) Reflect.ofClassOfInstance(reflectClass.clazz) - .method(internalMethodNameElementArray(), boolean.class) - .invoke(reflectClass.clazz, false); - for (EL element : elements) { - if (isEqualOurElement(element)) { - // the values in the elements array have to be copied - // (using special private methods in reflection api) before using it - Object reflectionFactoryOfClazz = Reflect.ofClassOfInstance(reflectClass.clazz) - .method("getReflectionFactory") - .invoke(reflectClass.clazz); - @SuppressWarnings("unchecked") - EL copiedElement = (EL) Reflect.ofClassOfInstance(reflectionFactoryOfClazz) - .method(internalMethodNameCopyElement(), element.getClass()) - .invoke(reflectionFactoryOfClazz, element); - EL el = copiedElement; - setAccessible(el); - return el; - } - } - } catch (ReflectiveOperationException e) { - ex.addSuppressed(e); - } - - throw ex; - } protected abstract EL fetchFromClass(Class clazz) throws EX; @@ -311,11 +305,11 @@ public class Reflect { super(c, name, bypassFilter); } - @Override protected Field fetchFromClass(Class clazz) throws NoSuchFieldException { return clazz.getDeclaredField(identifier); }; - @Override protected Field fetchFromReflectClass(ReflectClass rc) throws NoSuchFieldException { return rc.field(identifier).get(); }; - @Override protected boolean isEqualOurElement(Field el) { return identifier.equals(el.getName()); }; - @Override protected String internalMethodNameElementArray() { return "getDeclaredFields0"; }; - @Override protected String internalMethodNameCopyElement() { return "copyField"; }; + @Override protected Field fetchFromClass(Class clazz) throws NoSuchFieldException { return clazz.getDeclaredField(identifier); } + @Override protected Field fetchFromReflectClass(ReflectClass rc) throws NoSuchFieldException { return rc.field(identifier).get(); } + @Override protected boolean isEqualOurElement(Field el) { return identifier.equals(el.getName()); } + @Override protected String internalMethodNameElementArray() { return "getDeclaredFields0"; } + @Override protected String internalMethodNameCopyElement() { return "copyField"; } @Override protected void setAccessible(Field el) { el.setAccessible(true); } @@ -379,11 +373,11 @@ public class Reflect { super(c, methodId, bypassFilter); } - @Override protected Method fetchFromClass(Class clazz) throws NoSuchMethodException { return clazz.getDeclaredMethod(identifier.methodName, identifier.parameters); }; - @Override protected Method fetchFromReflectClass(ReflectClass rc) throws NoSuchMethodException { return rc.method(identifier, false).get(); }; - @Override protected boolean isEqualOurElement(Method el) { return identifier.methodName.equals(el.getName()) && Arrays.equals(identifier.parameters, el.getParameterTypes()); }; - @Override protected String internalMethodNameElementArray() { return "getDeclaredMethods0"; }; - @Override protected String internalMethodNameCopyElement() { return "copyMethod"; }; + @Override protected Method fetchFromClass(Class clazz) throws NoSuchMethodException { return clazz.getDeclaredMethod(identifier.methodName, identifier.parameters); } + @Override protected Method fetchFromReflectClass(ReflectClass rc) throws NoSuchMethodException { return rc.method(identifier, false).get(); } + @Override protected boolean isEqualOurElement(Method el) { return identifier.methodName.equals(el.getName()) && Arrays.equals(identifier.parameters, el.getParameterTypes()); } + @Override protected String internalMethodNameElementArray() { return "getDeclaredMethods0"; } + @Override protected String internalMethodNameCopyElement() { return "copyMethod"; } @Override protected void setAccessible(Method el) { el.setAccessible(true); } public Object invoke(Object instance, Object... values) throws ReflectiveOperationException { @@ -415,17 +409,16 @@ public class Reflect { // Override since we don't want to recursively search for a constructor @Override protected Constructor fetch() throws NoSuchMethodException { - Constructor el = null; - el = fetchFromClass(reflectClass.clazz); + Constructor el = fetchFromClass(reflectClass.clazz); setAccessible(el); return el; } - @Override protected Constructor fetchFromClass(Class clazz) throws NoSuchMethodException { return clazz.getDeclaredConstructor(identifier.parameters); }; - @Override protected Constructor fetchFromReflectClass(ReflectClass rc) throws NoSuchMethodException { throw new UnsupportedOperationException(); }; - @Override protected boolean isEqualOurElement(Constructor el) { return Arrays.equals(identifier.parameters, el.getParameterTypes()); }; - @Override protected String internalMethodNameElementArray() { return "getDeclaredConstructors0"; }; - @Override protected String internalMethodNameCopyElement() { return "copyConstructor"; }; + @Override protected Constructor fetchFromClass(Class clazz) throws NoSuchMethodException { return clazz.getDeclaredConstructor(identifier.parameters); } + @Override protected Constructor fetchFromReflectClass(ReflectClass rc) { throw new UnsupportedOperationException(); } + @Override protected boolean isEqualOurElement(Constructor el) { return Arrays.equals(identifier.parameters, el.getParameterTypes()); } + @Override protected String internalMethodNameElementArray() { return "getDeclaredConstructors0"; } + @Override protected String internalMethodNameCopyElement() { return "copyConstructor"; } @Override protected void setAccessible(Constructor el) { el.setAccessible(true); } public T instanciate(Object... values) throws ReflectiveOperationException { @@ -453,7 +446,7 @@ public class Reflect { - private static Cache, List>> subClassesLists = CacheBuilder.newBuilder() + private static final Cache, List>> subClassesLists = CacheBuilder.newBuilder() .expireAfterAccess(10, TimeUnit.MINUTES) .build(); @@ -468,7 +461,7 @@ public class Reflect { return classes; } catch(ExecutionException e) { Log.severe(e); - return null; + return new ArrayList<>(); } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/ServerPropertyFile.java b/Core/src/main/java/fr/pandacube/lib/core/util/ServerPropertyFile.java index 360a4ea..fa5dfe3 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/ServerPropertyFile.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/ServerPropertyFile.java @@ -6,12 +6,13 @@ import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; +import java.util.Objects; import com.google.gson.JsonSyntaxException; public class ServerPropertyFile { - private transient File file; + private final transient File file; private String name = "default_name"; private String memory = "512M"; @@ -22,8 +23,7 @@ public class ServerPropertyFile { private boolean run = true; public ServerPropertyFile(File f) { - if (f == null) throw new IllegalArgumentException("f ne doit pas être null"); - file = f; + file = Objects.requireNonNull(f, "f"); } @@ -101,7 +101,7 @@ public class ServerPropertyFile { } public void setMemory(String m) { - if (m == null || !m.matches("^[0-9]+[mgMG]$")) throw new IllegalArgumentException(); + if (m == null || !m.matches("^\\d+[mgMG]$")) throw new IllegalArgumentException(); memory = m; } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/ThrowableUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/ThrowableUtil.java index bbde9c9..2095c35 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/ThrowableUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/ThrowableUtil.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.InvocationTargetException; +import java.nio.charset.StandardCharsets; public class ThrowableUtil { @@ -11,11 +12,11 @@ public class ThrowableUtil { public static String stacktraceToString(Throwable t) { if (t == null) return null; try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { - try (PrintStream ps = new PrintStream(os, false, "UTF-8")) { + try (PrintStream ps = new PrintStream(os, false, StandardCharsets.UTF_8)) { t.printStackTrace(ps); ps.flush(); } - return os.toString("UTF-8"); + return os.toString(StandardCharsets.UTF_8); } catch (IOException e) { return null; } @@ -90,7 +91,7 @@ public class ThrowableUtil { */ @FunctionalInterface public interface SupplierException { - public T get() throws Exception; + T get() throws Exception; } @@ -100,7 +101,7 @@ public class ThrowableUtil { */ @FunctionalInterface public interface RunnableException { - public void run() throws Exception; + void run() throws Exception; } @@ -135,10 +136,6 @@ public class ThrowableUtil { er = new InstantiationError(); er.initCause(ce); } - else if (t instanceof IllegalAccessException ce) { - er = new IllegalAccessError(); - er.initCause(ce); - } if (er != null) throw er; diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/Tick.java b/Core/src/main/java/fr/pandacube/lib/core/util/Tick.java index 544c1ab..9b1593e 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/Tick.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/Tick.java @@ -14,7 +14,7 @@ public class Tick { /** * Returns the number of tick is the provided duration, in second - * @param seconds the duration in second + * @param minutes the duration in minutes * @return the same duration as provided, but in Minecraft server ticks */ public static long min(long minutes) { diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java b/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java index c336ff3..dcf77d7 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/TimeUtil.java @@ -1,5 +1,7 @@ package fr.pandacube.lib.core.util; +import fr.pandacube.lib.core.commands.SuggestionsSupplier; + import java.time.Instant; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; @@ -7,10 +9,10 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; +import java.util.Comparator; import java.util.GregorianCalendar; import java.util.List; import java.util.Locale; -import java.util.Objects; import java.util.TimeZone; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; @@ -19,21 +21,19 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; -import fr.pandacube.lib.core.commands.SuggestionsSupplier; - public class TimeUtil { - private static DateTimeFormatter cmpDayOfWeekFormatter = DateTimeFormatter.ofPattern("EEE", Locale.getDefault()); - private static DateTimeFormatter dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.getDefault()); - private static DateTimeFormatter dayOfMonthFormatter = DateTimeFormatter.ofPattern("d", Locale.getDefault()); - private static DateTimeFormatter cmpMonthFormatter = DateTimeFormatter.ofPattern("MMM", Locale.getDefault()); - private static DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", Locale.getDefault()); - private static DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("uuuu", Locale.getDefault()); + private static final DateTimeFormatter cmpDayOfWeekFormatter = DateTimeFormatter.ofPattern("EEE", Locale.getDefault()); + private static final DateTimeFormatter dayOfWeekFormatter = DateTimeFormatter.ofPattern("EEEE", Locale.getDefault()); + private static final DateTimeFormatter dayOfMonthFormatter = DateTimeFormatter.ofPattern("d", Locale.getDefault()); + private static final DateTimeFormatter cmpMonthFormatter = DateTimeFormatter.ofPattern("MMM", Locale.getDefault()); + private static final DateTimeFormatter monthFormatter = DateTimeFormatter.ofPattern("MMMM", Locale.getDefault()); + private static final DateTimeFormatter yearFormatter = DateTimeFormatter.ofPattern("uuuu", Locale.getDefault()); - private static DateTimeFormatter HMSFormatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault()); - private static DateTimeFormatter HMFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()); - private static DateTimeFormatter HFormatter = DateTimeFormatter.ofPattern("H'h'", Locale.getDefault()); + private static final DateTimeFormatter HMSFormatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault()); + private static final DateTimeFormatter HMFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()); + private static final DateTimeFormatter HFormatter = DateTimeFormatter.ofPattern("H'h'", Locale.getDefault()); public static String relativeDateFr(long displayTime, boolean showSeconds, boolean compactWords) { @@ -67,7 +67,7 @@ public class TimeUtil { if (timeDiffSec > -60*2) // dans 2 min return compactWords ? "dans 1min" : "dans une minute"; if (timeDiffSec > -3600) // dans moins d’1h - return "dans " + (int)Math.floor((-timeDiffSec)/60) + (compactWords ? "min" : " minutes"); + return "dans " + (-timeDiffSec/60) + (compactWords ? "min" : " minutes"); } if (relPrecision.morePreciseOrEqTo(RelativePrecision.HOURS)) { if (timeDiffSec > -3600) // dans moins d’1h @@ -75,7 +75,7 @@ public class TimeUtil { if (timeDiffSec > -3600*2) // dans moins de 2h return compactWords ? "dans 1h" : "dans une heure"; if (timeDiffSec > -3600*12) // dans moins de 12h - return "dans " + (int)Math.floor((-timeDiffSec)/3600) + (compactWords ? "h" : " heures"); + return "dans " + (-timeDiffSec/3600) + (compactWords ? "h" : " heures"); } if (relPrecision.morePreciseOrEqTo(RelativePrecision.DAYS)) { LocalDateTime nextMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0).plusDays(1); @@ -88,9 +88,7 @@ public class TimeUtil { + dayOfMonthFormatter.format(displayDateTime) + " à " + dayTimeFr(displayTime, dispPrecision); } - - return fullDateFr(displayTime, dispPrecision, true, compactWords); - + } else { // present and past @@ -107,7 +105,7 @@ public class TimeUtil { if (timeDiffSec < 60*2) // ya moins de 2 min return compactWords ? "il y a 1min" : "il y a une minute"; if (timeDiffSec < 3600) // ya moins d'1h - return "il y a " + (int)Math.floor(timeDiffSec/60) + (compactWords ? "min" : " minutes"); + return "il y a " + (timeDiffSec/60) + (compactWords ? "min" : " minutes"); } if (relPrecision.morePreciseOrEqTo(RelativePrecision.HOURS)) { if (timeDiffSec < 3600) // ya moins d'1h @@ -115,7 +113,7 @@ public class TimeUtil { if (timeDiffSec < 3600*2) // ya moins de 2h return "il y a une heure"; if (timeDiffSec < 3600*12) // ya moins de 12h - return "il y a " + (int)Math.floor(timeDiffSec/3600) + " heures"; + return "il y a " + (timeDiffSec/3600) + " heures"; } if (relPrecision.morePreciseOrEqTo(RelativePrecision.DAYS)) { LocalDateTime lastMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0); @@ -127,11 +125,10 @@ public class TimeUtil { return (compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " dernier à " + dayTimeFr(displayTime, dispPrecision); } - - return fullDateFr(displayTime, dispPrecision, true, compactWords); - + } - + return fullDateFr(displayTime, dispPrecision, true, compactWords); + } @@ -207,7 +204,7 @@ public class TimeUtil { String ret = Arrays.stream(TimeUnit.values()) .sequential() .filter(u -> u.compareTo(fLUnit) >= 0 && u.compareTo(fHUnit) <= 0) - .sorted((u1, u2) -> u2.compareTo(u1)) + .sorted(Comparator.reverseOrder()) .filter(u -> { if (u.convert(remainingTime.get(), TimeUnit.MILLISECONDS) == 0 && !oneDisplayed.get()) return false; @@ -226,40 +223,23 @@ public class TimeUtil { public static String timeUnitToSuffix(TimeUnit u, boolean fr) { - switch (u) { - case DAYS: - return fr ? "j" : "d"; - case HOURS: - return "h"; - case MINUTES: - return "m"; - case SECONDS: - return "s"; - case MILLISECONDS: - return "ms"; - case MICROSECONDS: - return "μs"; - case NANOSECONDS: - return "ns"; - default: - throw new IllegalArgumentException("Invalid TimeUnit: " + Objects.toString(u)); - } + return switch (u) { + case DAYS -> fr ? "j" : "d"; + case HOURS -> "h"; + case MINUTES -> "m"; + case SECONDS -> "s"; + case MILLISECONDS -> "ms"; + case MICROSECONDS -> "μs"; + case NANOSECONDS -> "ns"; + }; } public static int timeUnitToLeftPadLength(TimeUnit u) { - switch (u) { - case NANOSECONDS: - case MICROSECONDS: - case MILLISECONDS: - return 3; - case SECONDS: - case MINUTES: - case HOURS: - return 2; - case DAYS: - default: - return 1; - } + return switch (u) { + case NANOSECONDS, MICROSECONDS, MILLISECONDS -> 3; + case SECONDS, MINUTES, HOURS -> 2; + case DAYS -> 1; + }; } public static String toString(long value, int leftPad) { @@ -279,7 +259,6 @@ public class TimeUtil { * Equivalent to {@link #durationToLongString(long, TimeUnit, TimeUnit, boolean, boolean, boolean) TimeUnit.durationToLongString(msDuration, TimeUnit.DAYS, milliseconds ? TimeUnit.MILLISECONDS : TimeUnit.SECONDS, true, true, false)} * @param msDuration the duration in ms * @param milliseconds if the milliseconds are displayed or not - * @return */ public static String durationToString(long msDuration, boolean milliseconds) { return durationToLongString(msDuration, TimeUnit.DAYS, milliseconds ? TimeUnit.MILLISECONDS : TimeUnit.SECONDS, true, true, false); @@ -287,8 +266,7 @@ public class TimeUtil { /** * Equivalent to {@link #durationToLongString(long, TimeUnit, TimeUnit, boolean, boolean, boolean) TimeUnit.durationToLongString(msDuration, TimeUnit.DAYS, TimeUnit.SECONDS, true, true, false)} - * @param msDuration - * @return + * @param msDuration the duration in ms */ public static String durationToString(long msDuration) { return durationToLongString(msDuration, TimeUnit.DAYS, TimeUnit.SECONDS, true, true, false); @@ -296,8 +274,7 @@ public class TimeUtil { /** * Equivalent to {@link #durationToLongString(long, TimeUnit, TimeUnit, boolean, boolean, boolean) TimeUnit.durationToLongString(msDuration, TimeUnit.DAYS, TimeUnit.SECONDS, false, false, false)} - * @param msDuration - * @return + * @param msDuration the duration in ms */ public static String durationToParsableString(long msDuration) { return durationToLongString(msDuration, TimeUnit.DAYS, TimeUnit.SECONDS, false, false, false); @@ -306,9 +283,10 @@ public class TimeUtil { /** - * @see {@link com.earth2me.essentials.utils.DateUtil#parseDuration(String, boolean)} + * @see Essentials DateUtil#parseDuration(String, boolean) */ public static long parseDuration(String time, boolean future) throws Exception { + @SuppressWarnings("RegExpSimplifiable") Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?" @@ -367,17 +345,13 @@ public class TimeUtil { List remainingSuffixes = new ArrayList<>(allSuffixes); char[] tokenChars = token.toCharArray(); String accSuffix = ""; - for (int i = 0; i < tokenChars.length; i++) { - char c = tokenChars[i]; + for (char c : tokenChars) { if (Character.isDigit(c)) { scanAndRemovePastSuffixes(remainingSuffixes, accSuffix); accSuffix = ""; - continue; - } - else if (Character.isLetter(c)) { + } else if (Character.isLetter(c)) { accSuffix += c; - } - else + } else return Collections.emptyList(); } String prefixToken = token.substring(0, token.length() - accSuffix.length()); @@ -388,14 +362,12 @@ public class TimeUtil { }; } - private static List allSuffixes = Arrays.asList("y", "mo", "w", "d", "h", "m", "s"); - private static List emptyTokenSuggestions = allSuffixes.stream().map(p -> "1" + p).collect(Collectors.toList()); + private static final List allSuffixes = Arrays.asList("y", "mo", "w", "d", "h", "m", "s"); + private static final List emptyTokenSuggestions = allSuffixes.stream().map(p -> "1" + p).collect(Collectors.toList()); private static void scanAndRemovePastSuffixes(List suffixes, String foundSuffix) { for (int i = 0; i < suffixes.size(); i++) { if (foundSuffix.startsWith(suffixes.get(i))) { - for (int j = i; j >= 0; j--) { - suffixes.remove(j); - } + suffixes.subList(0, i + 1).clear(); return; } } diff --git a/Core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java b/Core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java index e93cce6..25237dd 100644 --- a/Core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java +++ b/Core/src/main/java/fr/pandacube/lib/core/util/TypeConverter.java @@ -116,9 +116,7 @@ public class TypeConverter { * @param o the object to convert to good type * @param mapIntKeys if the String key representing an int should be duplicated as integer type, * which map to the same value as the original String key. For example, if a key is "12" and map - * to the object o, an integer key 12 will be added and map to the same object - * o - * @return + * to the object o, an integer key 12 will be added and map to the same object o */ @SuppressWarnings("unchecked") public static Map toMap(Object o, boolean mapIntKeys) { @@ -139,7 +137,7 @@ public class TypeConverter { try { int intKey = Integer.parseInt((String)entry.getKey()); newEntries.put(intKey, entry.getValue()); - } catch (NumberFormatException e) { } + } catch (NumberFormatException ignored) { } } } if (!newEntries.isEmpty()) { @@ -150,8 +148,7 @@ public class TypeConverter { return currMap; } - if (o instanceof List) { - List list = (List) o; + if (o instanceof List list) { Map map = new HashMap<>(); for(int i = 0; i < list.size(); i++) { map.put(Integer.toString(i), list.get(i)); @@ -200,7 +197,6 @@ public class TypeConverter { public static class ConvertionException extends RuntimeException { - private static final long serialVersionUID = 1L; public ConvertionException(String m) { super(m); diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java index 390fc51..6d7c125 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIHotBar.java @@ -30,13 +30,13 @@ import fr.pandacube.lib.paper.util.BukkitEvent; */ public class GUIHotBar implements Listener { - private Map> itemsAndSetters = new HashMap<>(); + private final Map> itemsAndSetters = new HashMap<>(); - private Map> itemsAndRunnables = new HashMap<>(); + private final Map> itemsAndRunnables = new HashMap<>(); private final int defltSlot; - private List currentPlayers = new ArrayList<>(); + private final List currentPlayers = new ArrayList<>(); public GUIHotBar(int defaultSlot) { defltSlot = Math.max(0, Math.min(8, defaultSlot)); @@ -50,7 +50,6 @@ public class GUIHotBar implements Listener { * @param i the item stack * @param setter code executed to put the item in the inventory. Additionally check for permission before doing the addition. * @param run the Runnable to run when the user right click on the item in the hotbar. - * @return */ public GUIHotBar addItem(ItemStack i, BiConsumer setter, Consumer run) { itemsAndSetters.put(i, setter); @@ -65,8 +64,7 @@ public class GUIHotBar implements Listener { /** * Add the hotbar elements to this player. * - * The players is automatically removed when it quit. You can remove it before by calling {@link #removePlayer(Player)}. - * @param p + * The players is automatically removed when they quit. You can remove it before by calling {@link #removePlayer(Player)}. */ public void addPlayer(Player p) { if (!currentPlayers.contains(p)) @@ -81,7 +79,6 @@ public class GUIHotBar implements Listener { /** * Detach this player from this hotbar manager and removes the managed items from the players inventory. - * @param p */ public void removePlayer(Player p) { if (!currentPlayers.contains(p)) @@ -134,7 +131,7 @@ public class GUIHotBar implements Listener { ItemStack item = event.getItemDrop().getItemStack(); for (ItemStack managed : itemsAndSetters.keySet()) { - if (item != null && item.isSimilar(managed)) { + if (item.isSimilar(managed)) { event.setCancelled(true); return; } @@ -173,12 +170,10 @@ public class GUIHotBar implements Listener { @EventHandler public void onInventoryClick(InventoryClickEvent event) { - if (event.getClickedInventory() == null || !(event.getClickedInventory() instanceof PlayerInventory)) + if (event.getClickedInventory() == null || !(event.getClickedInventory() instanceof PlayerInventory inv)) return; - - PlayerInventory inv = (PlayerInventory) event.getClickedInventory(); - - if (!currentPlayers.contains(inv.getHolder())) + + if (!currentPlayers.contains((Player) inv.getHolder())) return; ItemStack item = event.getCurrentItem(); diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java index 19c5686..227fba2 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/gui/GUIInventory.java @@ -30,11 +30,11 @@ public class GUIInventory implements Listener { public static final Map FAKE_ENCHANT = ImmutableMap.of(Enchantment.DURABILITY, 1); - private Player player; - private Inventory inv; + private final Player player; + private final Inventory inv; private Consumer onCloseEvent; private boolean isOpened = false; - private Map> onClickEvents; + private final Map> onClickEvents; public GUIInventory(Player p, int nbLines, Chat title, Consumer closeEventAction, Plugin pl) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java index 1fc74c2..773486b 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/NMSReflect.java @@ -139,9 +139,8 @@ public class NMSReflect { /** * @param mojName the binary name of the desired class, on the mojang mapping. * @throws NullPointerException if there is no mapping for the provided Mojang mapped class. - * @throws ClassNotFoundException if there is a mapping, but the runtime class was not found. */ - public static ClassMapping mojClass(String mojName) throws ClassNotFoundException { + public static ClassMapping mojClass(String mojName) { return Objects.requireNonNull(CLASSES_BY_MOJ.get(mojName), "Unable to find the Mojang mapped class '" + mojName + "'"); } @@ -339,7 +338,6 @@ public class NMSReflect { * @param mojParametersType the list of parameters of the method. * Each parameter type must be an instance of one of the following type: * {@link Type}, {@link Class}, {@link ReflectClass} or {@link ClassMapping}. - * @return * @throws IllegalArgumentException if one of the parameter has an invalid type * @throws NullPointerException if one of the parameter is null, or if there is no mapping for the provided Mojang mapped method. * @throws ClassNotFoundException if there is no runtime class to represent one of the provided parametersType. @@ -368,7 +366,6 @@ public class NMSReflect { /** * * @param mojName the Mojang mapped name of the field. - * @return * @throws NullPointerException if there is no mapping for the provided Mojang mapped field. * @throws NoSuchFieldException if there is no runtime method to represent the provided method. */ @@ -393,9 +390,7 @@ public class NMSReflect { String classToPrint = isObfClass ? obfName : mojName; String classSimpleName = classToPrint.substring(classToPrint.lastIndexOf('.') + 1); String htmlTitle = classSimpleName.equals(classToPrint) ? "" : (" title='" + classToPrint + "'"); - String typeHTML = "" + classSimpleName + ""; - - return typeHTML; + return "" + classSimpleName + ""; } @@ -420,7 +415,7 @@ public class NMSReflect { String classToPrint = obf ? obfName : mojName; int packageSep = classToPrint.lastIndexOf('.'); String classSimpleName = classToPrint.substring(packageSep + 1); - String classPackages = classToPrint.substring(0, packageSep > 0 ? packageSep : 0); + String classPackages = classToPrint.substring(0, Math.max(packageSep, 0)); String classHTML = (packageSep >= 0 ? (classPackages + ".") : "") + "" + classSimpleName + ""; Type superClass = superClass(obf); @@ -487,7 +482,7 @@ public class NMSReflect { - private static record MethodId(String name, List parametersType) implements Comparable { + private record MethodId(String name, List parametersType) implements Comparable { @Override public int compareTo(MethodId o) { int cmp = name.compareTo(o.name); @@ -497,14 +492,13 @@ public class NMSReflect { } private String toHTML(boolean isObfClass, boolean isStatic, boolean isFinal) { - String paramsHTML = parametersType.stream().map(p -> p.toHTML(isObfClass)).collect(Collectors.joining(", ")); - String cl = "mtd"; - if (isStatic) - cl += " st"; - if (isFinal) - cl += " fn"; - String identifierHTML = "" + name + "(" + paramsHTML + ")"; - return identifierHTML; + String paramsHTML = parametersType.stream().map(p -> p.toHTML(isObfClass)).collect(Collectors.joining(", ")); + String cl = "mtd"; + if (isStatic) + cl += " st"; + if (isFinal) + cl += " fn"; + return "" + name + "(" + paramsHTML + ")"; } public String toString() { @@ -516,7 +510,7 @@ public class NMSReflect { - private static record MemberDesc>(I identifier, Type returnType) { + private record MemberDesc>(I identifier, Type returnType) { private String toHTML(boolean isObfClass, boolean isStatic, boolean isFinal) { String identifierHTML = ""; if (identifier instanceof MethodId mId) @@ -541,7 +535,7 @@ public class NMSReflect { List paramsType = new ArrayList<>(); - while ((r = (char) descReader.read()) != ')') { + while (((char) descReader.read()) != ')') { descReader.skip(-1); paramsType.add(Type.parse(descReader)); } @@ -565,8 +559,8 @@ public class NMSReflect { private static abstract class MemberMapping, R extends ReflectMember> { - private String htmlTypeChar; - /* package */ MemberDesc obfDesc, mojDesc; + private final String htmlTypeChar; + /* package */ final MemberDesc obfDesc, mojDesc; /* package */ ClassMapping declaringClass; private MemberMapping(String htmlType, MemberDesc obfDesc, MemberDesc mojDesc) { htmlTypeChar = htmlType; diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java index 868595b..65538d2 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/Type.java @@ -141,14 +141,11 @@ public class Type implements Comparable { /* package */ static Type parse(StringReader desc) { try { - StringBuilder sbRaw = new StringBuilder(); int arrayDepth = 0; char c; while ((c = (char) desc.read()) == '[') { - sbRaw.append(c); arrayDepth++; } - sbRaw.append(c); String type = switch(c) { case 'Z' -> "boolean"; case 'B' -> "byte"; @@ -162,10 +159,8 @@ public class Type implements Comparable { StringBuilder sbClass = new StringBuilder(); char r; while ((r = (char) desc.read()) != ';') { - sbRaw.append(c); sbClass.append(r); } - sbRaw.append(c); yield NMSReflect.binaryClassName(sbClass.toString()); } default -> "void"; diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectListWrapper.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectListWrapper.java index b9ace12..5947f4f 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectListWrapper.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectListWrapper.java @@ -1,18 +1,13 @@ package fr.pandacube.lib.paper.reflect.wrapper; -import java.util.AbstractList; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; -import java.util.ListIterator; -import java.util.Objects; import java.util.function.Supplier; -import static fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapper.unwrap; +import fr.pandacube.lib.core.util.MappedListView; -public class ReflectListWrapper extends AbstractList implements ReflectWrapperTypedI> { +public class ReflectListWrapper extends MappedListView implements ReflectWrapperTypedI> { - private final List wrappedList; private final Class expectedWrapperClass; /* package */ ReflectListWrapper(Class expectedWrapperClass) { @@ -25,159 +20,23 @@ public class ReflectListWrapper extends AbstractList< this((List) (listCreator == null ? new ArrayList<>() : listCreator.get()), expectedWrapperClass); } /* package */ ReflectListWrapper(List wrappedList, Class expectedWrapperClass) { - this.wrappedList = Objects.requireNonNull(wrappedList); + super(wrappedList, el -> ReflectWrapper.wrap(el, expectedWrapperClass), ReflectWrapper::unwrap); this.expectedWrapperClass = expectedWrapperClass; } @SuppressWarnings("unchecked") @Override public Class> __getRuntimeClass() { - return (Class>) wrappedList.getClass(); + return (Class>) backend.getClass(); } @Override public List __getRuntimeInstance() { - return wrappedList; - } - - private W wrap(Object el) { - return ReflectWrapper.wrap(el, expectedWrapperClass); - } - - @Override - public W get(int index) { - return wrap(wrappedList.get(index)); - } - - @Override - public int size() { - return wrappedList.size(); - } - - @Override - public boolean add(W w) { - return wrappedList.add(unwrap(w)); - } - - @Override - public W set(int index, W element) { - return wrap(wrappedList.set(index, unwrap(element))); - } - - @Override - public void add(int index, W element) { - wrappedList.add(index, unwrap(element)); - } - - @Override - public W remove(int index) { - return wrap(wrappedList.remove(index)); - } - - @Override - public int indexOf(Object o) { - return wrappedList.indexOf(o instanceof ReflectWrapperI w ? w.__getRuntimeInstance() : o); - } - - @Override - public int lastIndexOf(Object o) { - return wrappedList.lastIndexOf(o instanceof ReflectWrapperI w ? w.__getRuntimeInstance() : o); - } - - @Override - public void clear() { - wrappedList.clear(); - } - - @Override - public Iterator iterator() { - return new Iterator() { - final Iterator wrappedIt = wrappedList.iterator(); - @Override - public boolean hasNext() { - return wrappedIt.hasNext(); - } - - @Override - public W next() { - return wrap(wrappedIt.next()); - } - - @Override - public void remove() { - wrappedIt.remove(); - } - }; - } - - @Override - public ListIterator listIterator() { - return listIterator(0); - } - - @Override - public ListIterator listIterator(int index) { - return new ListIterator() { - final ListIterator wrappedIt = wrappedList.listIterator(index); - @Override - public boolean hasNext() { - return wrappedIt.hasNext(); - } - - @Override - public W next() { - return wrap(wrappedIt.next()); - } - - @Override - public boolean hasPrevious() { - return wrappedIt.hasPrevious(); - } - - @Override - public W previous() { - return wrap(wrappedIt.previous()); - } - - @Override - public int nextIndex() { - return wrappedIt.nextIndex(); - } - - @Override - public int previousIndex() { - return wrappedIt.previousIndex(); - } - - @Override - public void remove() { - wrappedIt.remove(); - } - - @Override - public void set(W w) { - wrappedIt.set(unwrap(w)); - } - - @Override - public void add(W w) { - wrappedIt.add(unwrap(w)); - } - }; + return backend; } @Override public List subList(int fromIndex, int toIndex) { - return new ReflectListWrapper<>(wrappedList.subList(fromIndex, toIndex), expectedWrapperClass); - } - - @Override - public boolean equals(Object o) { - return wrappedList.equals(o); - } - - @Override - public int hashCode() { - return wrappedList.hashCode(); + return new ReflectListWrapper<>(backend.subList(fromIndex, toIndex), expectedWrapperClass); } } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectWrapper.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectWrapper.java index 6ccf23a..dbb1417 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectWrapper.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/ReflectWrapper.java @@ -25,6 +25,9 @@ public abstract class ReflectWrapper implements ReflectWrapperI { public static W wrap(Object runtimeObj) { return wrap(runtimeObj, null); } + public static > W wrapTyped(T runtimeObj, Class expectedWrapperClass) { + return wrap(runtimeObj, expectedWrapperClass); + } public static W wrap(Object runtimeObj, Class expectedWrapperClass) { if (runtimeObj == null) return null; diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/WrapperRegistry.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/WrapperRegistry.java index 86978f8..bd60933 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/WrapperRegistry.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/WrapperRegistry.java @@ -214,23 +214,10 @@ public class WrapperRegistry { } - - - - - - private static class RegistryEntry { - Class runtimeClass; - Class wrapperClass; - Class concreteWrapperClass; - ReflectConstructor objectWrapperConstructor; - - public RegistryEntry(Class runtimeClass, Class wrapperClass, Class concreteWrapperClass, ReflectConstructor objectWrapperConstructor) { - this.runtimeClass = runtimeClass; - this.wrapperClass = wrapperClass; - this.concreteWrapperClass = concreteWrapperClass; - this.objectWrapperConstructor = objectWrapperConstructor; - } + private record RegistryEntry(Class runtimeClass, + Class wrapperClass, + Class concreteWrapperClass, + ReflectConstructor objectWrapperConstructor) { } } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/brigadier/CommandNode.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/brigadier/CommandNode.java index 280524a..971f2a5 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/brigadier/CommandNode.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/brigadier/CommandNode.java @@ -1,7 +1,6 @@ package fr.pandacube.lib.paper.reflect.wrapper.brigadier; import fr.pandacube.lib.core.util.Reflect; -import fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapper; import fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapperTyped; import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/WorldVersion.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/WorldVersion.java index 89dffff..461f359 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/WorldVersion.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/WorldVersion.java @@ -10,11 +10,11 @@ import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; @ConcreteWrapper(WorldVersion.__concrete.class) public interface WorldVersion extends ReflectWrapperI { - public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.WorldVersion")); + ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.WorldVersion")); - public static class __concrete extends ReflectWrapper implements WorldVersion { + class __concrete extends ReflectWrapper implements WorldVersion { private __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/BlockPosArgument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/BlockPosArgument.java index 4bcc06b..db5a752 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/BlockPosArgument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/BlockPosArgument.java @@ -12,8 +12,9 @@ public class BlockPosArgument extends ReflectWrapperTyped> { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.BlockPosArgument")); private static final Reflect.ReflectMethod blockPos = wrapEx(() -> MAPPING.mojMethod("blockPos")); - public static ArgumentType blockPos() { - return (ArgumentType) wrapReflectEx(() -> blockPos.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType blockPos() { + return (ArgumentType) wrapReflectEx(() -> blockPos.invokeStatic()); } protected BlockPosArgument(Object obj) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ComponentArgument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ComponentArgument.java index 8801109..4fbb75d 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ComponentArgument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ComponentArgument.java @@ -12,8 +12,9 @@ public class ComponentArgument extends ReflectWrapperTyped> { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.ComponentArgument")); private static final Reflect.ReflectMethod textComponent = wrapEx(() -> MAPPING.mojMethod("textComponent")); - public static ArgumentType textComponent() { - return (ArgumentType) wrapReflectEx(() -> textComponent.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType textComponent() { + return (ArgumentType) wrapReflectEx(() -> textComponent.invokeStatic()); } protected ComponentArgument(Object obj) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java index 77dad3d..8c31f42 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Coordinates.java @@ -15,19 +15,19 @@ import static fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapper.wrap; @ConcreteWrapper(Coordinates.__concrete.class) public interface Coordinates extends ReflectWrapperI { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Coordinates")); - public static final Reflect.ReflectMethod getPosition = wrapEx(() -> MAPPING.mojMethod("getPosition", CommandSourceStack.MAPPING)); - public static final Reflect.ReflectMethod getBlockPos = wrapEx(() -> MAPPING.mojMethod("getBlockPos", CommandSourceStack.MAPPING)); + NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Coordinates")); + Reflect.ReflectMethod getPosition = wrapEx(() -> MAPPING.mojMethod("getPosition", CommandSourceStack.MAPPING)); + Reflect.ReflectMethod getBlockPos = wrapEx(() -> MAPPING.mojMethod("getBlockPos", CommandSourceStack.MAPPING)); - public default Vec3 getPosition(BukkitBrigadierCommandSource source) { + default Vec3 getPosition(BukkitBrigadierCommandSource source) { return wrap(wrapReflectEx(() -> getPosition.invoke(__getRuntimeInstance(), source)), Vec3.class); } - public default BlockPos getBlockPos(BukkitBrigadierCommandSource source) { + default BlockPos getBlockPos(BukkitBrigadierCommandSource source) { return wrap(wrapReflectEx(() -> getBlockPos.invoke(__getRuntimeInstance(), source)), BlockPos.class); } - static class __concrete extends ReflectWrapper implements Coordinates { + class __concrete extends ReflectWrapper implements Coordinates { protected __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/EntityArgument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/EntityArgument.java index b73c05c..987fff5 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/EntityArgument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/EntityArgument.java @@ -15,20 +15,24 @@ public class EntityArgument extends ReflectWrapperTyped> { private static final Reflect.ReflectMethod player = wrapEx(() -> MAPPING.mojMethod("player")); private static final Reflect.ReflectMethod players = wrapEx(() -> MAPPING.mojMethod("players")); - public static ArgumentType entity() { - return (ArgumentType) wrapReflectEx(() -> entity.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType entity() { + return (ArgumentType) wrapReflectEx(() -> entity.invokeStatic()); } - public static ArgumentType entities() { - return (ArgumentType) wrapReflectEx(() -> entities.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType entities() { + return (ArgumentType) wrapReflectEx(() -> entities.invokeStatic()); } - public static ArgumentType player() { - return (ArgumentType) wrapReflectEx(() -> player.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType player() { + return (ArgumentType) wrapReflectEx(() -> player.invokeStatic()); } - public static ArgumentType players() { - return (ArgumentType) wrapReflectEx(() -> players.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType players() { + return (ArgumentType) wrapReflectEx(() -> players.invokeStatic()); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/GameProfileArgument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/GameProfileArgument.java index 6a4ff1a..5e5bc30 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/GameProfileArgument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/GameProfileArgument.java @@ -12,8 +12,9 @@ public class GameProfileArgument extends ReflectWrapperTyped> { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.GameProfileArgument")); private static final Reflect.ReflectMethod gameProfile = wrapEx(() -> MAPPING.mojMethod("gameProfile")); - public static ArgumentType gameProfile() { - return (ArgumentType) wrapReflectEx(() -> gameProfile.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType gameProfile() { + return (ArgumentType) wrapReflectEx(() -> gameProfile.invokeStatic()); } protected GameProfileArgument(Object obj) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ResourceLocationArgument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ResourceLocationArgument.java index e202675..4099173 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ResourceLocationArgument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/ResourceLocationArgument.java @@ -12,8 +12,9 @@ public class ResourceLocationArgument extends ReflectWrapperTyped NMSReflect.mojClass("net.minecraft.commands.arguments.ResourceLocationArgument")); private static final Reflect.ReflectMethod id = wrapEx(() -> MAPPING.mojMethod("id")); - public static ArgumentType id() { - return (ArgumentType) wrapReflectEx(() -> id.invokeStatic()); + @SuppressWarnings("unchecked") + public static ArgumentType id() { + return (ArgumentType) wrapReflectEx(() -> id.invokeStatic()); } protected ResourceLocationArgument(Object obj) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Vec3Argument.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Vec3Argument.java index 270ce8b..447b8fd 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Vec3Argument.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/commands/Vec3Argument.java @@ -12,8 +12,9 @@ public class Vec3Argument extends ReflectWrapperTyped> { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.commands.arguments.coordinates.Vec3Argument")); private static final Reflect.ReflectMethod vec3 = wrapEx(() -> MAPPING.mojMethod("vec3", boolean.class)); - public static ArgumentType vec3(boolean centerIntegers) { - return (ArgumentType) wrapReflectEx(() -> vec3.invokeStatic(centerIntegers)); + @SuppressWarnings("unchecked") + public static ArgumentType vec3(boolean centerIntegers) { + return (ArgumentType) wrapReflectEx(() -> vec3.invokeStatic(centerIntegers)); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/core/BlockPos.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/core/BlockPos.java index 9c053a3..3a368b4 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/core/BlockPos.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/core/BlockPos.java @@ -1,8 +1,6 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.core; -import fr.pandacube.lib.core.util.Reflect; import fr.pandacube.lib.paper.reflect.NMSReflect; -import fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapper; import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/Tag.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/Tag.java index 91ccdf1..35b17a6 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/Tag.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/nbt/Tag.java @@ -12,17 +12,17 @@ import fr.pandacube.lib.paper.reflect.wrapper.ReflectWrapperI; @ConcreteWrapper(Tag.__concrete.class) public interface Tag extends ReflectWrapperI { - public static final ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.Tag")); - public static final ReflectMethod getAsString = wrapEx(() -> MAPPING.mojMethod("getAsString")); + ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.nbt.Tag")); + ReflectMethod getAsString = wrapEx(() -> MAPPING.mojMethod("getAsString")); - public default String getAsString() { + default String getAsString() { return wrapReflectEx(() -> (String) getAsString.invoke(__getRuntimeInstance())); } - public static class __concrete extends ReflectWrapper implements Tag { + class __concrete extends ReflectWrapper implements Tag { private __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/chat/Component.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/chat/Component.java index 204639b..54b5498 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/chat/Component.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/chat/Component.java @@ -9,10 +9,10 @@ import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; @ConcreteWrapper(Component.__concrete.class) public interface Component extends ReflectWrapperI { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.network.chat.Component")); + NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.network.chat.Component")); - public class __concrete extends ReflectWrapper implements Component { + class __concrete extends ReflectWrapper implements Component { protected __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/protocol/Packet.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/protocol/Packet.java index 1b24119..2b9a8a3 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/protocol/Packet.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/network/protocol/Packet.java @@ -9,9 +9,9 @@ import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; @ConcreteWrapper(Packet.__concrete.class) public interface Packet extends ReflectWrapperI { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.network.protocol.Packet")); + NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.network.protocol.Packet")); - public class __concrete extends ReflectWrapper implements Packet { + class __concrete extends ReflectWrapper implements Packet { protected __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java index fe32d78..8b69e69 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/server/ServerPlayer.java @@ -1,5 +1,7 @@ package fr.pandacube.lib.paper.reflect.wrapper.minecraft.server; +import org.bukkit.entity.Player; + import fr.pandacube.lib.core.util.Reflect; import fr.pandacube.lib.paper.reflect.NMSReflect; import fr.pandacube.lib.paper.reflect.wrapper.minecraft.world.DamageSource; @@ -31,6 +33,11 @@ public class ServerPlayer extends Entity { // in NMS, ServerPlayer is not a dire return wrap(wrapReflectEx(() -> connection.getValue(__getRuntimeInstance())), ServerGamePacketListenerImpl.class); } + @Override + public Player getBukkitEntity() { + return (Player) super.getBukkitEntity(); + } + protected ServerPlayer(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/util/ProgressListener.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/util/ProgressListener.java index 6d2c5c8..31b8443 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/util/ProgressListener.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/util/ProgressListener.java @@ -9,10 +9,10 @@ import static fr.pandacube.lib.core.util.ThrowableUtil.wrapEx; @ConcreteWrapper(ProgressListener.__concrete.class) public interface ProgressListener extends ReflectWrapperI { - public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.util.ProgressListener")); + NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.util.ProgressListener")); - public class __concrete extends ReflectWrapper implements ProgressListener { + class __concrete extends ReflectWrapper implements ProgressListener { protected __concrete(Object obj) { super(obj); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/SavedData.java b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/SavedData.java index dab68f5..a7323cb 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/SavedData.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/reflect/wrapper/minecraft/world/SavedData.java @@ -9,13 +9,13 @@ import static fr.pandacube.lib.core.util.ThrowableUtil.wrapReflectEx; public class SavedData extends ReflectWrapper { public static final NMSReflect.ClassMapping MAPPING = wrapEx(() -> NMSReflect.mojClass("net.minecraft.world.level.saveddata.SavedData")); - public static final Reflect.ReflectMethod setDirty = wrapEx(() -> MAPPING.mojMethod("setDirty")); + private static final Reflect.ReflectMethod setDirty = wrapEx(() -> MAPPING.mojMethod("setDirty")); protected SavedData(Object obj) { super(obj); } - public void setDirty(boolean dirty) { - wrapReflectEx(() -> setDirty.invoke(__getRuntimeInstance(), dirty)); + public void setDirty() { + wrapReflectEx(() -> setDirty.invoke(__getRuntimeInstance())); } } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java index 6dd380d..d8e492c 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/scheduler/AutoUpdatedObject.java @@ -11,11 +11,11 @@ import org.bukkit.scheduler.BukkitTask; import fr.pandacube.lib.paper.PandaLibPaper; public class AutoUpdatedObject { - private static Plugin plugin = PandaLibPaper.getPlugin(); + private static final Plugin plugin = PandaLibPaper.getPlugin(); private Runnable updater; - private List tasks = new ArrayList<>(); + private final List tasks = new ArrayList<>(); protected AutoUpdatedObject() { } @@ -52,7 +52,7 @@ public class AutoUpdatedObject { } public synchronized void cancel() { - tasks.forEach(t -> t.cancel()); + tasks.forEach(BukkitTask::cancel); tasks.clear(); } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlock.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlock.java index 0280053..207c4ce 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlock.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlock.java @@ -53,7 +53,7 @@ public class AABBBlock implements Iterable { center = new Vector((p1x_ + p2x_) / 2d, (p1y_ + p2y_) / 2d, (p1z_ + p2z_) / 2d); - volume = Math.abs(p2x_ - p1x_) * Math.abs(p2x_ - p1x_) * Math.abs(p2x_ - p1x_); + volume = (long) Math.abs(p2x_ - p1x_) * Math.abs(p2x_ - p1x_) * Math.abs(p2x_ - p1x_); } public boolean overlaps(Entity e) { @@ -96,11 +96,11 @@ public class AABBBlock implements Iterable { @Override public Iterator iterator() { - return new Iterator() { + return new Iterator<>() { private int x = pos1.getBlockX(), y = pos1.getBlockY(), z = pos1.getBlockZ(); - + @Override public boolean hasNext() { return x < pos2.getBlockX(); @@ -108,7 +108,7 @@ public class AABBBlock implements Iterable { @Override public BlockVector next() { BlockVector bv = new BlockVector(x, y, z); - + z++; if (z >= pos2.getBlockZ()) { y++; @@ -118,7 +118,7 @@ public class AABBBlock implements Iterable { y = pos1.getBlockY(); } } - + return bv; } }; @@ -126,21 +126,16 @@ public class AABBBlock implements Iterable { public Iterable asBlockIterable(World w) { - return new Iterable() { + return () -> new Iterator<>() { + final Iterator nested = AABBBlock.this.iterator(); @Override - public Iterator iterator() { - return new Iterator() { - Iterator nested = AABBBlock.this.iterator(); - @Override - public boolean hasNext() { - return nested.hasNext(); - } - @Override - public Block next() { - BlockVector bv = nested.next(); - return w.getBlockAt(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ()); - } - }; + public boolean hasNext() { + return nested.hasNext(); + } + @Override + public Block next() { + BlockVector bv = nested.next(); + return w.getBlockAt(bv.getBlockX(), bv.getBlockY(), bv.getBlockZ()); } }; } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlockGroup.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlockGroup.java index 052b4c4..2d8cf4a 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlockGroup.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/AABBBlockGroup.java @@ -1,9 +1,6 @@ package fr.pandacube.lib.paper.util; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -20,11 +17,11 @@ public class AABBBlockGroup implements Iterable { public final List aabbBlocks; public AABBBlockGroup(Collection in) { - aabbBlocks = Collections.unmodifiableList(new ArrayList<>(in)); + aabbBlocks = List.copyOf(in); } public AABBBlockGroup(AABBBlock... in) { - aabbBlocks = Collections.unmodifiableList(Arrays.asList(in)); + aabbBlocks = List.of(in); } @@ -42,7 +39,7 @@ public class AABBBlockGroup implements Iterable { } public Vector getRandomPosition() { - double[] freq = aabbBlocks.stream().mapToDouble(b -> b.getVolume()).toArray(); + double[] freq = aabbBlocks.stream().mapToDouble(AABBBlock::getVolume).toArray(); int i = RandomUtil.randomIndexOfFrequencies(freq); return aabbBlocks.get(i).getRandomPosition(); } @@ -56,7 +53,7 @@ public class AABBBlockGroup implements Iterable { @Override public Iterator iterator() { - return IteratorIterator.ofCollectionOfIterator(aabbBlocks.stream().map(b -> b.iterator()).toList()); + return IteratorIterator.ofCollectionOfIterator(aabbBlocks.stream().map(AABBBlock::iterator).toList()); } } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/AutoUpdatedBossBar.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/AutoUpdatedBossBar.java index e2315ec..fe9f3a7 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/AutoUpdatedBossBar.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/AutoUpdatedBossBar.java @@ -95,17 +95,15 @@ public class AutoUpdatedBossBar implements Listener { public synchronized void followLoginLogout(Predicate condition) { playerCondition = condition; - if (followPlayerList == true) + if (followPlayerList) return; followPlayerList = true; BukkitEvent.register(this); - Bukkit.getServer().getOnlinePlayers().forEach(p -> { - onPlayerJoin(new PlayerJoinEvent(p, Component.text(""))); - }); + Bukkit.getServer().getOnlinePlayers().forEach(p -> onPlayerJoin(new PlayerJoinEvent(p, Component.text("")))); } public synchronized void unfollowPlayerList() { - if (followPlayerList == false) + if (!followPlayerList) return; followPlayerList = false; playerCondition = null; @@ -115,7 +113,7 @@ public class AutoUpdatedBossBar implements Listener { @EventHandler(priority=EventPriority.MONITOR) public synchronized void onPlayerJoin(PlayerJoinEvent event) { - if (followPlayerList == false) + if (!followPlayerList) return; if (playerCondition != null && !playerCondition.test(event.getPlayer())) return; @@ -126,17 +124,13 @@ public class AutoUpdatedBossBar implements Listener { @EventHandler(priority=EventPriority.HIGH) public synchronized void onPlayerQuit(PlayerQuitEvent event) { - if (followPlayerList == false) + if (!followPlayerList) return; synchronized (bar) { event.getPlayer().hideBossBar(bar); } } - - /** - * Utility method to update the progress of the bossbar without unnecessary packet. - * @param title - */ + public void removeAll() { synchronized (bar) { for (Player p : Bukkit.getOnlinePlayers()) @@ -164,14 +158,13 @@ public class AutoUpdatedBossBar implements Listener { @FunctionalInterface public interface BarUpdater { - public void update(AutoUpdatedBossBar bar); + void update(AutoUpdatedBossBar bar); } /** * Utility method to update the title of the bossbar without unnecessary packet. - * @param title */ public void setTitle(Chat title) { synchronized (bar) { @@ -179,14 +172,8 @@ public class AutoUpdatedBossBar implements Listener { } } - @Deprecated - public void setTitle(String title) { - setTitle(Chat.legacyText(title)); - } - /** * Utility method to update the color of the bossbar without unnecessary packet. - * @param title */ public void setColor(Color color) { synchronized (bar) { @@ -196,7 +183,6 @@ public class AutoUpdatedBossBar implements Listener { /** * Utility method to update the progress of the bossbar without unnecessary packet. - * @param title */ public void setProgress(double progress) { synchronized (bar) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitChatColorUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitChatColorUtil.java index 767bc9e..e0c7884 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitChatColorUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitChatColorUtil.java @@ -24,41 +24,24 @@ public class BukkitChatColorUtil { // hmmm this is not that simple, of course // black - switch (dye) { - case BLACK: - return ChatColor.of("#000000"); - case RED: - return ChatColor.of("#650000"); - case GREEN: - return ChatColor.of("#006500"); - case BROWN: - return ChatColor.of("#361B07"); - case BLUE: - return ChatColor.of("#000065"); - case PURPLE: - return ChatColor.of("#3F0C5F"); - case CYAN: - return ChatColor.of("#006565"); - case LIGHT_GRAY: - return ChatColor.of("#535353"); - case GRAY: - return ChatColor.of("#323232"); - case PINK: - return ChatColor.of("#652947"); - case LIME: - return ChatColor.of("#4B6500"); - case YELLOW: - return ChatColor.of("#656500"); - case LIGHT_BLUE: - return ChatColor.of("#3C4B51"); - case MAGENTA: - return ChatColor.of("#650065"); - case ORANGE: - return ChatColor.of("#65280C"); - case WHITE: - return ChatColor.of("#656565"); - } - throw new IllegalArgumentException("Unknown DyeColor: " + dye); + return switch (dye) { + case BLACK -> ChatColor.of("#000000"); + case RED -> ChatColor.of("#650000"); + case GREEN -> ChatColor.of("#006500"); + case BROWN -> ChatColor.of("#361B07"); + case BLUE -> ChatColor.of("#000065"); + case PURPLE -> ChatColor.of("#3F0C5F"); + case CYAN -> ChatColor.of("#006565"); + case LIGHT_GRAY -> ChatColor.of("#535353"); + case GRAY -> ChatColor.of("#323232"); + case PINK -> ChatColor.of("#652947"); + case LIME -> ChatColor.of("#4B6500"); + case YELLOW -> ChatColor.of("#656500"); + case LIGHT_BLUE -> ChatColor.of("#3C4B51"); + case MAGENTA -> ChatColor.of("#650065"); + case ORANGE -> ChatColor.of("#65280C"); + case WHITE -> ChatColor.of("#656565"); + }; } diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitEvent.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitEvent.java index a4fa3fe..e6f1e77 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitEvent.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/BukkitEvent.java @@ -1,9 +1,7 @@ package fr.pandacube.lib.paper.util; -import java.lang.reflect.Method; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; - +import fr.pandacube.lib.core.util.Reflect; +import fr.pandacube.lib.paper.PandaLibPaper; import org.bukkit.Bukkit; import org.bukkit.event.Event; import org.bukkit.event.EventException; @@ -11,12 +9,11 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.plugin.EventExecutor; -import org.bukkit.plugin.IllegalPluginAccessException; import org.bukkit.plugin.RegisteredListener; import org.bukkit.scheduler.BukkitTask; -import fr.pandacube.lib.core.util.Reflect; -import fr.pandacube.lib.paper.PandaLibPaper; +import java.util.List; +import java.util.concurrent.atomic.AtomicReference; public class BukkitEvent { @@ -58,21 +55,19 @@ public class BukkitEvent { // method retrieved from OB.plugin.SimplePluginManager#getEventListeners - public static HandlerList getHandlerList(Class type) throws IllegalPluginAccessException { + public static HandlerList getHandlerList(Class type) { try { - Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList", new Class[0]); - method.setAccessible(true); - return (HandlerList)method.invoke(null, new Object[0]); + return (HandlerList) Reflect.ofClass(getRegistrationClass(type)).method("getHandlerList").invokeStatic(); } - catch (Exception e) { + catch (ReflectiveOperationException e) { return null; } } // method retrieved from OB.plugin.SimplePluginManager - private static Class getRegistrationClass(Class clazz) throws IllegalPluginAccessException { + private static Class getRegistrationClass(Class clazz) { try { - clazz.getDeclaredMethod("getHandlerList", new Class[0]); + clazz.getDeclaredMethod("getHandlerList"); return clazz; } catch (NoSuchMethodException e) { @@ -87,7 +82,7 @@ public class BukkitEvent { public interface EventListener extends Listener, EventExecutor { - public abstract void onEvent(E event); + void onEvent(E event); @SuppressWarnings("unchecked") @Override @@ -123,13 +118,16 @@ public class BukkitEvent { } - private AtomicReference listenerCheckTask = new AtomicReference<>(); + private final AtomicReference listenerCheckTask = new AtomicReference<>(); private void checkIfListenerIsLast() { synchronized (listenerCheckTask) { if (listenerCheckTask.get() != null) return; - RegisteredListener[] listeners = BukkitEvent.getHandlerList(eventClass).getRegisteredListeners(); + HandlerList hList = BukkitEvent.getHandlerList(eventClass); + if (hList == null) + return; + RegisteredListener[] listeners = hList.getRegisteredListeners(); if (listeners[listeners.length - 1].getListener() != this) { listenerCheckTask.set(Bukkit.getScheduler().runTask(PandaLibPaper.getPlugin(), () -> { // need to re-register the event so we are last diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/ColorUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/ColorUtil.java index 2dc8bac..18211dc 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/ColorUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/ColorUtil.java @@ -10,13 +10,12 @@ public class ColorUtil { /* * Rainbow */ - private static List rainbowColors = new ArrayList<>(); + private static final List rainbowColors = new ArrayList<>(); /** * Get a rainbow color * @param variation from 0 (include) to 1 (exclude). * 0 is red, 1/6 is yellow, 2/6 is green, 3/6 is cyan, 4/6 is blue, 5/6 is magenta - * @return */ public static Color getRainbowColor(double variation) { synchronized (rainbowColors) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/ExperienceUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/ExperienceUtil.java index 17962da..1a161cc 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/ExperienceUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/ExperienceUtil.java @@ -4,7 +4,7 @@ import org.bukkit.entity.Player; /** * A utility for managing Player experience properly.
- * https://gist.github.com/Jikoo/30ec040443a4701b8980 + * Github Gist - Jikoo * * @author Jikoo */ @@ -13,7 +13,7 @@ public class ExperienceUtil { /** * Calculates a player's total exp based on level and progress to next. * - * @see http://minecraft.gamepedia.com/Experience#Leveling_up + * @see Experience (#leveling up) - Minecraft Wiki * * @param player the Player * @@ -26,14 +26,7 @@ public class ExperienceUtil { /** * Calculates total experience based on level. * - * @see http://minecraft.gamepedia.com/Experience#Leveling_up - * - * "One can determine how much experience has been collected to reach a - * level using the equations: - * - * Total Experience = [Level]2 + 6[Level] (at levels 0-15) - * 2.5[Level]2 - 40.5[Level] + 360 (at levels 16-30) - * 4.5[Level]2 - 162.5[Level] + 2220 (at level 31+)" + * @see Experience (#leveling up) - Minecraft Wiki * * @param level the level * @@ -60,7 +53,7 @@ public class ExperienceUtil { } /** - * @see http://minecraft.gamepedia.com/Experience#Leveling_up + * @see Experience (#leveling up) - Minecraft Wiki * * "The formulas for figuring out how many experience orbs you need to * get to the next level are as follows: diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java index 8c84d9c..cbcb186 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java @@ -17,7 +17,7 @@ import fr.pandacube.lib.core.util.RandomUtil; public class GameWorldUtils implements Listener { - private static BiMap gameWorld = new BiMap<>(); + private static final BiMap gameWorld = new BiMap<>(); public static World getOrLoadGameWorld(String world, Consumer operationOnLoad) throws IOException { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/GeometryUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/GeometryUtil.java index bf50bd0..9e348ab 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/GeometryUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/GeometryUtil.java @@ -127,11 +127,6 @@ public class GeometryUtil { /** * Check if the path from start location to end pass through * the axis aligned bounding box defined by min and max. - * @param start - * @param end - * @param min - * @param max - * @return */ public static boolean hasIntersection(Vector start, Vector end, Vector min, Vector max) { final double epsilon = 0.0001f; @@ -144,30 +139,30 @@ public class GeometryUtil { ad.setY(Math.abs(ad.getY())); ad.setZ(Math.abs(ad.getZ())); - if(Math.abs(c.getX()) > e.getX() + ad.getX()){ + if (Math.abs(c.getX()) > e.getX() + ad.getX()){ return false; } - if(Math.abs(c.getY()) > e.getY() + ad.getY()){ + if (Math.abs(c.getY()) > e.getY() + ad.getY()){ return false; } - if(Math.abs(c.getZ()) > e.getX() + ad.getZ()){ + if (Math.abs(c.getZ()) > e.getX() + ad.getZ()){ return false; } - if(Math.abs(d.getY() * c.getZ() - d.getZ() * c.getY()) > e.getY() * ad.getZ() + e.getZ() * ad.getY() + epsilon){ + if (Math.abs(d.getY() * c.getZ() - d.getZ() * c.getY()) > e.getY() * ad.getZ() + e.getZ() * ad.getY() + epsilon){ return false; } - if(Math.abs(d.getZ() * c.getX() - d.getX() * c.getZ()) > e.getZ() * ad.getX() + e.getX() * ad.getZ() + epsilon){ + if (Math.abs(d.getZ() * c.getX() - d.getX() * c.getZ()) > e.getZ() * ad.getX() + e.getX() * ad.getZ() + epsilon){ return false; } - - if(Math.abs(d.getX() * c.getY() - d.getY() * c.getX()) > e.getX() * ad.getY() + e.getY() * ad.getX() + epsilon){ + + if (Math.abs(d.getX() * c.getY() - d.getY() * c.getX()) > e.getX() * ad.getY() + e.getY() * ad.getX() + epsilon){ return false; } - + return true; } @@ -243,7 +238,6 @@ public class GeometryUtil { * contained in the provided {@link Location}. * {@link Location#getYaw()} and {@link Location#getPitch()} values are automatically * converted to conform {@link #yaw} and {@link #pitch} specification. - * @param l */ public DirectionalVector(Location l) { this( @@ -322,7 +316,6 @@ public class GeometryUtil { * Set the yaw and the pitch of the provided {@link Location} * with the values inside the current {@link DirectionalVector} * after conversion of these values - * @param l */ public void putIntoLocation(Location l) { /* std : -PI/2 : <0 ? +2PI : MC @@ -352,7 +345,6 @@ public class GeometryUtil { * If the current direction is the player face direction, * this method return the direction of the back of the head. * This is an alias of {@link #getOpposite()} - * @return */ public DirectionalVector getBackDirection() { return getOpposite(); @@ -361,7 +353,6 @@ public class GeometryUtil { /** * If the current direction is the player face direction, * this method return the direction of the bottom of the head. - * @return */ public DirectionalVector getBottomDirection() { return new DirectionalVector( @@ -373,7 +364,6 @@ public class GeometryUtil { /** * If the current direction is the player face direction, * this method return the direction of the top of the head. - * @return */ public DirectionalVector getTopDirection() { return new DirectionalVector( @@ -387,7 +377,6 @@ public class GeometryUtil { /** * If the current direction is the player face direction, * this method return the direction of the left of the head. - * @return */ public DirectionalVector getLeftDirection() { return new DirectionalVector( @@ -401,7 +390,6 @@ public class GeometryUtil { /** * If the current direction is the player face direction, * this method return the direction of the right of the head. - * @return */ public DirectionalVector getRightDirection() { return new DirectionalVector( diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java index a52777a..99b4827 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/ItemStackBuilder.java @@ -52,7 +52,6 @@ public class ItemStackBuilder { * of ItemStack as the parameter of this method. * * To create a builder that doesn’t modify the provided ItemStack, use {@link #of(ItemStack)}. - * @param stack * @return the builder */ public static ItemStackBuilder wrap(ItemStack stack) { @@ -70,7 +69,7 @@ public class ItemStackBuilder { - private ItemStack stack; + private final ItemStack stack; private ItemMeta cachedMeta; private ItemStackBuilder(ItemStack base) { @@ -91,10 +90,7 @@ public class ItemStackBuilder { } public ItemStackBuilder rawDisplayName(Component displayName) { - if (displayName != null) - getOrInitMeta().displayName(displayName); - else - getOrInitMeta().displayName(null); + getOrInitMeta().displayName(displayName); updateMeta(); return this; } @@ -106,7 +102,7 @@ public class ItemStackBuilder { return rawDisplayName(displayName.getAdv()); } else - return rawDisplayName((Component) null); + return rawDisplayName(null); } public ItemStackBuilder rawLore(List lore) { diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/LocationUtil.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/LocationUtil.java index 13dd553..ddee624 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/LocationUtil.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/LocationUtil.java @@ -29,9 +29,8 @@ public class LocationUtil { /** * Return a random secure location in the provided world, inside the current * WorldBorder. Will be on the surface, for non-nether world, or below the roof of the nether world - * @param w - * @param checkCubo true if the returned location can't be in a /cubo - * @return + * @param w the world in which to pick a location + * @param extraSecureCheck provides extra checks to determine location security */ public static CompletableFuture getRandomSecureLocation(World w, Predicate extraSecureCheck) { @@ -51,7 +50,7 @@ public class LocationUtil { private static final int maxTryBeforeCancelRandomLocation = 75; public static CompletableFuture getRandomSecureLocation(World w, Location min, Location max, Predicate extraSecureCheck) { - CompletableFuture future = new CompletableFuture(); + CompletableFuture future = new CompletableFuture<>(); AtomicReference t = new AtomicReference<>(); AtomicInteger count = new AtomicInteger(0); @@ -100,7 +99,7 @@ public class LocationUtil { /** * - * @param l + * @param l the source location * @return a secure location with the same X and Z coordinate as the * provided location, but Y modified to ensure security for player * who will be teleported to this location. @@ -128,7 +127,7 @@ public class LocationUtil { public static boolean isAir(Block b) { return b.getType() == Material.AIR; } public static boolean isSecureFloor(Block b) { return !isAir(b) && !dangerousBlocks.contains(b.getType()); } - public static Set dangerousBlocks = EnumSet.of( + public static final Set dangerousBlocks = EnumSet.of( Material.LAVA, Material.WATER, Material.COBWEB, @@ -141,7 +140,7 @@ public class LocationUtil { Material.END_PORTAL, Material.NETHER_PORTAL, Material.END_GATEWAY - ); + ); @@ -150,9 +149,6 @@ public class LocationUtil { /** * Check if the {@link Location} l is inside the cuboïd formed by the 2 others * Locations min and max. - * @param l - * @param min - * @param max * @return true if l is inside the cuboid min-max */ public static boolean isIn(Location l, Location min, Location max) { @@ -168,8 +164,6 @@ public class LocationUtil { /** * Return a new location based on the linear interpolation between p0 and p1, according to the value c. - * @param p0 - * @param p1 * @param c between 0 and 1. If 0, it returns p0 and if 1, returns p1. Other finite numbers are allowed, but the returned location wont be part of the {@code [p0;p1]} segment. * @return The location, linearly interpolated between p0 and p1 with the value c. The yaw and pitch in the returned location are those of p0. * @throws IllegalArgumentException if the provided locations are not in the same world. diff --git a/Paper/src/main/java/fr/pandacube/lib/paper/util/Skull.java b/Paper/src/main/java/fr/pandacube/lib/paper/util/Skull.java index 4c17944..b11173c 100644 --- a/Paper/src/main/java/fr/pandacube/lib/paper/util/Skull.java +++ b/Paper/src/main/java/fr/pandacube/lib/paper/util/Skull.java @@ -19,8 +19,8 @@ import fr.pandacube.lib.core.chat.Chat; * Represents some special mob heads, also support creating player skulls and custom skulls. * * @author xigsag, SBPrime - * - * @see https://github.com/TigerHix/Hex-Utils/blob/9954159a323d12733b29c287a56980991cee2948/hex/util/Skull.java + * + * @see github.com/TigerHix/Hex-Utils/hex/util/Skull.java */ public enum Skull { @@ -60,9 +60,9 @@ public enum Skull { TNT("MHF_TNT"), DYNAMITE("MHF_TNT2"); - private String name; + private final String name; - private Skull(String mcName) { + Skull(String mcName) { name = mcName; } @@ -102,7 +102,7 @@ public enum Skull { meta.displayName(dispName.getAdv()); if (lore != null) - meta.lore(lore.stream().map(c -> c.getAdv()).collect(Collectors.toList())); + meta.lore(lore.stream().map(Chat::getAdv).collect(Collectors.toList())); itemStack.setItemMeta(meta); return itemStack; @@ -175,7 +175,7 @@ public enum Skull { headMeta.displayName(dispName.getAdv()); if (lore != null) - headMeta.lore(lore.stream().map(c -> c.getAdv()).collect(Collectors.toList())); + headMeta.lore(lore.stream().map(Chat::getAdv).collect(Collectors.toList())); head.setItemMeta(headMeta); diff --git a/pom.xml b/pom.xml index bf59cf4..1b05f7b 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ junit junit - 4.12 + 4.13.2 test @@ -88,6 +88,7 @@ + ${describe} ${maven.build.timestamp}