Skip to content

Commit c17f516

Browse files
author
MrTwiggy
committed
Re-commit issue fixes.
1 parent c4afe3c commit c17f516

File tree

3 files changed

+124
-32
lines changed

3 files changed

+124
-32
lines changed

src/com/untamedears/PrisonPearl/PrisonPearlManager.java

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.bukkit.Bukkit;
1616
import org.bukkit.ChatColor;
17+
import org.bukkit.Chunk;
1718
import org.bukkit.Location;
1819
import org.bukkit.Material;
1920
import org.bukkit.OfflinePlayer;
@@ -41,20 +42,24 @@
4142
import org.bukkit.event.inventory.InventoryAction;
4243
import org.bukkit.event.inventory.InventoryClickEvent;
4344
import org.bukkit.event.inventory.InventoryDragEvent;
45+
import org.bukkit.event.inventory.InventoryMoveItemEvent;
4446
import org.bukkit.event.player.PlayerInteractEvent;
4547
import org.bukkit.event.player.PlayerItemHeldEvent;
4648
import org.bukkit.event.player.PlayerPickupItemEvent;
4749
import org.bukkit.event.player.PlayerQuitEvent;
4850
import org.bukkit.event.player.PlayerTeleportEvent;
4951
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
5052
import org.bukkit.event.world.ChunkUnloadEvent;
53+
import org.bukkit.event.world.ChunkLoadEvent;
5154
import org.bukkit.inventory.Inventory;
5255
import org.bukkit.inventory.InventoryHolder;
5356
import org.bukkit.inventory.ItemStack;
5457
import org.bukkit.inventory.PlayerInventory;
5558
import org.bukkit.inventory.meta.ItemMeta;
5659
import org.bukkit.scheduler.BukkitTask;
5760

61+
import com.untamedears.PrisonPearl.PrisonPearlEvent.Type;
62+
5863
class PrisonPearlManager implements Listener {
5964
private final PrisonPearlPlugin plugin;
6065
private final PrisonPearlStorage pearls;
@@ -335,23 +340,22 @@ public void onItemDespawn(ItemDespawnEvent event) {
335340
private Map<UUID, BukkitTask> unloadedPearls = new HashMap<UUID, BukkitTask>();
336341
// Free the pearl if its on a chunk that unloads
337342
@EventHandler(priority = EventPriority.MONITOR)
338-
public void onChunkUnload(ChunkUnloadEvent event) {
343+
public void onChunkUnload(ChunkUnloadEvent event) {
339344
for (Entity e : event.getChunk().getEntities()) {
340345
if (!(e instanceof Item))
341346
continue;
342347

343-
final PrisonPearl pp = pearls.getByItemStack(
344-
((Item) e).getItemStack());
345-
if (pp == null) {
348+
final PrisonPearl pp = pearls.getByItemStack(((Item) e).getItemStack());
349+
350+
if (pp == null)
346351
continue;
347-
}
348-
352+
349353
final Player player = Bukkit.getPlayer(pp.getImprisonedId());
350354
final Entity entity = e;
351355
// doing this in onChunkUnload causes weird things to happen
352356

353357
event.setCancelled(true);
354-
UUID uuid = pp.getImprisonedId();
358+
final UUID uuid = pp.getImprisonedId();
355359
if (unloadedPearls.containsKey(uuid))
356360
return;
357361
BukkitTask count = Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
@@ -360,14 +364,40 @@ public void run(){
360364
pp.getImprisonedId() + ") is being freed. Reason: Chunk with PrisonPearl unloaded."))
361365
{
362366
entity.remove();
367+
unloadedPearls.remove(uuid);
363368
}
364369

365370
}
366371
}, plugin.getPPConfig().getChunkUnloadDelay());
367372
unloadedPearls.put(uuid, count);
368373
}
369374
}
370-
375+
376+
// Prevent dropped Prison Pearl's from being despawned.
377+
// TODO: PrisonPearl items specifically aren't being picked up from chunk.getEntities()
378+
/*@EventHandler(priority = EventPriority.MONITOR)
379+
public void onChunkLoad(ChunkLoadEvent event) {
380+
// Don't need to check for unloaded pearls if there are none.
381+
if (unloadedPearls.isEmpty() || event.isNewChunk()) return;
382+
383+
// Search for currently unloaded Prison Pearls.
384+
for (Entity entity : event.getChunk().getEntities()) {
385+
if (entity instanceof Item) {
386+
Item item = (Item) entity;
387+
PrisonPearl prisonPearl = pearls.getByItemStack(item.getItemStack());
388+
389+
if (prisonPearl != null) {
390+
UUID imprisonedUuid = prisonPearl.getImprisonedId();
391+
392+
if (unloadedPearls.containsKey(imprisonedUuid)) {
393+
unloadedPearls.get(imprisonedUuid).cancel();
394+
unloadedPearls.remove(imprisonedUuid);
395+
}
396+
}
397+
}
398+
}
399+
}*/
400+
371401
// Free the pearl if it combusts in lava/fire
372402
@EventHandler(priority = EventPriority.MONITOR)
373403
public void onEntityCombustEvent(EntityCombustEvent event) {
@@ -384,7 +414,6 @@ public void onEntityCombustEvent(EntityCombustEvent event) {
384414
freePearl(pp, reason);
385415
}
386416

387-
388417
// Handle inventory dragging properly.
389418
@EventHandler(priority = EventPriority.HIGHEST)
390419
public void onInventoryDrag(InventoryDragEvent event) {
@@ -413,6 +442,20 @@ public void onInventoryDrag(InventoryDragEvent event) {
413442
}
414443
}
415444

445+
446+
// Prevent imprisoned players from placing PrisonPearls in their inventory.
447+
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
448+
public void onPrisonPearlClick(InventoryClickEvent event) {
449+
Player clicker = (Player) event.getWhoClicked();
450+
451+
if (pearls.isPrisonPearl(event.getCurrentItem())
452+
&& pearls.isImprisoned(clicker)) {
453+
clicker.sendMessage(ChatColor.RED + "Imprisoned players cannot pick up prison pearls!");
454+
event.setCancelled(true); // Prevent imprisoned player from grabbing PrisonPearls.
455+
}
456+
}
457+
458+
416459
// Track the location of a pearl
417460
// Forbid pearls from being put in storage minecarts
418461
@EventHandler(priority = EventPriority.HIGHEST)
@@ -554,7 +597,6 @@ private void updatePearlHolder(PrisonPearl pearl, InventoryHolder holder, Cancel
554597
}
555598
}
556599

