1414
1515import org .bukkit .Bukkit ;
1616import org .bukkit .ChatColor ;
17+ import org .bukkit .Chunk ;
1718import org .bukkit .Location ;
1819import org .bukkit .Material ;
1920import org .bukkit .OfflinePlayer ;
4142import org .bukkit .event .inventory .InventoryAction ;
4243import org .bukkit .event .inventory .InventoryClickEvent ;
4344import org .bukkit .event .inventory .InventoryDragEvent ;
45+ import org .bukkit .event .inventory .InventoryMoveItemEvent ;
4446import org .bukkit .event .player .PlayerInteractEvent ;
4547import org .bukkit .event .player .PlayerItemHeldEvent ;
4648import org .bukkit .event .player .PlayerPickupItemEvent ;
4749import org .bukkit .event .player .PlayerQuitEvent ;
4850import org .bukkit .event .player .PlayerTeleportEvent ;
4951import org .bukkit .event .player .PlayerTeleportEvent .TeleportCause ;
5052import org .bukkit .event .world .ChunkUnloadEvent ;
53+ import org .bukkit .event .world .ChunkLoadEvent ;
5154import org .bukkit .inventory .Inventory ;
5255import org .bukkit .inventory .InventoryHolder ;
5356import org .bukkit .inventory .ItemStack ;
5457import org .bukkit .inventory .PlayerInventory ;
5558import org .bukkit .inventory .meta .ItemMeta ;
5659import org .bukkit .scheduler .BukkitTask ;
5760
61+ import com .untamedears .PrisonPearl .PrisonPearlEvent .Type ;
62+
5863class 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+ }
0 commit comments