From 5a4a55fd9fa3886a2d3ea1ba4936529d1e85ee73 Mon Sep 17 00:00:00 2001 From: j502647092 Date: Fri, 21 Aug 2015 23:35:15 +0800 Subject: [PATCH] add TabComplete... --- src/main/java/cn/citycraft/Yum/Yum.java | 38 ++++++++++++++++++- .../cn/citycraft/Yum/utils/DownloadUtils.java | 11 +++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/Yum.java b/src/main/java/cn/citycraft/Yum/Yum.java index 376f6ba..8912ec3 100644 --- a/src/main/java/cn/citycraft/Yum/Yum.java +++ b/src/main/java/cn/citycraft/Yum/Yum.java @@ -3,11 +3,17 @@ */ 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.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.StringUtil; import cn.citycraft.Yum.utils.DownloadUtils; import cn.citycraft.Yum.utils.PluginUtil; @@ -18,6 +24,8 @@ import cn.citycraft.Yum.utils.PluginUtil; * @author 蒋天蓓 2015年8月21日下午5:14:39 */ public class Yum extends JavaPlugin { + DownloadUtils download; + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { switch (args.length) { @@ -38,7 +46,7 @@ public class Yum extends JavaPlugin { switch (args[0]) { case "install": if (plugin != null) { - if (DownloadUtils.download(sender, args[1])) { + if (download.download(sender, args[1])) { sender.sendMessage(PluginUtil.load(args[1])); } } else { @@ -55,7 +63,7 @@ public class Yum extends JavaPlugin { case "update": if (plugin != null) { sender.sendMessage(PluginUtil.unload(plugin)); - if (DownloadUtils.download(sender, args[1])) { + if (download.download(sender, args[1])) { sender.sendMessage(PluginUtil.load(args[1])); } } else { @@ -65,5 +73,31 @@ public class Yum extends JavaPlugin { } } return true; + }; + + @Override + public void onEnable() { + download = new DownloadUtils(this); + } + + @Override + public List 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 completions = new ArrayList<>(); + if (args.length == 1) { + String partialCommand = args[0]; + List commands = new ArrayList<>(Arrays.asList(COMMANDS)); + StringUtil.copyPartialMatches(partialCommand, commands, completions); + } + if (args.length == 2) { + String partialPlugin = args[1]; + List plugins = PluginUtil.getPluginNames(false); + StringUtil.copyPartialMatches(partialPlugin, plugins, completions); + } + Collections.sort(completions); + return completions; + } + return null; } } diff --git a/src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java b/src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java index 86a1a5e..c743c90 100644 --- a/src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java +++ b/src/main/java/cn/citycraft/Yum/utils/DownloadUtils.java @@ -10,12 +10,19 @@ import java.net.URL; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; /** * @author 蒋天蓓 2015年8月21日下午6:08:09 TODO */ 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"; BufferedInputStream in = 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(); for (int i = 0; i < 10; i++) { if (per > i) {