From 8b782a691053f8dbd88d21ab15d564d388427def Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 18 Aug 2015 16:54:29 +0800 Subject: [PATCH] add VersionChecer add TPR World and updata config class... --- .../TeleportRandom/TeleportRandom.java | 93 +++++++-------- .../TeleportRandom/utils/VersionChecker.java | 112 ++++++++++++++++++ .../citycraft/{Utils => }/config/Config.java | 2 +- .../{Utils => }/config/ConfigLoader.java | 2 +- .../{Utils => }/config/FileConfig.java | 2 +- src/plugin.yml | 2 +- 6 files changed, 162 insertions(+), 51 deletions(-) create mode 100644 src/cn/citycraft/TeleportRandom/utils/VersionChecker.java rename src/cn/citycraft/{Utils => }/config/Config.java (97%) rename src/cn/citycraft/{Utils => }/config/ConfigLoader.java (98%) rename src/cn/citycraft/{Utils => }/config/FileConfig.java (98%) diff --git a/src/cn/citycraft/TeleportRandom/TeleportRandom.java b/src/cn/citycraft/TeleportRandom/TeleportRandom.java index 8a914d2..8938817 100644 --- a/src/cn/citycraft/TeleportRandom/TeleportRandom.java +++ b/src/cn/citycraft/TeleportRandom/TeleportRandom.java @@ -2,6 +2,7 @@ package cn.citycraft.TeleportRandom; import java.util.Random; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -11,7 +12,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; -import cn.citycraft.Utils.config.Config; +import cn.citycraft.config.Config; public class TeleportRandom extends JavaPlugin implements CommandExecutor { public String servername; @@ -23,30 +24,37 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { } @Override - public boolean onCommand(CommandSender sender, Command cmd, String string, - String[] args) { - if (cmd.getName().equalsIgnoreCase("tpr")) { - if (args.length != 1) - return false; - if (args[0].equalsIgnoreCase("reload")) { - this.onLoad(); - sender.sendMessage(servername + pluginname - + getmessage("Message.Reload")); - return true; - } - if (sender instanceof Player) { - Player p = (Player) sender; - if (!p.hasPermission("tpr.use")) { - sender.sendMessage(servername + pluginname - + getmessage("Message.NoPerm")); - return true; - } - RandomTP(args, p); - return true; - } - sender.sendMessage(servername + pluginname + "控制台无法使用此命令!"); + public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) { + if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { + this.onLoad(); + sender.sendMessage(servername + pluginname + getmessage("Message.Reload")); return true; } + if (sender instanceof Player) { + Player p = (Player) sender; + if (!p.hasPermission("tpr.use")) { + sender.sendMessage(servername + pluginname + getmessage("Message.NoPerm")); + return true; + } + try { + switch (args.length) { + case 0: + RandomTP(0, p); + break; + case 1: + RandomTP(Integer.parseInt(args[0]), p); + break; + case 2: + RandomTP(Integer.parseInt(args[0]), Bukkit.getWorld(args[1]), p); + break; + } + } catch (Exception e) { + sender.sendMessage("§c非法的参数或不存在的世界!"); + } + return true; + } else { + sender.sendMessage("§c控制台无法使用此命令!"); + } return false; } @@ -57,27 +65,24 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { pluginname = getmessage("pluginname"); } - public void RandomTP(String[] limit, Player p) { + public void RandomTP(int limit, Player p) { + RandomTP(limit, p.getWorld(), p); + } + + public void RandomTP(int limit, World world, Player p) { Random rr = new Random(); int lr = Config.getInstance().getInt("default"); - if (limit.length != 1) { - p.sendMessage(servername + pluginname - + getmessage("Message.default1")); - p.sendMessage(servername + pluginname - + getmessage("Message.default2")); + if (limit == 0) { + p.sendMessage(servername + pluginname + getmessage("Message.default1")); + p.sendMessage(servername + pluginname + getmessage("Message.default2")); } else { - lr = Integer.parseInt(limit[0]); + lr = limit; int lrLimit = Config.getInstance().getInt("Limit"); if (lr > lrLimit) { lr = lrLimit; - p.sendMessage(servername - + pluginname - + getmessage("Message.Wran").replace("%limit%", - lrLimit + "")); + p.sendMessage(servername + pluginname + getmessage("Message.Wran").replace("%limit%", lrLimit + "")); } } - Location l = p.getLocation(); - World world = l.getWorld(); int x = rr.nextInt(lr); int z = rr.nextInt(lr); int xf = rr.nextInt(x); @@ -92,14 +97,11 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { final Location trl = new Location(world, x, y - 1, z); final Material rbm = trl.getBlock().getType(); String blockname = rbm.name(); - for (String protectblock : Config.getInstance().getStringList( - "ProtectBlock")) { + for (String protectblock : Config.getInstance().getStringList("ProtectBlock")) { if (protectblock.equalsIgnoreCase(blockname)) { trl.getBlock().setType(Material.GLASS); - p.sendMessage(servername + pluginname - + getmessage("Message.Protect")); - this.getServer().getScheduler() - .runTaskLater(this, new Runnable() { + p.sendMessage(servername + pluginname + getmessage("Message.Protect")); + this.getServer().getScheduler().runTaskLater(this, new Runnable() { @Override public void run() { trl.getBlock().setType(rbm); @@ -109,10 +111,7 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { } Location nrl = new Location(world, x, y + 3, z); p.teleport(nrl); - p.sendMessage(servername - + pluginname - + getmessage("Message.Tip") - .replaceAll("%world%", world.getName()) - .replaceAll("%x%", x + "").replaceAll("%z%", z + "")); + p.sendMessage(servername + pluginname + + getmessage("Message.Tip").replaceAll("%world%", world.getName()).replaceAll("%x%", x + "").replaceAll("%z%", z + "")); } } diff --git a/src/cn/citycraft/TeleportRandom/utils/VersionChecker.java b/src/cn/citycraft/TeleportRandom/utils/VersionChecker.java new file mode 100644 index 0000000..c95cfbe --- /dev/null +++ b/src/cn/citycraft/TeleportRandom/utils/VersionChecker.java @@ -0,0 +1,112 @@ +package cn.citycraft.TeleportRandom.utils; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.plugin.Plugin; + +import com.google.common.base.Charsets; + +/** + * 自动更新类 + * + * @author 蒋天蓓 + * 2015年8月14日下午4:01:15 + */ +public class VersionChecker implements Listener { + Plugin plugin; + public String checkurl = "https://coding.net/u/502647092/p/%s/git/raw/%s/src/plugin.yml"; + public String branch = "master"; + + /** + * @param plugin + * - 插件 + */ + public VersionChecker(Plugin plugin) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.versioncheck(null); + } + + /** + * @param plugin + * - 插件 + * @param branch + * - 分支名称 + */ + public VersionChecker(Plugin plugin, String branch) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.checkurl = branch; + this.versioncheck(null); + } + + /** + * 获取插件更新链接 + * + * @param pluginName + * - 插件名称 + * @param branch + * - 插件分支 + * @return 更新链接 + */ + public String getCheckUrl(String pluginName, String branch) { + return String.format(checkurl, pluginName, branch); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + if (e.getPlayer().isOp()) { + this.versioncheck(e.getPlayer()); + } + } + + /** + * 开始更新 + * + * @param player + * - 获取更新的玩家(null则默认为控制台) + */ + public void versioncheck(final Player player) { + Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { + @Override + public void run() { + String readURL = getCheckUrl(plugin.getName(), branch); + FileConfiguration config; + String currentVersion = plugin.getDescription().getVersion(); + try { + URL url = new URL(readURL); + BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); + config = YamlConfiguration.loadConfiguration(br); + String newVersion = config.getString("version"); + br.close(); + if (!newVersion.equals(currentVersion)) { + String[] msg = new String[] { + ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion, + ChatColor.RED + "服务器运行版本: v" + currentVersion, + ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite() + }; + if (player != null) { + player.sendMessage(msg); + } else { + plugin.getServer().getConsoleSender().sendMessage(msg); + } + } + } catch (IOException e) { + plugin.getLogger().warning("版本更新检查失败!"); + } + } + }); + } + +} diff --git a/src/cn/citycraft/Utils/config/Config.java b/src/cn/citycraft/config/Config.java similarity index 97% rename from src/cn/citycraft/Utils/config/Config.java rename to src/cn/citycraft/config/Config.java index 48b17ae..18c866b 100644 --- a/src/cn/citycraft/Utils/config/Config.java +++ b/src/cn/citycraft/config/Config.java @@ -1,4 +1,4 @@ -package cn.citycraft.Utils.config; +package cn.citycraft.config; import java.io.File; import java.io.IOException; diff --git a/src/cn/citycraft/Utils/config/ConfigLoader.java b/src/cn/citycraft/config/ConfigLoader.java similarity index 98% rename from src/cn/citycraft/Utils/config/ConfigLoader.java rename to src/cn/citycraft/config/ConfigLoader.java index e53e9eb..21ce458 100644 --- a/src/cn/citycraft/Utils/config/ConfigLoader.java +++ b/src/cn/citycraft/config/ConfigLoader.java @@ -1,4 +1,4 @@ -package cn.citycraft.Utils.config; +package cn.citycraft.config; import java.io.File; import java.io.IOException; diff --git a/src/cn/citycraft/Utils/config/FileConfig.java b/src/cn/citycraft/config/FileConfig.java similarity index 98% rename from src/cn/citycraft/Utils/config/FileConfig.java rename to src/cn/citycraft/config/FileConfig.java index 589081f..4261603 100644 --- a/src/cn/citycraft/Utils/config/FileConfig.java +++ b/src/cn/citycraft/config/FileConfig.java @@ -1,4 +1,4 @@ -package cn.citycraft.Utils.config; +package cn.citycraft.config; import java.io.BufferedReader; import java.io.File; diff --git a/src/plugin.yml b/src/plugin.yml index 8993f4e..486e3b2 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -4,7 +4,7 @@ version: 1.0 commands: tpr: description: 随机传送插件命令 - usage: 使用/tpr [随机传送范围] 进行随机传送! + usage: §6使用§a/tpr [随机传送范围] [随机传送世界]§6进行随机传送! permissions: tpr.*: description: 允许使用所有命令!