1
0
mirror of https://e.coding.net/circlecloud/GuiACK.git synced 2024-12-04 16:38:48 +00:00

fix select error while player join server already...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-21 15:13:23 +08:00
parent 51f7e90dd6
commit 6e30713886
5 changed files with 65 additions and 45 deletions

View File

@ -2,14 +2,17 @@ package cn.citycraft.GuiACK;
import java.io.IOException; import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics; import org.mcstats.Metrics;
import cn.citycraft.GuiACK.gui.VerifyGui; import cn.citycraft.GuiACK.gui.VerifyGui;
import cn.citycraft.GuiACK.listen.PlayerListen; import cn.citycraft.GuiACK.listen.PlayerListen;
import cn.citycraft.GuiACK.runnable.TaskManager;
import cn.citycraft.PluginHelper.config.FileConfig; import cn.citycraft.PluginHelper.config.FileConfig;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
@ -22,7 +25,7 @@ public class GuiACK extends JavaPlugin {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 1 && args[0].equalsIgnoreCase("reload") && sender.hasPermission("gack.reload")) { if (args.length == 1 && args[0].equalsIgnoreCase("reload") && (sender.hasPermission("gack.reload") || sender.isOp())) {
onLoad(); onLoad();
sender.sendMessage("配置文件已重载!"); sender.sendMessage("配置文件已重载!");
} }
@ -31,7 +34,6 @@ public class GuiACK extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
this.getServer().getPluginManager().registerEvents(new PlayerListen(this), this);
if (setupEconomy()) { if (setupEconomy()) {
isEconomy = true; isEconomy = true;
this.getLogger().info("发现Vault 载入数据..."); this.getLogger().info("发现Vault 载入数据...");
@ -45,6 +47,10 @@ public class GuiACK extends JavaPlugin {
metrics.start(); metrics.start();
} catch (IOException e) { } catch (IOException e) {
} }
this.getServer().getPluginManager().registerEvents(new PlayerListen(this), this);
for (Player p : Bukkit.getOnlinePlayers())
TaskManager.add(p);
this.getLogger().info("图形化防挂机验证码加载完毕!");
} }
@Override @Override
@ -53,6 +59,7 @@ public class GuiACK extends JavaPlugin {
tasktime = config.getInt("TaskTime"); tasktime = config.getInt("TaskTime");
reward = config.getDouble("Reward"); reward = config.getDouble("Reward");
VerifyGui.init(config.getStringList("VerifyList")); VerifyGui.init(config.getStringList("VerifyList"));
TaskManager.init(this);
} }
public boolean setupEconomy() { public boolean setupEconomy() {

View File

@ -5,30 +5,28 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class VerifyGui { public class VerifyGui {
static List<String> verifylist = new ArrayList<String>(); public static List<String> verifylist;
static List<String> playerlist = new ArrayList<String>(); public static List<String> playerlist;
public static String invname = "§v§e§r§c防挂机验证 §3请选择验证码: "; public static List<String> checklist;
public static String invname = "§c防挂机验证 §3请选择验证码: ";
public static void add(Player p) { public static void checkopen(Player p) {
playerlist.add(p.getName());
}
public static boolean checkopen(Player p) {
if (playerlist.contains(p.getName())) if (playerlist.contains(p.getName()))
return false; p.kickPlayer(ChatColor.RED + "验证码输入选择超时 请重新登录!");
else { else
open(p); open(p);
return true;
}
} }
public static void init(List<String> verifylist) { public static void init(List<String> verifylist) {
VerifyGui.verifylist = verifylist; VerifyGui.verifylist = verifylist;
playerlist = new ArrayList<String>();
checklist = new ArrayList<String>();
} }
public static void open(Player p) { public static void open(Player p) {
@ -45,10 +43,7 @@ public class VerifyGui {
Inventory inv = Bukkit.createInventory(null, 9, invname + list[ri].getItemMeta().getDisplayName()); Inventory inv = Bukkit.createInventory(null, 9, invname + list[ri].getItemMeta().getDisplayName());
inv.setContents(list); inv.setContents(list);
p.openInventory(inv); p.openInventory(inv);
add(p); playerlist.add(p.getName());
} checklist.add(p.getName());
public static void remove(Player p) {
playerlist.remove(p.getName());
} }
} }

View File

@ -1,7 +1,5 @@
package cn.citycraft.GuiACK.listen; package cn.citycraft.GuiACK.listen;
import java.util.HashMap;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -13,19 +11,15 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.GuiACK.GuiACK; import cn.citycraft.GuiACK.GuiACK;
import cn.citycraft.GuiACK.gui.VerifyGui; import cn.citycraft.GuiACK.gui.VerifyGui;
import cn.citycraft.GuiACK.runnable.TaskManager;
import cn.citycraft.GuiACK.runnable.VerifyTask; import cn.citycraft.GuiACK.runnable.VerifyTask;
public class PlayerListen implements Listener { public class PlayerListen implements Listener {
GuiACK plugin; GuiACK plugin;
HashMap<String, BukkitTask> tasklist = new HashMap<String, BukkitTask>();
HashMap<String, Boolean> checklist = new HashMap<String, Boolean>();
public PlayerListen(GuiACK plugin) { public PlayerListen(GuiACK plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -35,16 +29,12 @@ public class PlayerListen implements Listener {
String guiname = e.getView().getTitle(); String guiname = e.getView().getTitle();
if (!guiname.startsWith(VerifyGui.invname)) if (!guiname.startsWith(VerifyGui.invname))
return; return;
if (e.getInventory().getItem(0) == null)
return;
Player p = (Player) e.getPlayer(); Player p = (Player) e.getPlayer();
if (checklist.containsKey(p.getName()) && checklist.get(p.getName())) { if (VerifyGui.checklist.contains(p.getName())) {
checklist.put(p.getName(), false); if (p.isOnline())
VerifyGui.remove(p); Bukkit.getScheduler().runTaskLater(plugin, new VerifyTask(p), 3);
return; return;
} }
if (p.isOnline())
Bukkit.getScheduler().runTaskLater(plugin, new VerifyTask(p), 3);
} }
@EventHandler @EventHandler
@ -63,13 +53,13 @@ public class PlayerListen implements Listener {
if (!ci.getItemMeta().hasDisplayName()) if (!ci.getItemMeta().hasDisplayName())
return; return;
String clickName = ci.getItemMeta().getDisplayName(); String clickName = ci.getItemMeta().getDisplayName();
VerifyGui.remove(p); VerifyGui.playerlist.remove(p.getName());
if (!guiname.endsWith(clickName)) if (!guiname.endsWith(clickName))
p.kickPlayer("§c验证码选择错误 请重新登录!"); p.kickPlayer("§c验证码选择错误 请重新登录!");
else { else {
plugin.economy.depositPlayer(p, plugin.reward); plugin.economy.depositPlayer(p, plugin.reward);
p.sendMessage("§a验证成功 奖励金钱: " + plugin.reward); p.sendMessage("§a验证成功 奖励金钱: " + plugin.reward);
checklist.put(p.getName(), true); VerifyGui.checklist.remove(p.getName());
p.closeInventory(); p.closeInventory();
} }
} }
@ -77,18 +67,14 @@ public class PlayerListen implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent e) { public void onPlayerJoin(PlayerJoinEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
int tasktime = plugin.tasktime * 20 * 60; TaskManager.add(p);
BukkitTask task = Bukkit.getScheduler().runTaskTimer(plugin, new VerifyTask(p), tasktime, tasktime);
tasklist.put(p.getName(), task);
} }
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent e) { public void onPlayerQuit(PlayerQuitEvent e) {
Player p = e.getPlayer(); Player p = e.getPlayer();
if (tasklist.containsKey(p.getName())) TaskManager.remove(p);
tasklist.get(p.getName()).cancel(); VerifyGui.checklist.remove(p.getName());
tasklist.remove(p.getName()); VerifyGui.playerlist.remove(p.getName());
checklist.remove(p.getName());
VerifyGui.remove(p);
} }
} }

View File

@ -0,0 +1,32 @@
package cn.citycraft.GuiACK.runnable;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import cn.citycraft.GuiACK.GuiACK;
public class TaskManager {
static HashMap<String, BukkitTask> tasklist;
static GuiACK plugin;
static int tasktime;
public static void add(Player p) {
BukkitTask task = Bukkit.getScheduler().runTaskTimer(plugin, new VerifyTask(p), tasktime, tasktime);
tasklist.put(p.getName(), task);
}
public static void init(GuiACK plugin) {
TaskManager.plugin = plugin;
tasktime = plugin.tasktime * 20 * 60;
tasklist = new HashMap<String, BukkitTask>();
}
public static void remove(Player p) {
if (tasklist.containsKey(p.getName()))
tasklist.remove(p.getName()).cancel();
}
}

View File

@ -14,8 +14,8 @@ public class VerifyTask implements Runnable {
@Override @Override
public void run() { public void run() {
if (p.isOnline()) if (p.isOnline())
if (!VerifyGui.checkopen(p)) VerifyGui.checkopen(p);
p.kickPlayer("验证码输入超时 请重新登录!");
} }
} }