Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more Sponge commands: #3078

Merged
merged 1 commit into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ public static CommandSpec createSpongeCommand() {
nonFlagChildren.register(createSpongeVersionCommand(), "version");
nonFlagChildren.register(createSpongeBlockInfoCommand(), "blockInfo");
nonFlagChildren.register(createSpongeEntityInfoCommand(), "entityInfo");
nonFlagChildren.register(createSpongeAuditCommand(), "audit");
nonFlagChildren.register(createSpongeHeapCommand(), "heap");
nonFlagChildren.register(createSpongePluginsCommand(), "plugins");
nonFlagChildren.register(createSpongeTimingsCommand(), "timings");
nonFlagChildren.register(createSpongeWhichCommand(), "which");
// nonFlagChildren.register(createSpongeAuditCommand(), "audit");
// nonFlagChildren.register(createSpongeHeapCommand(), "heap");
// nonFlagChildren.register(createSpongePluginsCommand(), "plugins");
// nonFlagChildren.register(createSpongeTimingsCommand(), "timings");
// nonFlagChildren.register(createSpongeWhichCommand(), "which");
nonFlagChildren.register(createSpongeMetricsCommand(), "metrics");
flagChildren.register(createSpongeChunksCommand(), "chunks");
flagChildren.register(createSpongeTPSCommand(), "tps");
Expand Down Expand Up @@ -525,6 +525,7 @@ private static CommandSpec createSpongeVersionCommand() {
.build();
}

// need ray equivalent
private static CommandSpec createSpongeBlockInfoCommand() {
return CommandSpec.builder()
.description(Text.of("Display the tracked information of the Block you are looking at."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -83,7 +84,7 @@ public CompletableFuture<Suggestions> listSuggestions(
} catch (final NumberFormatException ex) {
builder.suggest(s);
}
} else {
} else if (s.toLowerCase(Locale.ROOT).startsWith(builder.getRemaining())) {
builder.suggest(s);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -134,7 +135,7 @@ public CompletableFuture<Suggestions> listSuggestions(
} catch (final NumberFormatException ex) {
builder.suggest(s);
}
} else {
} else if (s.toLowerCase(Locale.ROOT).startsWith(builder.getRemaining())) {
builder.suggest(s);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ public boolean hasAny(final Parameter.@NonNull Key<?> key) {

@Override
@NonNull
public <T> Optional<T> getOne(final Parameter.@NonNull Key<? super T> key) {
public <T> Optional<T> getOne(final Parameter.@NonNull Key<T> key) {
return Optional.ofNullable(this.getValue(key));
}

@Override
@NonNull
public <T> T requireOne(final Parameter.@NonNull Key<? super T> key) throws NoSuchElementException {
public <T> T requireOne(final Parameter.@NonNull Key<T> key) throws NoSuchElementException {
final T value = this.getValue(key);
if (value == null) {
throw new NoSuchElementException("No value exists for key " + key.key());
Expand All @@ -130,7 +130,7 @@ public <T> T requireOne(final Parameter.@NonNull Key<? super T> key) throws NoSu
@Override
@NonNull
@SuppressWarnings("unchecked")
public <T> Collection<? extends T> getAll(final Parameter.@NonNull Key<? super T> key) {
public <T> Collection<? extends T> getAll(final Parameter.@NonNull Key<T> key) {
final Collection<? extends T> values = (Collection<? extends T>) this.argumentMap.get(key);
if (values == null) {
return ImmutableList.of();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.mojang.brigadier.context.SuggestionContext;
import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.command.CommandCause;
import org.spongepowered.api.command.parameter.Parameter;
Expand Down Expand Up @@ -281,7 +280,7 @@ public boolean hasAny(final Parameter.@NonNull Key<?> key) {
@Override
@NonNull
@SuppressWarnings("unchecked")
public <T> Optional<T> getOne(final Parameter.@NonNull Key<? super T> key) {
public <T> Optional<T> getOne(final Parameter.@NonNull Key<T> key) {
final SpongeParameterKey<T> spongeParameterKey = SpongeParameterKey.getSpongeKey(key);
final Collection<?> collection = this.getFrom(spongeParameterKey);
if (collection.size() > 1) {
Expand All @@ -294,7 +293,7 @@ public <T> Optional<T> getOne(final Parameter.@NonNull Key<? super T> key) {
@Override
@NonNull
@SuppressWarnings("unchecked")
public <T> T requireOne(final Parameter.@NonNull Key<? super T> key) throws NoSuchElementException, IllegalArgumentException {
public <T> T requireOne(final Parameter.@NonNull Key<T> key) throws NoSuchElementException, IllegalArgumentException {
final SpongeParameterKey<T> spongeParameterKey = SpongeParameterKey.getSpongeKey(key);
final Collection<?> collection = this.getFrom(spongeParameterKey);
if (collection.size() > 1) {
Expand All @@ -309,7 +308,7 @@ public <T> T requireOne(final Parameter.@NonNull Key<? super T> key) throws NoSu
@Override
@NonNull
@SuppressWarnings("unchecked")
public <T> Collection<? extends T> getAll(final Parameter.@NonNull Key<? super T> key) {
public <T> Collection<? extends T> getAll(final Parameter.@NonNull Key<T> key) {
return (Collection<? extends T>) this.getFrom(SpongeParameterKey.getSpongeKey(key));
}

Expand All @@ -326,7 +325,7 @@ Collection<?> getFrom(final SpongeParameterKey<?> key) {
}

@Override
public <T> void putEntry(final Parameter.@NonNull Key<? super T> key, @NonNull final T object) {
public <T> void putEntry(final Parameter.@NonNull Key<T> key, @NonNull final T object) {
if (this.transaction != null && !this.transaction.isEmpty()) {
this.transaction.peek().putEntry(key, object);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
Expand All @@ -68,7 +69,13 @@ private static SuggestionProvider<CommandSource> createSuggestionProvider(@Nulla
}

return (context, builder) -> {
completer.complete((org.spongepowered.api.command.parameter.CommandContext) context).forEach(builder::suggest);
final String remaining = builder.getRemaining().toLowerCase(Locale.ROOT);
completer.complete((org.spongepowered.api.command.parameter.CommandContext) context)
.forEach(suggestion -> {
if (suggestion.toLowerCase(Locale.ROOT).startsWith(remaining)) {
builder.suggest(suggestion);
}
});
return builder.buildFuture();
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.google.common.collect.Multimap;
import com.google.common.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -91,15 +92,22 @@
public final class SpongeCommandManager implements CommandManager {

private final Game game;
private final Provider<SpongeCommand> spongeCommand;
private final Map<String, SpongeCommandMapping> commandMappings = new HashMap<>();
private final Multimap<SpongeCommandMapping, String> inverseCommandMappings = HashMultimap.create();
private final Multimap<PluginContainer, SpongeCommandMapping> pluginToCommandMap = HashMultimap.create();
private boolean isResetting = false;
private boolean hasStarted = false;

@Inject
public SpongeCommandManager(final Game game) {
public SpongeCommandManager(final Game game, final Provider<SpongeCommand> spongeCommand) {
this.game = game;
this.spongeCommand = spongeCommand;
}

@Override
public Set<String> getKnownAliases() {
return ImmutableSet.copyOf(this.commandMappings.keySet());
}

@NonNull
Expand Down Expand Up @@ -497,7 +505,7 @@ public void init() {
try {
SpongeParameterizedCommandRegistrar.INSTANCE.register(
Launcher.getInstance().getCommonPlugin(),
SpongeCommand.createSpongeCommand(),
this.spongeCommand.get().createSpongeCommand(),
"sponge"
);
} catch (final CommandFailedRegistrationException ex) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.command.sponge;

import com.google.common.collect.ImmutableList;
import net.kyori.adventure.text.TextComponent;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.exception.ArgumentParseException;
import org.spongepowered.api.command.manager.CommandMapping;
import org.spongepowered.api.command.parameter.ArgumentReader;
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.command.parameter.managed.ValueParameter;

import java.util.List;
import java.util.Locale;
import java.util.Optional;

public final class CommandAliasesParameter implements ValueParameter<CommandMapping> {

@Override
public List<String> complete(final CommandContext context) {
return ImmutableList.copyOf(Sponge.getGame().getCommandManager().getKnownAliases());
}

@Override
public Optional<? extends CommandMapping> getValue(
final Parameter.Key<? super CommandMapping> parameterKey,
final ArgumentReader.Mutable reader,
final CommandContext.Builder context) throws ArgumentParseException {
final String alias = reader.parseString().toLowerCase(Locale.ROOT);
final Optional<CommandMapping> mapping =
Sponge.getGame().getCommandManager().getCommandMapping(alias);
if (mapping.isPresent()) {
return mapping;
}
throw reader.createException(TextComponent.of("A command with alias " + alias + " does not exist."));
}

}
Loading