diff --git a/src/cn/citycraft/SimpleEssential/SimpleEssential.java b/src/cn/citycraft/SimpleEssential/SimpleEssential.java index 7467561..05631de 100644 --- a/src/cn/citycraft/SimpleEssential/SimpleEssential.java +++ b/src/cn/citycraft/SimpleEssential/SimpleEssential.java @@ -6,6 +6,9 @@ package cn.citycraft.SimpleEssential; import org.bukkit.plugin.java.JavaPlugin; import cn.citycraft.SimpleEssential.config.Config; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommandHandler; +import cn.citycraft.SimpleEssential.handler.teleport.TeleportControl; +import cn.citycraft.SimpleEssential.listen.PlayerLocationListen; /** * @author 蒋天蓓 @@ -13,6 +16,7 @@ import cn.citycraft.SimpleEssential.config.Config; * TODO */ public class SimpleEssential extends JavaPlugin { + public TeleportControl tpcontrol; @Override public void onLoad() { @@ -26,8 +30,9 @@ public class SimpleEssential extends JavaPlugin { @Override public void onEnable() { - - this.getLogger().info(""); + new SimpleEssentialCommandHandler(); + tpcontrol = new TeleportControl(this); + getServer().getPluginManager().registerEvents(new PlayerLocationListen(this), this); } } diff --git a/src/cn/citycraft/SimpleEssential/command/SimpleEssentialCommand.java b/src/cn/citycraft/SimpleEssential/command/SimpleEssentialCommand.java deleted file mode 100644 index 4ea871a..0000000 --- a/src/cn/citycraft/SimpleEssential/command/SimpleEssentialCommand.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * - */ -package cn.citycraft.SimpleEssential.command; - -import java.util.List; - -import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; - -import cn.citycraft.SimpleEssential.SimpleEssential; - -/** - * @author 蒋天蓓 - * 2015年8月11日下午4:00:35 - * TODO - */ -public class SimpleEssentialCommand extends SimpleEssential { - - @Override - public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - // TODO 自动生成的方法存根 - return super.onCommand(sender, command, label, args); - } - - @Override - public List onTabComplete(CommandSender sender, Command command, String alias, - String[] args) { - // TODO 自动生成的方法存根 - return super.onTabComplete(sender, command, alias, args); - } - -} diff --git a/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommand.java b/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommand.java new file mode 100644 index 0000000..e903d0b --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommand.java @@ -0,0 +1,119 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler; + +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午12:49:34 + * TODO + */ +public abstract class SimpleEssentialCommand { + private String name; + private String permission; + private String[] aliases; + + public SimpleEssentialCommand(String name) { + this(name, new String[0]); + } + + public SimpleEssentialCommand(String name, String... aliases) { + this.name = name; + this.aliases = aliases; + } + + /** + * 获取命令名称 + * + * @return 命令名称 + */ + public String getName() { + return name; + } + + /** + * 设置命令权限 + * + * @param permission + * - 命令权限 + */ + public void setPermission(String permission) { + this.permission = permission; + } + + /** + * 获得命令权限 + * + * @return 目录命令权限 + */ + public String getPermission() { + return permission; + } + + /** + * 检查Sender权限 + * + * @param sender + * - 命令发送者 + * @return 是否有权限执行命令 + */ + public final boolean hasPermission(CommandSender sender) { + if (permission == null) + return true; + return sender.hasPermission(permission); + } + + /** + * 获得可能的参数 + * + * @return + */ + public abstract String getPossibleArguments(); + + /** + * 获得最小参数组 + * + * @return 最小参数组 + */ + public abstract int getMinimumArguments(); + + /** + * 执行命令参数 + * + * @param sender + * - 命令发送者 + * @param label + * - 命令 + * @param args + * - 命令附加参数 + * @throws CommandException + * - 命令异常 + */ + public abstract void execute(CommandSender sender, String label, String[] args) + throws CommandException; + + /** + * 命令匹配检测 + * + * @param name + * - 命令 + * @return 是否匹配 + */ + public final boolean isValidTrigger(String name) { + if (this.name.equalsIgnoreCase(name)) { + return true; + } + if (aliases != null) { + for (String alias : aliases) { + if (alias.equalsIgnoreCase(name)) { + return true; + } + } + } + return false; + } + +} diff --git a/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommandHandler.java b/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommandHandler.java new file mode 100644 index 0000000..2256589 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/SimpleEssentialCommandHandler.java @@ -0,0 +1,77 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.command.CommandBack; +import cn.citycraft.SimpleEssential.handler.command.CommandTpa; +import cn.citycraft.SimpleEssential.handler.command.CommandTpaccept; +import cn.citycraft.SimpleEssential.handler.command.CommandTpdeny; + +/** + * @author 蒋天蓓 + * 2015年8月11日下午4:00:35 + * TODO + */ +public class SimpleEssentialCommandHandler extends SimpleEssential { + private List commandlist; + + public SimpleEssentialCommandHandler() { + commandlist = new ArrayList(); + + // registerSubCommand(new AddlineCommand()); + registerSubCommand(new CommandTpa(this)); + registerSubCommand(new CommandTpaccept(this)); + registerSubCommand(new CommandTpdeny(this)); + registerSubCommand(new CommandBack(this)); + } + + /** + * 注册命令 + * + * @param subCommand + * - 被注册的命令类 + */ + public void registerSubCommand(SimpleEssentialCommand subCommand) { + commandlist.add(subCommand); + } + + /** + * 获取命令列表 + * + * @return 命令列表 + */ + public List getSubCommands() { + return new ArrayList(commandlist); + } + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + for (SimpleEssentialCommand command : commandlist) { + if (command.isValidTrigger(label)) { + if (!command.hasPermission(sender)) { + sender.sendMessage(ChatColor.RED + "你没有此命令的权限."); + return true; + } + if (args.length >= command.getMinimumArguments()) { + try { + command.execute(sender, label, args); + return true; + } catch (CommandException e) { + sender.sendMessage(e.getMessage()); + } + } + } + } + return false; + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/command/CommandBack.java b/src/cn/citycraft/SimpleEssential/handler/command/CommandBack.java new file mode 100644 index 0000000..29f04a1 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/command/CommandBack.java @@ -0,0 +1,43 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.command; + +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommand; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:04:05 + * TODO + */ +public class CommandBack extends SimpleEssentialCommand { + SimpleEssential plugin; + + /** + * @param name + */ + public CommandBack(SimpleEssential main) { + super("tpback"); + this.plugin = main; + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + plugin.tpcontrol.back((Player) sender); + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/command/CommandTpa.java b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpa.java new file mode 100644 index 0000000..52ce16e --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpa.java @@ -0,0 +1,56 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.command; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommand; +import cn.citycraft.SimpleEssential.handler.teleport.TeleportType; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:04:05 + * TODO + */ +public class CommandTpa extends SimpleEssentialCommand { + SimpleEssential plugin; + + /** + * @param name + */ + public CommandTpa(SimpleEssential main) { + super("tpa"); + this.plugin = main; + } + + @Override + public String getPossibleArguments() { + return "<目标玩家>"; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player target = Bukkit.getPlayer(args[0]); + if (target == null) { + sender.sendMessage("玩家 " + args[0] + " 不存在或不在线!"); + return; + } + plugin.tpcontrol.addtp((Player) sender, Bukkit.getPlayer(args[0]), TeleportType.TPA); + sender.sendMessage("已经向玩家 " + target.getDisplayName() + " 发送传送请求!"); + target.sendMessage(new String[] { + "§a玩家: " + sender.getName() + "§a请求传送到你这里!", + "§a输入命令/tpaccept 或 /tpok 接受传送", + "§c输入命令/tpdeny 或 /tpno 拒绝传送" + }); + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/command/CommandTpaccept.java b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpaccept.java new file mode 100644 index 0000000..af8f28f --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpaccept.java @@ -0,0 +1,43 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.command; + +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommand; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:04:05 + * TODO + */ +public class CommandTpaccept extends SimpleEssentialCommand { + SimpleEssential plugin; + + /** + * @param name + */ + public CommandTpaccept(SimpleEssential main) { + super("tpaccept", "tpok"); + this.plugin = main; + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + plugin.tpcontrol.accept((Player) sender); + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/command/CommandTpdeny.java b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpdeny.java new file mode 100644 index 0000000..18c5970 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/command/CommandTpdeny.java @@ -0,0 +1,44 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.command; + +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommand; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:04:05 + * TODO + */ +public class CommandTpdeny extends SimpleEssentialCommand { + SimpleEssential plugin; + + /** + * @param name + */ + public CommandTpdeny(SimpleEssential main) { + super("tpdeny", "tpno"); + this.plugin = main; + } + + @Override + public String getPossibleArguments() { + return ""; + } + + @Override + public int getMinimumArguments() { + return 0; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + plugin.tpcontrol.accept((Player) sender); + + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/command/CommandTphere.java b/src/cn/citycraft/SimpleEssential/handler/command/CommandTphere.java new file mode 100644 index 0000000..6dbf84e --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/command/CommandTphere.java @@ -0,0 +1,56 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.command; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.handler.SimpleEssentialCommand; +import cn.citycraft.SimpleEssential.handler.teleport.TeleportType; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:04:05 + * TODO + */ +public class CommandTphere extends SimpleEssentialCommand { + SimpleEssential plugin; + + /** + * @param name + */ + public CommandTphere(SimpleEssential main) { + super("tphere", "tph"); + this.plugin = main; + } + + @Override + public String getPossibleArguments() { + return "<目标玩家>"; + } + + @Override + public int getMinimumArguments() { + return 1; + } + + @Override + public void execute(CommandSender sender, String label, String[] args) throws CommandException { + Player target = Bukkit.getPlayer(args[0]); + if (target == null) { + sender.sendMessage("玩家 " + args[0] + " 不存在或不在线!"); + return; + } + plugin.tpcontrol.addtp((Player) sender, Bukkit.getPlayer(args[0]), TeleportType.TPH); + sender.sendMessage("已经向玩家 " + target.getDisplayName() + " 发送传送请求!"); + target.sendMessage(new String[] { + "§b玩家: " + sender.getName() + "§b请求你传送到他那里!", + "§a输入命令/tpaccept 或 /tpok 接受传送", + "§c输入命令/tpdeny 或 /tpno 拒绝传送" + }); + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportControl.java b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportControl.java new file mode 100644 index 0000000..a0c249d --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportControl.java @@ -0,0 +1,179 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.teleport; + +import java.util.HashMap; + +import org.bukkit.Effect; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; + +import cn.citycraft.SimpleEssential.SimpleEssential; +import cn.citycraft.SimpleEssential.config.Config; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:26:10 + * 传送控制类 + */ +public class TeleportControl { + protected HashMap teleportList = new HashMap(); + protected HashMap lastlocList = new HashMap(); + private SimpleEssential plugin; + private int TpDelay = Config.getInstance().getInt("tpdelay", 3); + + public TeleportControl(SimpleEssential plugin) { + this.plugin = plugin; + } + + /** + * 添加传送请求到传送列表 + * + * @param player + * - 发出请求的玩家 + * @param target + * - 目标玩家 + * @param tptype + * - 传送类型 + */ + public void addtp(Player player, Player target, TeleportType tptype) { + teleportList.put(target, new TeleportInfo(player, tptype)); + } + + /** + * 玩家接受传送请求 + * + * @param player + * - 执行的玩家 + */ + public void accept(Player player) { + TeleportInfo ti = teleportList.remove(player); + if (ti != null) { + Player target = ti.getTarget(); + Location loc = null; + if (!target.isOnline()) { + player.sendMessage("§c目标玩家已离线 传送失败!"); + return; + } + target.sendMessage("§a玩家: " + player.getDisplayName() + "§a受了您的请求!"); + if (ti.getTptype() == TeleportType.TPA) { + target = ti.getTarget(); + loc = player.getLocation(); + } else { + target = player; + loc = ti.getTarget().getLocation(); + } + magicTeleport(target, loc, TpDelay); + return; + } + player.sendMessage("§c未找到需要处理的队列!"); + } + + /** + * 玩家拒绝传送请求 + * + * @param player + * - 执行的玩家 + */ + public void deny(Player player) { + TeleportInfo ti = teleportList.remove(player); + if (ti != null) { + player.sendMessage("§c已拒绝玩家的传送请求!"); + Player target = ti.getTarget(); + if (target.isOnline()) { + target.sendMessage("§c玩家: " + player.getDisplayName() + "§c拒绝了您的请求!"); + } + return; + } + player.sendMessage("§c未找到需要处理的队列!"); + } + + /** + * 设置最后的地址数据 + * + * @param player + * - 玩家 + * @param loc + * - 地点 + */ + public void setLastloc(Player player, Location loc) { + lastlocList.put(player, loc); + } + + /** + * 玩家返回上次传送的地点 + * + * @param player + * - 被传送的玩家 + */ + public void back(Player player) { + Location loc = lastlocList.get(player); + if (loc != null) { + magicTeleport(player, loc, 3); + } else { + player.sendMessage("§c未找到可以Back的地点!"); + } + } + + /** + * 魔法传送 + * + * @param player + * - 被传送的玩家 + * @param loc + * - 传送的地点 + * @param delay + * - 传送延时 + */ + public void magicTeleport(final Player player, final Location loc, final int delay) { + setLastloc(player, player.getLocation()); + player.sendMessage("§a传送开始 " + delay + "秒 后到达目的地 世界: " + loc.getWorld() + " X: " + + loc.getX() + " Z: " + loc.getZ() + "!"); + player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, delay, 255)); + plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() { + long timeoutmark = System.currentTimeMillis() + delay * 1000; + long lrng = 0; + + @Override + public void run() { + while (System.currentTimeMillis() < timeoutmark) { + if (player.isOnline()) + pEffect(player.getLocation(), lrng); + lrng++; + try { + Thread.sleep(50); + } catch (Exception e) { + } + } + } + }); + plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() { + @Override + public void run() { + if (player.isOnline()) + player.teleport(loc); + } + }, delay * 20); + } + + /** + * 粒子发生器 + * + * @param loc + * - 粒子产生的地点 + * @param range + * - 粒子的数量 + */ + static void pEffect(Location loc, long range) { + int i; + if (range < 2) + range = 2; + for (i = 0; i < range; i++) { + loc.getWorld().playEffect(loc, Effect.LAVA_POP, 10, 100); + loc.getWorld().playEffect(loc, Effect.PORTAL, 10, 100); + } + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportInfo.java b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportInfo.java new file mode 100644 index 0000000..a0814a5 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportInfo.java @@ -0,0 +1,29 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.teleport; + +import org.bukkit.entity.Player; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:56:25 + * TODO + */ +public class TeleportInfo { + protected TeleportType tptype; + protected Player target; + + public TeleportInfo(Player target, TeleportType tptype) { + this.target = target; + this.tptype = tptype; + } + + public TeleportType getTptype() { + return tptype; + } + + public Player getTarget() { + return target; + } +} diff --git a/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportType.java b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportType.java new file mode 100644 index 0000000..2e8c688 --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/handler/teleport/TeleportType.java @@ -0,0 +1,13 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.handler.teleport; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午2:56:59 + * TODO + */ +public enum TeleportType { + TPA, TPH +} diff --git a/src/cn/citycraft/SimpleEssential/listen/PlayerLocationListen.java b/src/cn/citycraft/SimpleEssential/listen/PlayerLocationListen.java new file mode 100644 index 0000000..4ce398d --- /dev/null +++ b/src/cn/citycraft/SimpleEssential/listen/PlayerLocationListen.java @@ -0,0 +1,32 @@ +/** + * + */ +package cn.citycraft.SimpleEssential.listen; + +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; + +import cn.citycraft.SimpleEssential.SimpleEssential; + +/** + * @author 蒋天蓓 + * 2015年8月12日下午8:19:33 + * TODO + */ +public class PlayerLocationListen implements Listener { + SimpleEssential plugin; + + public PlayerLocationListen(SimpleEssential main) { + this.plugin = main; + } + + @EventHandler + public void onPlayerDeath(PlayerDeathEvent e) { + Player player = e.getEntity(); + Location loc = player.getLocation(); + plugin.tpcontrol.setLastloc(player, loc); + } +} diff --git a/src/plugin.yml b/src/plugin.yml index 004e561..cfbce57 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -29,7 +29,7 @@ commands: permission-message: §c你没有 的权限来执行此命令! tpdeny: description: 拒绝传送或邀请 - aliases: [tono] + aliases: [tpno] usage: §b使用/tpdeny 拒绝传送或邀请! permission: se.tpdeny permission-message: §c你没有 的权限来执行此命令!