Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Re-commit issue fixes.
  • Loading branch information
MrTwiggy committed Nov 18, 2014
1 parent c4afe3c commit c17f516
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 32 deletions.
87 changes: 66 additions & 21 deletions src/com/untamedears/PrisonPearl/PrisonPearlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -41,20 +42,24 @@
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.scheduler.BukkitTask;

import com.untamedears.PrisonPearl.PrisonPearlEvent.Type;

class PrisonPearlManager implements Listener {
private final PrisonPearlPlugin plugin;
private final PrisonPearlStorage pearls;
Expand Down Expand Up @@ -335,23 +340,22 @@ public void onItemDespawn(ItemDespawnEvent event) {
private Map<UUID, BukkitTask> unloadedPearls = new HashMap<UUID, BukkitTask>();
// Free the pearl if its on a chunk that unloads
@EventHandler(priority = EventPriority.MONITOR)
public void onChunkUnload(ChunkUnloadEvent event) {
public void onChunkUnload(ChunkUnloadEvent event) {
for (Entity e : event.getChunk().getEntities()) {
if (!(e instanceof Item))
continue;

final PrisonPearl pp = pearls.getByItemStack(
((Item) e).getItemStack());
if (pp == null) {
final PrisonPearl pp = pearls.getByItemStack(((Item) e).getItemStack());

if (pp == null)
continue;
}


final Player player = Bukkit.getPlayer(pp.getImprisonedId());
final Entity entity = e;
// doing this in onChunkUnload causes weird things to happen

event.setCancelled(true);
UUID uuid = pp.getImprisonedId();
final UUID uuid = pp.getImprisonedId();
if (unloadedPearls.containsKey(uuid))
return;
BukkitTask count = Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
Expand All @@ -360,14 +364,40 @@ public void run(){
pp.getImprisonedId() + ") is being freed. Reason: Chunk with PrisonPearl unloaded."))
{
entity.remove();
unloadedPearls.remove(uuid);
}

}
}, plugin.getPPConfig().getChunkUnloadDelay());
unloadedPearls.put(uuid, count);
}
}


// Prevent dropped Prison Pearl's from being despawned.
// TODO: PrisonPearl items specifically aren't being picked up from chunk.getEntities()
/*@EventHandler(priority = EventPriority.MONITOR)
public void onChunkLoad(ChunkLoadEvent event) {
// Don't need to check for unloaded pearls if there are none.
if (unloadedPearls.isEmpty() || event.isNewChunk()) return;
// Search for currently unloaded Prison Pearls.
for (Entity entity : event.getChunk().getEntities()) {
if (entity instanceof Item) {
Item item = (Item) entity;
PrisonPearl prisonPearl = pearls.getByItemStack(item.getItemStack());
if (prisonPearl != null) {
UUID imprisonedUuid = prisonPearl.getImprisonedId();
if (unloadedPearls.containsKey(imprisonedUuid)) {
unloadedPearls.get(imprisonedUuid).cancel();
unloadedPearls.remove(imprisonedUuid);
}
}
}
}
}*/

// Free the pearl if it combusts in lava/fire
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityCombustEvent(EntityCombustEvent event) {
Expand All @@ -384,7 +414,6 @@ public void onEntityCombustEvent(EntityCombustEvent event) {
freePearl(pp, reason);
}


// Handle inventory dragging properly.
@EventHandler(priority = EventPriority.HIGHEST)
public void onInventoryDrag(InventoryDragEvent event) {
Expand Down Expand Up @@ -413,6 +442,20 @@ public void onInventoryDrag(InventoryDragEvent event) {
}
}


// Prevent imprisoned players from placing PrisonPearls in their inventory.
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onPrisonPearlClick(InventoryClickEvent event) {
Player clicker = (Player) event.getWhoClicked();

if (pearls.isPrisonPearl(event.getCurrentItem())
&& pearls.isImprisoned(clicker)) {
clicker.sendMessage(ChatColor.RED + "Imprisoned players cannot pick up prison pearls!");
event.setCancelled(true); // Prevent imprisoned player from grabbing PrisonPearls.
}
}


// Track the location of a pearl
// Forbid pearls from being put in storage minecarts
@EventHandler(priority = EventPriority.HIGHEST)
Expand Down Expand Up @@ -554,7 +597,6 @@ private void updatePearlHolder(PrisonPearl pearl, InventoryHolder holder, Cancel
}
}


