Tab complete, implement getOnlinePlayers for servers

This commit is contained in:
creeper123123321 2019-02-09 04:08:16 -02:00
parent 8d18fa53de
commit 07ffb40535
No known key found for this signature in database
GPG Key ID: 0AC57D54786721D1
4 changed files with 88 additions and 14 deletions

View File

@ -24,7 +24,6 @@
package com.github.creeper123123321.viafabric;
import com.github.creeper123123321.viafabric.commands.NMSCommandSender;
import com.github.creeper123123321.viafabric.commands.VRCommandHandler;
import com.github.creeper123123321.viafabric.platform.VRInjector;
import com.github.creeper123123321.viafabric.platform.VRLoader;
@ -32,11 +31,15 @@ import com.github.creeper123123321.viafabric.platform.VRPlatform;
import com.github.creeper123123321.viafabric.protocol.protocol1_7_6_10to1_7_1_5.Protocol1_7_6_10to1_7_1_5;
import com.github.creeper123123321.viafabric.protocol.protocol1_8to1_7_6_10.Protocol1_8TO1_7_6_10;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import io.github.cottonmc.clientcommands.ArgumentBuilders;
import io.github.cottonmc.clientcommands.ClientCommands;
import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.commands.CommandRegistry;
import net.fabricmc.fabric.events.ServerEvent;
import net.fabricmc.loader.FabricLoader;
import net.minecraft.server.command.ServerCommandSource;
import us.myles.ViaVersion.ViaManager;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.UserConnection;
@ -74,25 +77,31 @@ public class VRViaVersionInitializer {
if (FabricLoader.INSTANCE.getEnvironmentType() == EnvType.CLIENT) {
ClientCommands.registerCommand(command -> command
.register(
ArgumentBuilders.literal("viafabric")
ArgumentBuilders.literal("viafabricclient")
.then(
ArgumentBuilders
.argument("args", StringArgumentType.greedyString())
.executes(ctx -> {
String args = StringArgumentType.getString(ctx, "args");
Via.getManager()
.getCommandHandler()
.onCommand(
new NMSCommandSender(ctx.getSource()),
args.split(" ", -1)
);
return 1;
})
.executes(((VRCommandHandler) Via.getManager().getCommandHandler())::execute)
.suggests(((VRCommandHandler) Via.getManager().getCommandHandler())::suggestion)
)
.executes(((VRCommandHandler) Via.getManager().getCommandHandler())::execute)
)
);
}
CommandRegistry.INSTANCE.register(false, command -> command
.register(
LiteralArgumentBuilder.<ServerCommandSource>literal("viafabric")
.then(
RequiredArgumentBuilder
.<ServerCommandSource, String>argument("args", StringArgumentType.greedyString())
.executes(((VRCommandHandler) Via.getManager().getCommandHandler())::execute)
.suggests(((VRCommandHandler) Via.getManager().getCommandHandler())::suggestion)
)
.executes(((VRCommandHandler) Via.getManager().getCommandHandler())::execute)
)
);
ServerEvent.START.register(server -> {
try {
ProtocolRegistry.SERVER_PROTOCOL = Via.getManager().getInjector().getServerProtocolVersion();

View File

@ -25,8 +25,16 @@
package com.github.creeper123123321.viafabric.commands;
import com.github.creeper123123321.viafabric.commands.subs.LeakDetectSubCommand;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import net.minecraft.server.command.CommandSource;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.commands.ViaCommandHandler;
import java.util.concurrent.CompletableFuture;
public class VRCommandHandler extends ViaCommandHandler {
{
try {
@ -35,4 +43,40 @@ public class VRCommandHandler extends ViaCommandHandler {
e.printStackTrace();
}
}
public int execute(CommandContext<? extends CommandSource> ctx) {
String[] args = new String[0];
try {
args = StringArgumentType.getString(ctx, "args").split(" ");
} catch (IllegalArgumentException ignored) {
}
Via.getManager()
.getCommandHandler()
.onCommand(
new NMSCommandSender(ctx.getSource()),
args
);
return 1;
}
public CompletableFuture<Suggestions> suggestion(CommandContext<? extends CommandSource> ctx, SuggestionsBuilder builder) {
String[] args;
try {
args = StringArgumentType.getString(ctx, "args").split(" ", -1);
} catch (IllegalArgumentException ignored) {
args = new String[]{""};
}
String[] pref = args.clone();
pref[pref.length - 1] = "";
String prefix = String.join(" ", pref);
onTabComplete(new NMSCommandSender(ctx.getSource()), args)
.stream()
.map(it -> {
SuggestionsBuilder b = new SuggestionsBuilder(builder.getInput(), prefix.length() + builder.getStart());
b.suggest(it);
return b;
})
.forEach(builder::add);
return builder.buildFuture();
}
}

View File

@ -29,6 +29,8 @@ import us.myles.ViaVersion.api.command.ViaCommandSender;
import us.myles.ViaVersion.api.command.ViaSubCommand;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class LeakDetectSubCommand extends ViaSubCommand {
@Override
@ -56,4 +58,15 @@ public class LeakDetectSubCommand extends ViaSubCommand {
}
return true;
}
@Override
public List<String> onTabComplete(ViaCommandSender sender, String[] args) {
if (args.length == 1) {
return Arrays.stream(ResourceLeakDetector.Level.values())
.map(Enum::name)
.filter(it -> it.startsWith(args[0]))
.collect(Collectors.toList());
}
return super.onTabComplete(sender, args);
}
}

View File

@ -25,11 +25,13 @@
package com.github.creeper123123321.viafabric.platform;
import com.github.creeper123123321.viafabric.ViaFabric;
import com.github.creeper123123321.viafabric.commands.NMSCommandSender;
import com.github.creeper123123321.viafabric.commands.UserCommandSender;
import com.github.creeper123123321.viafabric.protocol.ClientSideInterceptor;
import com.github.creeper123123321.viafabric.util.FutureTaskId;
import net.fabricmc.loader.FabricLoader;
import net.fabricmc.loader.ModContainer;
import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sortme.ChatMessageType;
@ -46,7 +48,6 @@ import us.myles.ViaVersion.api.platform.ViaPlatform;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.dump.PluginInfo;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.ChatRewriter;
import us.myles.ViaVersion.sponge.VersionInfo;
import us.myles.ViaVersion.util.GsonUtil;
@ -149,6 +150,13 @@ public class VRPlatform implements ViaPlatform {
@Override
public ViaCommandSender[] getOnlinePlayers() {
MinecraftServer server = FabricLoader.INSTANCE.getEnvironmentHandler().getServerInstance();
if (server != null) {
return server.getPlayerManager().getPlayerList().stream()
.map(Entity::getCommandSource)
.map(NMSCommandSender::new)
.toArray(ViaCommandSender[]::new);
}
return Via.getManager().getPortedPlayers().values().stream()
.map(UserCommandSender::new)
.toArray(ViaCommandSender[]::new);
@ -170,7 +178,7 @@ public class VRPlatform implements ViaPlatform {
}
} else {
MinecraftServer server = FabricLoader.INSTANCE.getEnvironmentHandler().getServerInstance();
if (server == null) return ;
if (server == null) return;
ServerPlayerEntity player = server.getPlayerManager().getPlayer(uuid);
if (player == null) return;
player.sendChatMessage(TextComponent.Serializer.fromJsonString(ChatRewriter.legacyTextToJson(s)), ChatMessageType.SYSTEM);