use of net.minecraft.commands.CommandSourceStack in project MinecraftForge by MinecraftForge.
the class ConsoleCommandCompleter method complete.
@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
String buffer = line.line();
boolean prefix;
if (buffer.isEmpty() || buffer.charAt(0) != '/') {
buffer = '/' + buffer;
prefix = false;
} else {
prefix = true;
}
final String input = buffer;
// See NetHandlerPlayServer#processTabComplete
StringReader stringReader = new StringReader(input);
if (stringReader.canRead() && stringReader.peek() == '/')
stringReader.skip();
try {
ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(stringReader, this.server.createCommandSourceStack());
Suggestions tabComplete = this.server.getCommands().getDispatcher().getCompletionSuggestions(results).get();
for (Suggestion suggestion : tabComplete.getList()) {
String completion = suggestion.getText();
if (!completion.isEmpty()) {
boolean hasPrefix = prefix || completion.charAt(0) != '/';
Candidate candidate = new Candidate(hasPrefix ? completion : completion.substring(1));
candidates.add(candidate);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.error("Failed to tab complete", e);
}
}
use of net.minecraft.commands.CommandSourceStack in project MinecraftForge by MinecraftForge.
the class CommandEventTest method onCommand.
@SubscribeEvent
public static void onCommand(CommandEvent event) {
CommandDispatcher<CommandSourceStack> dispatcher = event.getParseResults().getContext().getDispatcher();
List<ParsedCommandNode<CommandSourceStack>> nodes = event.getParseResults().getContext().getNodes();
CommandSourceStack source = event.getParseResults().getContext().getSource();
// test: when the /time command is used with no arguments, automatically add default arguments (/time set day)
if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("time")) {
event.setParseResults(dispatcher.parse("time set day", source));
return;
}
// test: whenever a player uses the /give command, let everyone on the server know
if (nodes.size() > 0 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("give")) {
String msg = source.getTextName() + " used the give command: " + event.getParseResults().getReader().getString();
source.getServer().getPlayerList().getPlayers().forEach(player -> player.sendMessage(new TextComponent(msg), player.getUUID()));
return;
}
// this is annoying so I disabled it
// test: when the /kill command is used with no arguments, throw a custom exception
// if (nodes.size() == 1 && nodes.get(0).getNode() == dispatcher.getRoot().getChild("kill"))
// {
// event.setException(new CommandRuntimeException(new TextComponent("You tried to use the /kill command with no arguments")));
// event.setCanceled(true);
// return;
// }
}
use of net.minecraft.commands.CommandSourceStack in project Tropicraft by Tropicraft.
the class VolcanoGenerator method onServerStarting.
@SubscribeEvent
public static void onServerStarting(FMLServerStartingEvent event) {
// we don't really have a structure but we fake it
CommandDispatcher<CommandSourceStack> dispatcher = event.getServer().getCommands().getDispatcher();
LiteralArgumentBuilder<CommandSourceStack> locate = Commands.literal("locate").requires(source -> source.hasPermission(2));
dispatcher.register(locate.then(Commands.literal(Constants.MODID + ":volcano").executes(ctx -> {
CommandSourceStack source = ctx.getSource();
BlockPos pos = new BlockPos(source.getPosition());
BlockPos volcanoPos = getVolcanoNear(source.getLevel(), pos.getX() >> 4, pos.getZ() >> 4, 100);
if (volcanoPos == null) {
throw new SimpleCommandExceptionType(new TranslatableComponent("commands.locate.failed")).create();
} else {
return LocateCommand.showLocateResult(source, "Volcano", pos, volcanoPos, "commands.locate.success");
}
})));
}
Aggregations