Skip to content

Commit

Permalink
Barebone port of command api v2 (sponge) to 1.6.4
Browse files Browse the repository at this point in the history
  • Loading branch information
thecatcore committed May 5, 2024
1 parent 16d50f3 commit da5a3a9
Show file tree
Hide file tree
Showing 78 changed files with 8,662 additions and 17 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ For support, consider joining the [Legacy Fabric discord server](https://legacyf
? = Not sure if it was tested or working correctly\
❌ = Not working at all, likely crashing

| | 1.6.4 | 1.7.10 | 1.8 | 1.8.9 | 1.9.4 | 1.10.2 | 1.11.2 | 1.12.2 | 1.13.2 |
|----------------------|-------|--------|-----|-------|-------|--------|--------|--------|--------|
| api-base ||||||||||
| command-api-v1 ||||||||||
| command-api-v2 | |||||||||
| crash-report-info-v1 ||||||||||
| entity-events-v1 ||||||||||
| gamerule-api-v1 | ? |||||||| ? |
| item-groups-v1 ||||||||||
| keybindings-api-v1 ||||||||||
| lifecycle-events-v1 ||||||||||
| logger-api-v1 ||||||||||
| networking-api-v1 ||||||||||
| permissions-api-v1 | ? |||||||||
| registry-sync-api-v1 ||||||||||
| rendering-api-v1 ||||||||||
| resource-loader-v1 ||||||||||
| | 1.6.4 | 1.7.10 | 1.8 | 1.8.9 | 1.9.4 | 1.10.2 | 1.11.2 | 1.12.2 | 1.13.2 |
|----------------------|------------------------------------|--------|-----|-------|-------|--------|--------|--------|--------|
| api-base | |||||||||
| command-api-v1 | |||||||||
| command-api-v2 | ⚠ <br/>Text formatting need rework |||||||||
| crash-report-info-v1 | |||||||||
| entity-events-v1 | |||||||||
| gamerule-api-v1 | ? |||||||| ? |
| item-groups-v1 | |||||||||
| keybindings-api-v1 | |||||||||
| lifecycle-events-v1 | |||||||||
| logger-api-v1 | |||||||||
| networking-api-v1 | |||||||||
| permissions-api-v1 | |||||||||
| registry-sync-api-v1 | |||||||||
| rendering-api-v1 | |||||||||
| resource-loader-v1 | |||||||||
Empty file.
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,120 @@
/*
* 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.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

0 comments on commit da5a3a9

Please sign in to comment.