Migrate commands to v1 ()

* Migrate commands to v1

* Update fabric-commands-v1/src/main/resources/fabric.mod.json

Co-Authored-By: liach <7806504+liach@users.noreply.github.com>

* Update fabric-commands-v1/src/main/java/net/fabricmc/fabric/api/command/v1/CommandRegistrationCallback.java

Co-Authored-By: liach <7806504+liach@users.noreply.github.com>

* commands -> command and improved javadoc.

* Fix the stragglers

* swap dedicated and dispatcher params

* Update fabric-command-v1/src/main/java/net/fabricmc/fabric/api/command/v1/CommandRegistrationCallback.java

Whoops forgot that one

Co-Authored-By: Juuxel <6596629+Juuxel@users.noreply.github.com>

* module name order has been restored.

Co-authored-by: liach <7806504+liach@users.noreply.github.com>
Co-authored-by: Juuxel <6596629+Juuxel@users.noreply.github.com>
This commit is contained in:
i509VCB 2020-04-29 21:05:43 +00:00 committed by GitHub
parent 915c4463ea
commit 2e0c4ff552
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 113 additions and 123 deletions
fabric-command-api-v1
build.gradle
src/main
java/net/fabricmc/fabric
resources
fabric-commands-v0
build.gradle
src/main
java/net/fabricmc/fabric
resources
settings.gradle

View file

@ -0,0 +1,6 @@
archivesBaseName = "fabric-command-api-v1"
version = getSubprojectVersion(project, "1.0.0")
dependencies {
compile project(path: ':fabric-api-base', configuration: 'dev')
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 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.fabricmc.fabric.api.command.v1;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
/**
* Callback for when a server registers all commands.
*
* <p>To register some commands, you would register an event listener and implement the callback.
*
* <pre><code>
* CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
* // For example, this command is only registered on an integrated server
* if (!dedicated) dispatcher.register(CommandManager.literal("integrated_command").executes(context -> {...}));
* })};
* </code></pre>
*/
public interface CommandRegistrationCallback {
Event<CommandRegistrationCallback> EVENT = EventFactory.createArrayBacked(CommandRegistrationCallback.class, (callbacks) -> (dispatcher, dedicated) -> {
for (CommandRegistrationCallback callback : callbacks) {
callback.register(dispatcher, dedicated);
}
});
/**
* Called when the server is registering commands.
*
* @param dispatcher the command dispatcher to register commands to.
* @param dedicated whether the server this command is being registered on is a dedicated server.
*/
void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated);
}

View file