// Track the location of a pearl if it spawns as an item for any reason
@EventHandler(priority = EventPriority.MONITOR)
public void onItemSpawn(ItemSpawnEvent event) {
Expand All @@ -565,27 +607,30 @@ public void onItemSpawn(ItemSpawnEvent event) {
pp.markMove();
updatePearl(pp, item);
}


// Track the location of a pearl if a player picks it up
@EventHandler(priority = EventPriority.MONITOR)
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
PrisonPearl pp = pearls.getByItemStack(event.getItem().getItemStack());
if (pp == null)
return;

pp.markMove();
updatePearl(pp, event.getPlayer());
// For when a pearl is dropped in an unloaded chunk
if (unloadedPearls.isEmpty())
return;
UUID want = pp.getImprisonedId();
for (UUID uuid: unloadedPearls.keySet()){
if (want.equals(uuid)){
unloadedPearls.get(uuid).cancel();
unloadedPearls.remove(uuid);
}
}


// Prevent imprisoned players from picking up PrisonPearls.
@EventHandler(priority = EventPriority.NORMAL)
public void onPlayerPickupPearl(PlayerPickupItemEvent event) {
if (pearls.isPrisonPearl(event.getItem().getItemStack())
&& pearls.isImprisoned(event.getPlayer())) {
event.setCancelled(true);
}
}


// Deny pearls traveling to other worlds.
@EventHandler(priority = EventPriority.HIGHEST)
public void worldChangeEvent(PlayerTeleportEvent event){
Expand Down Expand Up @@ -668,4 +713,4 @@ private Configuration getConfig() {
return plugin.getConfig();
}

}
}
59 changes: 49 additions & 10 deletions src/com/untamedears/PrisonPearl/PrisonPearlPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,15 @@ public void onPrisonPearlEvent(PrisonPearlEvent event) {
if (event.getType() == PrisonPearlEvent.Type.NEW) {
updateAttachment(player);

// Log the capturing PrisonPearl event.
Player imprisoner = event.getImprisoner();
log.info(imprisoner.getDisplayName() + " has bound " + playerName + " to a PrisonPearl");
String imprisonerLoc = serializeLocation(imprisoner.getLocation());
String playerLoc = serializeLocation(player.getLocation());
String message = String.format("%s [%s] has bound %s [%s] to a PrisonPearl",
imprisoner.getDisplayName(), imprisonerLoc,
playerName, playerLoc);
log.info(message);

imprisoner.sendMessage(ChatColor.GREEN+"You've bound " + playerName + ChatColor.GREEN+" to a prison pearl!");
if (player != null) {
player.sendMessage(ChatColor.RED+"You've been bound to a prison pearl owned by " + imprisoner.getDisplayName());
Expand Down Expand Up @@ -636,8 +643,21 @@ public void onPrisonPearlEvent(PrisonPearlEvent event) {
}
UUID[] alts = altsList.getAltsArray(playerId);
checkBans(alts);

log.info(playerName + " was freed");

// Log the free'ing PrisonPearl event with coordinates.
Player imprisoner = event.getPrisonPearl().getHolderPlayer();
String playerLoc = player != null ? serializeLocation(player.getLocation()) : "[???]";
String message = String.format("%s [%s] was freed", playerName, playerLoc);

if (imprisoner != null) {
String imprisonerLoc = serializeLocation(imprisoner.getLocation());
message = String.format("%s [%s] was freed by %s [%s]",
playerName, playerLoc,
imprisoner.getDisplayName(), imprisonerLoc);
}

log.info(message);

if (player != null) {
player.sendMessage("You've been freed!");
broadcastman.broadcast(player, playerName + " was freed!");
Expand Down Expand Up @@ -932,7 +952,7 @@ public int checkBan(UUID id) {
}
if (pearledCount >= maxImprisonedAlts) {
if (!pearls.isImprisoned(id)) {
banAndKick(id, pearledCount, names);
banAndKick(id, pearledCount);
return 2;
}
int count = 0;
Expand All @@ -941,7 +961,7 @@ public int checkBan(UUID id) {
count++;
}
if (count >= maxImprisonedAlts) {
banAndKick(id, pearledCount, names);
banAndKick(id, pearledCount);
return 2;
}
}
Expand All @@ -957,17 +977,17 @@ public int checkBan(UUID id) {
return 0;
}

private void banAndKick(UUID id, int pearledCount, String names) {
private void banAndKick(UUID id, int pearledCount) {
Player p = this.getServer().getPlayer(id);
if (p != null) {
p.kickPlayer(kickMessage);
}
if (banManager_.isBanned(id)) {
log.info(id+" still banned for having "+pearledCount+" imprisoned alts: "+names);
log.info(id+" still banned for having "+pearledCount+" imprisoned alts.");
return;
}
banManager_.ban(id);
log.info("banning "+id+" for having "+pearledCount+" imprisoned alts: "+names);
log.info("banning "+id+" for having "+pearledCount+" imprisoned alts.");
}

private void checkBans(UUID[] ids) {
Expand All @@ -992,7 +1012,7 @@ private void checkBans(UUID[] ids) {
p.kickPlayer(kickMessage);
}
banManager_.ban(id);
log.info("banning " + id + ", for having " + pearledCount + " imprisoned alts: " + iNames);
log.info("banning " + id + ", for having " + pearledCount + " imprisoned alts.");
} else if (banManager_.isBanned(id)) {
banManager_.pardon(id);
log.info("unbanning " + id + ", no longer has too many imprisoned alts.");
Expand Down Expand Up @@ -1074,4 +1094,23 @@ public static PrisonPearlManager getPrisonPearlManager(){
public BanManager getBanManager() {
return banManager_;
}
}

/**
* Log a message to plugin {@link Logger}.
* @param message - the message to log.
*/
public static void log(String message) {
if (log != null) {
log.log(Level.INFO, message);
}
}

/**
* @param location - the location to serialize into user-friendly text.
* @return the serialized user-friendly string representing location.
*/
private static String serializeLocation(Location location) {
return String.format("%s, (%d, %d, %d)", location.getWorld().getName(),
location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
}
10 changes: 9 additions & 1 deletion src/com/untamedears/PrisonPearl/PrisonPearlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ public PrisonPearl getByImprisoned(Player player) {
return pearls_byimprisoned.get(player.getUniqueId());
}

/**
* @param itemStack - the item stack to check for being a PrisonPearl.
* @return true, if itemStack is a PrisonPearl, false otherwise.
*/
public boolean isPrisonPearl(ItemStack itemStack) {
return getByItemStack(itemStack) != null;
}

public Integer getPearlCount(){
return pearls_byimprisoned.size();
}
Expand Down Expand Up @@ -444,4 +452,4 @@ public String restorePearls(PrisonPearlManager pearlman, String config){
private Configuration getConfig() {
return plugin.getConfig();
}
}
}

0 comments on commit c17f516

Please sign in to comment.