557-
558600
// Track the location of a pearl if it spawns as an item for any reason
559601
@EventHandler(priority = EventPriority.MONITOR)
560602
public void onItemSpawn(ItemSpawnEvent event) {
@@ -565,27 +607,30 @@ public void onItemSpawn(ItemSpawnEvent event) {
565607
pp.markMove();
566608
updatePearl(pp, item);
567609
}
610+
568611

569612
// Track the location of a pearl if a player picks it up
570-
@EventHandler(priority = EventPriority.MONITOR)
613+
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
571614
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
572615
PrisonPearl pp = pearls.getByItemStack(event.getItem().getItemStack());
573616
if (pp == null)
574617
return;
618+
575619
pp.markMove();
576620
updatePearl(pp, event.getPlayer());
577-
// For when a pearl is dropped in an unloaded chunk
578-
if (unloadedPearls.isEmpty())
579-
return;
580-
UUID want = pp.getImprisonedId();
581-
for (UUID uuid: unloadedPearls.keySet()){
582-
if (want.equals(uuid)){
583-
unloadedPearls.get(uuid).cancel();
584-
unloadedPearls.remove(uuid);
585-
}
621+
}
622+
623+
624+
// Prevent imprisoned players from picking up PrisonPearls.
625+
@EventHandler(priority = EventPriority.NORMAL)
626+
public void onPlayerPickupPearl(PlayerPickupItemEvent event) {
627+
if (pearls.isPrisonPearl(event.getItem().getItemStack())
628+
&& pearls.isImprisoned(event.getPlayer())) {
629+
event.setCancelled(true);
586630
}
587631
}
588632

633+
589634
// Deny pearls traveling to other worlds.
590635
@EventHandler(priority = EventPriority.HIGHEST)
591636
public void worldChangeEvent(PlayerTeleportEvent event){
@@ -668,4 +713,4 @@ private Configuration getConfig() {
668713
return plugin.getConfig();
669714
}
670715

671-
}
716+
}

