use of net.minecraft.commands.CommandSourceStack in project MinecraftForge by MinecraftForge.
the class DimensionsCommand method register.
static ArgumentBuilder<CommandSourceStack, ?> register() {
return Commands.literal("dimensions").requires(// permission
cs -> cs.hasPermission(0)).executes(ctx -> {
ctx.getSource().sendSuccess(new TranslatableComponent("commands.forge.dimensions.list"), true);
final Registry<DimensionType> reg = ctx.getSource().registryAccess().registryOrThrow(Registry.DIMENSION_TYPE_REGISTRY);
Map<ResourceLocation, List<ResourceLocation>> types = new HashMap<>();
for (ServerLevel dim : ctx.getSource().getServer().getAllLevels()) {
types.computeIfAbsent(reg.getKey(dim.dimensionType()), k -> new ArrayList<>()).add(dim.dimension().location());
}
types.keySet().stream().sorted().forEach(key -> {
ctx.getSource().sendSuccess(new TextComponent(key + ": " + types.get(key).stream().map(ResourceLocation::toString).sorted().collect(Collectors.joining(", "))), false);
});
return 0;
});
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class SpongeCommandDispatcher method getCompletionSuggestions.
@Override
public CompletableFuture<Suggestions> getCompletionSuggestions(final ParseResults<CommandSourceStack> parse, final int cursor) {
final CommandContextBuilder<CommandSourceStack> context = parse.getContext();
// Sponge Start - redirect if this actually represents a non-Brig command
final CommandContextBuilder<CommandSourceStack> child = context.getLastChild();
if (child instanceof SpongeCommandContextBuilder) {
final SpongeCommandContextBuilder spongeChild = (SpongeCommandContextBuilder) child;
if (((SpongeCommandContextBuilder) child).representsNonBrigCommand()) {
// special handling!
final String rawCommand = parse.getReader().getString();
final String[] command = spongeChild.nonBrigCommand();
// we know this will exist.
final CommandMapping mapping = this.commandManager.commandMapping(command[0]).get();
return CommandUtil.createSuggestionsForRawCommand(rawCommand, spongeChild.nonBrigCommand(), spongeChild.cause(), mapping).buildFuture();
}
}
// Sponge End
final SuggestionContext<CommandSourceStack> nodeBeforeCursor = context.findSuggestionContext(cursor);
final CommandNode<CommandSourceStack> parent = nodeBeforeCursor.parent;
final int start = Math.min(nodeBeforeCursor.startPos, cursor);
final String fullInput = parse.getReader().getString();
final String truncatedInput = fullInput.substring(0, cursor);
// Sponge Start: the collection might be different.
final Collection<CommandNode<CommandSourceStack>> children;
if (parent instanceof SpongeNode) {
children = ((SpongeNode) parent).getChildrenForSuggestions();
} else {
children = parent.getChildren();
}
// @SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[parent.getChildren().size()];
@SuppressWarnings("unchecked") final CompletableFuture<Suggestions>[] futures = new CompletableFuture[children.size()];
// Sponge End
int i = 0;
for (final CommandNode<CommandSourceStack> node : children) {
// Sponge: parent.getChildren() -> children
CompletableFuture<Suggestions> future = Suggestions.empty();
try {
future = node.listSuggestions(context.build(truncatedInput), new SuggestionsBuilder(truncatedInput, start));
} catch (final CommandSyntaxException ignored) {
}
futures[i++] = future;
}
// See https://github.com/Mojang/brigadier/pull/81
return CompletableFuture.allOf(futures).handle((voidResult, exception) -> {
final List<Suggestions> suggestions = new ArrayList<>();
for (final CompletableFuture<Suggestions> future : futures) {
if (!future.isCompletedExceptionally()) {
suggestions.add(future.join());
}
}
return Suggestions.merge(fullInput, suggestions);
});
// Sponge End
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class SpongeCommandContextBuilder method findSuggestionContext.
@Override
public SuggestionContext<CommandSourceStack> findSuggestionContext(final int cursor) {
if (this.transaction != null && !this.transaction.isEmpty()) {
return this.transaction.peek().getCopyBuilder().findSuggestionContext(cursor);
}
// where appropriate. There is one change marked below.
if (this.getRange().getStart() <= cursor) {
if (this.getRange().getEnd() < cursor) {
if (this.getChild() != null) {
return this.getChild().findSuggestionContext(cursor);
} else if (!this.getNodes().isEmpty()) {
final ParsedCommandNode<CommandSourceStack> last = this.getNodes().get(this.getNodes().size() - 1);
return new SuggestionContext<>(last.getNode(), last.getRange().getEnd() + 1);
} else {
return new SuggestionContext<>(this.getRootNode(), this.getRange().getStart());
}
} else {
CommandNode<CommandSourceStack> prev = this.getRootNode();
for (final ParsedCommandNode<CommandSourceStack> node : this.getNodes()) {
final StringRange nodeRange = node.getRange();
// Sponge Start
if (SpongeCommandContextBuilder.checkNodeCannotBeEmpty(node.getNode(), nodeRange)) {
// Sponge End
if (nodeRange.getStart() <= cursor && cursor <= nodeRange.getEnd()) {
return new SuggestionContext<>(prev, nodeRange.getStart());
}
// Sponge Start: End if
}
// Sponge End
prev = node.getNode();
}
if (prev == null) {
throw new IllegalStateException("Can't find node before cursor");
}
return new SuggestionContext<>(prev, this.getRange().getStart());
}
}
throw new IllegalStateException("Can't find node before cursor");
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class SpongeParameterTranslator method createFlags.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void createFlags(final LiteralCommandNode<CommandSourceStack> node, final List<Flag> flags, @Nullable final SpongeCommandExecutorWrapper wrapper) {
final Collection<CommandNode<CommandSourceStack>> nodesToAddChildrenTo = new ArrayList<>();
for (final Flag flag : flags) {
// first create the literal.
final Iterator<String> aliasIterator = flag.aliases().iterator();
final LiteralArgumentBuilder<CommandSourceStack> flagLiteral = LiteralArgumentBuilder.literal(aliasIterator.next());
flagLiteral.requires((Predicate) flag.requirement());
final Collection<? extends CommandNode<CommandSourceStack>> toBeRedirected;
final SpongeFlagLiteralCommandNode flagNode = new SpongeFlagLiteralCommandNode(flagLiteral, flag);
if (flag.associatedParameter().isPresent()) {
toBeRedirected = this.createAndAttachNode(Collections.singleton(flagNode), Collections.singletonList(flag.associatedParameter().get()), wrapper, node.getCommand() != null, false);
} else {
toBeRedirected = Collections.singletonList(flagNode);
}
for (final CommandNode<CommandSourceStack> candidate : toBeRedirected) {
if (candidate instanceof SpongeNode && ((SpongeNode) candidate).canForceRedirect()) {
((SpongeNode) candidate).forceRedirect(node);
} else {
nodesToAddChildrenTo.add(candidate);
}
}
node.addChild(flagNode);
while (aliasIterator.hasNext()) {
final LiteralArgumentBuilder<CommandSourceStack> nextFlag = LiteralArgumentBuilder.<CommandSourceStack>literal(aliasIterator.next()).executes(flagNode.getCommand());
if (flagNode.getRedirect() != null) {
nextFlag.redirect(flagNode.getRedirect());
} else {
nextFlag.redirect(flagNode);
}
node.addChild(new SpongeFlagLiteralCommandNode(nextFlag, flag));
}
}
if (!nodesToAddChildrenTo.isEmpty()) {
for (final CommandNode<CommandSourceStack> target : node.getChildren()) {
if (!target.getChildren().isEmpty() && target instanceof SpongeFlagLiteralCommandNode) {
nodesToAddChildrenTo.forEach(x -> x.addChild(((SpongeFlagLiteralCommandNode) target).cloneWithRedirectToThis()));
} else {
nodesToAddChildrenTo.forEach(x -> x.addChild(target));
}
}
}
}
use of net.minecraft.commands.CommandSourceStack in project SpongeCommon by SpongePowered.
the class DifficultyCommandMixin method setDifficulty.
// @formatter:on
/**
* @author Zidane
* @reason Only apply difficulty for the world the command was ran in (as in Sponge, all worlds have difficulties)
*/
@Overwrite
public static int setDifficulty(CommandSourceStack source, Difficulty difficulty) throws CommandSyntaxException {
if (source.getLevel().getDifficulty() == difficulty) {
throw DifficultyCommandMixin.ERROR_ALREADY_DIFFICULT.create(difficulty.getKey());
} else {
final LevelData levelData = source.getLevel().getLevelData();
((ServerWorldProperties) levelData).setDifficulty((org.spongepowered.api.world.difficulty.Difficulty) (Object) difficulty);
source.getLevel().setSpawnSettings(((MinecraftServerAccessor) SpongeCommon.server()).invoker$isSpawningMonsters(), SpongeCommon.server().isSpawningAnimals());
source.getLevel().getPlayers(p -> true).forEach(p -> p.connection.send(new ClientboundChangeDifficultyPacket(levelData.getDifficulty(), levelData.isDifficultyLocked())));
source.sendSuccess(new TranslatableComponent("commands.difficulty.success", difficulty.getDisplayName()), true);
return 0;
}
}
Aggregations