From 853fe36a5d49d8d6957cd661f4dd0d8ea14c8945 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 25 Mar 2013 13:50:37 +0100 Subject: [PATCH] InventoryUtil changes and PlayerUtil.isJoined- --- .../mcore/util/InventoryUtil.java | 33 ++++++++++------ .../massivecraft/mcore/util/PlayerUtil.java | 38 +++++++++++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/com/massivecraft/mcore/util/InventoryUtil.java b/src/com/massivecraft/mcore/util/InventoryUtil.java index de01f523..d7db13ad 100644 --- a/src/com/massivecraft/mcore/util/InventoryUtil.java +++ b/src/com/massivecraft/mcore/util/InventoryUtil.java @@ -21,17 +21,29 @@ public class InventoryUtil // EVENT INTERPRETATION // -------------------------------------------- // + public static boolean isOutside(InventoryClickEvent event) + { + return event.getRawSlot() < 0; + } + public static boolean isTopInventory(InventoryClickEvent event) + { + if (isOutside(event)) return false; + return event.getRawSlot() < event.getInventory().getSize(); + } + public static boolean isBottomInventory(InventoryClickEvent event) + { + if (isOutside(event)) return false; + return event.getRawSlot() >= event.getInventory().getSize(); + } + public static boolean isGiving(InventoryClickEvent event) { - // Outside inventory? - if (event.getRawSlot() < 0) return false; - - // Was the upper inventory clicked? - boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); + if (isOutside(event)) return false; + boolean topClicked = isTopInventory(event); boolean ret = false; - if (upperClicked) + if (topClicked) { ret = (event.getCursor() != null && event.getCursor().getAmount() > 0); } @@ -45,15 +57,12 @@ public class InventoryUtil public static boolean isTaking(InventoryClickEvent event) { - // Outside inventory? - if (event.getRawSlot() < 0) return false; - - // Was the upper inventory clicked? - boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); + if (isOutside(event)) return false; + boolean topClicked = isTopInventory(event); boolean ret = false; - if (upperClicked) + if (topClicked) { ret = (event.getCurrentItem() != null && event.getCurrentItem().getAmount() > 0); } diff --git a/src/com/massivecraft/mcore/util/PlayerUtil.java b/src/com/massivecraft/mcore/util/PlayerUtil.java index 3e9bb42d..e88901a5 100644 --- a/src/com/massivecraft/mcore/util/PlayerUtil.java +++ b/src/com/massivecraft/mcore/util/PlayerUtil.java @@ -6,6 +6,7 @@ import java.util.Set; import java.util.TreeSet; import java.util.Map.Entry; import java.util.concurrent.ConcurrentSkipListMap; +import java.util.concurrent.ConcurrentSkipListSet; import net.minecraft.server.v1_5_R2.EntityPlayer; import net.minecraft.server.v1_5_R2.Packet8UpdateHealth; @@ -16,9 +17,13 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.plugin.Plugin; +import com.massivecraft.mcore.MCore; + public class PlayerUtil implements Listener { // -------------------------------------------- // @@ -42,6 +47,7 @@ public class PlayerUtil implements Listener */ protected static Map> lowerCaseStartOfNameToCorrectNames = new ConcurrentSkipListMap>(); + protected static Set joinedPlayerNames = new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER); // -------------------------------------------- // // CONSTRUCTOR AND EVENT LISTENER @@ -55,6 +61,12 @@ public class PlayerUtil implements Listener { nameToCorrectName.put(playername, playername); } + + joinedPlayerNames.clear(); + for (Player player : Bukkit.getOnlinePlayers()) + { + joinedPlayerNames.add(player.getName()); + } } @EventHandler(priority = EventPriority.LOWEST) @@ -76,10 +88,36 @@ public class PlayerUtil implements Listener } } + @EventHandler(priority = EventPriority.MONITOR) + public void joinMonitor(PlayerJoinEvent event) + { + final String playerName = event.getPlayer().getName(); + Bukkit.getScheduler().scheduleSyncDelayedTask(MCore.get(), new Runnable() + { + @Override + public void run() + { + joinedPlayerNames.add(playerName); + } + }); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void quitMonitor(PlayerQuitEvent event) + { + final String playerName = event.getPlayer().getName(); + joinedPlayerNames.remove(playerName); + } + // -------------------------------------------- // // PUBLIC METHODS // -------------------------------------------- // + public static boolean isJoined(Player player) + { + return joinedPlayerNames.contains(player.getName()); + } + public static Set getAllVisitorNames() { return nameToCorrectName.keySet();