src/com/untamedears/PrisonPearl/PrisonPearlPlugin.java

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,15 @@ public void onPrisonPearlEvent(PrisonPearlEvent event) {
596596
if (event.getType() == PrisonPearlEvent.Type.NEW) {
597597
updateAttachment(player);
598598

599+
// Log the capturing PrisonPearl event.
599600
Player imprisoner = event.getImprisoner();
600-
log.info(imprisoner.getDisplayName() + " has bound " + playerName + " to a PrisonPearl");
601+
String imprisonerLoc = serializeLocation(imprisoner.getLocation());
602+
String playerLoc = serializeLocation(player.getLocation());
603+
String message = String.format("%s [%s] has bound %s [%s] to a PrisonPearl",
604+
imprisoner.getDisplayName(), imprisonerLoc,
605+
playerName, playerLoc);
606+
log.info(message);
607+
601608
imprisoner.sendMessage(ChatColor.GREEN+"You've bound " + playerName + ChatColor.GREEN+" to a prison pearl!");
602609
if (player != null) {
603610
player.sendMessage(ChatColor.RED+"You've been bound to a prison pearl owned by " + imprisoner.getDisplayName());
@@ -636,8 +643,21 @@ public void onPrisonPearlEvent(PrisonPearlEvent event) {
636643
}
637644
UUID[] alts = altsList.getAltsArray(playerId);
638645
checkBans(alts);
639-
640-
log.info(playerName + " was freed");
646+
647+
// Log the free'ing PrisonPearl event with coordinates.
648+
Player imprisoner = event.getPrisonPearl().getHolderPlayer();
649+
String playerLoc = player != null ? serializeLocation(player.getLocation()) : "[???]";
650+
String message = String.format("%s [%s] was freed", playerName, playerLoc);
651+
652+
if (imprisoner != null) {
653+
String imprisonerLoc = serializeLocation(imprisoner.getLocation());
654+
message = String.format("%s [%s] was freed by %s [%s]",
655+
playerName, playerLoc,
656+
imprisoner.getDisplayName(), imprisonerLoc);
657+
}
658+
659+
log.info(message);
660+
641661
if (player != null) {
642662
player.sendMessage("You've been freed!");
643663
broadcastman.broadcast(player, playerName + " was freed!");
@@ -932,7 +952,7 @@ public int checkBan(UUID id) {
932952
}
933953
if (pearledCount >= maxImprisonedAlts) {
934954
if (!pearls.isImprisoned(id)) {
935-
banAndKick(id, pearledCount, names);
955+
banAndKick(id, pearledCount);
936956
return 2;
937957
}
938958
int count = 0;
@@ -941,7 +961,7 @@ public int checkBan(UUID id) {
941961
count++;
942962
}
943963
if (count >= maxImprisonedAlts) {
944-
banAndKick(id, pearledCount, names);
964+
banAndKick(id, pearledCount);
945965
return 2;
946966
}
947967
}
@@ -957,17 +977,17 @@ public int checkBan(UUID id) {
957977
return 0;
958978
}
959979

960-
private void banAndKick(UUID id, int pearledCount, String names) {
980+
private void banAndKick(UUID id, int pearledCount) {
961981
Player p = this.getServer().getPlayer(id);
962982
if (p != null) {
963983
p.kickPlayer(kickMessage);
964984
}
965985
if (banManager_.isBanned(id)) {
966-
log.info(id+" still banned for having "+pearledCount+" imprisoned alts: "+names);
986+
log.info(id+" still banned for having "+pearledCount+" imprisoned alts.");
967987
return;
968988
}
969989
banManager_.ban(id);
970-
log.info("banning "+id+" for having "+pearledCount+" imprisoned alts: "+names);
990+
log.info("banning "+id+" for having "+pearledCount+" imprisoned alts.");
971991
}
972992

973993
private void checkBans(UUID[] ids) {
@@ -992,7 +1012,7 @@ private void checkBans(UUID[] ids) {
9921012
p.kickPlayer(kickMessage);
9931013
}
9941014
banManager_.ban(id);
995-
log.info("banning " + id + ", for having " + pearledCount + " imprisoned alts: " + iNames);
1015+
log.info("banning " + id + ", for having " + pearledCount + " imprisoned alts.");
9961016
} else if (banManager_.isBanned(id)) {
9971017
banManager_.pardon(id);
9981018
log.info("unbanning " + id + ", no longer has too many imprisoned alts.");
@@ -1074,4 +1094,23 @@ public static PrisonPearlManager getPrisonPearlManager(){
10741094
public BanManager getBanManager() {
10751095
return banManager_;
10761096
}
1077-
}
1097+
1098+
/**
1099+
* Log a message to plugin {@link Logger}.
1100+
* @param message - the message to log.
1101+
*/
1102+
public static void log(String message) {
1103+
if (log != null) {
1104+
log.log(Level.INFO, message);
1105+
}
1106+
}
1107+
1108+
/**
1109+
* @param location - the location to serialize into user-friendly text.
1110+
* @return the serialized user-friendly string representing location.
1111+
*/
1112+
private static String serializeLocation(Location location) {
1113+
return String.format("%s, (%d, %d, %d)", location.getWorld().getName(),
1114+
location.getBlockX(), location.getBlockY(), location.getBlockZ());
1115+
}
1116+
}

src/com/untamedears/PrisonPearl/PrisonPearlStorage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ public PrisonPearl getByImprisoned(Player player) {
283283
return pearls_byimprisoned.get(player.getUniqueId());
284284
}
285285

286+
/**
287+
* @param itemStack - the item stack to check for being a PrisonPearl.
288+
* @return true, if itemStack is a PrisonPearl, false otherwise.
289+
*/
290+
public boolean isPrisonPearl(ItemStack itemStack) {
291+
return getByItemStack(itemStack) != null;
292+
}
293+
286294
public Integer getPearlCount(){
287295
return pearls_byimprisoned.size();
288296
}
@@ -444,4 +452,4 @@ public String restorePearls(PrisonPearlManager pearlman, String config){
444452
private Configuration getConfig() {
445453
return plugin.getConfig();
446454
}
447-
}
455+
}

0 commit comments

Comments
 (0)