From 19cb7cadc385df9c254452d88e89d56dfc2ee3ce Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 8 Aug 2016 20:39:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=89=A9=E4=BD=99?= =?UTF-8?q?=E4=BC=A0=E9=80=81=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/TeleportRandom/TeleportRandom.java | 108 ++++++++++-------- src/main/resources/config.yml | 4 +- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/main/java/pw/yumc/TeleportRandom/TeleportRandom.java b/src/main/java/pw/yumc/TeleportRandom/TeleportRandom.java index e29d18b..6e962e8 100644 --- a/src/main/java/pw/yumc/TeleportRandom/TeleportRandom.java +++ b/src/main/java/pw/yumc/TeleportRandom/TeleportRandom.java @@ -1,8 +1,8 @@ package pw.yumc.TeleportRandom; -import java.util.HashSet; +import java.util.HashMap; +import java.util.Map; import java.util.Random; -import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -22,7 +22,7 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { public final static Random rr = new Random(); public static FileConfig config; - private final Set cd = new HashSet<>(); + private final Map cd = new HashMap<>(); @Override public boolean onCommand(final CommandSender sender, final Command cmd, final String string, final String[] args) { @@ -88,54 +88,76 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { if (world == null || p == null) { throw new IllegalArgumentException(); } - if (!p.hasPermission("tpr.nocd") && cd.contains(p.getName())) { - p.sendMessage(getPrefix() + config.getMessage("Message.CD")); - return; - } - if (!config.getStringList("AllowWorld").contains(world.getName())) { - p.sendMessage(getPrefix() + config.getMessage("Message.NoPermWorld")); - } - int lr = config.getInt("default"); - if (limit == 0) { - p.sendMessage(getPrefix() + config.getMessage("Message.default1")); - p.sendMessage(getPrefix() + config.getMessage("Message.default2")); - } else { - lr = limit; - final int lrLimit = config.getInt("Limit"); - if (lr > lrLimit) { - lr = lrLimit; - p.sendMessage(String.format(getPrefix() + config.getMessage("Message.Wran"), lrLimit)); - } - } - final Point point = new Point(lr, world); - checkBlock(point, p); - point.tp(p); - cd.add(p.getName()); - getServer().getScheduler().runTaskLater(this, new Runnable() { + getServer().getScheduler().runTaskAsynchronously(this, new Runnable() { @Override public void run() { - cd.remove(p.getName()); + final long t = System.currentTimeMillis(); + if (!p.hasPermission("tpr.nocd") && cd.keySet().contains(p.getName())) { + p.sendMessage(getPrefix() + config.getMessage("Message.CDs").replace("{0}", String.format("%0.2f", (t - cd.get(p.getName())) / 1000.00))); + return; + } + if (!config.getStringList("AllowWorld").contains(world.getName())) { + p.sendMessage(getPrefix() + config.getMessage("Message.NoPermWorld")); + return; + } + int lr = config.getInt("default"); + if (limit == 0) { + p.sendMessage(getPrefix() + config.getMessage("Message.default1")); + p.sendMessage(getPrefix() + config.getMessage("Message.default2")); + } else { + lr = limit; + final int lrLimit = config.getInt("Limit"); + if (lr > lrLimit) { + lr = lrLimit; + p.sendMessage(String.format(getPrefix() + config.getMessage("Message.Wran"), lrLimit)); + } + } + final Point point = new Point(lr, world); + getServer().getScheduler().runTask(TeleportRandom.this, new Runnable() { + @Override + public void run() { + checkBlock(point, p); + tp(point, p); + } + }); + cd.put(p.getName(), t); + getServer().getScheduler().runTaskLater(TeleportRandom.this, new Runnable() { + @Override + public void run() { + cd.remove(p.getName()); + } + }, config.getLong("CD")); } - }, config.getLong("CD")); + }); + } + + public void tp(final Point point, final Player p) { + final Location loc = getTpLocation(point); + p.teleport(loc); + p.sendMessage(String.format(getPluginname() + config.getMessage("Message.Tip"), loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); } private void checkBlock(final Point point, final Player p) { - final Material rbm = point.getBlock().getType(); + final Material rbm = getBlock(point).getType(); final String blockname = rbm.name(); for (final String protectblock : config.getStringList("ProtectBlock")) { if (protectblock.equalsIgnoreCase(blockname)) { - point.getBlock().setType(Material.GLASS); + getBlock(point).setType(Material.GLASS); p.sendMessage(getPrefix() + config.getMessage("Message.Protect")); this.getServer().getScheduler().runTaskLater(this, new Runnable() { @Override public void run() { - point.getBlock().setType(rbm); + getBlock(point).setType(rbm); } }, 200); } } } + private Block getBlock(final Point point) { + return new Location(point.world, point.x, point.world.getHighestBlockYAt(point.x, point.z) - 1, point.z).getBlock(); + } + private String getPluginname() { return config.getMessage("pluginname"); } @@ -148,9 +170,12 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { return config.getMessage("servername"); } + private Location getTpLocation(final Point point) { + return new Location(point.world, point.x, point.world.getHighestBlockYAt(point.x, point.z) + 3, point.z); + } + class Point { public int x; - public int y; public int z; public int limit; public World world; @@ -168,23 +193,6 @@ public class TeleportRandom extends JavaPlugin implements CommandExecutor { if (yf % 2 != 0) { z = -z; } - y = world.getHighestBlockYAt(x, z); - } - - public Block getBlock() { - return new Location(world, x, y - 1, z).getBlock(); - } - - public Location getTpLocation() { - return new Location(world, x, y + 3, z); - } - - public void sendMessage(final Player p) { - p.sendMessage(String.format(getPluginname() + config.getMessage("Message.Tip"), world.getName(), x, y, z)); - } - - public void tp(final Player p) { - p.teleport(getTpLocation()); } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 62cd7f7..c984146 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,5 +1,5 @@ #本文件为随机传送插件的主配置文件 -Version: 1.4 +Version: 1.5 #服务器名称 servername: '' #插件名称 @@ -25,7 +25,7 @@ Message: #当前世界不允许传送 NoPermWorld: '&c当前世界不允许使用随机传送!' #当前世界不允许传送 - CD: '&c随机传送冷却中!' + CDs: '&c随机传送冷却中 剩余 {0} 秒!' #允许传送的世界(区分大小写) AllowWorld: - world