2015-02-08 14:08:50 +01:00
package com.sekwah.advancedportals.portals ;
2015-01-15 20:56:13 +01:00
2015-01-15 23:16:16 +01:00
import com.google.common.io.ByteArrayDataOutput ;
import com.google.common.io.ByteStreams ;
import com.sekwah.advancedportals.AdvancedPortalsPlugin ;
import com.sekwah.advancedportals.ConfigAccessor ;
2016-08-02 05:51:16 +02:00
import com.sekwah.advancedportals.PluginMessages ;
2016-09-01 21:11:07 +02:00
import com.sekwah.advancedportals.api.portaldata.PortalArg ;
2015-01-15 23:16:16 +01:00
import com.sekwah.advancedportals.destinations.Destination ;
2016-09-01 21:11:07 +02:00
import org.bukkit.* ;
2016-01-19 23:31:16 +01:00
import org.bukkit.configuration.ConfigurationSection ;
2015-01-15 20:56:13 +01:00
import org.bukkit.entity.Player ;
2015-11-15 23:52:29 +01:00
import org.bukkit.permissions.PermissionAttachment ;
2016-09-01 21:11:07 +02:00
import org.bukkit.util.Vector ;
2015-01-15 20:56:13 +01:00
2016-01-19 23:31:16 +01:00
import java.util.ArrayList ;
2016-07-28 20:13:52 +02:00
import java.util.HashMap ;
2015-01-15 23:16:16 +01:00
import java.util.Set ;
import java.util.logging.Level ;
2015-01-15 20:56:13 +01:00
public class Portal {
2016-09-01 21:11:07 +02:00
2016-07-28 20:13:52 +02:00
public static HashMap < Player , Long > cooldown = new HashMap < Player , Long > ( ) ;
// Config values
2016-03-29 13:38:03 +02:00
public static boolean portalsActive = false ;
public static AdvancedPortal [ ] Portals = new AdvancedPortal [ 0 ] ;
private static AdvancedPortalsPlugin plugin ;
public static ConfigAccessor portalData = new ConfigAccessor ( plugin , " portals.yml " ) ;
private static boolean ShowBungeeMessage ;
2016-09-01 21:11:07 +02:00
private static int cooldelay ;
private static double throwback ;
2016-03-29 13:38:03 +02:00
public Portal ( AdvancedPortalsPlugin plugin ) {
ConfigAccessor config = new ConfigAccessor ( plugin , " config.yml " ) ;
ShowBungeeMessage = config . getConfig ( ) . getBoolean ( " ShowBungeeWarpMessage " ) ;
2016-09-01 21:11:07 +02:00
cooldelay = config . getConfig ( ) . getInt ( " PortalCooldown " , 5 ) ;
throwback = config . getConfig ( ) . getDouble ( " ThrowbackAmount " , 0 . 7 ) ;
2016-03-29 13:38:03 +02:00
Portal . plugin = plugin ;
Portal . loadPortals ( ) ;
}
/ * *
* This can be used to move the get keys to different sections
* < p >
* ConfigurationSection section = portalData . getSection ( " sectionname " ) ;
* < p >
* section . getKeys ( false ) ;
* /
public static void loadPortals ( ) {
portalData = new ConfigAccessor ( plugin , " portals.yml " ) ;
Set < String > PortalSet = portalData . getConfig ( ) . getKeys ( false ) ;
if ( PortalSet . size ( ) > 0 ) {
Portals = new AdvancedPortal [ PortalSet . toArray ( ) . length ] ;
2015-11-15 23:52:29 +01:00
2016-01-19 23:31:16 +01:00
/ * for ( int i = 0 ; i < = PortalSet . toArray ( ) . length - 1 ; i + + ) {
2016-03-29 13:38:03 +02:00
Portals [ i ] = new AdvancedPortal ( ) ;
2016-01-19 23:31:16 +01:00
} * /
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
int portalId = 0 ;
for ( Object portal : PortalSet . toArray ( ) ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
ConfigurationSection portalConfigSection = portalData . getConfig ( ) . getConfigurationSection ( portal . toString ( ) ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
Material blockType = Material . PORTAL ;
String BlockID = portalConfigSection . getString ( " triggerblock " ) ;
2016-03-29 01:20:04 +02:00
2016-03-29 13:38:03 +02:00
try {
Integer . parseInt ( BlockID ) ;
System . out . println ( " Block names must be given not IDs " ) ;
} catch ( NumberFormatException e ) {
blockType = Material . getMaterial ( BlockID ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( blockType = = null ) {
blockType = Material . PORTAL ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
ConfigurationSection portalArgsConf = portalConfigSection . getConfigurationSection ( " portalArgs " ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
ArrayList < PortalArg > extraData = new ArrayList < > ( ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
if ( portalArgsConf ! = null ) {
Set < String > argsSet = portalArgsConf . getKeys ( true ) ;
2016-02-29 19:54:24 +01:00
2016-03-29 13:38:03 +02:00
for ( Object argName : argsSet . toArray ( ) ) {
if ( portalArgsConf . isString ( argName . toString ( ) ) ) {
extraData . add ( new PortalArg ( argName . toString ( ) , portalArgsConf . getString ( argName . toString ( ) ) ) ) ;
}
}
}
2016-01-19 23:31:16 +01:00
2016-02-29 19:54:24 +01:00
2016-03-29 13:38:03 +02:00
String worldName = portalData . getConfig ( ) . getString ( portal . toString ( ) + " .world " ) ;
2016-02-29 19:54:24 +01:00
2016-03-29 13:38:03 +02:00
World world = Bukkit . getWorld ( worldName ) ;
Location pos1 = new Location ( world , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos1.X " ) , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos1.Y " ) , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos1.Z " ) ) ;
Location pos2 = new Location ( world , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos2.X " ) , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos2.Y " ) , portalData . getConfig ( ) . getInt ( portal . toString ( ) + " .pos2.Z " ) ) ;
2016-02-29 19:54:24 +01:00
2016-03-29 13:38:03 +02:00
PortalArg [ ] portalArgs = new PortalArg [ extraData . size ( ) ] ;
extraData . toArray ( portalArgs ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
Portals [ portalId ] = new AdvancedPortal ( portal . toString ( ) , blockType , pos1 , pos2 , worldName , portalArgs ) ;
2016-02-29 19:54:24 +01:00
2016-03-29 13:38:03 +02:00
Portals [ portalId ] . bungee = portalConfigSection . getString ( " bungee " ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
Portals [ portalId ] . destiation = portalConfigSection . getString ( " destination " ) ;
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
portalId + + ;
}
portalsActive = true ;
} else {
portalsActive = false ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static String create ( Location pos1 , Location pos2 , String name , String destination , Material triggerBlock , PortalArg . . . extraData ) {
return create ( pos1 , pos2 , name , destination , triggerBlock , null , extraData ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static String create ( Location pos1 , Location pos2 , String name , String destination , Material triggerBlock , String serverName , PortalArg . . . portalArgs ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( ! pos1 . getWorld ( ) . equals ( pos2 . getWorld ( ) ) ) {
plugin . getLogger ( ) . log ( Level . WARNING , " pos1 and pos2 must be in the same world! " ) ;
return " \ u00A7cPortal creation error, pos1 and pos2 must be in the same world! " ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
int LowX = 0 ;
int LowY = 0 ;
int LowZ = 0 ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
int HighX = 0 ;
int HighY = 0 ;
int HighZ = 0 ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( pos1 . getX ( ) > pos2 . getX ( ) ) {
LowX = ( int ) pos2 . getX ( ) ;
HighX = ( int ) pos1 . getX ( ) ;
} else {
LowX = ( int ) pos1 . getX ( ) ;
HighX = ( int ) pos2 . getX ( ) ;
}
if ( pos1 . getY ( ) > pos2 . getY ( ) ) {
LowY = ( int ) pos2 . getY ( ) ;
HighY = ( int ) pos1 . getY ( ) ;
} else {
LowY = ( int ) pos1 . getY ( ) ;
HighY = ( int ) pos2 . getY ( ) ;
}
if ( pos1 . getZ ( ) > pos2 . getZ ( ) ) {
LowZ = ( int ) pos2 . getZ ( ) ;
HighZ = ( int ) pos1 . getZ ( ) ;
} else {
LowZ = ( int ) pos1 . getZ ( ) ;
HighZ = ( int ) pos2 . getZ ( ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
Location checkpos1 = new Location ( pos1 . getWorld ( ) , HighX , HighY , HighZ ) ;
Location checkpos2 = new Location ( pos2 . getWorld ( ) , LowX , LowY , LowZ ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkPortalOverlap ( checkpos1 , checkpos2 ) ) {
plugin . getLogger ( ) . log ( Level . WARNING , " Portals must not overlap! " ) ;
return " \ u00A7cPortal creation error, portals must not overlap! " ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .world " , pos1 . getWorld ( ) . getName ( ) ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .triggerblock " , checkMaterial ( triggerBlock ) ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .destination " , destination ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .bungee " , serverName ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos1.X " , HighX ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Y " , HighY ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Z " , HighZ ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos2.X " , LowX ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Y " , LowY ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Z " , LowZ ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
for ( PortalArg arg : portalArgs ) {
portalData . getConfig ( ) . set ( name + " .portalArgs. " + arg . argName , arg . value ) ;
}
2015-03-25 23:51:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . saveConfig ( ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
loadPortals ( ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
return " \ u00A7aPortal creation successful! " ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
// make this actually work!
private static boolean checkPortalOverlap ( Location pos1 , Location pos2 ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( portalsActive ) {
int portalId = 0 ;
for ( @SuppressWarnings ( " unused " ) Object portal : Portal . Portals ) {
if ( Portals [ portalId ] . worldName . equals ( pos2 . getWorld ( ) . getName ( ) ) ) { // checks that the cubes arnt overlapping by seeing if all 4 corners are not in side another
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos1 . getBlockX ( ) , Portals [ portalId ] . pos1 . getBlockY ( ) , Portals [ portalId ] . pos1 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos1 . getBlockX ( ) , Portals [ portalId ] . pos1 . getBlockY ( ) , Portals [ portalId ] . pos2 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos1 . getBlockX ( ) , Portals [ portalId ] . pos2 . getBlockY ( ) , Portals [ portalId ] . pos1 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos2 . getBlockX ( ) , Portals [ portalId ] . pos1 . getBlockY ( ) , Portals [ portalId ] . pos1 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos2 . getBlockX ( ) , Portals [ portalId ] . pos2 . getBlockY ( ) , Portals [ portalId ] . pos2 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos2 . getBlockX ( ) , Portals [ portalId ] . pos1 . getBlockY ( ) , Portals [ portalId ] . pos2 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos1 . getBlockX ( ) , Portals [ portalId ] . pos2 . getBlockY ( ) , Portals [ portalId ] . pos2 . getBlockZ ( ) ) ) {
return true ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( checkOverLapPortal ( pos1 , pos2 , Portals [ portalId ] . pos2 . getBlockX ( ) , Portals [ portalId ] . pos2 . getBlockY ( ) , Portals [ portalId ] . pos1 . getBlockZ ( ) ) ) {
return true ;
}
}
portalId + + ;
}
}
return false ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
private static boolean checkOverLapPortal ( Location pos1 , Location pos2 , int posX , int posY , int posZ ) {
if ( pos1 . getX ( ) > = posX & & pos1 . getY ( ) > = posX & & pos1 . getZ ( ) > = posZ ) {
if ( ( pos2 . getX ( ) ) < = posX & & pos2 . getY ( ) < = posY & & pos2 . getZ ( ) < = posZ ) {
return true ;
}
}
return false ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
private static String checkMaterial ( Material triggerBlock ) {
if ( triggerBlock . equals ( Material . WATER ) ) {
return " STATIONARY_WATER " ;
} else if ( triggerBlock . equals ( Material . LAVA ) ) {
return " STATIONARY_LAVA " ;
}
return triggerBlock . toString ( ) ;
}
2015-01-15 20:56:13 +01:00
2016-03-29 13:38:03 +02:00
@SuppressWarnings ( " deprecation " )
public static String create ( Location pos1 , Location pos2 , String name , String destination , String serverName , PortalArg . . . extraData ) { // add stuff for destination names or coordinates
ConfigAccessor config = new ConfigAccessor ( plugin , " config.yml " ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
Material triggerBlockType ;
String BlockID = config . getConfig ( ) . getString ( " DefaultPortalTriggerBlock " ) ;
try {
triggerBlockType = Material . getMaterial ( Integer . parseInt ( BlockID ) ) ;
} catch ( Exception e ) {
triggerBlockType = Material . getMaterial ( BlockID ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
if ( triggerBlockType = = null ) {
triggerBlockType = Material . PORTAL ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
// TODO add a for loop which scans through the addArgs and adds them to the portalData so that the application can use them
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
String result = create ( pos1 , pos2 , name , destination , triggerBlockType , serverName , extraData ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
return result ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static void redefine ( Location pos1 , Location pos2 , String name ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos1.X " , pos1 . getX ( ) ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Y " , pos1 . getY ( ) ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Z " , pos1 . getZ ( ) ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos2.X " , pos2 . getX ( ) ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Y " , pos2 . getY ( ) ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Z " , pos2 . getZ ( ) ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . saveConfig ( ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
loadPortals ( ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static String getDestination ( String portalName ) {
return portalData . getConfig ( ) . getString ( portalName + " .destination " ) ;
}
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
public static void remove ( String name ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
Object [ ] keys = portalData . getConfig ( ) . getKeys ( true ) . toArray ( ) ;
for ( int i = keys . length - 1 ; i > = 0 ; i - - ) {
String key = keys [ i ] . toString ( ) ;
if ( key . startsWith ( name + " . " ) ) {
portalData . getConfig ( ) . set ( key , null ) ;
}
}
portalData . getConfig ( ) . set ( name , null ) ;
2015-02-08 14:08:50 +01:00
2016-03-29 13:38:03 +02:00
// TODO add code to check if people have the portal selected and notify if removed.
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
/ * * Set < String > keys = portalData . getConfig ( ) . getKeys ( true ) ;
for ( String key : keys ) {
if ( key . startsWith ( name ) ) {
portalData . getConfig ( ) . set ( key , null ) ;
}
} * /
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
/ * * portalData . getConfig ( ) . set ( name + " .world " , null ) ;
portalData . getConfig ( ) . set ( name + " .triggerblock " , null ) ;
portalData . getConfig ( ) . set ( name + " .destination " , null ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos1.X " , null ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Y " , null ) ;
portalData . getConfig ( ) . set ( name + " .pos1.Z " , null ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos2.X " , null ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Y " , null ) ;
portalData . getConfig ( ) . set ( name + " .pos2.Z " , null ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name + " .pos1 " , null ) ;
portalData . getConfig ( ) . set ( name + " .pos2 " , null ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . getConfig ( ) . set ( name , null ) ; * /
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
portalData . saveConfig ( ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
loadPortals ( ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static boolean portalExists ( String portalName ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
String posX = portalData . getConfig ( ) . getString ( portalName + " .pos1.X " ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
return posX ! = null ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static boolean activate ( Player player , String portalName ) {
for ( AdvancedPortal portal : Portal . Portals ) {
if ( portal . portalName . equals ( portalName ) ) return activate ( player , portal ) ;
}
plugin . getLogger ( ) . log ( Level . SEVERE , " Portal not found by name of: " + portalName ) ;
return false ;
}
2016-01-19 23:31:16 +01:00
2016-03-29 13:38:03 +02:00
public static boolean activate ( Player player , AdvancedPortal portal ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
// add other variables or filter code here, or somehow have a way to register them
2015-11-15 23:52:29 +01:00
2016-03-29 15:15:22 +02:00
// TODO on load and unload recode the permissions to try to register themselves
// https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/plugin/PluginManager.html#addPermission(org.bukkit.permissions.Permission)
// check they havent been registered before too and store a list of ones made by this plugin to remove when portals are unloaded.
// When a portal is added or removed it reloads all portals(i think) so add code for unloading too.
2016-03-29 13:38:03 +02:00
String permission = portal . getArg ( " permission " ) ;
2016-03-29 01:20:04 +02:00
/*if((permission == null || (permission != null && player.hasPermission(permission)) || player.isOp())){*/
2016-03-29 15:15:22 +02:00
// 3 checks, 1st is if it doesnt need perms. 2nd is if it does do they have it. And third is are they op.
if ( ! ( permission = = null | | ( permission ! = null & & player . hasPermission ( permission ) ) | | player . isOp ( ) ) ) {
2016-08-02 05:51:16 +02:00
player . sendMessage ( PluginMessages . customPrefix + " \ u00A7c You do not have permission to use this portal! " ) ;
2016-09-01 21:11:07 +02:00
throwPlayerBack ( player ) ;
2016-03-29 13:38:03 +02:00
return false ;
}
2016-07-28 20:13:52 +02:00
if ( cooldown . get ( player ) ! = null ) {
int diff = ( int ) ( ( System . currentTimeMillis ( ) - cooldown . get ( player ) ) / 1000 ) ;
if ( diff < cooldelay ) {
player . sendMessage ( ChatColor . RED + " Please wait " + ChatColor . YELLOW + ( cooldelay - diff ) + ChatColor . RED + " seconds until attempting to teleport again. " ) ;
2016-09-01 21:11:07 +02:00
throwPlayerBack ( player ) ;
2016-07-28 20:13:52 +02:00
return false ;
}
}
cooldown . put ( player , System . currentTimeMillis ( ) ) ;
2017-04-13 15:41:16 +02:00
boolean showFailMessage = ! portal . hasArg ( " command.1 " ) ;
2016-04-24 18:54:14 +02:00
2017-03-07 01:12:13 +01:00
//plugin.getLogger().info(portal.portalName + ":" + portal.destiation);
boolean warped = false ;
if ( portal . bungee ! = null ) {
if ( ShowBungeeMessage ) {
player . sendMessage ( PluginMessages . customPrefix + " \ u00A7a Attempting to warp to \ u00A7e " + portal . bungee + " \ u00A7a. " ) ;
}
ByteArrayDataOutput out = ByteStreams . newDataOutput ( ) ;
out . writeUTF ( " Connect " ) ;
out . writeUTF ( portal . bungee ) ;
player . sendPluginMessage ( plugin , " BungeeCord " , out . toByteArray ( ) ) ;
// Down to bungee to sort out the teleporting but yea theoretically they should warp.
}
else if ( portal . destiation ! = null ) {
ConfigAccessor configDesti = new ConfigAccessor ( plugin , " destinations.yml " ) ;
if ( configDesti . getConfig ( ) . getString ( portal . destiation + " .world " ) ! = null ) {
warped = Destination . warp ( player , portal . destiation ) ;
2017-03-07 13:06:17 +01:00
if ( ! warped ) {
throwPlayerBack ( player ) ;
}
2017-03-07 01:12:13 +01:00
}
} else {
if ( showFailMessage ) {
player . sendMessage ( PluginMessages . customPrefix + " \ u00A7c The portal you are trying to use doesn't have a destination! " ) ;
plugin . getLogger ( ) . log ( Level . SEVERE , " The portal ' " + portal . portalName + " ' has just had a warp "
+ " attempt and either the data is corrupt or portal doesn't exist! " ) ;
throwPlayerBack ( player ) ;
}
}
2017-04-13 15:41:16 +02:00
if ( ! portal . hasArg ( " command.1 " ) ) {
2016-04-24 18:54:14 +02:00
int commandLine = 1 ;
String command = portal . getArg ( " command. " + commandLine ) ; //portalData.getConfig().getString(portal.portalName+ ".portalArgs.command." + commandLine);
do {
// (?i) makes the search case insensitive
command = command . replaceAll ( " @player " , player . getName ( ) ) ;
plugin . getLogger ( ) . log ( Level . INFO , " Portal command: " + command ) ;
2017-04-20 04:31:25 +02:00
if ( command . startsWith ( " # " ) & & plugin . getSettings ( ) . hasCommandLevel ( " c " ) ) {
2016-04-24 18:54:14 +02:00
command = command . substring ( 1 ) ;
plugin . getLogger ( ) . log ( Level . INFO , " Portal command: " + command ) ;
2016-05-15 11:29:52 +02:00
try {
2016-09-01 21:11:07 +02:00
plugin . getServer ( ) . dispatchCommand ( Bukkit . getConsoleSender ( ) , command ) ;
2016-05-15 11:29:52 +02:00
}
catch ( Exception e ) {
plugin . getLogger ( ) . warning ( " Error while executing: " + command ) ;
}
2017-04-20 04:31:25 +02:00
} else if ( command . startsWith ( " ! " ) & & plugin . getSettings ( ) . hasCommandLevel ( " o " ) ) {
2016-04-24 18:54:14 +02:00
command = command . substring ( 1 ) ;
boolean wasOp = player . isOp ( ) ;
try {
player . setOp ( true ) ;
player . performCommand ( command ) ;
} finally {
player . setOp ( wasOp ) ;
}
} else if ( command . startsWith ( " ^ " ) ) {
command = command . substring ( 1 ) ;
PermissionAttachment permissionAttachment = null ;
try {
permissionAttachment = player . addAttachment ( plugin , " * " , true ) ;
player . performCommand ( command ) ;
} finally {
player . removeAttachment ( permissionAttachment ) ;
}
} else {
player . performCommand ( command ) ;
}
command = portal . getArg ( " command. " + + + commandLine ) ;
} while ( command ! = null ) ;
}
2017-03-07 01:12:13 +01:00
return warped ;
2016-03-29 13:38:03 +02:00
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
public static void rename ( String oldName , String newName ) {
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
// set it so it gets all data from one and puts it into another place
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
ConfigAccessor config = new ConfigAccessor ( plugin , " portals.yml " ) ;
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
Set < String > keys = config . getConfig ( ) . getKeys ( true ) ;
for ( String key : keys ) {
if ( key . startsWith ( oldName + " . " ) ) {
if ( config . getConfig ( ) . getString ( key ) ! = null ) {
try {
int intData = Integer . parseInt ( config . getConfig ( ) . getString ( key ) ) ;
config . getConfig ( ) . set ( key . replace ( oldName + " . " , newName + " . " ) , intData ) ;
} catch ( Exception e ) {
config . getConfig ( ) . set ( key . replace ( oldName + " . " , newName + " . " ) , config . getConfig ( ) . getString ( key ) ) ;
}
2015-11-15 23:52:29 +01:00
2016-03-29 13:38:03 +02:00
}
}
}
config . saveConfig ( ) ;
remove ( oldName ) ;
}
public static boolean addCommand ( String portalName , String portalCommand ) {
ConfigAccessor config = new ConfigAccessor ( plugin , " portals.yml " ) ;
if ( portalExists ( portalName ) ) {
int commandLine = 0 ;
while ( config . getConfig ( ) . getString ( portalName + " .portalArgs.command. " + + + commandLine ) ! = null )
; //Loops increasing commandLine till 1 is null
config . getConfig ( ) . set ( portalName + " .portalArgs.command. " + commandLine , portalCommand ) ;
config . saveConfig ( ) ;
loadPortals ( ) ;
return true ;
} else {
return false ;
}
}
2016-07-31 18:26:04 +02:00
2016-08-01 08:45:24 +02:00
public static boolean inPortalTriggerRegion ( Location loc ) {
for ( AdvancedPortal portal : Portal . Portals )
if ( Portal . locationInPortalTrigger ( portal , loc ) )
return true ;
return false ;
}
public static boolean locationInPortalTrigger ( AdvancedPortal portal , Location loc ) {
if ( portal . trigger . equals ( loc . getBlock ( ) . getType ( ) ) )
return locationInPortal ( portal , loc , 0 ) ;
return false ;
}
public static boolean inPortalRegion ( Location loc , int additionalArea ) {
for ( AdvancedPortal portal : Portal . Portals )
if ( Portal . locationInPortal ( portal , loc , additionalArea ) )
return true ;
return false ;
}
public static boolean locationInPortal ( AdvancedPortal portal , Location loc , int additionalArea ) {
if ( ! portalsActive )
return false ;
if ( loc . getWorld ( ) ! = null & & portal . worldName . equals ( loc . getWorld ( ) . getName ( ) ) )
2016-08-01 08:59:56 +02:00
if ( ( portal . pos1 . getX ( ) + 1 + additionalArea ) > = loc . getX ( ) & & ( portal . pos1 . getY ( ) + 1 + additionalArea ) > loc . getY ( ) & & ( portal . pos1 . getZ ( ) + 1 + additionalArea ) > = loc . getZ ( ) )
2016-08-01 08:45:24 +02:00
if ( portal . pos2 . getX ( ) - additionalArea < = loc . getX ( ) & & portal . pos2 . getY ( ) - additionalArea < = loc . getY ( ) & & portal . pos2 . getZ ( ) - additionalArea < = loc . getZ ( ) )
return true ;
return false ;
}
2016-08-04 03:43:58 +02:00
2016-09-01 21:11:07 +02:00
public static void throwPlayerBack ( Player player ) {
// Not ensured to remove them out of the portal but it makes it feel nicer for the player.
if ( throwback > 0 ) {
Vector velocity = player . getLocation ( ) . getDirection ( ) ;
player . setVelocity ( velocity . setY ( 0 ) . normalize ( ) . multiply ( - 1 ) . setY ( throwback ) ) ;
2016-07-31 18:26:04 +02:00
}
}
2015-01-15 20:56:13 +01:00
}