@ -16,28 +16,26 @@
package net.fabricmc.fabric.mixin.command;
import com.mojang.brigadier.AmbiguityConsumer;
import com.mojang.brigadier.CommandDispatcher;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.Redirect;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.fabricmc.fabric.impl.command.CommandRegistryImpl;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
@Mixin(CommandManager.class)
public class MixinCommandManagerIntegrated {
@Shadow
private CommandDispatcher<ServerCommandSource> dispatcher;
@Inject(method = "<init>(Z)V", at = @At("RETURN"))
public void addMethods(boolean dedicated, CallbackInfo info) {
// TODO: Run before findAmbiguities
if (!dedicated) {
CommandRegistryImpl.INSTANCE.entries(false).forEach((e) -> e.accept(dispatcher));
}
public abstract class MixinCommandManager {
/**
* @reason Add commands before ambiguities are calculated.
*/
@Redirect(at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/CommandDispatcher;findAmbiguities(Lcom/mojang/brigadier/AmbiguityConsumer;)V"), method = "<init>")
private void fabric_addCommands(CommandDispatcher<ServerCommandSource> dispatcher, AmbiguityConsumer<ServerCommandSource> ambiguityConsumer, boolean isDedicated) {
CommandRegistrationCallback.EVENT.invoker().register(dispatcher, isDedicated);
// Now mimic vanilla logic by calling findAmbiguities.
dispatcher.findAmbiguities(ambiguityConsumer);
}
}

View file

@ -3,10 +3,9 @@
"package": "net.fabricmc.fabric.mixin.command",
"compatibilityLevel": "JAVA_8",
"mixins": [
"MixinMinecraftDedicatedServer"
"MixinCommandManager"
],
"client": [
"MixinCommandManagerIntegrated"
],
"injectors": {
"defaultRequire": 1

View file

@ -0,0 +1,26 @@
{
"schemaVersion": 1,
"id": "fabric-command-api-v1",
"name": "Fabric Command API (v1)",
"version": "${version}",
"environment": "*",
"license": "Apache-2.0",
"icon": "assets/fabric-command-api-v1/icon.png",
"contact": {
"homepage": "https://fabricmc.net",
"irc": "irc://irc.esper.net:6667/fabric",
"issues": "https://github.com/FabricMC/fabric/issues",
"sources": "https://github.com/FabricMC/fabric"
},
"authors": [
"FabricMC"
],
"depends": {
"fabricloader": ">=0.7.0",
"fabric-api-base": "*"
},
"description": "Adds command-related hooks.",
"mixins": [
"fabric-command-api-v1.mixins.json"
]
}

View file

@ -1,6 +1,7 @@
archivesBaseName = "fabric-commands-v0"
version = getSubprojectVersion(project, "0.1.2")
version = getSubprojectVersion(project, "0.2.0")
dependencies {
compile project(path: ':fabric-api-base', configuration: 'dev')
compile project(path: ':fabric-command-api-v1', configuration: 'dev')
}

View file

@ -22,11 +22,12 @@ import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
import net.fabricmc.fabric.impl.command.CommandRegistryImpl;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
/**
* Registry for server-side command providers.
* @deprecated Please migrate to v1. Please use {@link CommandRegistrationCallback} instead.
*/
@Deprecated
public class CommandRegistry {
public static final CommandRegistry INSTANCE = new CommandRegistry();
@ -37,6 +38,12 @@ public class CommandRegistry {
* @param consumer The command provider, consuming {@link CommandDispatcher}.
*/
public void register(boolean dedicated, Consumer<CommandDispatcher<ServerCommandSource>> consumer) {
CommandRegistryImpl.INSTANCE.register(dedicated, consumer);
CommandRegistrationCallback.EVENT.register(((dispatcher, isDedicated) -> {
if (dedicated && !isDedicated) {
return;
}
consumer.accept(dispatcher);
}));
}
}

View file

@ -1,50 +0,0 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 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.fabricmc.fabric.impl.command;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.command.ServerCommandSource;
public class CommandRegistryImpl {
public static final CommandRegistryImpl INSTANCE = new CommandRegistryImpl();
private final List<Consumer<CommandDispatcher<ServerCommandSource>>> serverCommands;
private final List<Consumer<CommandDispatcher<ServerCommandSource>>> dedicatedServerCommands;
public CommandRegistryImpl() {
this.serverCommands = new ArrayList<>();
this.dedicatedServerCommands = new ArrayList<>();
}
public List<Consumer<CommandDispatcher<ServerCommandSource>>> entries(boolean dedicated) {
return Collections.unmodifiableList(dedicated ? dedicatedServerCommands : serverCommands);
}
public void register(boolean dedicated, Consumer<CommandDispatcher<ServerCommandSource>> consumer) {
if (dedicated) {
dedicatedServerCommands.add(consumer);
} else {
serverCommands.add(consumer);
}
}
}

View file

@ -1,50 +0,0 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 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.fabricmc.fabric.mixin.command;
import java.io.File;
import java.net.Proxy;
import com.mojang.authlib.GameProfileRepository;
import com.mojang.authlib.minecraft.MinecraftSessionService;
import com.mojang.authlib.yggdrasil.YggdrasilAuthenticationService;
import com.mojang.datafixers.DataFixer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldGenerationProgressListenerFactory;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.dedicated.MinecraftDedicatedServer;
import net.minecraft.util.UserCache;
import net.fabricmc.fabric.impl.command.CommandRegistryImpl;
@Mixin(MinecraftDedicatedServer.class)
public abstract class MixinMinecraftDedicatedServer extends MinecraftServer {
public MixinMinecraftDedicatedServer(File file_1, Proxy proxy_1, DataFixer dataFixer_1, CommandManager serverCommandManager_1, YggdrasilAuthenticationService yggdrasilAuthenticationService_1, MinecraftSessionService minecraftSessionService_1, GameProfileRepository gameProfileRepository_1, UserCache userCache_1, WorldGenerationProgressListenerFactory worldGenerationProgressListenerFactory_1, String string_1) {
super(file_1, proxy_1, dataFixer_1, serverCommandManager_1, yggdrasilAuthenticationService_1, minecraftSessionService_1, gameProfileRepository_1, userCache_1, worldGenerationProgressListenerFactory_1, string_1);
}
@Inject(method = "setupServer", at = @At("HEAD"))
private void setupServer(CallbackInfoReturnable<Boolean> info) {
CommandRegistryImpl.INSTANCE.entries(false).forEach((e) -> e.accept(getCommandManager().getDispatcher()));
CommandRegistryImpl.INSTANCE.entries(true).forEach((e) -> e.accept(getCommandManager().getDispatcher()));
}
}

View file

@ -17,10 +17,10 @@
],
"depends": {
"fabricloader": ">=0.4.0",
"fabric-api-base": "*"
"fabric-api-base": "*",
"fabric-command-api-v1": "*"
},
"description": "Adds command-related hooks.",
"description": "Deprecated. Please migrate to v1",
"mixins": [
"fabric-commands-v0.mixins.json"
]
}

View file

@ -18,6 +18,7 @@ include 'fabric-biomes-v1'
include 'fabric-blockrenderlayer-v1'
include 'fabric-climbable-api-v1'
include 'fabric-commands-v0'
include 'fabric-command-api-v1'
include 'fabric-containers-v0'
include 'fabric-content-registries-v0'
include 'fabric-crash-report-info-v1'