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

Port command api v2 (sponge) to 1.6.4 #183

Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For support, consider joining the [Legacy Fabric discord server](https://legacyf
|----------------------|-------------------------------|--------|-----|-------|-------|--------|--------|--------|--------|
| api-base | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| command-api-v1 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| command-api-v2 | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| command-api-v2 | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| crash-report-info-v1 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| entity-events-v1 | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ❌ |
| gamerule-api-v1 | ? | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ? |
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ def moduleDependencies(Project project, List<String> projectNames) {
def commonProject = findProject(":${moduleName}_common")

if (commonProject != null) {
depNames.add(0, commonProject.name)
def predicate = VersionHelper.parsePredicate(getMCRange(commonProject))
if (predicate.test(projectMCVersion)) depNames.add(0, commonProject.name)
}
} else {
depNames.removeIf { !it.endsWith("_common")}
Expand Down
3 changes: 3 additions & 0 deletions legacy-fabric-command-api-v2/1.6.4/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
loom {
accessWidenerPath = file("src/main/resources/command-api-v2.accesswidener")
}
2 changes: 2 additions & 0 deletions legacy-fabric-command-api-v2/1.6.4/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minVersionIncluded=1.6.4
maxVersionIncluded=1.6.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.command.v2;

import net.legacyfabric.fabric.api.command.v2.lib.sponge.CommandManager;
import net.legacyfabric.fabric.api.event.Event;
import net.legacyfabric.fabric.api.event.EventFactory;

/**
* An entrypoint and event for registering commands to the {@link CommandManager}.
*/
@FunctionalInterface
public interface CommandRegistrar {
Event<CommandRegistrar> EVENT = EventFactory.createArrayBacked(CommandRegistrar.class, listeners -> (manager, dedicated) -> {
for (CommandRegistrar registrar : listeners) {
registrar.register(manager, dedicated);
}
});

/**
* Register your commands here.
*
* @param manager The command manager
* @param dedicated Whether the mod is running on a dedicated server
*/
void register(CommandManager manager, boolean dedicated);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.command.v2;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import net.minecraft.command.CommandSource;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;

public enum Selector {
ALL_ENTITIES('e') {
@Override
public Set<Entity> resolve(CommandSource sender) {
return Sets.newHashSet(sender.getWorld().entities);
}
},
ALL_PLAYERS('a') {
@Override
public Set<Entity> resolve(CommandSource sender) {
return (Set<Entity>) sender.getWorld().playerEntities.stream().map(e -> (Entity) e).collect(Collectors.toSet());
}
},
NEAREST_PLAYER('p') {
@Override
public Set<Entity> resolve(CommandSource sender) {
return Sets.newHashSet(sender.getWorld().getClosestPlayer(sender.method_4086().x, sender.method_4086().y, sender.method_4086().z, 50.0D));
}
},
RANDOM_PLAYER('r') {
@Override
public Set<Entity> resolve(CommandSource sender) {
try {
return Sets.newHashSet((Entity) MinecraftServer.getServer().getPlayerManager().players.stream().findAny().orElseThrow(NullPointerException::new));
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
},
EXECUTING_ENTITY('s') {
@Override
public Set<Entity> resolve(CommandSource sender) {
//TODO @s didn't exist this early in the game's development, and there seems to be no code to handle it, so maybe this'll work?
return Sets.newHashSet(sender.getWorld().getPlayerByName(sender.getUsername()));
}
};

private final char key;
private static final Map<String, Selector> MAP;

Selector(char key) {
this.key = key;
}

public char getKey() {
return this.key;
}

public abstract Set<Entity> resolve(CommandSource sender);

public static List<String> complete(String s) {
if (s.startsWith("@") && s.length() == 2) {
return Arrays.stream(values()).map(Selector::getKey).map(String::valueOf).distinct().collect(Collectors.toList());
}

return ImmutableList.of();
}

public static Selector parse(String value) {
if (MAP.containsKey(value)) {
return MAP.get(value);
}

throw new IllegalArgumentException("Unknown selector");
}

static {
ImmutableMap.Builder<String, Selector> builder = ImmutableMap.builder();

for (Selector s : values()) {
builder.put("@" + s.getKey(), s);
}

MAP = builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2020 - 2024 Legacy Fabric
* Copyright (c) 2016 - 2022 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.legacyfabric.fabric.api.command.v2;

/**
* Specifies a method to parse {@link String}s in a command.
*/
public enum StringType {
SINGLE_WORD(false),
GREEDY_PHRASE(true);

private final boolean all;

StringType(boolean all) {
this.all = all;
}

public boolean isAll() {
return this.all;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* This file is part of SpongeAPI, 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 net.legacyfabric.fabric.api.command.v2.lib.sponge;

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

import org.jetbrains.annotations.Nullable;

import net.minecraft.text.ChatMessage;
import net.minecraft.world.World;

import net.legacyfabric.fabric.api.command.v2.lib.sponge.spec.CommandSpec;
import net.legacyfabric.fabric.api.permission.v1.PermissibleCommandSource;
import net.legacyfabric.fabric.api.util.Location;

/**
* A low-level interface for commands that can be executed. For almost all use
* cases, higher-level tools should be used instead, like {@link CommandSpec}.
*
* <p>Implementations are not required to implement a sane
* {@link Object#equals(Object)} but really should.</p>
*/
public interface CommandCallable {
/**
* Execute the command based on input arguments.
*
* <p>The implementing class must perform the necessary permission
* checks.</p>
*
* @param source The caller of the command
* @param arguments The raw arguments for this command
* @return The result of a command being processed
* @throws CommandException Thrown on a command error
*/
CommandResult process(PermissibleCommandSource source, String arguments) throws CommandException;

/**
* Gets a list of suggestions based on input.
*
* <p>If a suggestion is chosen by the user, it will replace the last
* word.</p>
*
* @param source The command source
* @param arguments The arguments entered up to this point
* @param targetPosition The position the source is looking at when
* performing tab completion
* @return A list of suggestions
* @throws CommandException Thrown if there was a parsing error
*/
List<String> getSuggestions(PermissibleCommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException;

/**
* Test whether this command can probably be executed by the given source.
*
* <p>If implementations are unsure if the command can be executed by
* the source, {@code true} should be returned. Return values of this method
* may be used to determine whether this command is listed in command
* listings.</p>
*
* @param source The caller of the command
* @return Whether permission is (probably) granted
*/
boolean testPermission(PermissibleCommandSource source);

/**
* Gets a short one-line description of this command.
*
* <p>The help system may display the description in the command list.</p>
*
* @param source The source of the help request
* @return A description
*/
Optional<ChatMessage> getShortDescription(PermissibleCommandSource source);

/**
* Gets a longer formatted help message about this command.
*
* <p>It is recommended to use the default text color and style. Sections
* with text actions (e.g. hyperlinks) should be underlined.</p>
*
* <p>Multi-line messages can be created by separating the lines with
* {@code \n}.</p>
*
* <p>The help system may display this message when a source requests
* detailed information about a command.</p>
*
* @param source The source of the help request
* @return A help text
*/
Optional<ChatMessage> getHelp(PermissibleCommandSource source);

/**
* Gets the usage string of this command.
*
* <p>A usage string may look like
* {@code [-w &lt;world&gt;] &lt;var1&gt; &lt;var2&gt;}.</p>
*
* <p>The string must not contain the command alias.</p>
*
* @param source The source of the help request
* @return A usage string
*/
ChatMessage getUsage(PermissibleCommandSource source);
}
Loading