2015-09-02 13:06:48 +02:00
package de.epiceric.shopchest ;
2016-06-22 20:51:19 +02:00
import de.epiceric.shopchest.config.Regex ;
2017-03-21 14:25:49 +01:00
import de.epiceric.shopchest.event.* ;
2016-06-22 20:51:19 +02:00
import de.epiceric.shopchest.language.LanguageUtils ;
import de.epiceric.shopchest.language.LocalizedMessage ;
2016-08-05 15:25:24 +02:00
import de.epiceric.shopchest.nms.JsonBuilder ;
2016-07-05 18:41:31 +02:00
import de.epiceric.shopchest.shop.Shop ;
2016-04-13 17:30:34 +02:00
import de.epiceric.shopchest.shop.Shop.ShopType ;
2016-11-18 21:14:01 +01:00
import de.epiceric.shopchest.utils.* ;
2015-09-04 20:16:33 +02:00
import de.epiceric.shopchest.utils.ClickType.EnumClickType ;
2016-05-07 15:08:40 +02:00
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult ;
2016-05-28 15:47:57 +02:00
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
import org.bukkit.Material ;
2017-03-25 15:12:09 +01:00
import org.bukkit.command.* ;
2016-05-28 15:47:57 +02:00
import org.bukkit.enchantments.Enchantment ;
import org.bukkit.entity.Player ;
import org.bukkit.inventory.ItemStack ;
2017-03-25 15:12:09 +01:00
import org.bukkit.plugin.Plugin ;
2016-05-28 15:47:57 +02:00
2017-03-25 15:12:09 +01:00
import java.lang.reflect.Constructor ;
import java.lang.reflect.Field ;
import java.lang.reflect.InvocationTargetException ;
2016-11-18 22:13:23 +01:00
import java.util.Locale ;
2015-09-02 13:06:48 +02:00
2017-03-25 15:12:09 +01:00
class ShopCommand implements CommandExecutor {
2015-09-02 13:06:48 +02:00
2016-05-28 15:47:57 +02:00
private ShopChest plugin ;
2017-03-25 15:12:09 +01:00
private String name ;
2016-07-13 15:03:54 +02:00
private ShopUtils shopUtils ;
2017-03-25 15:12:09 +01:00
private PluginCommand pluginCommand ;
2016-05-28 15:47:57 +02:00
2017-03-25 15:12:09 +01:00
ShopCommand ( ShopChest plugin ) {
2016-05-28 15:47:57 +02:00
this . plugin = plugin ;
2017-03-25 15:12:09 +01:00
this . name = plugin . getShopChestConfig ( ) . main_command_name ;
2016-07-13 15:03:54 +02:00
this . shopUtils = plugin . getShopUtils ( ) ;
2017-03-25 15:12:09 +01:00
this . pluginCommand = createPluginCommand ( ) ;
register ( ) ;
}
public PluginCommand getCommand ( ) {
return pluginCommand ;
2016-05-28 15:47:57 +02:00
}
2017-03-25 15:12:09 +01:00
private PluginCommand createPluginCommand ( ) {
plugin . debug ( " Creating plugin command " ) ;
try {
Constructor < PluginCommand > c = PluginCommand . class . getDeclaredConstructor ( String . class , Plugin . class ) ;
c . setAccessible ( true ) ;
PluginCommand cmd = c . newInstance ( name , plugin ) ;
cmd . setDescription ( " Manage players' shops or this plugin. " ) ;
cmd . setUsage ( " / " + name ) ;
cmd . setExecutor ( this ) ;
cmd . setPermission ( Permissions . BUY ) ;
cmd . setTabCompleter ( new ShopTabCompleter ( plugin ) ) ;
return cmd ;
} catch ( NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e ) {
plugin . getLogger ( ) . severe ( " Failed to create command " ) ;
plugin . debug ( " Failed to create plugin command " ) ;
plugin . debug ( e ) ;
}
return null ;
}
private void register ( ) {
if ( pluginCommand = = null ) return ;
plugin . debug ( " Registering command " + name ) ;
try {
Field f = Bukkit . getPluginManager ( ) . getClass ( ) . getDeclaredField ( " commandMap " ) ;
f . setAccessible ( true ) ;
Object commandMapObject = f . get ( Bukkit . getPluginManager ( ) ) ;
if ( commandMapObject instanceof CommandMap ) {
CommandMap commandMap = ( CommandMap ) commandMapObject ;
commandMap . register ( plugin . getName ( ) , pluginCommand ) ;
}
} catch ( NoSuchFieldException | IllegalAccessException e ) {
plugin . getLogger ( ) . severe ( " Failed to register command " ) ;
plugin . debug ( " Failed to register plugin command " ) ;
plugin . debug ( e ) ;
}
2016-05-28 15:47:57 +02:00
}
@Override
2017-03-25 15:12:09 +01:00
public boolean onCommand ( CommandSender sender , Command command , String label , String [ ] args ) {
2016-11-18 22:13:23 +01:00
boolean needsHelp = true ;
if ( args . length > 0 ) {
if ( ! ( sender instanceof Player ) ) {
switch ( args [ 0 ] . toUpperCase ( Locale . US ) ) {
case " CREATE " :
case " REMOVE " :
case " INFO " :
case " LIMITS " :
2017-03-21 14:25:49 +01:00
case " OPEN " :
2016-11-18 22:13:23 +01:00
sender . sendMessage ( ChatColor . RED + " Only players can use this command. " ) ;
return true ;
}
2016-05-28 15:47:57 +02:00
} else {
2016-11-18 22:13:23 +01:00
Player p = ( Player ) sender ;
2016-05-28 15:47:57 +02:00
if ( args [ 0 ] . equalsIgnoreCase ( " create " ) ) {
2016-11-18 22:13:23 +01:00
2016-11-18 21:14:01 +01:00
if ( p . hasPermission ( Permissions . CREATE ) ) {
2016-05-28 15:47:57 +02:00
if ( args . length = = 4 ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-05-28 15:47:57 +02:00
create ( args , ShopType . NORMAL , p ) ;
} else if ( args . length = = 5 ) {
2016-05-29 13:41:14 +02:00
if ( args [ 4 ] . equalsIgnoreCase ( " normal " ) ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-05-28 15:47:57 +02:00
create ( args , ShopType . NORMAL , p ) ;
} else if ( args [ 4 ] . equalsIgnoreCase ( " admin " ) ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-11-18 21:14:01 +01:00
if ( p . hasPermission ( Permissions . CREATE_ADMIN ) ) {
2016-05-28 15:47:57 +02:00
create ( args , ShopType . ADMIN , p ) ;
} else {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_PERMISSION_CREATE_ADMIN ) ) ;
2016-05-28 15:47:57 +02:00
}
}
}
} else {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_PERMISSION_CREATE ) ) ;
2016-05-28 15:47:57 +02:00
}
} else if ( args [ 0 ] . equalsIgnoreCase ( " remove " ) ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-05-28 15:47:57 +02:00
remove ( p ) ;
} else if ( args [ 0 ] . equalsIgnoreCase ( " info " ) ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-05-28 15:47:57 +02:00
info ( p ) ;
} else if ( args [ 0 ] . equalsIgnoreCase ( " limits " ) ) {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
2016-11-12 13:11:08 +01:00
plugin . debug ( p . getName ( ) + " is viewing his shop limits: " + shopUtils . getShopAmount ( p ) + " / " + shopUtils . getShopLimit ( p ) ) ;
int limit = shopUtils . getShopLimit ( p ) ;
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . OCCUPIED_SHOP_SLOTS ,
new LocalizedMessage . ReplacedRegex ( Regex . LIMIT , ( limit < 0 ? " ∞ " : String . valueOf ( limit ) ) ) ,
new LocalizedMessage . ReplacedRegex ( Regex . AMOUNT , String . valueOf ( shopUtils . getShopAmount ( p ) ) ) ) ) ;
2017-03-21 14:25:49 +01:00
} else if ( args [ 0 ] . equalsIgnoreCase ( " open " ) ) {
needsHelp = false ;
open ( p ) ;
2016-11-18 22:13:23 +01:00
}
}
2016-06-22 20:51:19 +02:00
2016-11-18 22:13:23 +01:00
if ( args [ 0 ] . equalsIgnoreCase ( " reload " ) ) {
needsHelp = false ;
if ( sender . hasPermission ( Permissions . RELOAD ) ) {
reload ( sender ) ;
} else {
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_PERMISSION_RELOAD ) ) ;
}
} else if ( args [ 0 ] . equalsIgnoreCase ( " update " ) ) {
needsHelp = false ;
if ( sender . hasPermission ( Permissions . UPDATE ) ) {
checkUpdates ( sender ) ;
} else {
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_PERMISSION_UPDATE ) ) ;
}
} else if ( args [ 0 ] . equalsIgnoreCase ( " config " ) ) {
if ( sender . hasPermission ( Permissions . CONFIG ) ) {
if ( args . length > = 4 ) {
needsHelp = false ;
changeConfig ( sender , args ) ;
2016-07-08 16:55:57 +02:00
}
2016-05-28 15:47:57 +02:00
} else {
2016-11-18 22:13:23 +01:00
needsHelp = false ;
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_PERMISSION_CONFIG ) ) ;
2016-05-28 15:47:57 +02:00
}
}
2016-11-18 22:13:23 +01:00
}
2016-05-28 15:47:57 +02:00
2016-11-18 22:13:23 +01:00
if ( needsHelp ) sendBasicHelpMessage ( sender ) ;
return true ;
}
private void changeConfig ( CommandSender sender , String [ ] args ) {
plugin . debug ( sender . getName ( ) + " is changing the configuration " ) ;
String property = args [ 2 ] ;
String value = args [ 3 ] ;
if ( args [ 1 ] . equalsIgnoreCase ( " set " ) ) {
plugin . getShopChestConfig ( ) . set ( property , value ) ;
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CHANGED_CONFIG_SET , new LocalizedMessage . ReplacedRegex ( Regex . PROPERTY , property ) , new LocalizedMessage . ReplacedRegex ( Regex . VALUE , value ) ) ) ;
} else if ( args [ 1 ] . equalsIgnoreCase ( " add " ) ) {
plugin . getShopChestConfig ( ) . add ( property , value ) ;
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CHANGED_CONFIG_ADDED , new LocalizedMessage . ReplacedRegex ( Regex . PROPERTY , property ) , new LocalizedMessage . ReplacedRegex ( Regex . VALUE , value ) ) ) ;
} else if ( args [ 1 ] . equalsIgnoreCase ( " remove " ) ) {
plugin . getShopChestConfig ( ) . remove ( property , value ) ;
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CHANGED_CONFIG_REMOVED , new LocalizedMessage . ReplacedRegex ( Regex . PROPERTY , property ) , new LocalizedMessage . ReplacedRegex ( Regex . VALUE , value ) ) ) ;
2016-05-28 15:47:57 +02:00
} else {
2016-11-18 22:13:23 +01:00
sendBasicHelpMessage ( sender ) ;
2016-05-28 15:47:57 +02:00
}
}
2016-06-30 21:59:06 +02:00
/ * *
* A given player checks for updates
2016-11-18 22:13:23 +01:00
* @param sender The command executor
2016-06-30 21:59:06 +02:00
* /
2016-11-18 22:13:23 +01:00
private void checkUpdates ( CommandSender sender ) {
plugin . debug ( sender . getName ( ) + " is checking for updates " ) ;
2016-08-03 13:49:31 +02:00
2016-11-18 22:13:23 +01:00
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_CHECKING ) ) ;
2016-05-28 15:47:57 +02:00
2016-06-30 21:59:06 +02:00
UpdateChecker uc = new UpdateChecker ( ShopChest . getInstance ( ) ) ;
2016-07-06 17:05:30 +02:00
UpdateCheckerResult result = uc . check ( ) ;
2016-05-28 15:47:57 +02:00
if ( result = = UpdateCheckerResult . TRUE ) {
2016-06-22 20:51:19 +02:00
plugin . setLatestVersion ( uc . getVersion ( ) ) ;
plugin . setDownloadLink ( uc . getLink ( ) ) ;
plugin . setUpdateNeeded ( true ) ;
2016-05-28 15:47:57 +02:00
2016-11-18 22:13:23 +01:00
if ( sender instanceof Player ) {
JsonBuilder jb = new JsonBuilder ( plugin , LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_AVAILABLE , new LocalizedMessage . ReplacedRegex ( Regex . VERSION , uc . getVersion ( ) ) ) , LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_CLICK_TO_DOWNLOAD ) , uc . getLink ( ) ) ;
jb . sendJson ( ( Player ) sender ) ;
} else {
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_AVAILABLE , new LocalizedMessage . ReplacedRegex ( Regex . VERSION , uc . getVersion ( ) ) ) ) ;
}
2016-05-28 15:47:57 +02:00
} else if ( result = = UpdateCheckerResult . FALSE ) {
2016-06-22 20:51:19 +02:00
plugin . setLatestVersion ( " " ) ;
plugin . setDownloadLink ( " " ) ;
plugin . setUpdateNeeded ( false ) ;
2016-11-18 22:13:23 +01:00
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_NO_UPDATE ) ) ;
2016-05-28 15:47:57 +02:00
} else {
2016-06-22 20:51:19 +02:00
plugin . setLatestVersion ( " " ) ;
plugin . setDownloadLink ( " " ) ;
plugin . setUpdateNeeded ( false ) ;
2016-11-18 22:13:23 +01:00
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . UPDATE_ERROR ) ) ;
2016-05-28 15:47:57 +02:00
}
}
2016-06-30 21:59:06 +02:00
/ * *
* A given player reloads the shops
2016-11-18 22:13:23 +01:00
* @param sender The command executor
2016-06-30 21:59:06 +02:00
* /
2016-11-18 22:13:23 +01:00
private void reload ( CommandSender sender ) {
plugin . debug ( sender . getName ( ) + " is reloading the shops " ) ;
2016-08-03 13:49:31 +02:00
2016-11-18 22:13:23 +01:00
ShopReloadEvent event = new ShopReloadEvent ( sender ) ;
2016-07-05 18:41:31 +02:00
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
2016-08-03 13:49:31 +02:00
if ( event . isCancelled ( ) ) {
plugin . debug ( " Reload event cancelled " ) ;
return ;
}
2016-07-05 18:41:31 +02:00
2016-10-12 18:41:45 +02:00
int count = shopUtils . reloadShops ( true , true ) ;
2016-11-18 22:13:23 +01:00
plugin . debug ( sender . getName ( ) + " has reloaded " + count + " shops " ) ;
sender . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . RELOADED_SHOPS , new LocalizedMessage . ReplacedRegex ( Regex . AMOUNT , String . valueOf ( count ) ) ) ) ;
2016-05-28 15:47:57 +02:00
}
2016-06-30 21:59:06 +02:00
/ * *
* A given player creates a shop
* @param args Arguments of the entered command
* @param shopType The { @link ShopType } , the shop will have
* @param p The command executor
* /
2016-05-28 15:47:57 +02:00
private void create ( String [ ] args , ShopType shopType , Player p ) {
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " wants to create a shop " ) ;
2016-05-28 15:47:57 +02:00
int amount ;
double buyPrice , sellPrice ;
2016-07-13 15:03:54 +02:00
int limit = shopUtils . getShopLimit ( p ) ;
2016-05-28 15:47:57 +02:00
if ( limit ! = - 1 ) {
2016-07-13 15:03:54 +02:00
if ( shopUtils . getShopAmount ( p ) > = limit ) {
2016-07-08 16:55:57 +02:00
if ( shopType ! = ShopType . ADMIN | | ! plugin . getShopChestConfig ( ) . exclude_admin_shops ) {
2016-06-24 18:13:33 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . SHOP_LIMIT_REACHED , new LocalizedMessage . ReplacedRegex ( Regex . LIMIT , String . valueOf ( limit ) ) ) ) ;
return ;
}
2016-05-28 15:47:57 +02:00
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " has not reached the limit " ) ;
2016-05-28 15:47:57 +02:00
try {
amount = Integer . parseInt ( args [ 1 ] ) ;
buyPrice = Double . parseDouble ( args [ 2 ] ) ;
sellPrice = Double . parseDouble ( args [ 3 ] ) ;
} catch ( NumberFormatException e ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . AMOUNT_PRICE_NOT_NUMBER ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
2016-10-12 18:25:36 +02:00
if ( amount < = 0 ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . AMOUNT_IS_ZERO ) ) ;
return ;
}
2016-08-18 15:33:58 +02:00
plugin . debug ( p . getName ( ) + " has entered numbers as prices and amount " ) ;
if ( ! plugin . getShopChestConfig ( ) . allow_decimals_in_price & & ( buyPrice ! = ( int ) buyPrice | | sellPrice ! = ( int ) sellPrice ) ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . PRICES_CONTAIN_DECIMALS ) ) ;
return ;
}
plugin . debug ( p . getName ( ) + " has entered the numbers correctly (according to allow-decimals configuration) " ) ;
2016-08-03 13:49:31 +02:00
2016-09-06 11:46:24 +02:00
boolean buyEnabled = buyPrice > 0 ;
boolean sellEnabled = sellPrice > 0 ;
2016-05-28 15:47:57 +02:00
if ( ! buyEnabled & & ! sellEnabled ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . BUY_SELL_DISABLED ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " has enabled buying, selling or both " ) ;
2016-08-09 14:41:41 +02:00
if ( Utils . getPreferredItemInHand ( p ) = = null ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . NO_ITEM_IN_HAND ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " has an item in his hand " ) ;
2016-07-08 16:55:57 +02:00
for ( String item : plugin . getShopChestConfig ( ) . blacklist ) {
2016-05-28 15:47:57 +02:00
ItemStack itemStack ;
if ( item . contains ( " : " ) ) {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( item . split ( " : " ) [ 0 ] ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in blacklist: " + item ) ;
plugin . debug ( " Invalid item in blacklist: " + item ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 , Short . parseShort ( item . split ( " : " ) [ 1 ] ) ) ;
2016-05-28 15:47:57 +02:00
} else {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( item ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in blacklist: " + item ) ;
plugin . debug ( " Invalid item in blacklist: " + item ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 ) ;
2016-05-28 15:47:57 +02:00
}
2016-08-09 14:41:41 +02:00
if ( itemStack . getType ( ) . equals ( Utils . getPreferredItemInHand ( p ) . getType ( ) ) & & itemStack . getDurability ( ) = = Utils . getPreferredItemInHand ( p ) . getDurability ( ) ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CANNOT_SELL_ITEM ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " 's item is not on the blacklist " ) ;
2016-07-08 16:55:57 +02:00
for ( String key : plugin . getShopChestConfig ( ) . minimum_prices ) {
2016-05-28 15:47:57 +02:00
ItemStack itemStack ;
2017-01-05 14:35:42 +01:00
double minPrice = plugin . getConfig ( ) . getDouble ( " minimum-prices. " + key ) ;
2016-05-28 15:47:57 +02:00
if ( key . contains ( " : " ) ) {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( key . split ( " : " ) [ 0 ] ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in minimum-prices: " + key ) ;
plugin . debug ( " Invalid item in minimum-prices: " + key ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 , Short . parseShort ( key . split ( " : " ) [ 1 ] ) ) ;
2016-05-28 15:47:57 +02:00
} else {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( key ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in minimum-prices: " + key ) ;
plugin . debug ( " Invalid item in minimum-prices: " + key ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 ) ;
2016-05-28 15:47:57 +02:00
}
2016-08-09 14:41:41 +02:00
if ( itemStack . getType ( ) . equals ( Utils . getPreferredItemInHand ( p ) . getType ( ) ) & & itemStack . getDurability ( ) = = Utils . getPreferredItemInHand ( p ) . getDurability ( ) ) {
2016-05-28 15:47:57 +02:00
if ( buyEnabled ) {
2017-01-05 14:35:42 +01:00
if ( ( buyPrice < amount * minPrice ) & & ( buyPrice > 0 ) ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . BUY_PRICE_TOO_LOW , new LocalizedMessage . ReplacedRegex ( Regex . MIN_PRICE , String . valueOf ( amount * minPrice ) ) ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
}
if ( sellEnabled ) {
2017-01-05 14:35:42 +01:00
if ( ( sellPrice < amount * minPrice ) & & ( sellPrice > 0 ) ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . SELL_PRICE_TOO_LOW , new LocalizedMessage . ReplacedRegex ( Regex . MIN_PRICE , String . valueOf ( amount * minPrice ) ) ) ) ;
return ;
}
}
}
}
plugin . debug ( p . getName ( ) + " 's prices are higher than the minimum " ) ;
for ( String key : plugin . getShopChestConfig ( ) . maximum_prices ) {
ItemStack itemStack ;
double maxPrice = plugin . getConfig ( ) . getDouble ( " maximum-prices. " + key ) ;
if ( key . contains ( " : " ) ) {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( key . split ( " : " ) [ 0 ] ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in maximum-prices: " + key ) ;
plugin . debug ( " Invalid item in maximum-prices: " + key ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 , Short . parseShort ( key . split ( " : " ) [ 1 ] ) ) ;
2017-01-05 14:35:42 +01:00
} else {
2017-01-28 12:23:10 +01:00
Material mat = Material . getMaterial ( key ) ;
if ( mat = = null ) {
plugin . getLogger ( ) . warning ( " Invalid item found in maximum-prices: " + key ) ;
plugin . debug ( " Invalid item in maximum-prices: " + key ) ;
continue ;
}
itemStack = new ItemStack ( mat , 1 ) ;
2017-01-05 14:35:42 +01:00
}
if ( itemStack . getType ( ) . equals ( Utils . getPreferredItemInHand ( p ) . getType ( ) ) & & itemStack . getDurability ( ) = = Utils . getPreferredItemInHand ( p ) . getDurability ( ) ) {
if ( buyEnabled ) {
if ( ( buyPrice > amount * maxPrice ) & & ( buyPrice > 0 ) ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . BUY_PRICE_TOO_HIGH , new LocalizedMessage . ReplacedRegex ( Regex . MAX_PRICE , String . valueOf ( amount * maxPrice ) ) ) ) ;
return ;
}
}
if ( sellEnabled ) {
if ( ( sellPrice > amount * maxPrice ) & & ( sellPrice > 0 ) ) {
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . SELL_PRICE_TOO_HIGH , new LocalizedMessage . ReplacedRegex ( Regex . MAX_PRICE , String . valueOf ( amount * maxPrice ) ) ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
}
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " 's prices are higher than the minimum " ) ;
2016-05-28 15:47:57 +02:00
if ( sellEnabled & & buyEnabled ) {
2016-07-08 16:55:57 +02:00
if ( plugin . getShopChestConfig ( ) . buy_greater_or_equal_sell ) {
2016-05-28 15:47:57 +02:00
if ( buyPrice < sellPrice ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . BUY_PRICE_TOO_LOW , new LocalizedMessage . ReplacedRegex ( Regex . MIN_PRICE , String . valueOf ( sellPrice ) ) ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " 's buy price is high enough " ) ;
2016-08-09 14:41:41 +02:00
ItemStack itemStack = new ItemStack ( Utils . getPreferredItemInHand ( p ) . getType ( ) , amount , Utils . getPreferredItemInHand ( p ) . getDurability ( ) ) ;
itemStack . setItemMeta ( Utils . getPreferredItemInHand ( p ) . getItemMeta ( ) ) ;
2016-05-28 15:47:57 +02:00
if ( Enchantment . DURABILITY . canEnchantItem ( itemStack ) ) {
2016-08-02 13:23:47 +02:00
if ( itemStack . getDurability ( ) > 0 & & ! plugin . getShopChestConfig ( ) . allow_broken_items ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CANNOT_SELL_BROKEN_ITEM ) ) ;
2016-05-28 15:47:57 +02:00
return ;
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " 's item is not broken (or broken items are allowed through config) " ) ;
2016-07-08 16:55:57 +02:00
double creationPrice = ( shopType = = ShopType . NORMAL ) ? plugin . getShopChestConfig ( ) . shop_creation_price_normal : plugin . getShopChestConfig ( ) . shop_creation_price_admin ;
2016-06-03 16:28:44 +02:00
if ( creationPrice > 0 ) {
2016-07-01 14:33:13 +02:00
if ( plugin . getEconomy ( ) . getBalance ( p ) < creationPrice ) {
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . SHOP_CREATE_NOT_ENOUGH_MONEY , new LocalizedMessage . ReplacedRegex ( Regex . CREATION_PRICE , String . valueOf ( creationPrice ) ) ) ) ;
2016-06-03 16:28:44 +02:00
return ;
}
}
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " can pay the creation price " ) ;
ShopPreCreateEvent event = new ShopPreCreateEvent ( p , Shop . createImaginaryShop ( p , itemStack , null , buyPrice , sellPrice , shopType ) ) ;
2016-07-05 18:41:31 +02:00
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
2016-05-28 15:47:57 +02:00
2016-07-05 18:41:31 +02:00
if ( ! event . isCancelled ( ) ) {
ClickType . setPlayerClickType ( p , new ClickType ( EnumClickType . CREATE , itemStack , buyPrice , sellPrice , shopType ) ) ;
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " can now click a chest " ) ;
2016-07-05 18:41:31 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CLICK_CHEST_CREATE ) ) ;
2016-08-03 13:49:31 +02:00
} else {
plugin . debug ( " Shop pre create event cancelled " ) ;
2016-07-05 18:41:31 +02:00
}
2016-05-28 15:47:57 +02:00
}
2016-06-30 21:59:06 +02:00
/ * *
* A given player removes a shop
* @param p The command executor
* /
2016-05-28 15:47:57 +02:00
private void remove ( Player p ) {
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " wants to remove a shop " ) ;
2016-07-05 18:41:31 +02:00
ShopPreRemoveEvent event = new ShopPreRemoveEvent ( p ) ;
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
2016-08-03 13:49:31 +02:00
if ( event . isCancelled ( ) ) {
plugin . debug ( " Shop pre remove event cancelled " ) ;
return ;
}
2016-07-05 18:41:31 +02:00
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " can now click a chest " ) ;
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CLICK_CHEST_REMOVE ) ) ;
2016-06-30 21:59:06 +02:00
ClickType . setPlayerClickType ( p , new ClickType ( EnumClickType . REMOVE ) ) ;
2016-05-28 15:47:57 +02:00
}
2016-06-30 21:59:06 +02:00
/ * *
* A given player retrieves information about a shop
* @param p The command executor
* /
2016-05-28 15:47:57 +02:00
private void info ( Player p ) {
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " wants to retrieve information " ) ;
2016-07-05 18:41:31 +02:00
ShopPreInfoEvent event = new ShopPreInfoEvent ( p ) ;
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
2016-08-03 13:49:31 +02:00
if ( event . isCancelled ( ) ) {
plugin . debug ( " Shop pre info event cancelled " ) ;
return ;
}
2016-07-05 18:41:31 +02:00
2016-08-03 13:49:31 +02:00
plugin . debug ( p . getName ( ) + " can now click a chest " ) ;
2016-06-22 20:51:19 +02:00
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CLICK_CHEST_INFO ) ) ;
2016-06-30 21:59:06 +02:00
ClickType . setPlayerClickType ( p , new ClickType ( EnumClickType . INFO ) ) ;
2016-05-28 15:47:57 +02:00
}
2017-03-21 14:25:49 +01:00
/ * *
* A given player opens a shop
* @param p The command executor
* /
private void open ( Player p ) {
plugin . debug ( p . getName ( ) + " wants to open a shop " ) ;
ShopPreOpenEvent event = new ShopPreOpenEvent ( p ) ;
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
if ( event . isCancelled ( ) ) {
plugin . debug ( " Shop pre open event cancelled " ) ;
return ;
}
plugin . debug ( p . getName ( ) + " can now click a chest " ) ;
p . sendMessage ( LanguageUtils . getMessage ( LocalizedMessage . Message . CLICK_CHEST_OPEN ) ) ;
ClickType . setPlayerClickType ( p , new ClickType ( EnumClickType . OPEN ) ) ;
}
2016-06-30 21:59:06 +02:00
/ * *
2016-11-18 22:13:23 +01:00
* Sends the basic help message
* @param sender { @link CommandSender } who will receive the message
2016-06-30 21:59:06 +02:00
* /
2016-11-18 22:13:23 +01:00
private void sendBasicHelpMessage ( CommandSender sender ) {
plugin . debug ( " Sending basic help message to " + sender . getName ( ) ) ;
2016-11-12 15:55:36 +01:00
2016-11-18 22:13:23 +01:00
if ( sender instanceof Player ) {
if ( sender . hasPermission ( Permissions . CREATE_ADMIN ) ) {
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " create <amount> <buy-price> <sell-price> [normal|admin] - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_CREATE ) ) ;
} else {
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " create <amount> <buy-price> <sell-price> - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_CREATE ) ) ;
}
2016-11-12 15:55:36 +01:00
2016-11-18 22:13:23 +01:00
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " remove - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_REMOVE ) ) ;
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " info - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_INFO ) ) ;
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " limits - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_LIMITS ) ) ;
2017-03-21 14:25:49 +01:00
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " open - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_OPEN ) ) ;
2016-11-12 15:55:36 +01:00
}
2016-11-18 22:13:23 +01:00
if ( sender . hasPermission ( Permissions . RELOAD ) ) {
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " reload - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_RELOAD ) ) ;
2016-11-12 15:55:36 +01:00
}
2016-11-18 22:13:23 +01:00
if ( sender . hasPermission ( Permissions . UPDATE ) ) {
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " update - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_UPDATE ) ) ;
}
2016-11-12 15:55:36 +01:00
2016-11-18 22:13:23 +01:00
if ( sender . hasPermission ( Permissions . CONFIG ) ) {
sender . sendMessage ( ChatColor . GREEN + " / " + plugin . getShopChestConfig ( ) . main_command_name + " config <set|add|remove> <property> <value> - " + LanguageUtils . getMessage ( LocalizedMessage . Message . COMMAND_DESC_CONFIG ) ) ;
2016-11-12 15:55:36 +01:00
}
2015-09-02 13:06:48 +02:00
}
}