Add tab complete support

This commit is contained in:
Norbi Peti 2020-08-01 23:15:10 +02:00
parent de13d9876d
commit 3ce2da7f7f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 318 additions and 198 deletions

View file

@ -59,6 +59,16 @@
<artifactId>VirtualComputer-MSCOM</artifactId> <artifactId>VirtualComputer-MSCOM</artifactId>
<version>2.1-SNAPSHOT</version> <version>2.1-SNAPSHOT</version>
</dependency> </dependency>
<!-- <dependency>
<groupId>me.lucko</groupId>
<artifactId>commodore</artifactId>
<version>1.8</version>
</dependency> -->
<dependency>
<groupId>com.github.PandacubeFr</groupId>
<artifactId>commodore</artifactId>
<version>patch-custom-suggests-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>
<profile> <profile>

View file

@ -1,13 +1,31 @@
package sznp.virtualcomputer; package sznp.virtualcomputer;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode;
import lombok.val;
import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;
import me.lucko.commodore.file.CommodoreFileFormat;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.virtualbox_6_1.MouseButtonState;
import org.virtualbox_6_1.VBoxException; import org.virtualbox_6_1.VBoxException;
import sznp.virtualcomputer.util.Scancode;
public class Commands implements CommandExecutor { import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Commands implements CommandExecutor, TabCompleter {
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
@ -25,6 +43,9 @@ public class Commands implements CommandExecutor {
} }
Computer.getInstance().Start(sender, c); Computer.getInstance().Start(sender, c);
break; break;
case "list":
Computer.getInstance().List(sender);
break;
case "stop": case "stop":
case "poweroff": case "poweroff":
case "off": case "off":
@ -82,11 +103,9 @@ public class Commands implements CommandExecutor {
showusage = false; showusage = false;
} }
} }
} } catch (VBoxException e) {
catch (VBoxException e) {
e.printStackTrace(); e.printStackTrace();
} } catch (Exception ignored) { //It will show the usage here
catch (Exception ignored) { //It will show the usage here
} }
} }
if (showusage) { if (showusage) {
@ -131,7 +150,7 @@ public class Commands implements CommandExecutor {
} }
try { try {
MouseLockerPlayerListener.LockedSpeed = Float.parseFloat(args[2]); MouseLockerPlayerListener.LockedSpeed = Float.parseFloat(args[2]);
} catch(NumberFormatException e) { } catch (NumberFormatException e) {
sender.sendMessage("§cThe speed must be a number."); sender.sendMessage("§cThe speed must be a number.");
break; break;
} }
@ -143,6 +162,49 @@ public class Commands implements CommandExecutor {
return true; return true;
} }
private boolean tabSetup = true;
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
if (CommodoreProvider.isSupported() && tabSetup) {
tabSetup = false;
new Object() {
private void setup(Command command) {
val com = CommodoreProvider.getCommodore(PluginMain.Instance);
try {
val node = CommodoreFileFormat.parse(PluginMain.Instance.getResource("computer.commodore"));
CommandNode<Object> arg = RequiredArgumentBuilder.argument("index", IntegerArgumentType.integer()).build();
replaceChildren(node.getChild("start"), arg);
replaceChildren(node.getChild("on"), arg);
arg = RequiredArgumentBuilder.argument("key", StringArgumentType.word())
.suggests((context, builder) -> {
Arrays.stream(Scancode.values()).map(Scancode::name)
.map(name -> name.replace("sc_", "")).forEach(builder::suggest);
return builder.buildFuture();
}).build();
replaceChildren(node.getChild("key"), arg);
replaceChildren(node.getChild("press"), arg);
arg = RequiredArgumentBuilder.argument("button", StringArgumentType.word())
.suggests((context, builder) -> {
Arrays.stream(MouseButtonState.values()).map(MouseButtonState::name).forEach(builder::suggest);
return builder.buildFuture();
}).build();
replaceChildren(node.getChild("mouse"), arg);
com.register(command, node);
} catch (IOException e) {
e.printStackTrace();
}
}
private void replaceChildren(CommandNode<Object> target, CommandNode<Object> node) {
target.getChildren().clear();
target.addChild(node);
}
}.setup(command);
}
return Collections.emptyList();
}
/** /**
* Checks the 2nd parameter * Checks the 2nd parameter
* *

View file

@ -2,6 +2,7 @@ package sznp.virtualcomputer;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import lombok.Getter; import lombok.Getter;
import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -71,6 +72,14 @@ public final class Computer {
}); });
} }
public void List(CommandSender sender) {
val machines = vbox.getMachines();
for (int i = 0; i < machines.size(); i++) {
val m = machines.get(i);
sender.sendMessage("[" + i + "] " + m.getName() + " - " + m.getState());
}
}
/** /**
* Gets called when the machine is locked after {@link #Start(CommandSender, int)} * Gets called when the machine is locked after {@link #Start(CommandSender, int)}
* *

View file

@ -0,0 +1,35 @@
computer {
start {
index brigadier:integer;
}
on {
index brigadier:integer;
}
list;
stop;
off;
shutdown;
kill;
powerbutton;
reset;
restart;
fix;
fixscreen;
key {
key brigadier:string single_word;
}
press {
key brigadier:string single_word;
}
mouse {
button brigadier:string single_word;
}
input {
key;
mouse;
}
show {
key;
mouse;
}
}

View file

@ -33,6 +33,10 @@
<id>spigot-repo</id> <id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> <url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository> </repository>
<repository>
<id>jitpack</id>
<url>https://jitpack.io/</url>
</repository>
</repositories> </repositories>
<dependencies> <dependencies>