2013-10-07 21:11:46 +02:00
package com.sekwah.advancedportals ;
2013-12-15 23:36:07 +01:00
import org.bukkit.Bukkit ;
import org.bukkit.GameMode ;
2013-10-08 21:03:30 +02:00
import org.bukkit.Location ;
2013-10-07 22:26:03 +02:00
import org.bukkit.Material ;
2013-10-08 21:03:30 +02:00
import org.bukkit.entity.Player ;
2013-10-07 22:26:03 +02:00
import org.bukkit.event.EventHandler ;
2013-12-15 23:36:07 +01:00
import org.bukkit.event.EventPriority ;
2013-10-07 21:11:46 +02:00
import org.bukkit.event.Listener ;
2013-10-07 22:26:03 +02:00
import org.bukkit.event.block.Action ;
import org.bukkit.event.player.PlayerInteractEvent ;
import org.bukkit.event.player.PlayerMoveEvent ;
2013-10-08 21:03:30 +02:00
import org.bukkit.metadata.FixedMetadataValue ;
2013-12-14 22:29:24 +01:00
import org.bukkit.event.player.PlayerPortalEvent ;
2013-12-15 23:36:07 +01:00
import com.sekwah.advancedportals.DataCollector.DataCollector ;
2013-12-08 20:59:55 +01:00
import com.sekwah.advancedportals.portalcontrolls.Portal ;
2013-10-07 22:26:03 +02:00
public class Listeners implements Listener {
2013-10-07 21:11:46 +02:00
2013-10-08 21:03:30 +02:00
private final AdvancedPortalsPlugin plugin ;
2013-10-08 23:46:12 +02:00
// The needed config values will be stored so they are easier to access later
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<22> eP...
2013-12-08 21:29:04 +01:00
private static boolean UseOnlyServerAxe = false ;
2013-12-08 20:59:55 +01:00
2013-12-08 21:29:04 +01:00
private static Material WandMaterial ;
2013-10-08 23:46:12 +02:00
2013-12-14 22:29:24 +01:00
@SuppressWarnings ( " deprecation " )
public Listeners ( AdvancedPortalsPlugin plugin ) {
2013-10-08 21:03:30 +02:00
this . plugin = plugin ;
2013-10-09 21:31:16 +02:00
2013-10-08 23:46:12 +02:00
ConfigAccessor config = new ConfigAccessor ( plugin , " Config.yml " ) ;
2013-12-14 22:29:24 +01:00
UseOnlyServerAxe = config . getConfig ( ) . getBoolean ( " UseOnlyServerMadeAxe " ) ;
2013-10-09 21:31:16 +02:00
2013-12-08 20:59:55 +01:00
String ItemID = config . getConfig ( ) . getString ( " AxeItemId " ) ;
try
{
WandMaterial = Material . getMaterial ( Integer . parseInt ( ItemID ) ) ;
}
catch ( Exception e )
{
WandMaterial = Material . getMaterial ( ItemID ) ;
}
2013-10-08 21:03:30 +02:00
plugin . getServer ( ) . getPluginManager ( ) . registerEvents ( this , plugin ) ;
}
2013-12-08 21:29:04 +01:00
2013-12-14 22:29:24 +01:00
@SuppressWarnings ( " deprecation " )
public static void reloadValues ( AdvancedPortalsPlugin plugin ) {
2013-12-08 21:29:04 +01:00
ConfigAccessor config = new ConfigAccessor ( plugin , " Config.yml " ) ;
UseOnlyServerAxe = config . getConfig ( ) . getBoolean ( " UseOnlyServerMadeAxe " ) ;
String ItemID = config . getConfig ( ) . getString ( " AxeItemId " ) ;
try
{
WandMaterial = Material . getMaterial ( Integer . parseInt ( ItemID ) ) ;
}
catch ( Exception e )
{
WandMaterial = Material . getMaterial ( ItemID ) ;
}
}
2013-10-08 21:03:30 +02:00
2013-12-15 23:36:07 +01:00
@EventHandler ( priority = EventPriority . HIGHEST )
2013-10-07 22:26:03 +02:00
public void onMoveEvent ( PlayerMoveEvent event ) {
// will check if the player is in the portal or not.
2013-12-15 23:36:07 +01:00
if ( Portal . portalsActive ) {
Player player = event . getPlayer ( ) ;
Location loc = player . getLocation ( ) ;
Object [ ] portals = Portal . Portals ;
int portalId = 0 ;
for ( Object portal : portals ) {
if ( Portal . worldName [ portalId ] . equals ( player . getWorld ( ) . getName ( ) ) ) {
if ( Portal . triggers [ portalId ] . equals ( player . getLocation ( ) . getBlock ( ) . getType ( ) )
| | Portal . triggers [ portalId ] . equals ( player . getEyeLocation ( ) . getBlock ( ) . getType ( ) ) ) {
if ( ( Portal . pos1 [ portalId ] . getX ( ) + 1D ) > = loc . getX ( ) & & ( Portal . pos1 [ portalId ] . getY ( ) + 1D ) > = loc . getY ( ) & & ( Portal . pos1 [ portalId ] . getZ ( ) + 1D ) > = loc . getZ ( ) ) {
if ( Portal . pos2 [ portalId ] . getX ( ) < = loc . getX ( ) & & Portal . pos2 [ portalId ] . getY ( ) < = loc . getY ( ) & & Portal . pos2 [ portalId ] . getZ ( ) < = loc . getZ ( ) ) {
Portal . activate ( player , portal . toString ( ) ) ;
DataCollector . playerWarped ( ) ;
final Player finalplayer = event . getPlayer ( ) ;
if ( player . getGameMode ( ) . equals ( GameMode . CREATIVE ) ) {
player . setMetadata ( " HasWarped " , new FixedMetadataValue ( plugin , true ) ) ;
Bukkit . getScheduler ( ) . scheduleSyncDelayedTask ( plugin , new Runnable ( ) {
public void run ( ) {
finalplayer . removeMetadata ( " HasWarped " , plugin ) ;
}
} , 20 ) ;
}
}
}
}
}
portalId + + ;
}
2013-12-08 20:59:55 +01:00
}
2013-10-07 22:26:03 +02:00
}
2013-12-14 22:29:24 +01:00
@SuppressWarnings ( " deprecation " )
@EventHandler
public void onPortalEvent ( PlayerPortalEvent event ) {
2013-12-15 23:36:07 +01:00
if ( Portal . portalsActive ) {
Player player = event . getPlayer ( ) ;
if ( player . hasMetadata ( " HasWarped " ) ) {
event . setCancelled ( true ) ;
return ;
}
Location loc = player . getLocation ( ) ;
Object [ ] portals = Portal . Portals ;
int portalId = 0 ;
for ( Object portal : portals ) {
if ( Portal . worldName [ portalId ] . equals ( player . getWorld ( ) . getName ( ) ) ) {
if ( ( Portal . pos1 [ portalId ] . getX ( ) + 3D ) > = loc . getX ( ) & & ( Portal . pos1 [ portalId ] . getY ( ) + 3D ) > = loc . getY ( ) & & ( Portal . pos1 [ portalId ] . getZ ( ) + 3D ) > = loc . getZ ( ) ) {
if ( ( Portal . pos2 [ portalId ] . getX ( ) - 3D ) < = loc . getX ( ) & & ( Portal . pos2 [ portalId ] . getY ( ) - 3D ) < = loc . getY ( ) & & ( Portal . pos2 [ portalId ] . getZ ( ) - 3D ) < = loc . getZ ( ) ) {
event . setCancelled ( true ) ;
}
}
}
portalId + + ;
}
}
2013-12-14 22:29:24 +01:00
}
2013-10-07 22:27:40 +02:00
@SuppressWarnings ( " deprecation " )
2013-10-08 11:36:42 +02:00
@EventHandler
2013-10-07 22:26: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
2013-10-08 21:03:30 +02:00
Player player = event . getPlayer ( ) ;
2013-10-09 23:14:44 +02:00
if ( player . hasPermission ( " AdvancedPortals.CreatePortal " ) ) {
2013-10-08 23:22:03 +02:00
// UseOnlyServerMadeAxe being set to true makes is so only the axe generated by the server can be used so other iron axes can be used normally,
// by default its false but it is a nice feature in case the user wants to use the axe normally too, such as a admin playing survival or it being used
// as a weapon.
2013-12-08 20:59:55 +01:00
if ( ( ! UseOnlyServerAxe | | event . getItem ( ) . getItemMeta ( ) . getDisplayName ( ) . equals ( " <EFBFBD> ePortal Region Selector" ) ) & & event . getPlayer ( ) . getItemInHand ( ) . getTypeId ( ) = = WandMaterial . getId ( ) ) {
2013-10-07 22:26:03 +02:00
2013-10-08 23:22: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 ) {
2013-10-15 22:45:44 +02:00
Location blockloc = event . getClickedBlock ( ) . getLocation ( ) ;
2013-10-08 23:22:03 +02:00
// 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 ( ) ) ) ;
2013-10-09 23:42:03 +02:00
player . setMetadata ( " Pos1World " , new FixedMetadataValue ( plugin , blockloc . getWorld ( ) . getName ( ) ) ) ;
2013-10-15 22:45:44 +02:00
player . sendMessage ( " <EFBFBD> eYou have selected pos1! X:" + blockloc . getBlockX ( ) + " Y: " + blockloc . getBlockY ( ) + " Z: " + blockloc . getBlockZ ( ) + " World: " + blockloc . getWorld ( ) . getName ( ) ) ;
2013-10-08 23:22: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)
return ;
}
else if ( event . getAction ( ) = = Action . RIGHT_CLICK_BLOCK ) {
2013-10-15 22:45:44 +02:00
Location blockloc = event . getClickedBlock ( ) . getLocation ( ) ;
2013-10-08 23:22:03 +02:00
player . setMetadata ( " Pos2X " , new FixedMetadataValue ( plugin , blockloc . getBlockX ( ) ) ) ;
player . setMetadata ( " Pos2Y " , new FixedMetadataValue ( plugin , blockloc . getBlockY ( ) ) ) ;
player . setMetadata ( " Pos2Z " , new FixedMetadataValue ( plugin , blockloc . getBlockZ ( ) ) ) ;
2013-10-09 23:42:03 +02:00
player . setMetadata ( " Pos2World " , new FixedMetadataValue ( plugin , blockloc . getWorld ( ) . getName ( ) ) ) ;
2013-10-15 22:45:44 +02:00
player . sendMessage ( " <EFBFBD> eYou have selected pos2! X:" + blockloc . getBlockX ( ) + " Y: " + blockloc . getBlockY ( ) + " Z: " + blockloc . getBlockZ ( ) + " World: " + blockloc . getWorld ( ) . getName ( ) ) ;
2013-10-08 23:22: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)
return ;
}
2013-10-08 21:03:30 +02:00
2013-10-07 22:26:03 +02:00
}
2013-10-08 23:22:03 +02:00
2013-10-07 22:26:03 +02:00
}
2013-10-08 23:22:03 +02:00
2013-10-07 22:26:03 +02:00
}
2013-10-07 21:11:46 +02:00
}