2011-12-14 14:18:18 +01:00
package com.wimbli.WorldBorder ;
2019-03-20 19:02:55 +01:00
import org.bukkit.Chunk ;
2012-02-08 03:58:28 +01:00
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.Listener ;
2011-12-14 14:18:18 +01:00
import org.bukkit.event.player.PlayerTeleportEvent ;
2013-03-30 08:56:12 +01:00
import org.bukkit.event.player.PlayerPortalEvent ;
2012-11-11 12:07:27 +01:00
import org.bukkit.event.world.ChunkLoadEvent ;
2011-12-14 14:18:18 +01:00
import org.bukkit.Location ;
2019-03-20 19:02:55 +01:00
import org.bukkit.event.world.ChunkUnloadEvent ;
2011-12-14 14:18:18 +01:00
2012-02-08 03:58:28 +01:00
public class WBListener implements Listener
2011-12-14 14:18:18 +01:00
{
2013-03-30 08:56:12 +01:00
@EventHandler ( priority = EventPriority . LOWEST , ignoreCancelled = true )
2011-12-14 14:18:18 +01:00
public void onPlayerTeleport ( PlayerTeleportEvent event )
{
2013-03-30 08:56:12 +01:00
// if knockback is set to 0, simply return
if ( Config . KnockBack ( ) = = 0 . 0 )
return ;
2013-07-05 22:33:57 +02:00
if ( Config . Debug ( ) )
2014-02-17 13:37:18 +01:00
Config . log ( " Teleport cause: " + event . getCause ( ) . toString ( ) ) ;
2013-07-05 22:33:57 +02:00
2013-03-30 08:56:12 +01:00
Location newLoc = BorderCheckTask . checkPlayer ( event . getPlayer ( ) , event . getTo ( ) , true , true ) ;
if ( newLoc ! = null )
2014-02-17 12:51:40 +01:00
{
if ( event . getCause ( ) = = PlayerTeleportEvent . TeleportCause . ENDER_PEARL & & Config . getDenyEnderpearl ( ) )
{
event . setCancelled ( true ) ;
return ;
}
2013-03-30 08:56:12 +01:00
event . setTo ( newLoc ) ;
2014-02-17 12:51:40 +01:00
}
2013-03-30 08:56:12 +01:00
}
2011-12-14 14:18:18 +01:00
2013-03-30 08:56:12 +01:00
@EventHandler ( priority = EventPriority . LOWEST , ignoreCancelled = true )
public void onPlayerPortal ( PlayerPortalEvent event )
{
2013-04-14 12:18:07 +02:00
// if knockback is set to 0, or portal redirection is disabled, simply return
if ( Config . KnockBack ( ) = = 0 . 0 | | ! Config . portalRedirection ( ) )
2012-11-04 23:46:46 +01:00
return ;
2013-03-30 08:56:12 +01:00
Location newLoc = BorderCheckTask . checkPlayer ( event . getPlayer ( ) , event . getTo ( ) , true , false ) ;
2011-12-14 14:18:18 +01:00
if ( newLoc ! = null )
event . setTo ( newLoc ) ;
}
2012-11-11 12:07:27 +01:00
@EventHandler ( priority = EventPriority . MONITOR )
public void onChunkLoad ( ChunkLoadEvent event )
{
2013-10-20 05:43:07 +02:00
/ * // tested, found to spam pretty rapidly as client repeatedly requests the same chunks since they're not being sent
// definitely too spammy at only 16 blocks outside border
// potentially useful at standard 208 block padding as it was triggering only occasionally while trying to get out all along edge of round border, though sometimes up to 3 triggers within a second corresponding to 3 adjacent chunks
2013-11-16 11:05:02 +01:00
// would of course need to be further worked on to have it only affect chunks outside a border, along with an option somewhere to disable it or even set specified distance outside border for it to take effect; maybe send client chunk composed entirely of air to shut it up
2013-10-20 05:43:07 +02:00
// method to prevent new chunks from being generated, core method courtesy of code from NoNewChunk plugin (http://dev.bukkit.org/bukkit-plugins/nonewchunk/)
if ( event . isNewChunk ( ) )
{
Chunk chunk = event . getChunk ( ) ;
chunk . unload ( false , false ) ;
2014-02-17 13:37:18 +01:00
Config . logWarn ( " New chunk generation has been prevented at X " + chunk . getX ( ) + " , Z " + chunk . getZ ( ) ) ;
2013-10-20 05:43:07 +02:00
}
* /
2012-11-11 12:07:27 +01:00
// make sure our border monitoring task is still running like it should
if ( Config . isBorderTimerRunning ( ) ) return ;
2014-02-17 13:37:18 +01:00
Config . logWarn ( " Border-checking task was not running! Something on your server apparently killed it. It will now be restarted. " ) ;
2012-11-11 12:07:27 +01:00
Config . StartBorderTimer ( ) ;
}
2019-03-20 19:02:55 +01:00
2019-03-20 19:27:10 +01:00
/ *
* Check if there is a fill task running , and if yes , if it ' s for the
* world that the unload event refers to and if the chunk should be
* kept in memory because generation still needs it .
* /
@EventHandler
public void onChunkUnload ( ChunkUnloadEvent e )
{
if ( Config . fillTask ! = null )
{
Chunk chunk = e . getChunk ( ) ;
if ( e . getWorld ( ) = = Config . fillTask . getWorld ( )
& & Config . fillTask . chunkOnUnloadPreventionList ( chunk . getX ( ) , chunk . getZ ( ) ) )
{
e . setCancelled ( true ) ;
}
}
}
2019-03-20 19:02:55 +01:00
2011-12-14 14:18:18 +01:00
}