2016-06-07 22:39:45 +02:00
package me.wiefferink.areashop ;
import com.sk89q.worldedit.bukkit.WorldEditPlugin ;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin ;
2018-08-05 00:34:50 +02:00
import com.sk89q.worldguard.protection.managers.RegionManager ;
2016-06-07 22:39:45 +02:00
import me.wiefferink.areashop.interfaces.AreaShopInterface ;
import me.wiefferink.areashop.interfaces.WorldEditInterface ;
import me.wiefferink.areashop.interfaces.WorldGuardInterface ;
import me.wiefferink.areashop.listeners.PlayerLoginLogoutListener ;
2017-03-29 20:15:44 +02:00
import me.wiefferink.areashop.managers.CommandManager ;
import me.wiefferink.areashop.managers.FeatureManager ;
import me.wiefferink.areashop.managers.FileManager ;
import me.wiefferink.areashop.managers.Manager ;
import me.wiefferink.areashop.managers.SignLinkerManager ;
2017-03-09 15:09:05 +01:00
import me.wiefferink.areashop.tools.Analytics ;
2017-06-12 19:56:49 +02:00
import me.wiefferink.areashop.tools.GithubUpdateCheck ;
2016-12-23 16:03:22 +01:00
import me.wiefferink.areashop.tools.Utils ;
2017-11-26 15:47:44 +01:00
import me.wiefferink.bukkitdo.Do ;
2017-02-02 22:59:00 +01:00
import me.wiefferink.interactivemessenger.processing.Message ;
import me.wiefferink.interactivemessenger.source.LanguageManager ;
2016-06-07 22:39:45 +02:00
import net.milkbowl.vault.economy.Economy ;
2016-07-23 02:57:31 +02:00
import org.apache.commons.lang.StringUtils ;
2016-08-01 16:18:05 +02:00
import org.apache.commons.lang.exception.ExceptionUtils ;
2016-06-07 22:39:45 +02:00
import org.bukkit.Bukkit ;
2017-10-20 13:40:53 +02:00
import org.bukkit.OfflinePlayer ;
2018-08-05 00:34:50 +02:00
import org.bukkit.World ;
2016-06-07 22:39:45 +02:00
import org.bukkit.command.CommandSender ;
import org.bukkit.configuration.ConfigurationSection ;
import org.bukkit.configuration.file.YamlConfiguration ;
import org.bukkit.entity.Player ;
2017-03-09 16:01:36 +01:00
import org.bukkit.event.HandlerList ;
2016-06-07 22:39:45 +02:00
import org.bukkit.permissions.Permission ;
import org.bukkit.plugin.Plugin ;
import org.bukkit.plugin.RegisteredServiceProvider ;
import org.bukkit.plugin.java.JavaPlugin ;
2019-01-08 00:16:53 +01:00
import java.util.Arrays ;
2016-12-25 23:02:23 +01:00
import java.util.HashSet ;
import java.util.List ;
import java.util.Set ;
2016-06-21 22:42:56 +02:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Main class for the AreaShop plugin .
* Contains methods to get parts of the plugins functionality and definitions for constants .
2016-06-07 22:39:45 +02:00
* /
public final class AreaShop extends JavaPlugin implements AreaShopInterface {
2016-06-18 23:23:22 +02:00
// Statically available instance
2016-06-07 22:39:45 +02:00
private static AreaShop instance = null ;
2016-06-18 23:23:22 +02:00
// General variables
2016-06-07 22:39:45 +02:00
private WorldGuardPlugin worldGuard = null ;
private WorldGuardInterface worldGuardInterface = null ;
private WorldEditPlugin worldEdit = null ;
private WorldEditInterface worldEditInterface = null ;
private FileManager fileManager = null ;
private LanguageManager languageManager = null ;
private CommandManager commandManager = null ;
private SignLinkerManager signLinkerManager = null ;
2016-12-23 16:03:22 +01:00
private FeatureManager featureManager = null ;
private Set < Manager > managers = null ;
2016-06-07 22:39:45 +02:00
private boolean debug = false ;
private List < String > chatprefix = null ;
private boolean ready = false ;
2017-06-12 19:56:49 +02:00
private GithubUpdateCheck githubUpdateCheck = null ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Folders and file names
public static final String languageFolder = " lang " ;
public static final String schematicFolder = " schem " ;
public static final String regionsFolder = " regions " ;
public static final String groupsFile = " groups.yml " ;
2016-06-13 17:03:20 +02:00
public static final String defaultFile = " default.yml " ;
public static final String configFile = " config.yml " ;
public static final String configFileHidden = " hiddenConfig.yml " ;
2016-06-07 22:39:45 +02:00
public static final String versionFile = " versions " ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Euro tag for in the config
public static final String currencyEuro = " %euro% " ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Constants for handling file versions
public static final String versionFiles = " files " ;
public static final int versionFilesCurrent = 3 ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Keys for replacing parts of flags, commands, strings
2016-10-25 20:57:58 +02:00
public static final String tagPlayerName = " player " ;
public static final String tagPlayerUUID = " uuid " ;
public static final String tagWorldName = " world " ;
public static final String tagRegionName = " region " ;
public static final String tagRegionType = " type " ;
public static final String tagPrice = " price " ;
public static final String tagRawPrice = " rawprice " ;
public static final String tagDuration = " duration " ;
public static final String tagRentedUntil = " until " ;
public static final String tagRentedUntilShort = " untilshort " ;
public static final String tagWidth = " width " ; // x-axis
public static final String tagHeight = " height " ; // y-axis
public static final String tagDepth = " depth " ; // z-axis
2017-10-05 12:20:03 +02:00
public static final String tagVolume = " volume " ; // Number of blocks in the region (accounting for polygon regions)
2016-10-25 20:57:58 +02:00
public static final String tagTimeLeft = " timeleft " ;
public static final String tagClicker = " clicker " ;
public static final String tagResellPrice = " resellprice " ;
public static final String tagRawResellPrice = " rawresellprice " ;
public static final String tagFriends = " friends " ;
public static final String tagFriendsUUID = " friendsuuid " ;
public static final String tagMoneyBackPercentage = " moneybackpercent " ;
public static final String tagMoneyBackAmount = " moneyback " ;
public static final String tagRawMoneyBackAmount = " rawmoneyback " ;
2017-01-13 16:56:02 +01:00
public static final String tagTimesExtended = " timesExtended " ;
2016-10-25 20:57:58 +02:00
public static final String tagMaxExtends = " maxextends " ;
public static final String tagExtendsLeft = " extendsleft " ;
public static final String tagMaxRentTime = " maxrenttime " ;
public static final String tagMaxInactiveTime = " inactivetime " ;
public static final String tagLandlord = " landlord " ;
public static final String tagLandlordUUID = " landlorduuid " ;
public static final String tagDateTime = " datetime " ;
public static final String tagDateTimeShort = " datetimeshort " ;
public static final String tagYear = " year " ;
public static final String tagMonth = " month " ;
public static final String tagDay = " day " ;
public static final String tagHour = " hour " ;
public static final String tagMinute = " minute " ;
public static final String tagSecond = " second " ;
public static final String tagMillisecond = " millisecond " ;
public static final String tagEpoch = " epoch " ;
public static final String tagTeleportX = " tpx " ;
public static final String tagTeleportY = " tpy " ;
public static final String tagTeleportZ = " tpz " ;
public static final String tagTeleportBlockX = " tpblockx " ;
public static final String tagTeleportBlockY = " tpblocky " ;
public static final String tagTeleportBlockZ = " tpblockz " ;
public static final String tagTeleportPitch = " tppitch " ;
public static final String tagTeleportYaw = " tpyaw " ;
public static final String tagTeleportPitchRound = " tppitchround " ;
public static final String tagTeleportYawRound = " tpyawround " ;
public static final String tagTeleportWorld = " tpworld " ;
2016-06-07 22:39:45 +02:00
public static AreaShop getInstance ( ) {
return AreaShop . instance ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Called on start or reload of the server .
2016-06-07 22:39:45 +02:00
* /
public void onEnable ( ) {
AreaShop . instance = this ;
2017-11-27 10:02:51 +01:00
Do . init ( this ) ;
2016-12-23 16:03:22 +01:00
managers = new HashSet < > ( ) ;
2016-06-07 22:39:45 +02:00
boolean error = false ;
2017-03-29 20:15:44 +02:00
2019-01-08 00:16:53 +01:00
// Find WorldEdit integration version to load
2018-12-27 17:42:24 +01:00
String weVersion = null ;
String rawWeVersion = null ;
String weBeta = null ;
Plugin plugin = getServer ( ) . getPluginManager ( ) . getPlugin ( " WorldEdit " ) ;
if ( ! ( plugin instanceof WorldEditPlugin ) | | ! plugin . isEnabled ( ) ) {
error ( " WorldEdit plugin is not present or has not loaded correctly " ) ;
error = true ;
} else {
worldEdit = ( WorldEditPlugin ) plugin ;
rawWeVersion = worldEdit . getDescription ( ) . getVersion ( ) ;
// Find beta version
Pattern pattern = Pattern . compile ( " beta-? \\ d+ " ) ;
Matcher matcher = pattern . matcher ( rawWeVersion ) ;
if ( matcher . find ( ) ) {
weBeta = matcher . group ( ) ;
}
// Get correct WorldEditInterface (handles things that changed version to version)
if ( worldEdit . getDescription ( ) . getVersion ( ) . startsWith ( " 5. " ) ) {
weVersion = " 5 " ;
} else if ( worldEdit . getDescription ( ) . getVersion ( ) . startsWith ( " 6. " ) ) {
weVersion = " 6 " ;
} else if ( " beta-01 " . equalsIgnoreCase ( weBeta ) ) {
weVersion = " 7_beta_1 " ;
} else {
// beta-02 and beta-03 also have the new vector system already
weVersion = " 7_beta_4 " ;
}
2019-01-08 00:16:53 +01:00
weVersion = " WorldEditHandler " + weVersion ;
2018-12-27 17:42:24 +01:00
}
2019-01-08 00:16:53 +01:00
// Find WorldGuard integration version to load
2016-06-07 22:39:45 +02:00
String wgVersion = null ;
2018-12-27 17:42:24 +01:00
String rawWgVersion = null ;
2017-03-29 20:15:44 +02:00
int major = 0 ;
int minor = 0 ;
int fixes = 0 ;
2016-06-21 22:42:56 +02:00
Integer build = null ;
2018-12-27 17:42:24 +01:00
plugin = getServer ( ) . getPluginManager ( ) . getPlugin ( " WorldGuard " ) ;
2018-05-23 23:46:57 +02:00
if ( ! ( plugin instanceof WorldGuardPlugin ) | | ! plugin . isEnabled ( ) ) {
2016-08-08 22:06:31 +02:00
error ( " WorldGuard plugin is not present or has not loaded correctly " ) ;
2016-06-18 23:23:22 +02:00
error = true ;
2017-03-29 20:15:44 +02:00
} else {
worldGuard = ( WorldGuardPlugin ) plugin ;
// Get correct WorldGuardInterface (handles things that changed version to version)
2016-06-21 22:42:56 +02:00
try {
2018-12-27 17:42:24 +01:00
rawWgVersion = worldGuard . getDescription ( ) . getVersion ( ) ;
if ( rawWgVersion . contains ( " -SNAPSHOT; " ) ) {
String buildNumber = rawWgVersion . substring ( rawWgVersion . indexOf ( " -SNAPSHOT; " ) + 10 ) ;
2016-08-09 21:52:53 +02:00
if ( buildNumber . contains ( " - " ) ) {
buildNumber = buildNumber . substring ( 0 , buildNumber . indexOf ( " - " ) ) ;
2016-08-09 22:38:33 +02:00
try {
build = Integer . parseInt ( buildNumber ) ;
} catch ( NumberFormatException e ) {
2018-12-27 17:42:24 +01:00
warn ( " Could not correctly parse the build of WorldGuard, raw version: " + rawWgVersion + " , buildNumber: " + buildNumber ) ;
2016-08-09 22:38:33 +02:00
}
2016-08-09 21:52:53 +02:00
}
2016-06-21 22:42:56 +02:00
}
2016-08-09 21:52:53 +02:00
// Clear stuff from the version string that is not a number
2018-12-27 17:42:24 +01:00
String [ ] versionParts = rawWgVersion . split ( " \\ . " ) ;
2016-08-09 21:52:53 +02:00
for ( int i = 0 ; i < versionParts . length ; i + + ) {
Pattern pattern = Pattern . compile ( " ^ \\ d+ " ) ;
Matcher matcher = pattern . matcher ( versionParts [ i ] ) ;
if ( matcher . find ( ) ) {
versionParts [ i ] = matcher . group ( ) ;
}
2016-06-21 22:42:56 +02:00
}
2016-08-09 21:52:53 +02:00
// Find major, minor and fix numbers
try {
if ( versionParts . length > 0 ) {
major = Integer . parseInt ( versionParts [ 0 ] ) ;
}
if ( versionParts . length > 1 ) {
minor = Integer . parseInt ( versionParts [ 1 ] ) ;
}
if ( versionParts . length > 2 ) {
fixes = Integer . parseInt ( versionParts [ 2 ] ) ;
}
} catch ( NumberFormatException e ) {
2018-12-27 17:42:24 +01:00
warn ( " Something went wrong while parsing WorldGuard version number: " + rawWgVersion ) ;
2016-06-21 22:42:56 +02:00
}
2018-12-27 17:42:24 +01:00
2016-08-09 21:52:53 +02:00
// Determine correct implementation to use
2018-12-27 17:42:24 +01:00
if ( rawWgVersion . startsWith ( " 5. " ) ) {
2016-08-09 22:56:21 +02:00
wgVersion = " 5 " ;
} else if ( major = = 6 & & minor = = 1 & & fixes < 3 ) {
wgVersion = " 6 " ;
2018-08-05 00:34:50 +02:00
} else if ( major = = 6 ) {
2016-08-09 22:56:21 +02:00
if ( build ! = null & & build = = 1672 ) {
2016-08-09 21:52:53 +02:00
error = true ;
error ( " Build 1672 of WorldGuard is broken, update to a later build or a stable version! " ) ;
2016-08-09 22:56:21 +02:00
} else if ( build ! = null & & build < 1672 ) {
wgVersion = " 6 " ;
} else {
wgVersion = " 6_1_3 " ;
2016-08-09 21:52:53 +02:00
}
2018-12-27 17:42:24 +01:00
} else if ( " beta-01 " . equalsIgnoreCase ( weBeta ) ) {
// When using WorldEdit beta-01, we need to use the WorldGuard variant with the old vector system
wgVersion = " 7_beta_1 " ;
2018-08-05 00:34:50 +02:00
} else {
2018-12-27 17:42:24 +01:00
// Even though the WorldGuard file is called beta-02, the reported version is still beta-01!
wgVersion = " 7_beta_2 " ;
2016-06-18 23:23:22 +02:00
}
2016-08-09 21:52:53 +02:00
} catch ( Exception e ) { // If version detection fails, at least try to load the latest version
2018-12-27 17:42:24 +01:00
warn ( " Parsing the WorldGuard version failed, assuming version 7_beta_2: " , rawWgVersion ) ;
wgVersion = " 7_beta_2 " ;
2016-06-18 23:23:22 +02:00
}
2019-01-08 00:16:53 +01:00
wgVersion = " WorldGuardHandler " + wgVersion ;
}
// Check if FastAsyncWorldEdit is installed
boolean fawe ;
try {
Class . forName ( " com.boydti.fawe.Fawe " ) ;
fawe = true ;
} catch ( ClassNotFoundException ignore ) {
fawe = false ;
}
if ( fawe ) {
boolean useNewIntegration = true ;
List < String > standardIntegrationVersions = Arrays . asList ( " 1.7 " , " 1.8 " , " 1.9 " , " 1.10 " , " 1.11 " , " 1.12 " ) ;
for ( String standardIntegrationVersion : standardIntegrationVersions ) {
String version = Bukkit . getBukkitVersion ( ) ;
// Detects '1.8', '1.8.3', '1.8-pre1' style versions
if ( version . equals ( standardIntegrationVersion )
| | version . startsWith ( standardIntegrationVersion + " . " )
| | version . startsWith ( standardIntegrationVersion + " - " ) ) {
useNewIntegration = false ;
break ;
2016-06-18 23:23:22 +02:00
}
2017-03-29 20:15:44 +02:00
}
2019-01-08 00:16:53 +01:00
if ( useNewIntegration ) {
weVersion = " FastAsyncWorldEditHandler " ;
wgVersion = " FastAsyncWorldEditWorldGuardHandler " ;
}
}
// Load WorldEdit
try {
final Class < ? > clazz = Class . forName ( " me.wiefferink.areashop.handlers. " + weVersion ) ;
// Check if we have a NMSHandler class at that location.
if ( WorldEditInterface . class . isAssignableFrom ( clazz ) ) { // Make sure it actually implements WorldEditInterface
worldEditInterface = ( WorldEditInterface ) clazz . getConstructor ( AreaShopInterface . class ) . newInstance ( this ) ; // Set our handler
}
} catch ( final Exception e ) {
error ( " Could not load the handler for WorldEdit (tried to load " + weVersion + " ), report this problem to the author: " + ExceptionUtils . getStackTrace ( e ) ) ;
error = true ;
weVersion = null ;
}
// Load WorldGuard
try {
final Class < ? > clazz = Class . forName ( " me.wiefferink.areashop.handlers. " + wgVersion ) ;
// Check if we have a NMSHandler class at that location.
if ( WorldGuardInterface . class . isAssignableFrom ( clazz ) ) { // Make sure it actually implements WorldGuardInterface
worldGuardInterface = ( WorldGuardInterface ) clazz . getConstructor ( AreaShopInterface . class ) . newInstance ( this ) ; // Set our handler
}
} catch ( final Exception e ) {
error ( " Could not load the handler for WorldGuard (tried to load " + wgVersion + " ), report this problem to the author: " + ExceptionUtils . getStackTrace ( e ) ) ;
error = true ;
wgVersion = null ;
2017-03-29 20:15:44 +02:00
}
// Check if Vault is present
if ( getServer ( ) . getPluginManager ( ) . getPlugin ( " Vault " ) = = null ) {
2016-08-08 22:06:31 +02:00
error ( " Vault plugin is not present or has not loaded correctly " ) ;
2016-06-18 23:23:22 +02:00
error = true ;
2017-03-29 20:15:44 +02:00
}
2016-06-07 22:39:45 +02:00
// Load all data from files and check versions
2016-12-23 16:03:22 +01:00
fileManager = new FileManager ( ) ;
managers . add ( fileManager ) ;
2017-03-29 20:15:44 +02:00
error = error | ! fileManager . loadFiles ( false ) ;
2016-12-25 23:02:23 +01:00
// Print loaded version of WG and WE in debug
2017-03-29 20:15:44 +02:00
if ( wgVersion ! = null ) {
2018-12-27 17:42:24 +01:00
AreaShop . debug ( " Loaded WorldGuardHandler " , wgVersion , " (raw version: " + rawWgVersion + " , major: " + major + " , minor: " + minor + " , fixes: " + fixes + " , build: " + build + " ) " ) ;
2017-03-29 20:15:44 +02:00
}
if ( weVersion ! = null ) {
2018-12-27 17:42:24 +01:00
AreaShop . debug ( " Loaded WorldEditHandler " , weVersion , " (raw version: " + rawWeVersion + " , beta: " + weBeta + " ) " ) ;
2016-06-21 22:42:56 +02:00
}
2017-02-02 22:59:00 +01:00
setupLanguageManager ( ) ;
2016-06-07 22:39:45 +02:00
if ( error ) {
2016-08-08 22:06:31 +02:00
error ( " The plugin has not started, fix the errors listed above " ) ;
2016-06-07 22:39:45 +02:00
} else {
2016-12-23 16:03:22 +01:00
featureManager = new FeatureManager ( ) ;
managers . add ( featureManager ) ;
2016-06-07 22:39:45 +02:00
// Register the event listeners
2016-06-18 23:23:22 +02:00
getServer ( ) . getPluginManager ( ) . registerEvents ( new PlayerLoginLogoutListener ( this ) , this ) ;
2016-06-07 22:39:45 +02:00
setupTasks ( ) ;
2017-03-29 20:15:44 +02:00
// Startup the CommandManager (registers itself for the command)
2016-12-23 16:03:22 +01:00
commandManager = new CommandManager ( ) ;
managers . add ( commandManager ) ;
// Create a signLinkerManager
signLinkerManager = new SignLinkerManager ( ) ;
managers . add ( signLinkerManager ) ;
2017-03-29 20:15:44 +02:00
// Enable Metrics if config allows it
2016-06-18 23:23:22 +02:00
if ( getConfig ( ) . getBoolean ( " sendStats " ) ) {
2017-03-29 20:15:44 +02:00
Analytics . start ( ) ;
}
2016-06-07 22:39:45 +02:00
// Register dynamic permission (things declared in config)
registerDynamicPermissions ( ) ;
2017-03-02 18:13:52 +01:00
2017-03-29 20:15:44 +02:00
// Don't initialize the updatechecker if disabled in the config
if ( getConfig ( ) . getBoolean ( " checkForUpdates " ) ) {
2017-06-12 19:56:49 +02:00
githubUpdateCheck = new GithubUpdateCheck (
AreaShop . getInstance ( ) ,
" NLThijs48 " ,
" AreaShop "
) . withVersionComparator ( ( latestVersion , currentVersion ) - >
! cleanVersion ( latestVersion ) . equals ( cleanVersion ( currentVersion ) )
) . checkUpdate ( ( result ) - > {
AreaShop . debug ( " Update check result: " , result ) ;
if ( ! result . hasUpdate ( ) ) {
return ;
}
AreaShop . info ( " Update from AreaShop V " + cleanVersion ( result . getCurrentVersion ( ) ) + " to AreaShop V " + cleanVersion ( result . getLatestVersion ( ) ) + " available, get the latest version at https://www.spigotmc.org/resources/areashop.2991/ " ) ;
for ( Player player : Utils . getOnlinePlayers ( ) ) {
notifyUpdate ( player ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2016-06-07 22:39:45 +02:00
}
}
}
2017-03-29 20:15:44 +02:00
2017-06-12 19:56:49 +02:00
/ * *
* Notify a player about an update if he wants notifications about it and an update is available .
* @param sender CommandSender to notify
* /
public void notifyUpdate ( CommandSender sender ) {
if ( githubUpdateCheck ! = null & & githubUpdateCheck . hasUpdate ( ) & & sender . hasPermission ( " areashop.notifyupdate " ) ) {
AreaShop . getInstance ( ) . message ( sender , " update-playerNotify " , cleanVersion ( githubUpdateCheck . getCurrentVersion ( ) ) , cleanVersion ( githubUpdateCheck . getLatestVersion ( ) ) ) ;
}
}
/ * *
* Cleanup a version number .
* @param version Version to clean
* @return Cleaned up version ( removed prefixes and suffixes )
* /
private String cleanVersion ( String version ) {
version = version . toLowerCase ( ) ;
// Strip 'v' as used on Github tags
if ( version . startsWith ( " v " ) ) {
version = version . substring ( 1 ) ;
}
// Strip build number as used by Jenkins
if ( version . contains ( " # " ) ) {
version = version . substring ( 0 , version . indexOf ( " # " ) ) ;
}
return version ;
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Called on shutdown or reload of the server .
2016-06-07 22:39:45 +02:00
* /
public void onDisable ( ) {
2016-12-23 16:03:22 +01:00
2016-06-07 22:39:45 +02:00
Bukkit . getServer ( ) . getScheduler ( ) . cancelTasks ( this ) ;
2016-12-23 16:03:22 +01:00
// Cleanup managers
for ( Manager manager : managers ) {
manager . shutdown ( ) ;
}
managers = null ;
2016-06-07 22:39:45 +02:00
fileManager = null ;
languageManager = null ;
commandManager = null ;
2016-12-23 16:03:22 +01:00
signLinkerManager = null ;
featureManager = null ;
// Cleanup plugins
worldGuard = null ;
worldGuardInterface = null ;
worldEdit = null ;
worldEditInterface = null ;
// Cleanup other stuff
2016-06-07 22:39:45 +02:00
chatprefix = null ;
debug = false ;
ready = false ;
2017-03-09 16:01:36 +01:00
HandlerList . unregisterAll ( this ) ;
2016-06-07 22:39:45 +02:00
}
/ * *
2017-03-29 20:15:44 +02:00
* Indicates if the plugin is ready to be used .
2016-06-07 22:39:45 +02:00
* @return true if the plugin is ready , false otherwise
* /
public boolean isReady ( ) {
return ready ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Set if the plugin is ready to be used or not ( not to be used from another plugin ! ) .
2016-06-07 22:39:45 +02:00
* @param ready Indicate if the plugin is ready to be used
* /
public void setReady ( boolean ready ) {
this . ready = ready ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Set if the plugin should output debug messages ( loaded from config normally ) .
2016-06-07 22:39:45 +02:00
* @param debug Indicates if the plugin should output debug messages or not
* /
public void setDebug ( boolean debug ) {
this . debug = debug ;
}
2017-02-02 22:59:00 +01:00
/ * *
2017-03-29 20:15:44 +02:00
* Setup a new LanguageManager .
2017-02-02 22:59:00 +01:00
* /
private void setupLanguageManager ( ) {
languageManager = new LanguageManager (
this ,
languageFolder ,
getConfig ( ) . getString ( " language " ) ,
" EN " ,
2017-03-09 16:01:36 +01:00
chatprefix
2017-02-02 22:59:00 +01:00
) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Set the chatprefix to use in the chat ( loaded from config normally ) .
2016-06-13 20:36:17 +02:00
* @param chatprefix The string to use in front of chat messages ( supports formatting codes )
2016-06-07 22:39:45 +02:00
* /
public void setChatprefix ( List < String > chatprefix ) {
this . chatprefix = chatprefix ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get the WorldGuard plugin .
2016-06-07 22:39:45 +02:00
* @return WorldGuardPlugin
* /
public WorldGuardPlugin getWorldGuard ( ) {
2017-03-29 20:15:44 +02:00
return worldGuard ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get WorldGuardInterface for version dependent things .
2016-06-07 22:39:45 +02:00
* @return WorldGuardInterface
* /
public WorldGuardInterface getWorldGuardHandler ( ) {
return this . worldGuardInterface ;
}
2017-03-29 20:15:44 +02:00
2018-08-05 00:34:50 +02:00
/ * *
* Get the RegionManager .
* @param world World to get the RegionManager for
* @return RegionManager for the given world , if there is one , otherwise null
* /
public RegionManager getRegionManager ( World world ) {
return this . worldGuardInterface . getRegionManager ( world ) ;
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get the WorldEdit plugin .
2016-06-07 22:39:45 +02:00
* @return WorldEditPlugin
* /
public WorldEditPlugin getWorldEdit ( ) {
2017-03-29 20:15:44 +02:00
return worldEdit ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get WorldGuardInterface for version dependent things .
2016-06-07 22:39:45 +02:00
* @return WorldGuardInterface
* /
public WorldEditInterface getWorldEditHandler ( ) {
return this . worldEditInterface ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get the LanguageManager .
2016-06-07 22:39:45 +02:00
* @return the LanguageManager
* /
public LanguageManager getLanguageManager ( ) {
2017-03-29 20:15:44 +02:00
return languageManager ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get the CommandManager .
2016-06-07 22:39:45 +02:00
* @return the CommandManager
* /
public CommandManager getCommandManager ( ) {
return commandManager ;
}
2016-12-23 16:03:22 +01:00
/ * *
2017-03-29 20:15:44 +02:00
* Get the SignLinkerManager .
* Handles sign linking mode .
2016-12-23 16:03:22 +01:00
* @return The SignLinkerManager
* /
2016-06-07 22:39:45 +02:00
public SignLinkerManager getSignlinkerManager ( ) {
return signLinkerManager ;
}
2016-12-23 16:03:22 +01:00
/ * *
2017-03-29 20:15:44 +02:00
* Get the FeatureManager .
* Manages region specific features .
2016-12-23 16:03:22 +01:00
* @return The FeatureManager
* /
public FeatureManager getFeatureManager ( ) {
return featureManager ;
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Function to get the Vault plugin .
2016-06-07 22:39:45 +02:00
* @return Economy
* /
public Economy getEconomy ( ) {
RegisteredServiceProvider < Economy > economy = getServer ( ) . getServicesManager ( ) . getRegistration ( net . milkbowl . vault . economy . Economy . class ) ;
2017-03-29 20:15:44 +02:00
if ( economy = = null | | economy . getProvider ( ) = = null ) {
2016-08-08 22:06:31 +02:00
error ( " There is no economy provider to support Vault, make sure you installed an economy plugin " ) ;
2016-06-18 23:23:22 +02:00
return null ;
2017-03-29 20:15:44 +02:00
}
return economy . getProvider ( ) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2017-10-05 12:20:03 +02:00
/ * *
* Get the Vault permissions provider .
* @return Vault permissions provider
* /
2018-08-05 00:34:50 +02:00
public net . milkbowl . vault . permission . Permission getPermissionProvider ( ) {
2017-10-05 12:20:03 +02:00
RegisteredServiceProvider < net . milkbowl . vault . permission . Permission > permissionProvider = getServer ( ) . getServicesManager ( ) . getRegistration ( net . milkbowl . vault . permission . Permission . class ) ;
if ( permissionProvider = = null | | permissionProvider . getProvider ( ) = = null ) {
return null ;
}
return permissionProvider . getProvider ( ) ;
}
2017-10-20 13:40:53 +02:00
/ * *
* Check for a permission of a ( possibly offline ) player .
* @param offlinePlayer OfflinePlayer to check
* @param permission Permission to check
* @return true if the player has the permission , false if the player does not have permission or , is offline and there is not Vault - compatible permission plugin
* /
public boolean hasPermission ( OfflinePlayer offlinePlayer , String permission ) {
// Online, return through Bukkit
if ( offlinePlayer . getPlayer ( ) ! = null ) {
return offlinePlayer . getPlayer ( ) . hasPermission ( permission ) ;
}
// Resolve while offline if possible
net . milkbowl . vault . permission . Permission permissionProvider = getPermissionProvider ( ) ;
if ( permissionProvider ! = null ) {
// TODO: Should we provide a world here?
return permissionProvider . playerHas ( null , offlinePlayer , permission ) ;
}
// Player offline and no offline permission provider available, safely say that there is no permission
return false ;
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Method to get the FileManager ( loads / save regions and can be used to get regions ) .
2016-06-07 22:39:45 +02:00
* @return The fileManager
* /
public FileManager getFileManager ( ) {
return fileManager ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Register dynamic permissions controlled by config settings .
2016-06-07 22:39:45 +02:00
* /
2017-03-09 16:01:36 +01:00
private void registerDynamicPermissions ( ) {
2016-06-07 22:39:45 +02:00
// Register limit groups of amount of regions a player can have
ConfigurationSection section = getConfig ( ) . getConfigurationSection ( " limitGroups " ) ;
if ( section = = null ) {
return ;
}
for ( String group : section . getKeys ( false ) ) {
if ( ! " default " . equals ( group ) ) {
Permission perm = new Permission ( " areashop.limits. " + group ) ;
try {
Bukkit . getPluginManager ( ) . addPermission ( perm ) ;
} catch ( IllegalArgumentException e ) {
2017-03-29 20:15:44 +02:00
warn ( " Could not add the following permission to be used as limit: " + perm . getName ( ) ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-03-29 20:15:44 +02:00
}
2016-06-07 22:39:45 +02:00
Bukkit . getPluginManager ( ) . recalculatePermissionDefaults ( Bukkit . getPluginManager ( ) . getPermission ( " playerwarps.limits " ) ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Register all required tasks .
2016-06-07 22:39:45 +02:00
* /
2017-03-29 20:15:44 +02:00
private void setupTasks ( ) {
// Rent expiration timer
2016-06-07 22:39:45 +02:00
long expirationCheck = Utils . millisToTicks ( Utils . getDurationFromSecondsOrString ( " expiration.delay " ) ) ;
final AreaShop finalPlugin = this ;
if ( expirationCheck > 0 ) {
2017-11-26 15:47:44 +01:00
Do . syncTimer ( expirationCheck , ( ) - > {
2017-06-12 19:56:49 +02:00
if ( isReady ( ) ) {
finalPlugin . getFileManager ( ) . checkRents ( ) ;
AreaShop . debugTask ( " Checking rent expirations... " ) ;
} else {
AreaShop . debugTask ( " Skipped checking rent expirations, plugin not ready " ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2017-03-29 20:15:44 +02:00
}
2017-06-12 19:56:49 +02:00
2017-03-29 20:15:44 +02:00
// Inactive unrenting/selling timer
2016-06-07 22:39:45 +02:00
long inactiveCheck = Utils . millisToTicks ( Utils . getDurationFromMinutesOrString ( " inactive.delay " ) ) ;
if ( inactiveCheck > 0 ) {
2017-11-26 15:47:44 +01:00
Do . syncTimer ( inactiveCheck , ( ) - > {
2017-06-12 19:56:49 +02:00
if ( isReady ( ) ) {
finalPlugin . getFileManager ( ) . checkForInactiveRegions ( ) ;
AreaShop . debugTask ( " Checking for regions with players that are inactive too long... " ) ;
} else {
AreaShop . debugTask ( " Skipped checking for regions of inactive players, plugin not ready " ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2017-03-29 20:15:44 +02:00
}
2017-06-12 19:56:49 +02:00
2017-03-29 20:15:44 +02:00
// Periodic updating of signs for timeleft tags
2016-06-07 22:39:45 +02:00
long periodicUpdate = Utils . millisToTicks ( Utils . getDurationFromSecondsOrString ( " signs.delay " ) ) ;
if ( periodicUpdate > 0 ) {
2017-11-26 15:47:44 +01:00
Do . syncTimer ( periodicUpdate , ( ) - > {
2017-06-12 19:56:49 +02:00
if ( isReady ( ) ) {
finalPlugin . getFileManager ( ) . performPeriodicSignUpdate ( ) ;
AreaShop . debugTask ( " Performing periodic sign update... " ) ;
} else {
AreaShop . debugTask ( " Skipped performing periodic sign update, plugin not ready " ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2017-03-29 20:15:44 +02:00
}
2017-06-12 19:56:49 +02:00
2017-03-29 20:15:44 +02:00
// Saving regions and group settings
2016-06-07 22:39:45 +02:00
long saveFiles = Utils . millisToTicks ( Utils . getDurationFromMinutesOrString ( " saving.delay " ) ) ;
if ( saveFiles > 0 ) {
2017-11-26 15:47:44 +01:00
Do . syncTimer ( saveFiles , ( ) - > {
2017-06-12 19:56:49 +02:00
if ( isReady ( ) ) {
finalPlugin . getFileManager ( ) . saveRequiredFiles ( ) ;
AreaShop . debugTask ( " Saving required files... " ) ;
} else {
AreaShop . debugTask ( " Skipped saving required files, plugin not ready " ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2017-03-29 20:15:44 +02:00
}
2017-06-12 19:56:49 +02:00
2017-03-29 20:15:44 +02:00
// Sending warnings about rent regions to online players
2016-06-07 22:39:45 +02:00
long expireWarning = Utils . millisToTicks ( Utils . getDurationFromMinutesOrString ( " expireWarning.delay " ) ) ;
if ( expireWarning > 0 ) {
2017-11-26 15:47:44 +01:00
Do . syncTimer ( expireWarning , ( ) - > {
2017-06-12 19:56:49 +02:00
if ( isReady ( ) ) {
finalPlugin . getFileManager ( ) . sendRentExpireWarnings ( ) ;
AreaShop . debugTask ( " Sending rent expire warnings... " ) ;
} else {
AreaShop . debugTask ( " Skipped sending rent expire warnings, plugin not ready " ) ;
2016-06-07 22:39:45 +02:00
}
2017-06-12 19:56:49 +02:00
} ) ;
2017-03-29 20:15:44 +02:00
}
2017-06-12 19:56:49 +02:00
2017-03-29 20:15:44 +02:00
// Update all regions on startup
if ( getConfig ( ) . getBoolean ( " updateRegionsOnStartup " ) ) {
2017-11-26 15:47:44 +01:00
Do . syncLater ( 20 , ( ) - > {
2017-06-12 19:56:49 +02:00
finalPlugin . getFileManager ( ) . updateAllRegions ( ) ;
AreaShop . debugTask ( " Updating all regions at startup... " ) ;
} ) ;
2017-03-29 20:15:44 +02:00
}
2016-06-07 22:39:45 +02:00
}
2016-08-08 22:06:31 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Send a message to a target without a prefix .
2016-08-08 22:06:31 +02:00
* @param target The target to send the message to
* @param key The key of the language string
* @param replacements The replacements to insert in the message
* /
2016-06-07 22:39:45 +02:00
public void messageNoPrefix ( Object target , String key , Object . . . replacements ) {
Message . fromKey ( key ) . replacements ( replacements ) . send ( target ) ;
}
2016-08-08 22:06:31 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Send a message to a target , prefixed by the default chat prefix .
2016-08-08 22:06:31 +02:00
* @param target The target to send the message to
* @param key The key of the language string
* @param replacements The replacements to insert in the message
* /
2016-06-07 22:39:45 +02:00
public void message ( Object target , String key , Object . . . replacements ) {
Message . fromKey ( key ) . prefix ( ) . replacements ( replacements ) . send ( target ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Return the config .
2016-06-07 22:39:45 +02:00
* /
@Override
public YamlConfiguration getConfig ( ) {
return fileManager . getConfig ( ) ;
}
/ * *
2017-03-29 20:15:44 +02:00
* Sends an debug message to the console .
2016-06-07 22:39:45 +02:00
* @param message The message that should be printed to the console
* /
2016-07-23 02:57:31 +02:00
public static void debug ( Object . . . message ) {
2016-06-07 22:39:45 +02:00
if ( AreaShop . getInstance ( ) . debug ) {
2017-03-29 20:15:44 +02:00
info ( " Debug: " + StringUtils . join ( message , " " ) ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Non - static debug to use as implementation of the interface .
2018-01-22 18:12:15 +01:00
* @param message Object parts of the message that should be logged , toString ( ) will be used
2016-06-07 22:39:45 +02:00
* /
2016-08-10 16:07:39 +02:00
public void debugI ( Object . . . message ) {
AreaShop . debug ( StringUtils . join ( message , " " ) ) ;
2016-06-07 22:39:45 +02:00
}
2016-08-08 22:06:31 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Print an information message to the console .
2016-08-08 22:06:31 +02:00
* @param message The message to print
* /
2016-08-10 16:07:39 +02:00
public static void info ( Object . . . message ) {
2016-08-08 22:06:31 +02:00
AreaShop . getInstance ( ) . getLogger ( ) . info ( StringUtils . join ( message , " " ) ) ;
}
2016-08-01 16:18:05 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Print a warning to the console .
2016-08-01 16:18:05 +02:00
* @param message The message to print
* /
2016-08-10 16:07:39 +02:00
public static void warn ( Object . . . message ) {
2016-08-01 16:18:05 +02:00
AreaShop . getInstance ( ) . getLogger ( ) . warning ( StringUtils . join ( message , " " ) ) ;
}
/ * *
2017-03-29 20:15:44 +02:00
* Print an error to the console .
2018-08-12 16:23:43 +02:00
* @param message The message to print
2016-08-01 16:18:05 +02:00
* /
2016-08-10 16:07:39 +02:00
public static void error ( Object . . . message ) {
2016-08-08 22:06:31 +02:00
AreaShop . getInstance ( ) . getLogger ( ) . severe ( StringUtils . join ( message , " " ) ) ;
2016-08-01 16:18:05 +02:00
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Print debug message for periodic task .
2016-06-07 22:39:45 +02:00
* @param message The message to print
* /
2016-08-10 16:07:39 +02:00
public static void debugTask ( Object . . . message ) {
2016-06-07 22:39:45 +02:00
if ( AreaShop . getInstance ( ) . getConfig ( ) . getBoolean ( " debugTask " ) ) {
2016-08-10 16:07:39 +02:00
AreaShop . debug ( StringUtils . join ( message , " " ) ) ;
2016-06-07 22:39:45 +02:00
}
}
/ * *
2017-03-29 20:15:44 +02:00
* Reload all files of the plugin and update all regions .
* @param confirmationReceiver The CommandSender which should be notified when complete , null for nobody
2016-06-07 22:39:45 +02:00
* /
public void reload ( final CommandSender confirmationReceiver ) {
setReady ( false ) ;
fileManager . saveRequiredFilesAtOnce ( ) ;
2016-12-25 23:02:23 +01:00
fileManager . loadFiles ( true ) ;
2017-02-02 22:59:00 +01:00
setupLanguageManager ( ) ;
2016-06-07 22:39:45 +02:00
message ( confirmationReceiver , " reload-reloading " ) ;
fileManager . checkRents ( ) ;
fileManager . updateAllRegions ( confirmationReceiver ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Reload all files of the plugin and update all regions .
2016-06-07 22:39:45 +02:00
* /
public void reload ( ) {
reload ( null ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
}