2011-09-17 20:59:37 +02:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Multiverse 2 Copyright ( c ) the Multiverse Team 2011 . *
* Multiverse 2 is licensed under the BSD License . *
* For more information please check the README . md file included *
* with this project . *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2011-10-08 02:26:40 +02:00
package com.onarandombox.MultiverseCore.utils ;
2011-09-17 17:59:38 +02:00
2020-12-12 03:49:49 +01:00
import com.dumptruckman.minecraft.util.Logging ;
2011-09-17 17:59:38 +02:00
import com.onarandombox.MultiverseCore.MultiverseCore ;
2011-11-27 03:38:57 +01:00
import com.onarandombox.MultiverseCore.api.MultiverseWorld ;
2019-01-24 05:50:47 +01:00
import org.bukkit.Material ;
2011-12-22 06:20:58 +01:00
import org.bukkit.command.CommandSender ;
import org.bukkit.command.ConsoleCommandSender ;
2011-11-27 03:38:57 +01:00
import org.bukkit.entity.Player ;
2011-09-17 17:59:38 +02:00
import org.bukkit.permissions.Permission ;
2012-07-03 22:25:56 +02:00
import java.util.logging.Level ;
2012-01-03 16:26:26 +01:00
/ * *
* Utility - class for permissions .
* /
2011-09-17 17:59:38 +02:00
public class PermissionTools {
private MultiverseCore plugin ;
public PermissionTools ( MultiverseCore plugin ) {
this . plugin = plugin ;
}
2011-10-10 17:34:04 +02:00
2012-01-03 16:26:26 +01:00
/ * *
* Adds a permission to the parent - permissions .
* @param permString The new permission as { @link String } .
* /
2011-09-17 17:59:38 +02:00
public void addToParentPerms ( String permString ) {
String permStringChopped = permString . replace ( " .* " , " " ) ;
String [ ] seperated = permStringChopped . split ( " \\ . " ) ;
String parentPermString = getParentPerm ( seperated ) ;
if ( parentPermString = = null ) {
addToRootPermission ( " * " , permStringChopped ) ;
addToRootPermission ( " *.* " , permStringChopped ) ;
return ;
}
Permission parentPermission = this . plugin . getServer ( ) . getPluginManager ( ) . getPermission ( parentPermString ) ;
// Creat parent and grandparents
if ( parentPermission = = null ) {
parentPermission = new Permission ( parentPermString ) ;
this . plugin . getServer ( ) . getPluginManager ( ) . addPermission ( parentPermission ) ;
this . addToParentPerms ( parentPermString ) ;
}
// Create actual perm.
Permission actualPermission = this . plugin . getServer ( ) . getPluginManager ( ) . getPermission ( permString ) ;
// Extra check just to make sure the actual one is added
if ( actualPermission = = null ) {
actualPermission = new Permission ( permString ) ;
this . plugin . getServer ( ) . getPluginManager ( ) . addPermission ( actualPermission ) ;
}
if ( ! parentPermission . getChildren ( ) . containsKey ( permString ) ) {
parentPermission . getChildren ( ) . put ( actualPermission . getName ( ) , true ) ;
this . plugin . getServer ( ) . getPluginManager ( ) . recalculatePermissionDefaults ( parentPermission ) ;
}
}
private void addToRootPermission ( String rootPerm , String permStringChopped ) {
Permission rootPermission = this . plugin . getServer ( ) . getPluginManager ( ) . getPermission ( rootPerm ) ;
if ( rootPermission = = null ) {
rootPermission = new Permission ( rootPerm ) ;
this . plugin . getServer ( ) . getPluginManager ( ) . addPermission ( rootPermission ) ;
}
rootPermission . getChildren ( ) . put ( permStringChopped + " .* " , true ) ;
this . plugin . getServer ( ) . getPluginManager ( ) . recalculatePermissionDefaults ( rootPermission ) ;
}
/ * *
* If the given permission was ' multiverse . core . tp . self ' , this would return ' multiverse . core . tp . * ' .
*
* @param separatedPermissionString The array of a dot separated perm string .
* @return The dot separated parent permission string .
* /
2012-03-03 17:07:07 +01:00
private static String getParentPerm ( String [ ] separatedPermissionString ) {
2011-09-17 17:59:38 +02:00
if ( separatedPermissionString . length = = 1 ) {
return null ;
}
String returnString = " " ;
for ( int i = 0 ; i < separatedPermissionString . length - 1 ; i + + ) {
returnString + = separatedPermissionString [ i ] + " . " ;
}
return returnString + " * " ;
}
2011-11-27 03:38:57 +01:00
2012-01-03 16:26:26 +01:00
/ * *
* Checks if the given { @link Player } has enough money to enter the specified { @link MultiverseWorld } .
* @param fromWorld The { @link MultiverseWorld } the player is coming from .
* @param toWorld The { @link MultiverseWorld } the player is going to .
* @param teleporter The teleporter .
* @param teleportee The teleportee .
* @param pay If the player has to pay the money .
* @return True if the player can enter the world .
* /
2011-12-22 06:20:58 +01:00
public boolean playerHasMoneyToEnter ( MultiverseWorld fromWorld , MultiverseWorld toWorld , CommandSender teleporter , Player teleportee , boolean pay ) {
2011-12-22 06:34:35 +01:00
Player teleporterPlayer ;
2012-02-04 14:22:55 +01:00
if ( plugin . getMVConfig ( ) . getTeleportIntercept ( ) ) {
2011-12-22 06:34:35 +01:00
if ( teleporter instanceof ConsoleCommandSender ) {
return true ;
}
2011-12-22 06:20:58 +01:00
2011-12-22 16:50:06 +01:00
if ( teleporter = = null ) {
2011-12-22 06:34:35 +01:00
teleporter = teleportee ;
}
2011-12-22 06:20:58 +01:00
2011-12-22 06:34:35 +01:00
if ( ! ( teleporter instanceof Player ) ) {
return false ;
}
teleporterPlayer = ( Player ) teleporter ;
} else {
if ( teleporter instanceof Player ) {
teleporterPlayer = ( Player ) teleporter ;
} else {
teleporterPlayer = null ;
}
// Old-style!
if ( teleporterPlayer = = null ) {
return true ;
}
2011-12-22 06:20:58 +01:00
}
2012-01-06 06:16:34 +01:00
// If the toWorld isn't controlled by MV,
// We don't care.
2012-01-23 05:44:43 +01:00
if ( toWorld = = null ) {
2012-01-06 06:16:34 +01:00
return true ;
}
2011-11-27 03:38:57 +01:00
// Only check payments if it's a different world:
if ( ! toWorld . equals ( fromWorld ) ) {
2015-07-14 21:00:33 +02:00
final double price = toWorld . getPrice ( ) ;
2012-07-03 22:25:56 +02:00
// Don't bother checking economy stuff if it doesn't even cost to enter.
2015-07-14 21:00:33 +02:00
if ( price = = 0D ) {
2012-07-03 22:25:56 +02:00
return true ;
}
2011-11-27 03:38:57 +01:00
// If the player does not have to pay, return now.
if ( this . plugin . getMVPerms ( ) . hasPermission ( teleporter , toWorld . getExemptPermission ( ) . getName ( ) , true ) ) {
return true ;
}
2015-07-14 21:00:33 +02:00
final MVEconomist economist = plugin . getEconomist ( ) ;
2019-01-24 05:50:47 +01:00
final Material currency = toWorld . getCurrency ( ) ;
2015-07-14 21:00:33 +02:00
final String formattedAmount = economist . formatPrice ( price , currency ) ;
if ( economist . isPlayerWealthyEnough ( teleporterPlayer , price , currency ) ) {
if ( pay ) {
if ( price < 0D ) {
economist . deposit ( teleporterPlayer , - price , currency ) ;
2012-10-21 02:49:15 +02:00
} else {
2015-07-14 21:00:33 +02:00
economist . withdraw ( teleporterPlayer , price , currency ) ;
2012-10-21 02:49:15 +02:00
}
2015-07-14 21:47:20 +02:00
sendTeleportPaymentMessage ( economist , teleporterPlayer , teleportee , toWorld . getColoredWorldString ( ) , price , currency ) ;
2012-10-21 02:43:58 +02:00
}
} else {
2015-07-14 21:00:33 +02:00
if ( teleportee . equals ( teleporter ) ) {
teleporterPlayer . sendMessage ( economist . getNSFMessage ( currency ,
" You need " + formattedAmount + " to enter " + toWorld . getColoredWorldString ( ) ) ) ;
} else {
teleporterPlayer . sendMessage ( economist . getNSFMessage ( currency ,
2015-07-14 21:47:20 +02:00
" You need " + formattedAmount + " to send " + teleportee . getName ( ) + " to " + toWorld . getColoredWorldString ( ) ) ) ;
2012-10-21 02:43:58 +02:00
}
2015-07-14 21:00:33 +02:00
return false ;
2011-11-27 03:38:57 +01:00
}
}
return true ;
}
2019-01-24 05:50:47 +01:00
private void sendTeleportPaymentMessage ( MVEconomist economist , Player teleporterPlayer , Player teleportee , String toWorld , double price , Material currency ) {
2015-07-14 21:47:20 +02:00
price = Math . abs ( price ) ;
if ( teleporterPlayer . equals ( teleportee ) ) {
teleporterPlayer . sendMessage ( " You were " + ( price > 0D ? " charged " : " given " ) + economist . formatPrice ( price , currency ) + " for teleporting to " + toWorld ) ;
} else {
teleporterPlayer . sendMessage ( " You were " + ( price > 0D ? " charged " : " given " ) + economist . formatPrice ( price , currency ) + " for teleporting " + teleportee . getName ( ) + " to " + toWorld ) ;
}
}
2011-11-27 03:38:57 +01:00
/ * *
* Checks to see if player can go to a world given their current status .
2011-12-10 11:21:26 +01:00
* < p >
2011-11-27 03:38:57 +01:00
* The return is a little backwards , and will return a value safe for event . setCancelled .
*
* @param fromWorld The MultiverseWorld they are in .
* @param toWorld The MultiverseWorld they want to go to .
2011-12-22 06:20:58 +01:00
* @param teleporter The CommandSender that wants to send someone somewhere . If null ,
* will be given the same value as teleportee .
* @param teleportee The player going somewhere .
2011-11-27 03:38:57 +01:00
* @return True if they can ' t go to the world , False if they can .
* /
2011-12-22 06:20:58 +01:00
public boolean playerCanGoFromTo ( MultiverseWorld fromWorld , MultiverseWorld toWorld , CommandSender teleporter , Player teleportee ) {
2020-12-12 03:49:49 +01:00
Logging . finest ( " Checking ' " + teleporter + " ' can send ' " + teleportee + " ' somewhere " ) ;
2011-12-22 06:20:58 +01:00
2011-12-22 06:34:35 +01:00
Player teleporterPlayer ;
2012-02-04 14:22:55 +01:00
if ( plugin . getMVConfig ( ) . getTeleportIntercept ( ) ) {
2011-12-22 06:34:35 +01:00
// The console can send anyone anywhere
if ( teleporter instanceof ConsoleCommandSender ) {
return true ;
}
2011-12-22 06:20:58 +01:00
2011-12-22 06:34:35 +01:00
// Make sure we have a teleporter of some kind, even if it's inferred to be the teleportee
if ( teleporter = = null ) {
teleporter = teleportee ;
}
2011-12-22 06:20:58 +01:00
2011-12-22 06:34:35 +01:00
// Now make sure we can cast the teleporter to a player, 'cause I'm tired of console things now
if ( ! ( teleporter instanceof Player ) ) {
return false ;
}
teleporterPlayer = ( Player ) teleporter ;
} else {
if ( teleporter instanceof Player ) {
teleporterPlayer = ( Player ) teleporter ;
} else {
teleporterPlayer = null ;
}
// Old-style!
if ( teleporterPlayer = = null ) {
return true ;
}
2011-12-22 06:20:58 +01:00
}
// Actual checks
2011-11-27 03:38:57 +01:00
if ( toWorld ! = null ) {
2011-12-22 06:20:58 +01:00
if ( ! this . plugin . getMVPerms ( ) . canEnterWorld ( teleporterPlayer , toWorld ) ) {
2011-11-27 03:38:57 +01:00
if ( teleportee . equals ( teleporter ) ) {
teleporter . sendMessage ( " You don't have access to go here... " ) ;
} else {
teleporter . sendMessage ( " You can't send " + teleportee . getName ( ) + " here... " ) ;
}
return false ;
}
} else {
2012-01-03 16:26:26 +01:00
// TODO: Determine if this value is false because a world didn't exist
2011-11-27 03:38:57 +01:00
// or if it was because a world wasn't imported.
return true ;
}
if ( fromWorld ! = null ) {
if ( fromWorld . getWorldBlacklist ( ) . contains ( toWorld . getName ( ) ) ) {
if ( teleportee . equals ( teleporter ) ) {
teleporter . sendMessage ( " You don't have access to go to " + toWorld . getColoredWorldString ( ) + " from " + fromWorld . getColoredWorldString ( ) ) ;
} else {
2012-01-03 16:26:26 +01:00
teleporter . sendMessage ( " You don't have access to send " + teleportee . getName ( ) + " from "
+ fromWorld . getColoredWorldString ( ) + " to " + toWorld . getColoredWorldString ( ) ) ;
2011-11-27 03:38:57 +01:00
}
return false ;
}
}
return true ;
}
2012-11-05 16:40:06 +01:00
/ * *
* Checks to see if a player can bypass the player limit .
*
* @param toWorld The world travelling to .
* @param teleporter The player that initiated the teleport .
* @param teleportee The player travelling .
* @return True if they can bypass the player limit .
* /
2012-10-25 00:43:15 +02:00
public boolean playerCanBypassPlayerLimit ( MultiverseWorld toWorld , CommandSender teleporter , Player teleportee ) {
if ( teleporter = = null ) {
teleporter = teleportee ;
}
2012-11-05 16:40:06 +01:00
2012-10-25 00:43:15 +02:00
if ( ! ( teleporter instanceof Player ) ) {
2012-11-05 16:40:06 +01:00
return true ;
2012-10-25 00:43:15 +02:00
}
2012-11-05 16:40:06 +01:00
2012-10-25 00:43:15 +02:00
MVPermissions perms = plugin . getMVPerms ( ) ;
if ( perms . hasPermission ( teleportee , " mv.bypass.playerlimit. " + toWorld . getName ( ) , false ) ) {
return true ;
} else {
teleporter . sendMessage ( " The world " + toWorld . getColoredWorldString ( ) + " is full " ) ;
return false ;
}
}
2012-01-03 16:26:26 +01:00
2011-12-20 01:20:10 +01:00
/ * *
* Checks to see if a player should bypass game mode restrictions .
2012-01-03 16:26:26 +01:00
*
2011-12-20 01:20:10 +01:00
* @param toWorld world travelling to .
* @param teleportee player travelling .
* @return True if they should bypass restrictions
* /
public boolean playerCanIgnoreGameModeRestriction ( MultiverseWorld toWorld , Player teleportee ) {
if ( toWorld ! = null ) {
return this . plugin . getMVPerms ( ) . canIgnoreGameModeRestriction ( teleportee , toWorld ) ;
} else {
2012-01-03 16:26:26 +01:00
// TODO: Determine if this value is false because a world didn't exist
2011-12-20 01:20:10 +01:00
// or if it was because a world wasn't imported.
return true ;
}
}
2021-08-03 14:01:47 +02:00
/ * *
* Checks to see if a player should bypass fly restrictions .
*
* @param toWorld world travelling to .
* @param teleportee player travelling .
* @return True if they should bypass restrictions
* /
public boolean playerCanIgnoreFlyRestriction ( MultiverseWorld toWorld , Player teleportee ) {
if ( toWorld ! = null ) {
return this . plugin . getMVPerms ( ) . canIgnoreFlyRestriction ( teleportee , toWorld ) ;
} else {
// TODO: Determine if this value is false because a world didn't exist
// or if it was because a world wasn't imported.
return true ;
}
}
2011-09-17 17:59:38 +02:00
}