From 96e7aff476c7774a7ad378ff3e396a1b3135b1b0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 22 Jun 2021 11:31:36 +0800 Subject: [PATCH] Fixes #292 --- CHANGELOG.md | 4 +++- .../commons/command/dispatcher/Dispatcher.java | 16 +++++++++++++--- .../commons/command/dispatcher/Exceptions.java | 4 ++-- .../command/dispatcher/DispatcherTest.java | 16 +++++++++++++--- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 455ca5f0..d3acef17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This update adds argument type matching and overhauls code generation for the command framework. The Scribe project has also been removed. Underneath the hood, we have streamlined the project's infrastructure and documentation. Documentation is now applied on the master branch -and the staging branch has been removed. The minimum Java version has also been bumped to Java 16 +and the staging branch has been removed. The minimum Java version has also been bumped to Java 16. ### Annotations @@ -59,7 +59,9 @@ Typist. - Change `KnowledgeBookBuilder.of(Material)` to `KnowledgeBookBuilder.of()` - only knowledge books contain a `KnowledgeBookMeta` - Change `SkullBuilder` to `HeadBuilder` - Change `TropicalFishBucketBuilder.of(Material)` to `TropicalFishBucketBuilder.of()` - only tropical fish buckets contain a `TropicalFishBucketMeta` +- Change the length at which a displayed command is trimmed when an error occurs from 10 to 20 - Fix incorrect capitalization of names in `com.karuslabs.commons.item.Head` +- Fix commands not being sent to players after server reload - see #292 - Remove `com.karuslabs.commons.command.aot.*` - Remove methods prefixed with `as` in `com.karuslabs.commons.item.ItemBuilder` - replaced with equivalent methods without prefixes - Remove `LeatherArmourBuilder.of(Material)` - replaced with equivalent methods for specific leather armour items diff --git a/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Dispatcher.java b/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Dispatcher.java index 4590ceb4..f719f13b 100644 --- a/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Dispatcher.java +++ b/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Dispatcher.java @@ -43,6 +43,8 @@ import org.bukkit.event.server.ServerLoadEvent; import org.bukkit.plugin.Plugin; +import static org.bukkit.event.server.ServerLoadEvent.LoadType.STARTUP; + /** * A {@code CommandDispatcher} that facilities the registration of commands between * a Spigot plugin and the server. @@ -134,15 +136,23 @@ public void update() { } /** - * Synchronizes this dispatcher with the server when the server is started or - * reloaded. + * Synchronizes this dispatcher with the server when the server is reloaded or + * restarted. The changes to the server are then sent to all clients if the + * server was reloaded. * * @param event the event that denotes the starting and reloading of the server */ @EventHandler protected void update(ServerLoadEvent event) { dispatcher = server.getCommands().getDispatcher(); - walker.prune(dispatcher.getRoot(), getRoot().getChildren()); + if (event.getType() == STARTUP) { + // We don't have to manually send the changes since the startup event + // is fired before connections to the server are allowed. + walker.prune(dispatcher.getRoot(), getRoot().getChildren()); + + } else { + update(); + } } @Override diff --git a/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Exceptions.java b/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Exceptions.java index 26073dc0..ea11af58 100644 --- a/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Exceptions.java +++ b/commons/src/main/java/com/karuslabs/commons/command/dispatcher/Exceptions.java @@ -87,11 +87,11 @@ static void report(CommandSender sender, CommandSyntaxException exception) { style.withClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, input)) ); - if (index > 10) { + if (index > 20) { text.append("..."); } - text.append(input.substring(Math.max(0, index - 10), index)); + text.append(input.substring(Math.max(0, index - 20), index)); if (index < input.length()) { var error = new TextComponent(input.substring(index)).withStyle(new ChatFormatting[]{ChatFormatting.RED, ChatFormatting.UNDERLINE}); diff --git a/commons/src/test/java/com/karuslabs/commons/command/dispatcher/DispatcherTest.java b/commons/src/test/java/com/karuslabs/commons/command/dispatcher/DispatcherTest.java index 798b3a07..ef82ce19 100644 --- a/commons/src/test/java/com/karuslabs/commons/command/dispatcher/DispatcherTest.java +++ b/commons/src/test/java/com/karuslabs/commons/command/dispatcher/DispatcherTest.java @@ -40,7 +40,11 @@ import org.bukkit.craftbukkit.v1_17_R1.command.CraftCommandMap; import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer; import org.bukkit.craftbukkit.v1_17_R1.scheduler.CraftScheduler; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.event.server.ServerLoadEvent.LoadType; import org.junit.jupiter.api.*; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -118,15 +122,21 @@ void update() { } - @Test - void update_server_reload() { + @ParameterizedTest + @CsvSource({"STARTUP, 0", "RELOAD, 1"}) + void update_server_event(LoadType type, int times) { + var player = mock(CraftPlayer.class); + when(craftserver.getOnlinePlayers()).thenReturn(List.of(player)); + dispatcher.getRoot().addChild(Literal.of("a").build()); dispatcher.dispatcher = null; - dispatcher.update(null); + dispatcher.update(new ServerLoadEvent(type)); assertNotNull(dispatcher.dispatcher.getRoot().getChild("a")); assertSame(internal, dispatcher.dispatcher); + + verify(player, times(times)).updateCommands(); } }