2020-01-20 11:55:18 +01:00
package com.sekwah.advancedportals.bukkit.listeners ;
import com.sekwah.advancedportals.bukkit.AdvancedPortalsPlugin ;
import com.sekwah.advancedportals.bukkit.ConfigAccessor ;
import com.sekwah.advancedportals.bukkit.PluginMessages ;
import com.sekwah.advancedportals.bukkit.api.events.WarpEvent ;
import com.sekwah.advancedportals.bukkit.destinations.Destination ;
import com.sekwah.advancedportals.bukkit.portals.AdvancedPortal ;
import com.sekwah.advancedportals.bukkit.portals.Portal ;
2019-06-03 04:11:34 +02:00
import org.bukkit.* ;
2018-07-23 04:02:42 +02:00
import org.bukkit.block.data.BlockData ;
2018-07-31 03:57:30 +02:00
import org.bukkit.block.data.Orientable ;
2015-01-15 20:56:13 +01:00
import org.bukkit.entity.Player ;
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
import org.bukkit.event.Listener ;
import org.bukkit.event.block.Action ;
2017-11-30 19:56:42 +01:00
import org.bukkit.event.entity.CreatureSpawnEvent ;
2015-09-16 23:45:33 +02:00
import org.bukkit.event.entity.EntityCombustEvent ;
import org.bukkit.event.entity.EntityDamageEvent ;
2017-07-14 01:15:08 +02:00
import org.bukkit.event.player.* ;
2017-06-12 16:44:03 +02:00
import org.bukkit.inventory.ItemStack ;
2015-01-15 20:56:13 +01:00
import org.bukkit.metadata.FixedMetadataValue ;
2018-07-12 14:49:20 +02:00
import java.util.UUID ;
2015-01-15 20:56:13 +01:00
public class Listeners implements Listener {
2019-05-31 05:04:48 +02:00
2016-03-29 13:38:03 +02:00
private static boolean UseOnlyServerAxe = false ;
private static Material WandMaterial ;
2016-08-04 05:26:08 +02:00
2016-03-29 13:38:03 +02:00
private final AdvancedPortalsPlugin plugin ;
@SuppressWarnings ( " deprecation " )
public Listeners ( AdvancedPortalsPlugin plugin ) {
this . plugin = plugin ;
ConfigAccessor config = new ConfigAccessor ( plugin , " config.yml " ) ;
UseOnlyServerAxe = config . getConfig ( ) . getBoolean ( " UseOnlyServerMadeAxe " ) ;
String ItemID = config . getConfig ( ) . getString ( " AxeItemId " ) ;
2017-06-12 16:44:03 +02:00
if ( ItemID = = null ) {
WandMaterial = Material . IRON_AXE ;
2016-03-29 13:38:03 +02:00
}
2017-06-12 16:44:03 +02:00
else {
2018-07-23 04:02:42 +02:00
WandMaterial = Material . getMaterial ( ItemID ) ;
2017-06-12 16:44:03 +02:00
}
2016-03-29 13:38:03 +02:00
plugin . getServer ( ) . getPluginManager ( ) . registerEvents ( this , plugin ) ;
}
@SuppressWarnings ( " deprecation " )
public static void reloadValues ( AdvancedPortalsPlugin plugin ) {
ConfigAccessor config = new ConfigAccessor ( plugin , " config.yml " ) ;
UseOnlyServerAxe = config . getConfig ( ) . getBoolean ( " UseOnlyServerMadeAxe " ) ;
String ItemID = config . getConfig ( ) . getString ( " AxeItemId " ) ;
2018-07-23 04:02:42 +02:00
WandMaterial = Material . getMaterial ( ItemID ) ;
2016-03-29 13:38:03 +02:00
}
2016-07-31 07:17:06 +02:00
@EventHandler
public void onJoinEvent ( PlayerJoinEvent event ) {
2020-04-04 17:12:09 +02:00
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
2020-01-20 11:55:18 +01:00
2020-01-20 16:45:55 +01:00
/ * if ( plugin . PlayerDestiMap . containsKey ( event . getPlayer ( ) ) ) {
2020-01-20 11:55:18 +01:00
String desti = plugin . PlayerDestiMap . get ( event . getPlayer ( ) ) ;
Destination . warp ( event . getPlayer ( ) , desti ) ;
2020-01-20 16:45:55 +01:00
} * /
2017-07-14 01:15:08 +02:00
}
2017-07-17 00:42:22 +02:00
@EventHandler
public void onWorldChangeEvent ( PlayerChangedWorldEvent event ) {
2020-04-04 17:12:09 +02:00
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
2017-07-17 00:42:22 +02:00
}
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2017-07-17 00:42:22 +02:00
public void onTeleportEvent ( PlayerTeleportEvent event ) {
2020-04-04 17:12:09 +02:00
// TODO: make relability
// Portal.cooldown.put(event.getPlayer().getName(), System.currentTimeMillis());
2017-07-17 00:42:22 +02:00
}
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2017-11-30 19:56:42 +01:00
public void spawnMobEvent ( CreatureSpawnEvent event ) {
if ( event . getSpawnReason ( ) = = CreatureSpawnEvent . SpawnReason . NETHER_PORTAL & & Portal . inPortalRegion ( event . getLocation ( ) , Portal . getPortalProtectionRadius ( ) ) ) {
event . setCancelled ( true ) ;
}
}
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2017-07-14 01:15:08 +02:00
public void onLeaveEvent ( PlayerQuitEvent event ) {
Portal . cooldown . remove ( event . getPlayer ( ) . getName ( ) ) ;
2018-07-12 14:49:20 +02:00
UUID uuid = event . getPlayer ( ) . getUniqueId ( ) ;
for ( AdvancedPortal portal : Portal . portals ) {
portal . inPortal . remove ( uuid ) ;
}
2016-07-31 07:17:06 +02:00
}
2019-06-03 00:25:29 +02:00
@EventHandler ( priority = EventPriority . HIGHEST , ignoreCancelled = true )
2016-03-29 13:38:03 +02:00
public void onMoveEvent ( PlayerMoveEvent event ) {
// will check if the player is in the portal or not.
2018-01-14 21:34:48 +01:00
if ( ! Portal . portalsActive | | event . isCancelled ( ) ) {
2016-03-29 13:38:03 +02:00
return ;
}
Player player = event . getPlayer ( ) ;
2019-05-31 05:04:48 +02:00
2016-03-29 13:38:03 +02:00
Location loc = event . getTo ( ) ;
Location eyeLoc = new Location ( loc . getWorld ( ) , loc . getX ( ) , loc . getY ( ) + player . getEyeHeight ( ) , loc . getZ ( ) ) ;
2017-06-13 19:06:48 +02:00
2019-05-31 05:04:48 +02:00
checkTriggerLocations ( player , false , loc , eyeLoc ) ;
2017-06-13 19:06:48 +02:00
2019-05-31 05:04:48 +02:00
}
2016-03-29 13:38:03 +02:00
2019-05-31 05:04:48 +02:00
public void checkTriggerLocations ( Player player , boolean useDelayed , Location . . . locations ) {
for ( AdvancedPortal portal : Portal . portals ) {
boolean delayed = portal . hasArg ( " delayed " ) & & portal . getArg ( " delayed " ) . equalsIgnoreCase ( " true " ) ;
for ( Location loc : locations ) {
if ( delayed = = useDelayed ) {
if ( delayed ? Portal . locationInPortal ( portal , loc , 1 ) : Portal . locationInPortalTrigger ( portal , loc ) ) {
2019-06-02 05:39:08 +02:00
if ( portal . getTriggers ( ) . contains ( Material . NETHER_PORTAL ) ) {
2019-05-31 05:04:48 +02:00
if ( player . getGameMode ( ) . equals ( GameMode . CREATIVE ) ) {
player . setMetadata ( " hasWarped " , new FixedMetadataValue ( plugin , true ) ) ;
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( plugin , new RemoveWarpData ( player ) , 10 ) ;
}
2019-06-02 05:39:08 +02:00
}
if ( portal . getTriggers ( ) . contains ( Material . LAVA ) ) {
2019-05-31 05:04:48 +02:00
player . setMetadata ( " lavaWarped " , new FixedMetadataValue ( plugin , true ) ) ;
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( plugin , new RemoveLavaData ( player ) , 10 ) ;
}
if ( portal . inPortal . contains ( player . getUniqueId ( ) ) ) return ;
WarpEvent warpEvent = new WarpEvent ( player , portal ) ;
plugin . getServer ( ) . getPluginManager ( ) . callEvent ( warpEvent ) ;
if ( ! warpEvent . isCancelled ( ) ) Portal . activate ( player , portal ) ;
if ( ! delayed ) portal . inPortal . add ( player . getUniqueId ( ) ) ;
return ;
} else if ( ! delayed ) portal . inPortal . remove ( player . getUniqueId ( ) ) ;
}
}
}
2016-03-29 13:38:03 +02:00
}
2016-03-29 14:44:39 +02:00
// These are here because java 7 can only take finals straight into a runnable
class RemoveLavaData implements Runnable {
private Player player ;
public RemoveLavaData ( Player player ) {
this . player = player ;
}
@Override
public void run ( ) {
player . removeMetadata ( " lavaWarped " , plugin ) ;
player . setFireTicks ( 0 ) ;
}
} ;
class RemoveWarpData implements Runnable {
private Player player ;
public RemoveWarpData ( Player player ) {
this . player = player ;
}
@Override
public void run ( ) {
if ( player ! = null & & player . isOnline ( ) ) {
player . removeMetadata ( " hasWarped " , plugin ) ;
}
}
2019-05-31 05:04:48 +02:00
}
2016-03-29 14:44:39 +02:00
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2016-03-29 13:38:03 +02:00
public void onCombustEntityEvent ( EntityCombustEvent event ) {
2019-06-03 00:25:29 +02:00
if ( event . getEntity ( ) instanceof Player & & Portal . inPortalTriggerRegion ( event . getEntity ( ) . getLocation ( ) ) )
2016-08-01 08:45:24 +02:00
event . setCancelled ( true ) ;
2016-03-29 13:38:03 +02:00
}
2016-04-01 23:52:38 +02:00
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2016-03-29 13:38:03 +02:00
public void onDamEvent ( EntityDamageEvent event ) {
2019-06-03 00:25:29 +02:00
if ( event . getEntity ( ) instanceof Player & & ( event . getCause ( ) = = EntityDamageEvent . DamageCause . LAVA | | event . getCause ( ) = = EntityDamageEvent . DamageCause . FIRE | | event . getCause ( ) = = EntityDamageEvent . DamageCause . FIRE_TICK ) ) {
2016-08-01 08:45:24 +02:00
if ( event . getEntity ( ) . hasMetadata ( " lavaWarped " ) | Portal . inPortalTriggerRegion ( event . getEntity ( ) . getLocation ( ) ) )
2016-03-29 13:38:03 +02:00
event . setCancelled ( true ) ;
}
}
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2016-03-29 13:38:03 +02:00
public void onPortalEvent ( PlayerPortalEvent event ) {
2019-05-31 05:04:48 +02:00
if ( event . isCancelled ( ) ) {
return ;
}
2016-03-29 13:38:03 +02:00
Player player = event . getPlayer ( ) ;
2019-05-31 05:04:48 +02:00
if ( ! player . hasMetadata ( " hasWarped " ) ) {
Location loc = event . getFrom ( ) ;
Location eyeLoc = new Location ( loc . getWorld ( ) , loc . getX ( ) , loc . getY ( ) + player . getEyeHeight ( ) , loc . getZ ( ) ) ;
checkTriggerLocations ( player , true , loc , eyeLoc ) ;
}
2017-06-11 19:32:53 +02:00
if ( player . hasMetadata ( " hasWarped " ) | Portal . inPortalRegion ( event . getFrom ( ) , 1 ) )
2016-03-29 13:38:03 +02:00
event . setCancelled ( true ) ;
}
2019-06-03 00:25:29 +02:00
@EventHandler ( ignoreCancelled = true )
2016-03-29 13:38:03 +02:00
public void onItemInteract ( PlayerInteractEvent event ) {
// will detect if the player is using an axe so the points of a portal can be set
// also any other detections such as sign interaction or basic block protection
Player player = event . getPlayer ( ) ;
if ( player . hasMetadata ( " selectingPortal " ) & & ( event . getAction ( ) = = Action . LEFT_CLICK_BLOCK | | event . getAction ( ) = = Action . RIGHT_CLICK_BLOCK ) ) {
2017-05-19 07:09:57 +02:00
for ( AdvancedPortal portal : Portal . portals ) {
2016-08-01 08:45:24 +02:00
if ( Portal . locationInPortal ( portal , event . getClickedBlock ( ) . getLocation ( ) , 0 ) ) {
2017-06-13 19:06:48 +02:00
player . sendMessage ( PluginMessages . customPrefix + " \ u00A7a You have selected: \ u00A7e " + portal . getName ( ) ) ;
player . setMetadata ( " selectedPortal " , new FixedMetadataValue ( plugin , portal . getName ( ) ) ) ; // adds the name to the metadata of the character
2016-08-01 08:45:24 +02:00
event . setCancelled ( true ) ;
player . removeMetadata ( " selectingPortal " , plugin ) ;
return ;
2016-03-29 13:38:03 +02:00
}
}
2016-08-02 05:51:16 +02:00
player . sendMessage ( PluginMessages . customPrefixFail + " \ u00A7c No portal was selected. If you would like to stop selecting please type \ u00A7e/portal select \ u00A7cagain! " ) ;
2016-03-29 13:38:03 +02:00
event . setCancelled ( true ) ;
return ;
}
2017-06-11 19:32:53 +02:00
if ( player . hasPermission ( " advancedportals.createportal " ) ) {
2016-03-29 13:38:03 +02:00
2017-06-13 03:33:47 +02:00
if ( event . getItem ( ) ! = null & & event . getItem ( ) . getType ( ) = = WandMaterial // was type id
2017-06-12 16:44:03 +02:00
& & ( ! UseOnlyServerAxe | | ( checkItemForName ( event . getItem ( ) ) & & event . getItem ( ) . getItemMeta ( ) . getDisplayName ( ) . equals ( " \ u00A7ePortal Region Selector " ) ) ) ) {
2016-03-29 13:38:03 +02:00
// This checks if the action was a left or right click and if it was directly effecting a block.
if ( event . getAction ( ) = = Action . LEFT_CLICK_BLOCK ) {
Location blockloc = event . getClickedBlock ( ) . getLocation ( ) ;
// stores the selection as metadata on the character so then it isn't saved anywhere, if the player logs out it will
// have to be selected again if the player joins, also it does not affect any other players.
player . setMetadata ( " Pos1X " , new FixedMetadataValue ( plugin , blockloc . getBlockX ( ) ) ) ;
player . setMetadata ( " Pos1Y " , new FixedMetadataValue ( plugin , blockloc . getBlockY ( ) ) ) ;
player . setMetadata ( " Pos1Z " , new FixedMetadataValue ( plugin , blockloc . getBlockZ ( ) ) ) ;
player . setMetadata ( " Pos1World " , new FixedMetadataValue ( plugin , blockloc . getWorld ( ) . getName ( ) ) ) ;
2017-06-11 19:32:53 +02:00
player . sendMessage ( " \ u00A7eYou have selected pos1! X: " + blockloc . getBlockX ( ) + " Y: " + blockloc . getBlockY ( )
+ " Z: " + blockloc . getBlockZ ( ) + " World: " + blockloc . getWorld ( ) . getName ( ) ) ;
2016-03-29 13:38:03 +02:00
// Stops the event so the block is not damaged
event . setCancelled ( true ) ;
// Returns the event so no more code is executed(stops unnecessary code being executed)
} else if ( event . getAction ( ) = = Action . RIGHT_CLICK_BLOCK ) {
Location blockloc = event . getClickedBlock ( ) . getLocation ( ) ;
player . setMetadata ( " Pos2X " , new FixedMetadataValue ( plugin , blockloc . getBlockX ( ) ) ) ;
player . setMetadata ( " Pos2Y " , new FixedMetadataValue ( plugin , blockloc . getBlockY ( ) ) ) ;
player . setMetadata ( " Pos2Z " , new FixedMetadataValue ( plugin , blockloc . getBlockZ ( ) ) ) ;
player . setMetadata ( " Pos2World " , new FixedMetadataValue ( plugin , blockloc . getWorld ( ) . getName ( ) ) ) ;
2017-06-11 19:32:53 +02:00
player . sendMessage ( " \ u00A7eYou have selected pos2! X: " + blockloc . getBlockX ( ) + " Y: " + blockloc . getBlockY ( )
+ " Z: " + blockloc . getBlockZ ( ) + " World: " + blockloc . getWorld ( ) . getName ( ) ) ;
2016-03-29 13:38:03 +02:00
// Stops the event so the block is not interacted with
event . setCancelled ( true ) ;
// Returns the event so no more code is executed(stops unnecessary code being executed)
}
2017-06-12 16:44:03 +02:00
} else if ( checkItemForName ( event . getItem ( ) ) & & event . getItem ( ) . getItemMeta ( ) . getDisplayName ( ) . equals ( " \ u00A75Portal Block Placer " ) & &
2018-07-23 04:02:42 +02:00
event . getAction ( ) = = Action . LEFT_CLICK_BLOCK & & event . getClickedBlock ( ) . getType ( ) = = Material . NETHER_PORTAL ) {
BlockData block = event . getClickedBlock ( ) . getBlockData ( ) ;
2018-08-27 01:36:51 +02:00
2018-07-31 03:57:30 +02:00
if ( block instanceof Orientable ) {
Orientable rotatable = ( Orientable ) block ;
if ( rotatable . getAxis ( ) = = Axis . X ) {
rotatable . setAxis ( Axis . Z ) ;
2018-07-23 04:02:42 +02:00
} else {
2018-07-31 03:57:30 +02:00
rotatable . setAxis ( Axis . X ) ;
2018-07-23 04:02:42 +02:00
}
2018-08-27 01:36:51 +02:00
event . getClickedBlock ( ) . setBlockData ( rotatable ) ;
2017-06-11 19:32:53 +02:00
}
event . setCancelled ( true ) ;
}
2016-03-29 13:38:03 +02:00
}
}
2015-09-16 23:45:33 +02:00
2017-06-12 16:44:03 +02:00
private boolean checkItemForName ( ItemStack item ) {
return item ! = null & & item . hasItemMeta ( ) & & item . getItemMeta ( ) . hasDisplayName ( ) ;
}
2015-09-16 23:45:33 +02:00
2015-01-15 20:56:13 +01:00
}