mirror of
https://github.com/Minestom/Minestom.git
synced 2024-09-29 23:17:59 +02:00
Fixed CommandProcessor commands, allow to disable writing tracking
This commit is contained in:
parent
aecf0f427a
commit
981d247f02
@ -75,6 +75,11 @@ public class SimpleCommand implements CommandProcessor {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean enableWritingTracking() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] onWrite(String text) {
|
public String[] onWrite(String text) {
|
||||||
return new String[]{"Complete1", "Complete2"};
|
return new String[]{"Complete1", "Complete2"};
|
||||||
|
@ -21,6 +21,7 @@ import net.minestom.server.event.player.PlayerCommandEvent;
|
|||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
import net.minestom.server.utils.ArrayUtils;
|
import net.minestom.server.utils.ArrayUtils;
|
||||||
import net.minestom.server.utils.validate.Check;
|
import net.minestom.server.utils.validate.Check;
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -211,38 +212,44 @@ public class CommandManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<String> simpleCommands = new ArrayList<>();
|
// Pair<CommandName,EnabledTracking>
|
||||||
|
final List<Pair<String, Boolean>> commandsPair = new ArrayList<>();
|
||||||
for (CommandProcessor commandProcessor : commandProcessorMap.values()) {
|
for (CommandProcessor commandProcessor : commandProcessorMap.values()) {
|
||||||
|
final boolean enableTracking = commandProcessor.enableWritingTracking();
|
||||||
// Do not show command if return false
|
// Do not show command if return false
|
||||||
if (!commandProcessor.hasAccess(player))
|
if (!commandProcessor.hasAccess(player))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
simpleCommands.add(commandProcessor.getCommandName());
|
commandsPair.add(Pair.of(commandProcessor.getCommandName(), enableTracking));
|
||||||
final String[] aliases = commandProcessor.getAliases();
|
final String[] aliases = commandProcessor.getAliases();
|
||||||
if (aliases == null || aliases.length == 0)
|
if (aliases == null || aliases.length == 0)
|
||||||
continue;
|
continue;
|
||||||
for (String alias : aliases) {
|
for (String alias : aliases) {
|
||||||
simpleCommands.add(alias);
|
commandsPair.add(Pair.of(alias, enableTracking));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String simpleCommand : simpleCommands) {
|
for (Pair<String, Boolean> pair : commandsPair) {
|
||||||
|
final String name = pair.getLeft();
|
||||||
|
final boolean tracking = pair.getRight();
|
||||||
// Server suggestion (ask_server)
|
// Server suggestion (ask_server)
|
||||||
{
|
{
|
||||||
DeclareCommandsPacket.Node tabNode = new DeclareCommandsPacket.Node();
|
DeclareCommandsPacket.Node tabNode = new DeclareCommandsPacket.Node();
|
||||||
tabNode.flags = getFlag(NodeType.ARGUMENT, true, true, true);
|
tabNode.flags = getFlag(NodeType.ARGUMENT, true, false, tracking);
|
||||||
tabNode.name = "tab_completion";
|
tabNode.name = tracking ? "tab_completion" : "args";
|
||||||
tabNode.parser = "brigadier:string";
|
tabNode.parser = "brigadier:string";
|
||||||
tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase
|
tabNode.properties = packetWriter -> packetWriter.writeVarInt(2); // Greedy phrase
|
||||||
tabNode.children = new int[0];
|
tabNode.children = new int[0];
|
||||||
tabNode.suggestionsType = "minecraft:ask_server";
|
if (tracking) {
|
||||||
|
tabNode.suggestionsType = "minecraft:ask_server";
|
||||||
|
}
|
||||||
|
|
||||||
nodes.add(tabNode);
|
nodes.add(tabNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
|
DeclareCommandsPacket.Node literalNode = new DeclareCommandsPacket.Node();
|
||||||
literalNode.flags = getFlag(NodeType.LITERAL, true, false, false);
|
literalNode.flags = getFlag(NodeType.LITERAL, true, false, false);
|
||||||
literalNode.name = simpleCommand;
|
literalNode.name = name;
|
||||||
literalNode.children = new int[]{nodes.size() - 1};
|
literalNode.children = new int[]{nodes.size() - 1};
|
||||||
|
|
||||||
rootChildren.add(nodes.size());
|
rootChildren.add(nodes.size());
|
||||||
|
@ -44,8 +44,20 @@ public interface CommandProcessor {
|
|||||||
*/
|
*/
|
||||||
boolean hasAccess(Player player);
|
boolean hasAccess(Player player);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disabling it will deactivate the {@link #onWrite(String)} callback
|
||||||
|
* Enabling it will result in a degression of performance
|
||||||
|
*
|
||||||
|
* @return true to enable writing tracking (and server auto completion)
|
||||||
|
*/
|
||||||
|
default boolean enableWritingTracking() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow for tab auto completion, this is called everytime the player press a key in the chat
|
* Allow for tab auto completion, this is called everytime the player press a key in the chat
|
||||||
|
* <p>
|
||||||
|
* WARNING: {@link #enableWritingTracking()} needs to return true, you need to override it by default
|
||||||
*
|
*
|
||||||
* @param text the whole player text
|
* @param text the whole player text
|
||||||
* @return the array containing all the suggestion for the current arg (split " ")
|
* @return the array containing all the suggestion for the current arg (split " ")
|
||||||
|
Loading…
Reference in New Issue
Block a user