diff --git a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java index 049e26a0e..a06c43033 100644 --- a/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java +++ b/src/main/java/net/minestom/server/command/builder/CommandDispatcher.java @@ -1,5 +1,7 @@ package net.minestom.server.command.builder; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.arguments.Argument; @@ -12,6 +14,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; /** @@ -22,6 +25,10 @@ public class CommandDispatcher { private final Map commandMap = new HashMap<>(); private final Set commands = new HashSet<>(); + private final Cache cache = CacheBuilder.newBuilder() + .expireAfterWrite(30, TimeUnit.SECONDS) + .build(); + /** * Registers a command, * be aware that registering a command name or alias will override the previous entry. @@ -99,6 +106,14 @@ public class CommandDispatcher { public CommandResult parse(@NotNull String commandString) { commandString = commandString.trim(); + // Verify if the result is cached + { + final CommandResult cachedResult = cache.getIfPresent(commandString); + if (cachedResult != null) { + return cachedResult; + } + } + // Split space final String[] parts = commandString.split(StringUtils.SPACE); final String commandName = parts[0]; @@ -119,6 +134,12 @@ public class CommandDispatcher { result.input = commandString; // Find the used syntax and fill CommandResult#type and CommandResult#parsedCommand findParsedCommand(command, args, result); + + // Cache result + { + this.cache.put(commandString, result); + } + return result; }