Created
September 21, 2012 17:39
-
-
Save aadnk/3762840 to your computer and use it in GitHub Desktop.
A class that keeps track of recent block events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import org.apache.commons.lang.NullArgumentException; | |
import org.bukkit.Location; | |
import org.bukkit.entity.HumanEntity; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.event.EventPriority; | |
import org.bukkit.event.Listener; | |
import org.bukkit.event.inventory.InventoryCloseEvent; | |
import org.bukkit.event.player.PlayerInteractEvent; | |
import org.bukkit.event.player.PlayerQuitEvent; | |
/** | |
* Keeps track of a player's last block interaction. | |
* | |
* @author Kristian | |
*/ | |
public class PlayerInteractionListener implements Listener { | |
// Last clicked block | |
private Map<String, ClickEvent> lastRightClicked = new ConcurrentHashMap<String, ClickEvent>(); | |
// Last clicked event | |
private class ClickEvent { | |
// public org.bukkit.event.block.Action ... | |
public long time; | |
public Location location; | |
} | |
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = false) | |
public void onPlayerInteractEvent(PlayerInteractEvent event) { | |
Player player = event.getPlayer(); | |
// Make sure this is a valid block right-click event | |
if (player != null && event.hasBlock() && | |
event.getAction() == org.bukkit.event.block.Action.RIGHT_CLICK_BLOCK) { | |
// Store relevant information | |
ClickEvent click = new ClickEvent(); | |
click.location = event.getClickedBlock().getLocation(); | |
click.time = System.currentTimeMillis(); | |
// Store this block (by copy, so we don't keep chunks in memory) | |
lastRightClicked.put(player.getName(), click); | |
} | |
} | |
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | |
public void onInventoryCloseEvent(InventoryCloseEvent event) { | |
long current = System.currentTimeMillis(); | |
HumanEntity player = event.getPlayer(); | |
// Make sure this is a valid inventory open event | |
if (player instanceof Player) { | |
ClickEvent click = lastRightClicked.get(player.getName()); | |
// Ignore very recent close events - they are likely to be wrong | |
if (click != null && (current - click.time) >= 25) { | |
// This information is now outdated | |
lastRightClicked.remove(player.getName()); | |
} | |
} | |
} | |
/** | |
* Retrieves the given player's most recent right click event. | |
* @param player - the player whose interaction we're looking for. | |
* @param maxAge - the maximum age (in milliseconds) of the action. NULL indicates infinity. | |
* @return The location of the last clicked block. | |
*/ | |
public Location getLastRightClick(Player player, Integer maxAge) { | |
if (player == null) | |
throw new NullArgumentException("player"); | |
return getLastRightClick(player, maxAge, System.currentTimeMillis()); | |
} | |
/** | |
* Retrieves the given player's most recent right click event. | |
* @param player - the player whose interaction we're looking for. | |
* @param maxAge - the maximum age (in milliseconds) of the action. NULL indicates infinity. | |
* @param currentTime - the current time in milliseconds since midnight, January 1, 1970. | |
* @return The location of the last clicked block. | |
*/ | |
public Location getLastRightClick(Player player, Integer maxAge, long currentTime) { | |
if (player == null) | |
throw new NullArgumentException("player"); | |
ClickEvent last = lastRightClicked.get(player.getName()); | |
// Make sure we're not outside the age limit | |
if (last != null && ( | |
maxAge == null || | |
last.time + maxAge > currentTime | |
)) { | |
return last.location; | |
} | |
// No action found | |
return null; | |
} | |
/** | |
* Determines if a last right click event has been recorded for a given player. | |
* @param player - the given player. | |
* @return TRUE if a right click event has been recorded, FALSE otherwise. | |
*/ | |
public boolean hasLastRightClick(Player player) { | |
return lastRightClicked.containsKey(player.getName()); | |
} | |
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) | |
public void onPlayerQuitEvent(PlayerQuitEvent event) { | |
Player player = event.getPlayer(); | |
if (player != null) { | |
String name = player.getName(); | |
// Cleanup | |
lastRightClicked.remove(name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment