InventoryUtil changes and PlayerUtil.isJoined-

This commit is contained in:
Olof Larsson 2013-03-25 13:50:37 +01:00
parent f8d29d98aa
commit 853fe36a5d
2 changed files with 59 additions and 12 deletions

View File

@ -21,17 +21,29 @@ public class InventoryUtil
// EVENT INTERPRETATION // 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) public static boolean isGiving(InventoryClickEvent event)
{ {
// Outside inventory? if (isOutside(event)) return false;
if (event.getRawSlot() < 0) return false; boolean topClicked = isTopInventory(event);
// Was the upper inventory clicked?
boolean upperClicked = event.getRawSlot() < event.getInventory().getSize();
boolean ret = false; boolean ret = false;
if (upperClicked) if (topClicked)
{ {
ret = (event.getCursor() != null && event.getCursor().getAmount() > 0); ret = (event.getCursor() != null && event.getCursor().getAmount() > 0);
} }
@ -45,15 +57,12 @@ public class InventoryUtil
public static boolean isTaking(InventoryClickEvent event) public static boolean isTaking(InventoryClickEvent event)
{ {
// Outside inventory? if (isOutside(event)) return false;
if (event.getRawSlot() < 0) return false; boolean topClicked = isTopInventory(event);
// Was the upper inventory clicked?
boolean upperClicked = event.getRawSlot() < event.getInventory().getSize();
boolean ret = false; boolean ret = false;
if (upperClicked) if (topClicked)
{ {
ret = (event.getCurrentItem() != null && event.getCurrentItem().getAmount() > 0); ret = (event.getCurrentItem() != null && event.getCurrentItem().getAmount() > 0);
} }

View File

@ -6,6 +6,7 @@ import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap; 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.EntityPlayer;
import net.minecraft.server.v1_5_R2.Packet8UpdateHealth; 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.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.massivecraft.mcore.MCore;
public class PlayerUtil implements Listener public class PlayerUtil implements Listener
{ {
// -------------------------------------------- // // -------------------------------------------- //
@ -42,6 +47,7 @@ public class PlayerUtil implements Listener
*/ */
protected static Map<String, Set<String>> lowerCaseStartOfNameToCorrectNames = new ConcurrentSkipListMap<String, Set<String>>(); protected static Map<String, Set<String>> lowerCaseStartOfNameToCorrectNames = new ConcurrentSkipListMap<String, Set<String>>();
protected static Set<String> joinedPlayerNames = new ConcurrentSkipListSet<String>(String.CASE_INSENSITIVE_ORDER);
// -------------------------------------------- // // -------------------------------------------- //
// CONSTRUCTOR AND EVENT LISTENER // CONSTRUCTOR AND EVENT LISTENER
@ -55,6 +61,12 @@ public class PlayerUtil implements Listener
{ {
nameToCorrectName.put(playername, playername); nameToCorrectName.put(playername, playername);
} }
joinedPlayerNames.clear();
for (Player player : Bukkit.getOnlinePlayers())
{
joinedPlayerNames.add(player.getName());
}
} }
@EventHandler(priority = EventPriority.LOWEST) @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 METHODS
// -------------------------------------------- // // -------------------------------------------- //
public static boolean isJoined(Player player)
{
return joinedPlayerNames.contains(player.getName());
}
public static Set<String> getAllVisitorNames() public static Set<String> getAllVisitorNames()
{ {
return nameToCorrectName.keySet(); return nameToCorrectName.keySet();