From 71b65347d7270e253ac5a889f115506150bb9bf2 Mon Sep 17 00:00:00 2001 From: GeekFrog Date: Sat, 15 Jul 2017 04:58:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=92=E4=BB=B6=E5=9F=BA=E6=9C=AC=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++- .../frog/mc/permissionstime/PluginMain.java | 12 ++- .../mc/permissionstime/command/GiveCmd.java | 19 ++-- .../permissionstime/command/MainCommand.java | 4 +- .../permissionstime/command/RemoveAllCmd.java | 25 ++---- .../mc/permissionstime/command/RemoveCmd.java | 19 ++-- .../mc/permissionstime/command/SetCmd.java | 19 ++-- .../permissionstime/config/PackagesCfg.java | 10 ++- .../impl/SqlitePlayerDataService.java | 7 +- .../listener/MainListener.java | 73 ++++++++++++++++ .../permissionstime/listener/TheListener.java | 39 --------- .../model/cfg/PermissionPackageBean.java | 86 +++++++++++++++---- src/resources/plugin.yml | 3 + 13 files changed, 213 insertions(+), 113 deletions(-) create mode 100644 src/main/gg/frog/mc/permissionstime/listener/MainListener.java delete mode 100644 src/main/gg/frog/mc/permissionstime/listener/TheListener.java diff --git a/README.md b/README.md index 36d6472..2d6fcbb 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,14 @@ #### 构建地址:[http://ci.frog.gg/jenkins/job/PermissionsTime/](http://ci.frog.gg/jenkins/job/PermissionsTime/) -#### 已知问题: +#### 已知问题(作者提醒): 如果出现问题一般退出服务器重进就能解决。一次不行再来一次。 - 如果权限组有继承关系, 被继承的权限组会被大组覆盖, 大组到期被删除, 小组的权限就没有了, 只能重新登录游戏。 +- 服务器关闭时, 插件被禁用的顺序不一, 会导致玩家有权限留存。 如果配置的权限包删掉了原来设置的权限, 会导致插件无法清理原有的权限及权限组。(你可以配置一个不使用的权限包, 权限包内含有你想清理的权限及权限组即可。) -##### 插件进度: +#### 插件进度: 加粗项已完成 @@ -20,11 +21,12 @@ - **命令支持给玩家添加、设置、移除、查询自身权限包时间** - **添加、设置、移除-命令执行失败记录** - **支持不同世界权限** -- 调用vault API 设置玩家权限 -- 登录时(添加 移除)、游戏中(**命令添加/移除** 延迟移除)、退出时移除 +- **调用vault API 设置玩家权限 -- 登录时(添加 移除)、游戏中(命令添加/移除 延迟移除)、退出时移除** - mysql保存数据 - 支持跨服 - 手动删除过期的或无效数据 +- 语言支持整理 - 支持分页? -##### 使用统计: +#### 使用统计: ![image](http://i.mcstats.org/PermissionsTime/Global+Statistics.borderless.png) \ No newline at end of file diff --git a/src/main/gg/frog/mc/permissionstime/PluginMain.java b/src/main/gg/frog/mc/permissionstime/PluginMain.java index 24577c4..2d86468 100644 --- a/src/main/gg/frog/mc/permissionstime/PluginMain.java +++ b/src/main/gg/frog/mc/permissionstime/PluginMain.java @@ -15,7 +15,7 @@ import gg.frog.mc.permissionstime.command.MainCommand; import gg.frog.mc.permissionstime.config.ConfigManager; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; -import gg.frog.mc.permissionstime.listener.TheListener; +import gg.frog.mc.permissionstime.listener.MainListener; import gg.frog.mc.permissionstime.utils.FileUtil; import gg.frog.mc.permissionstime.utils.StrUtil; import net.milkbowl.vault.permission.Permission; @@ -76,7 +76,7 @@ public class PluginMain extends JavaPlugin { * 这里可以注册多个 */ private void registerListeners() { - pm.getServer().getPluginManager().registerEvents(new TheListener(pm), pm); + pm.getServer().getPluginManager().registerEvents(new MainListener(pm), pm); } /** @@ -84,7 +84,13 @@ public class PluginMain extends JavaPlugin { * 这里可以注册多个,一般注册一个就够用 */ private void registerCommands() { - pm.getCommand(PLUGIN_NAME_LOWER_CASE).setExecutor(new MainCommand(pm)); + MainCommand mcmd = new MainCommand(pm); + if (getDescription().getCommands().containsKey(PLUGIN_NAME_LOWER_CASE)) { + getCommand(PLUGIN_NAME_LOWER_CASE).setExecutor(mcmd); + } + if (getDescription().getCommands().containsKey("pt")) { + getCommand("pt").setExecutor(mcmd); + } } public ConfigManager getConfigManager() { diff --git a/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java b/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java index 5c8c570..71243b3 100644 --- a/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java +++ b/src/main/gg/frog/mc/permissionstime/command/GiveCmd.java @@ -1,5 +1,6 @@ package gg.frog.mc.permissionstime.command; +import java.util.List; import java.util.UUID; import org.bukkit.OfflinePlayer; @@ -11,6 +12,7 @@ import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.utils.StrUtil; public class GiveCmd implements Runnable { @@ -55,16 +57,15 @@ public class GiveCmd implements Runnable { } if (sm.giveTime(uuid.toString(), packageName, days)) { if (player.isOnline()) { - pm.getServer().getScheduler().runTask(pm, new Runnable() { - @Override - public void run() { - pack.givePlayer(player, sender, pm.getPermission()); - } - }); - Player p = pm.getServer().getPlayer(uuid); - if (p != null) { - p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}给予你 {1}天的 {2}", sender.getName(), time, pack.getDisplayName())); + Player p = player.getPlayer(); + try { + List pdbList = sm.getTime(player.getUniqueId().toString()); + PermissionPackageBean.reloadPlayerPermissions(player, pdbList, pm); + } catch (Exception e) { + e.printStackTrace(); + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "修改权限失败, 请重新进入服务器!")); } + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}给予你 {1}天的 {2}", sender.getName(), time, pack.getDisplayName())); } sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "给予玩家 {0} {1}天的 {2}", playerName, time, pack.getDisplayName())); } else { diff --git a/src/main/gg/frog/mc/permissionstime/command/MainCommand.java b/src/main/gg/frog/mc/permissionstime/command/MainCommand.java index fe7d5d5..5198dd7 100644 --- a/src/main/gg/frog/mc/permissionstime/command/MainCommand.java +++ b/src/main/gg/frog/mc/permissionstime/command/MainCommand.java @@ -23,7 +23,7 @@ public class MainCommand implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { - if (commandLabel.equalsIgnoreCase(pm.PLUGIN_NAME_LOWER_CASE)) { + if (commandLabel.equalsIgnoreCase(pm.PLUGIN_NAME_LOWER_CASE) || commandLabel.equalsIgnoreCase("pt")) { boolean isPlayer = false; if (sender instanceof Player) { isPlayer = true; @@ -94,7 +94,7 @@ public class MainCommand implements CommandExecutor { } private void getHelp(CommandSender sender, boolean isPlayer) { - sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + pm.PLUGIN_NAME + " Version:" + pm.PLUGIN_VERSION + " =====")); + sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "&a===== " + pm.PLUGIN_NAME + " Version:" + pm.PLUGIN_VERSION + (pm.getDescription().getCommands().containsKey("pt") ? " Aliases:/pt" : "") + " =====")); if (!isPlayer || sender.isOp() || sender.hasPermission(pm.PLUGIN_NAME_LOWER_CASE + ".me")) { sender.sendMessage(StrUtil.messageFormat("/" + pm.PLUGIN_NAME_LOWER_CASE + " me \n - View self package.")); } diff --git a/src/main/gg/frog/mc/permissionstime/command/RemoveAllCmd.java b/src/main/gg/frog/mc/permissionstime/command/RemoveAllCmd.java index 59af054..0c5b4e3 100644 --- a/src/main/gg/frog/mc/permissionstime/command/RemoveAllCmd.java +++ b/src/main/gg/frog/mc/permissionstime/command/RemoveAllCmd.java @@ -8,7 +8,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import gg.frog.mc.permissionstime.PluginMain; -import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; @@ -40,23 +39,17 @@ public class RemoveAllCmd implements Runnable { if (PluginCfg.IS_DEBUG) { sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + uuid.toString())); } - List pdbList = sm.getTime(uuid.toString()); if (sm.removeAllTime(uuid.toString())) { - if (player.isOnline() && pdbList.size() > 0) { - pm.getServer().getScheduler().runTask(pm, new Runnable() { - @Override - public void run() { - for (PlayerDataBean pdb : pdbList) { - String packageName = pdb.getPackageName(); - PermissionPackageBean pack = PackagesCfg.PACKAGES.get(packageName); - pack.clearPlayer(player, sender, pm.getPermission()); - } - } - }); - Player p = pm.getServer().getPlayer(uuid); - if (p != null) { - p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}删除了你的所有权限包", sender.getName())); + if (player.isOnline()) { + Player p = player.getPlayer(); + try { + List pdbList = sm.getTime(player.getUniqueId().toString()); + PermissionPackageBean.reloadPlayerPermissions(player, pdbList, pm); + } catch (Exception e) { + e.printStackTrace(); + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "修改权限失败, 请重新进入服务器!")); } + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}删除了你的所有权限包", sender.getName())); } sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "删除玩家 {0} 的所有权限包", playerName)); } else { diff --git a/src/main/gg/frog/mc/permissionstime/command/RemoveCmd.java b/src/main/gg/frog/mc/permissionstime/command/RemoveCmd.java index 6144d69..13f115e 100644 --- a/src/main/gg/frog/mc/permissionstime/command/RemoveCmd.java +++ b/src/main/gg/frog/mc/permissionstime/command/RemoveCmd.java @@ -1,5 +1,6 @@ package gg.frog.mc.permissionstime.command; +import java.util.List; import java.util.UUID; import org.bukkit.OfflinePlayer; @@ -11,6 +12,7 @@ import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.utils.StrUtil; public class RemoveCmd implements Runnable { @@ -43,16 +45,15 @@ public class RemoveCmd implements Runnable { } if (sm.removeTime(uuid.toString(), packageName)) { if (player.isOnline()) { - pm.getServer().getScheduler().runTask(pm, new Runnable() { - @Override - public void run() { - pack.clearPlayer(player, sender, pm.getPermission()); - } - }); - Player p = pm.getServer().getPlayer(uuid); - if (p != null) { - p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}删除了你的 {1}权限包", sender.getName(), pack.getDisplayName())); + Player p = player.getPlayer(); + try { + List pdbList = sm.getTime(player.getUniqueId().toString()); + PermissionPackageBean.reloadPlayerPermissions(player, pdbList, pm); + } catch (Exception e) { + e.printStackTrace(); + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "修改权限失败, 请重新进入服务器!")); } + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}删除了你的 {1}权限包", sender.getName(), pack.getDisplayName())); } sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "删除玩家 {0} 的 {1}", playerName, pack.getDisplayName())); } else { diff --git a/src/main/gg/frog/mc/permissionstime/command/SetCmd.java b/src/main/gg/frog/mc/permissionstime/command/SetCmd.java index c20b461..7408159 100644 --- a/src/main/gg/frog/mc/permissionstime/command/SetCmd.java +++ b/src/main/gg/frog/mc/permissionstime/command/SetCmd.java @@ -1,5 +1,6 @@ package gg.frog.mc.permissionstime.command; +import java.util.List; import java.util.UUID; import org.bukkit.OfflinePlayer; @@ -11,6 +12,7 @@ import gg.frog.mc.permissionstime.config.PackagesCfg; import gg.frog.mc.permissionstime.config.PluginCfg; import gg.frog.mc.permissionstime.database.SqlManager; import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.utils.StrUtil; public class SetCmd implements Runnable { @@ -55,16 +57,15 @@ public class SetCmd implements Runnable { } if (sm.setTime(uuid.toString(), packageName, days)) { if (player.isOnline()) { - pm.getServer().getScheduler().runTask(pm, new Runnable() { - @Override - public void run() { - pack.givePlayer(player, sender, pm.getPermission()); - } - }); - Player p = pm.getServer().getPlayer(uuid); - if (p != null) { - p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}设置你 {1}天的 {2}", sender.getName(), time, pack.getDisplayName())); + Player p = player.getPlayer(); + try { + List pdbList = sm.getTime(player.getUniqueId().toString()); + PermissionPackageBean.reloadPlayerPermissions(player, pdbList, pm); + } catch (Exception e) { + e.printStackTrace(); + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "修改权限失败, 请重新进入服务器!")); } + p.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "{0}设置你 {1}天的 {2}", sender.getName(), time, pack.getDisplayName())); } sender.sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "设置玩家 {0} {1}天的 {2}", playerName, time, pack.getDisplayName())); } else { diff --git a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java index 170ef52..8196ab6 100644 --- a/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java +++ b/src/main/gg/frog/mc/permissionstime/config/PackagesCfg.java @@ -1,7 +1,10 @@ package gg.frog.mc.permissionstime.config; +import java.util.Collections; +import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import gg.frog.mc.permissionstime.PluginMain; @@ -13,6 +16,8 @@ public class PackagesCfg extends PluginConfig { public static String PACKAGES_VERSION = null; public static String DEFAULT_GROUP = null; public static Map PACKAGES = new ConcurrentHashMap<>(); + public static Set allPermissions = Collections.synchronizedSet(new HashSet()); + public static Set allGroups = Collections.synchronizedSet(new HashSet()); public PackagesCfg(String fileName, PluginMain pm) { super(fileName, pm); @@ -34,6 +39,9 @@ public class PackagesCfg extends PluginConfig { System.out.println(p.getKey() + ":" + p.getValue()); } } + for (PermissionPackageBean p : PACKAGES.values()) { + allPermissions.addAll(p.getPermissions()); + allGroups.addAll(p.getGroups()); + } } - } diff --git a/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java index 4c156a5..8bcf134 100644 --- a/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java +++ b/src/main/gg/frog/mc/permissionstime/database/impl/SqlitePlayerDataService.java @@ -17,6 +17,9 @@ public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerData private PluginMain pm; + // private static long TIME_UNIT = 24 * 60 * 60 * 1000L;//一天 + private static long TIME_UNIT = 2 * 60 * 1000L; + public SqlitePlayerDataService(PluginMain pm, SqlManager sm) { super(sm); this.pm = pm; @@ -71,7 +74,7 @@ public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerData @Override public boolean setTime(String uuid, String packageName, int days) throws Exception { long now = new Date().getTime(); - long addTime = days * 24 * 60 * 60 * 1000L; + long addTime = days * TIME_UNIT; long expire = now + addTime; PlayerDataBean pdb = queryPlayerData(uuid, packageName); if (pdb == null) { @@ -86,7 +89,7 @@ public class SqlitePlayerDataService extends DatabaseUtil implements IPlayerData @Override public boolean addTime(String uuid, String packageName, int days) throws Exception { long now = new Date().getTime(); - long addTime = days * 24 * 60 * 60 * 1000L; + long addTime = days * TIME_UNIT; long expire = now + addTime; PlayerDataBean pdb = queryPlayerData(uuid, packageName); if (pdb == null) { diff --git a/src/main/gg/frog/mc/permissionstime/listener/MainListener.java b/src/main/gg/frog/mc/permissionstime/listener/MainListener.java new file mode 100644 index 0000000..9499501 --- /dev/null +++ b/src/main/gg/frog/mc/permissionstime/listener/MainListener.java @@ -0,0 +1,73 @@ +package gg.frog.mc.permissionstime.listener; + +import java.util.List; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import gg.frog.mc.permissionstime.PluginMain; +import gg.frog.mc.permissionstime.config.PluginCfg; +import gg.frog.mc.permissionstime.model.cfg.PermissionPackageBean; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; +import gg.frog.mc.permissionstime.utils.StrUtil; + +public class MainListener implements Listener { + + private PluginMain pm; + + public MainListener(PluginMain pm) { + this.pm = pm; + } + + /** + * 一个监听器例子 + * + * @param e + */ + @EventHandler + public void onJoin(PlayerJoinEvent event) { + new Thread(new Runnable() { + @Override + public void run() { + try { + List pdbList = pm.getSqlManager().getTime(event.getPlayer().getUniqueId().toString()); + PermissionPackageBean.reloadPlayerPermissions(event.getPlayer(), pdbList, pm); + } catch (Exception e) { + e.printStackTrace(); + event.getPlayer().sendMessage(StrUtil.messageFormat(PluginCfg.PLUGIN_PREFIX + "修改权限失败, 请重新进入服务器!")); + } + } + }).start(); + } + + @EventHandler + public void onQuit(PlayerQuitEvent event) { + new Thread(new Runnable() { + @Override + public void run() { + try { + PermissionPackageBean.delPlayerAllPermissions(event.getPlayer(), pm); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } + + @EventHandler + public void onKick(PlayerKickEvent event) { + new Thread(new Runnable() { + @Override + public void run() { + try { + PermissionPackageBean.delPlayerAllPermissions(event.getPlayer(), pm); + } catch (Exception e) { + e.printStackTrace(); + } + } + }).start(); + } +} diff --git a/src/main/gg/frog/mc/permissionstime/listener/TheListener.java b/src/main/gg/frog/mc/permissionstime/listener/TheListener.java deleted file mode 100644 index c398367..0000000 --- a/src/main/gg/frog/mc/permissionstime/listener/TheListener.java +++ /dev/null @@ -1,39 +0,0 @@ -package gg.frog.mc.permissionstime.listener; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerQuitEvent; - -import gg.frog.mc.permissionstime.PluginMain; - -public class TheListener implements Listener { - - private PluginMain pm; - - public TheListener(PluginMain pm) { - this.pm = pm; - } - - /** - * 一个监听器例子 - * - * @param e - */ - @EventHandler - public void onJoin(PlayerJoinEvent e) { - pm.getServer().broadcastMessage("[" + e.getPlayer().getName() + "]进入了服务器!"); - e.setJoinMessage("[" + e.getPlayer().getName() + "]进入了服务器!"); - } - - @EventHandler - public void onQuit(PlayerQuitEvent e) { - pm.getServer().broadcastMessage("[" + e.getPlayer().getName() + "]退出了服务器!"); - } - - @EventHandler - public void onKick(PlayerKickEvent e) { - pm.getServer().broadcastMessage("[" + e.getPlayer().getName() + "]被踢出了服务器!"); - } -} diff --git a/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java b/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java index d249ae6..08d38fa 100644 --- a/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java +++ b/src/main/gg/frog/mc/permissionstime/model/cfg/PermissionPackageBean.java @@ -1,14 +1,21 @@ package gg.frog.mc.permissionstime.model.cfg; import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.bukkit.OfflinePlayer; +import org.bukkit.Server; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.scheduler.BukkitTask; +import gg.frog.mc.permissionstime.PluginMain; +import gg.frog.mc.permissionstime.config.PackagesCfg; +import gg.frog.mc.permissionstime.model.db.PlayerDataBean; import gg.frog.mc.permissionstime.utils.config.IConfigBean; import net.milkbowl.vault.permission.Permission; @@ -21,10 +28,10 @@ import net.milkbowl.vault.permission.Permission; public class PermissionPackageBean implements IConfigBean { private String displayName = null; - private Integer days = null; private Boolean global = null; private List permissions = new ArrayList<>(); private List groups = new ArrayList<>(); + private static Map taskMap = new ConcurrentHashMap<>(); public String getDisplayName() { return displayName; @@ -34,14 +41,6 @@ public class PermissionPackageBean implements IConfigBean { this.displayName = displayName; } - public Integer getDays() { - return days; - } - - public void setDays(Integer days) { - this.days = days; - } - public Boolean getGlobal() { return global; } @@ -70,7 +69,6 @@ public class PermissionPackageBean implements IConfigBean { public YamlConfiguration toConfig() { YamlConfiguration config = new YamlConfiguration(); config.set("displayName", displayName); - config.set("days", days); config.set("global", global); config.set("permissions", permissions); config.set("groups", groups); @@ -83,7 +81,6 @@ public class PermissionPackageBean implements IConfigBean { if (displayName == null) { displayName = "No Name"; } - days = config.getInt("days"); global = config.getBoolean("global"); permissions = config.getStringList("permissions"); groups = config.getStringList("groups"); @@ -91,11 +88,11 @@ public class PermissionPackageBean implements IConfigBean { @Override public String toString() { - return "PermissionPackageBean [displayName=" + displayName + ", days=" + days + ", global=" + global + ", permissions=" + permissions + ", groups=" + groups + "]"; + return "PermissionPackageBean [displayName=" + displayName + ", global=" + global + ", permissions=" + permissions + ", groups=" + groups + "]"; } - public void givePlayer(OfflinePlayer player, CommandSender sender, Permission permission) { - List worlds = sender.getServer().getWorlds(); + private void givePlayer(OfflinePlayer player, Server server, Permission permission) { + List worlds = server.getWorlds(); for (String pem : permissions) { String[] args = pem.split(":"); pem = args[0]; @@ -128,20 +125,20 @@ public class PermissionPackageBean implements IConfigBean { } } - public void clearPlayer(OfflinePlayer player, CommandSender sender, Permission permission) { - List worlds = sender.getServer().getWorlds(); + private void clearPlayer(OfflinePlayer player, Server server, Permission permission) { + List worlds = server.getWorlds(); for (String pem : permissions) { String[] args = pem.split(":"); pem = args[0]; if (args.length > 1) { for (int i = 1; i < args.length; i++) { String worldName = args[i]; - permission.playerAdd(worldName, player, pem); + permission.playerRemove(worldName, player, pem); } } else { for (World world : worlds) { String worldName = world.getName(); - permission.playerAdd(worldName, player, pem); + permission.playerRemove(worldName, player, pem); } } } @@ -162,4 +159,55 @@ public class PermissionPackageBean implements IConfigBean { } } + public static void reloadPlayerPermissions(OfflinePlayer player, List pdbList, PluginMain plugin) { + long delay = -1; + long now = new Date().getTime(); + PermissionPackageBean addPpb = new PermissionPackageBean(); + PermissionPackageBean subPpb = new PermissionPackageBean(); + subPpb.getPermissions().addAll(PackagesCfg.allPermissions); + subPpb.getGroups().addAll(PackagesCfg.allGroups); + for (PlayerDataBean pdb : pdbList) { + long leftTime = pdb.getExpire() - now; + if (leftTime > 0) { + if (delay == -1) { + delay = leftTime; + } else if (delay > leftTime) { + delay = leftTime; + } + } + PermissionPackageBean p = PackagesCfg.PACKAGES.get(pdb.getPackageName()); + addPpb.getPermissions().addAll(p.getPermissions()); + subPpb.getPermissions().removeAll(p.getPermissions()); + addPpb.getGroups().addAll(p.getGroups()); + subPpb.getGroups().removeAll(p.getGroups()); + } + addPpb.givePlayer(player, plugin.getServer(), plugin.getPermission()); + subPpb.clearPlayer(player, plugin.getServer(), plugin.getPermission()); + BukkitTask task = taskMap.get(player.getUniqueId().toString()); + if (task != null) { + plugin.getServer().getScheduler().cancelTask(task.getTaskId()); + } + if (pdbList.size() > 0) { + delay = (delay / 1000 + 1) * 20;// 1秒=20ticks + task = plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { + @Override + public void run() { + List tpdbList = plugin.getSqlManager().getTime(player.getUniqueId().toString()); + reloadPlayerPermissions(player, tpdbList, plugin); + } + }, delay); + taskMap.put(player.getUniqueId().toString(), task); + } + } + + public static void delPlayerAllPermissions(OfflinePlayer player, PluginMain plugin) throws Exception { + PermissionPackageBean subPpb = new PermissionPackageBean(); + subPpb.getPermissions().addAll(PackagesCfg.allPermissions); + subPpb.getGroups().addAll(PackagesCfg.allGroups); + subPpb.clearPlayer(player, plugin.getServer(), plugin.getPermission()); + BukkitTask task = taskMap.get(player.getUniqueId().toString()); + if (task != null) { + plugin.getServer().getScheduler().cancelTask(task.getTaskId()); + } + } } diff --git a/src/resources/plugin.yml b/src/resources/plugin.yml index 33e2d6a..e8da508 100644 --- a/src/resources/plugin.yml +++ b/src/resources/plugin.yml @@ -9,6 +9,9 @@ commands: permissionstime: description: Show all commands. usage: /permissionstime [param] + pt: + description: Aliases commands for permissionstime. + usage: /pt [param] permissions: permissionstime.*: description: Gives access to all PermissionsTime commands.