add TabComplete...

This commit is contained in:
j502647092 2015-08-21 23:35:15 +08:00
parent ee8afe08ed
commit 5a4a55fd9f
2 changed files with 45 additions and 4 deletions

View File

@ -3,11 +3,17 @@
*/ */
package cn.citycraft.Yum; package cn.citycraft.Yum;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.StringUtil;
import cn.citycraft.Yum.utils.DownloadUtils; import cn.citycraft.Yum.utils.DownloadUtils;
import cn.citycraft.Yum.utils.PluginUtil; import cn.citycraft.Yum.utils.PluginUtil;
@ -18,6 +24,8 @@ import cn.citycraft.Yum.utils.PluginUtil;
* @author 蒋天蓓 2015年8月21日下午5:14:39 * @author 蒋天蓓 2015年8月21日下午5:14:39
*/ */
public class Yum extends JavaPlugin { public class Yum extends JavaPlugin {
DownloadUtils download;
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
switch (args.length) { switch (args.length) {
@ -38,7 +46,7 @@ public class Yum extends JavaPlugin {
switch (args[0]) { switch (args[0]) {
case "install": case "install":
if (plugin != null) { if (plugin != null) {
if (DownloadUtils.download(sender, args[1])) { if (download.download(sender, args[1])) {
sender.sendMessage(PluginUtil.load(args[1])); sender.sendMessage(PluginUtil.load(args[1]));
} }
} else { } else {
@ -55,7 +63,7 @@ public class Yum extends JavaPlugin {
case "update": case "update":
if (plugin != null) { if (plugin != null) {
sender.sendMessage(PluginUtil.unload(plugin)); sender.sendMessage(PluginUtil.unload(plugin));
if (DownloadUtils.download(sender, args[1])) { if (download.download(sender, args[1])) {
sender.sendMessage(PluginUtil.load(args[1])); sender.sendMessage(PluginUtil.load(args[1]));
} }
} else { } else {
@ -65,5 +73,31 @@ public class Yum extends JavaPlugin {
} }
} }
return true; return true;
};
@Override
public void onEnable() {
download = new DownloadUtils(this);
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
final String[] COMMANDS = { "install", "remove", "list", "update" };
if (sender.isOp() || sender.hasPermission("yum.admin") || sender.hasPermission("yum." + args[0])) {
List<String> completions = new ArrayList<>();
if (args.length == 1) {
String partialCommand = args[0];
List<String> commands = new ArrayList<>(Arrays.asList(COMMANDS));
StringUtil.copyPartialMatches(partialCommand, commands, completions);
}
if (args.length == 2) {
String partialPlugin = args[1];
List<String> plugins = PluginUtil.getPluginNames(false);
StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
}
Collections.sort(completions);
return completions;
}
return null;
} }
} }

View File

@ -10,12 +10,19 @@ import java.net.URL;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
/** /**
* @author 蒋天蓓 2015年8月21日下午6:08:09 TODO * @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
*/ */
public class DownloadUtils { public class DownloadUtils {
public static boolean download(CommandSender sender, String pluginname) { Plugin plugin;
public DownloadUtils(Plugin main) {
this.plugin = main;
}
public boolean download(CommandSender sender, String pluginname) {
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar"; String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
BufferedInputStream in = null; BufferedInputStream in = null;
FileOutputStream fout = null; FileOutputStream fout = null;
@ -67,7 +74,7 @@ public class DownloadUtils {
} }
} }
private static String getPer(double per) { private String getPer(double per) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
if (per > i) { if (per > i) {