2019-11-02 01:28:07 +00:00
package me.totalfreedom.totalfreedommod.shop ;
2020-04-08 02:20:01 +00:00
import java.util.ArrayList ;
import java.util.Arrays ;
2020-12-25 19:46:43 +00:00
import java.util.Collections ;
2020-04-08 08:34:08 +00:00
import java.util.Date ;
2020-04-08 02:20:01 +00:00
import java.util.List ;
2020-12-25 19:46:43 +00:00
import java.util.Objects ;
2019-11-02 01:28:07 +00:00
import me.totalfreedom.totalfreedommod.FreedomService ;
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
2020-06-30 07:25:38 +00:00
import me.totalfreedom.totalfreedommod.player.PlayerData ;
2020-08-05 07:01:21 +00:00
import me.totalfreedom.totalfreedommod.util.FLog ;
2019-11-02 01:28:07 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
import org.bukkit.ChatColor ;
2020-04-08 02:20:01 +00:00
import org.bukkit.Material ;
2020-07-02 08:42:27 +00:00
import org.bukkit.boss.BarColor ;
import org.bukkit.boss.BarStyle ;
import org.bukkit.boss.BossBar ;
2020-04-08 02:20:01 +00:00
import org.bukkit.enchantments.Enchantment ;
2019-11-02 01:28:07 +00:00
import org.bukkit.entity.Player ;
import org.bukkit.event.EventHandler ;
import org.bukkit.event.EventPriority ;
2020-07-01 03:38:29 +00:00
import org.bukkit.event.inventory.InventoryClickEvent ;
2020-04-08 02:20:01 +00:00
import org.bukkit.inventory.Inventory ;
import org.bukkit.inventory.ItemStack ;
2020-08-04 22:16:11 +00:00
import org.bukkit.inventory.PlayerInventory ;
2020-04-08 02:20:01 +00:00
import org.bukkit.inventory.meta.ItemMeta ;
2020-04-08 08:34:08 +00:00
import org.bukkit.scheduler.BukkitRunnable ;
import org.bukkit.scheduler.BukkitTask ;
2019-11-02 01:28:07 +00:00
public class Shop extends FreedomService
{
2020-04-08 08:34:08 +00:00
public final int coinsPerReactionWin = ConfigEntry . SHOP_REACTIONS_COINS_PER_WIN . getInteger ( ) ;
2020-07-02 08:42:27 +00:00
public final String prefix = ChatColor . DARK_GRAY + " [ " + ChatColor . YELLOW + " Reaction " + ChatColor . DARK_GRAY + " ] " ;
2020-12-25 19:46:43 +00:00
private final String LOGIN_MESSAGE_GUI_TITLE = ChatColor . DARK_GREEN + ChatColor . BOLD . toString ( ) + " Login Messages " ;
public String reactionString = " " ;
public Date reactionStartTime ;
2020-07-02 08:42:27 +00:00
public BukkitTask countdownTask ;
2020-12-25 19:46:43 +00:00
private BukkitTask reactions ;
2020-07-02 08:42:27 +00:00
private BossBar countdownBar = null ;
2019-11-02 01:28:07 +00:00
@Override
2020-07-01 01:51:06 +00:00
public void onStart ( )
2019-11-02 01:28:07 +00:00
{
2020-04-08 08:34:08 +00:00
if ( ConfigEntry . SHOP_REACTIONS_ENABLED . getBoolean ( ) )
{
2020-07-02 08:42:27 +00:00
startReactionTimer ( ) ;
}
}
public void startReactionTimer ( )
{
long interval = ConfigEntry . SHOP_REACTIONS_INTERVAL . getInteger ( ) * 20L ;
2020-04-08 08:34:08 +00:00
2020-07-02 08:42:27 +00:00
reactions = new BukkitRunnable ( )
{
@Override
public void run ( )
2020-04-08 08:34:08 +00:00
{
2020-07-02 08:42:27 +00:00
startReaction ( ) ;
}
} . runTaskLater ( plugin , interval ) ;
}
2020-04-08 08:34:08 +00:00
2020-07-02 08:42:27 +00:00
public void forceStartReaction ( )
{
reactions . cancel ( ) ;
startReaction ( ) ;
}
public void startReaction ( )
{
2020-08-05 07:01:21 +00:00
if ( ! ConfigEntry . SHOP_ENABLED . getBoolean ( ) )
{
FLog . debug ( " The shop is not enabled, therefore a reaction did not start. " ) ;
return ;
}
2020-08-08 05:51:09 +00:00
reactionString = FUtil . randomAlphanumericString ( ConfigEntry . SHOP_REACTIONS_STRING_LENGTH . getInteger ( ) ) ;
2020-07-02 08:42:27 +00:00
2020-07-07 05:21:35 +00:00
FUtil . bcastMsg ( prefix + ChatColor . AQUA + " Enter the code above to win " + ChatColor . GOLD + coinsPerReactionWin + ChatColor . AQUA + " coins! " , false ) ;
2020-07-02 08:42:27 +00:00
reactionStartTime = new Date ( ) ;
countdownBar = server . createBossBar ( reactionString , BarColor . GREEN , BarStyle . SOLID ) ;
for ( Player player : server . getOnlinePlayers ( ) )
{
countdownBar . addPlayer ( player ) ;
}
countdownBar . setVisible ( true ) ;
countdownTask = new BukkitRunnable ( )
{
double seconds = 30 ;
2020-12-25 19:46:43 +00:00
final double max = seconds ;
2020-08-05 07:01:21 +00:00
2020-07-02 08:42:27 +00:00
@Override
public void run ( )
{
if ( ( seconds - = 1 ) = = 0 )
{
endReaction ( null ) ;
}
else
2020-04-08 08:34:08 +00:00
{
2020-07-02 08:42:27 +00:00
countdownBar . setProgress ( seconds / max ) ;
if ( ! countdownBar . getColor ( ) . equals ( BarColor . YELLOW ) & & seconds / max < = 0 . 25 )
2020-04-09 03:08:37 +00:00
{
2020-07-02 08:42:27 +00:00
countdownBar . setColor ( BarColor . YELLOW ) ;
2020-04-09 03:08:37 +00:00
}
2020-04-08 08:34:08 +00:00
}
2020-07-02 08:42:27 +00:00
}
} . runTaskTimer ( plugin , 0 , 20 ) ;
}
public void endReaction ( String winner )
{
countdownTask . cancel ( ) ;
countdownBar . removeAll ( ) ;
countdownBar = null ;
reactionString = " " ;
if ( winner ! = null )
{
Date currentTime = new Date ( ) ;
long seconds = ( currentTime . getTime ( ) - reactionStartTime . getTime ( ) ) / 1000 ;
FUtil . bcastMsg ( prefix + ChatColor . GREEN + winner + ChatColor . AQUA + " won in " + seconds + " seconds! " , false ) ;
2020-07-15 08:40:48 +00:00
startReactionTimer ( ) ;
2020-07-02 08:42:27 +00:00
return ;
2020-04-08 08:34:08 +00:00
}
2020-07-02 08:42:27 +00:00
FUtil . bcastMsg ( prefix + ChatColor . RED + " No one reacted fast enough " , false ) ;
startReactionTimer ( ) ;
2019-11-02 01:28:07 +00:00
}
@Override
2020-07-01 01:51:06 +00:00
public void onStop ( )
2019-11-02 01:28:07 +00:00
{
2020-04-08 08:34:08 +00:00
if ( ConfigEntry . SHOP_REACTIONS_ENABLED . getBoolean ( ) )
{
reactions . cancel ( ) ;
}
2019-11-02 01:28:07 +00:00
}
2020-08-05 07:01:21 +00:00
2019-11-02 01:28:07 +00:00
public String getShopPrefix ( )
{
2020-04-08 02:20:01 +00:00
return FUtil . colorize ( ConfigEntry . SHOP_PREFIX . getString ( ) ) ;
}
public String getShopTitle ( )
{
return FUtil . colorize ( ConfigEntry . SHOP_TITLE . getString ( ) ) ;
2019-11-02 01:28:07 +00:00
}
2020-06-30 07:25:38 +00:00
public Inventory generateShopGUI ( PlayerData playerData )
2020-04-08 02:20:01 +00:00
{
Inventory gui = server . createInventory ( null , 36 , getShopTitle ( ) ) ;
for ( int slot = 0 ; slot < 36 ; slot + + )
2019-11-02 01:28:07 +00:00
{
2020-04-08 02:20:01 +00:00
ItemStack blank = new ItemStack ( Material . WHITE_STAINED_GLASS_PANE ) ;
ItemMeta meta = blank . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert meta ! = null ;
2020-04-08 02:20:01 +00:00
meta . setDisplayName ( " " ) ;
blank . setItemMeta ( meta ) ;
gui . setItem ( slot , blank ) ;
}
for ( ShopItem shopItem : ShopItem . values ( ) )
{
2020-06-30 07:25:38 +00:00
ItemStack item = shopGUIItem ( shopItem , playerData ) ;
2020-04-08 02:20:01 +00:00
gui . setItem ( shopItem . getSlot ( ) , item ) ;
2019-11-02 01:28:07 +00:00
}
2020-04-08 02:20:01 +00:00
// Coins
ItemStack coins = new ItemStack ( Material . GOLD_NUGGET ) ;
ItemMeta meta = coins . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert meta ! = null ;
2020-06-30 07:25:38 +00:00
meta . setDisplayName ( FUtil . colorize ( " &c&lYou have &e&l " + playerData . getCoins ( ) + " &c&l coins " ) ) ;
2020-04-08 02:20:01 +00:00
coins . setItemMeta ( meta ) ;
gui . setItem ( 35 , coins ) ;
return gui ;
}
2020-10-02 09:27:06 +00:00
public Inventory generateLoginMessageGUI ( Player player )
{
Inventory gui = server . createInventory ( null , 36 , LOGIN_MESSAGE_GUI_TITLE ) ;
int slot = 0 ;
for ( String loginMessage : ConfigEntry . SHOP_LOGIN_MESSAGES . getStringList ( ) )
{
ItemStack icon = new ItemStack ( Material . NAME_TAG ) ;
ItemMeta meta = icon . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert meta ! = null ;
2020-10-02 09:27:06 +00:00
meta . setDisplayName ( FUtil . colorize ( plugin . rm . craftLoginMessage ( player , loginMessage ) ) ) ;
icon . setItemMeta ( meta ) ;
gui . setItem ( slot , icon ) ;
slot + + ;
}
ItemStack clear = new ItemStack ( Material . BARRIER ) ;
ItemMeta meta = clear . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert meta ! = null ;
2020-10-02 09:27:06 +00:00
meta . setDisplayName ( ChatColor . RED + " Clear login message " ) ;
clear . setItemMeta ( meta ) ;
gui . setItem ( 35 , clear ) ;
return gui ;
}
2020-08-04 22:16:11 +00:00
public boolean isRealItem ( PlayerData data , ShopItem shopItem , PlayerInventory inventory , ItemStack realItem )
{
2020-12-25 19:46:43 +00:00
return isRealItem ( data , shopItem , inventory . getItemInMainHand ( ) , realItem ) | | isRealItem ( data , shopItem , inventory . getItemInOffHand ( ) , realItem ) ;
2020-08-04 22:16:11 +00:00
}
2020-06-30 07:25:38 +00:00
public boolean isRealItem ( PlayerData data , ShopItem shopItem , ItemStack givenItem , ItemStack realItem )
2020-04-08 02:20:01 +00:00
{
if ( ! data . hasItem ( shopItem ) | | ! givenItem . getType ( ) . equals ( realItem . getType ( ) ) )
{
return false ;
}
ItemMeta givenMeta = givenItem . getItemMeta ( ) ;
ItemMeta realMeta = realItem . getItemMeta ( ) ;
2019-11-02 01:28:07 +00:00
2020-12-25 19:46:43 +00:00
assert givenMeta ! = null ;
assert realMeta ! = null ;
return givenMeta . getDisplayName ( ) . equals ( realMeta . getDisplayName ( ) ) & & Objects . equals ( givenMeta . getLore ( ) , realMeta . getLore ( ) ) ;
2020-04-08 02:20:01 +00:00
}
public ItemStack getLightningRod ( )
{
ItemStack itemStack = new ItemStack ( Material . BLAZE_ROD ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-04-08 02:20:01 +00:00
itemMeta . setDisplayName ( FUtil . colorize ( " &bL&3i&bg&3h&bt&3i&bn&3g &b&bR&3o&bd " ) ) ;
itemMeta . setLore ( Arrays . asList ( ChatColor . AQUA + " Strike others down with the power of lightning. " , ChatColor . RED + ChatColor . ITALIC . toString ( ) + " The classic way to exterminate annoyances. " ) ) ;
itemMeta . addEnchant ( Enchantment . CHANNELING , 1 , false ) ;
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
public ItemStack getGrapplingHook ( )
{
ItemStack itemStack = new ItemStack ( Material . FISHING_ROD ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-04-08 02:20:01 +00:00
itemMeta . setDisplayName ( ChatColor . YELLOW + " Grappling Hook " ) ;
2020-12-25 19:46:43 +00:00
itemMeta . setLore ( Collections . singletonList ( ChatColor . GREEN + " be spider-man but ghetto " ) ) ;
2020-04-08 02:20:01 +00:00
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
public ItemStack getFireBall ( )
{
ItemStack itemStack = new ItemStack ( Material . FIRE_CHARGE ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-04-08 02:20:01 +00:00
itemMeta . setDisplayName ( ChatColor . RED + " Fire Ball " ) ;
2020-12-25 19:46:43 +00:00
itemMeta . setLore ( Collections . singletonList ( ChatColor . GOLD + " Yeet this at people " ) ) ;
2020-04-08 02:20:01 +00:00
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
public ItemStack getRideablePearl ( )
{
ItemStack itemStack = new ItemStack ( Material . ENDER_PEARL ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-04-08 02:20:01 +00:00
itemMeta . setDisplayName ( ChatColor . DARK_PURPLE + " Rideable Ender Pearl " ) ;
itemMeta . setLore ( Arrays . asList ( ChatColor . LIGHT_PURPLE + " What the title says. " , " " , ChatColor . WHITE + ChatColor . ITALIC . toString ( ) + " TotalFreedom is not responsible for any injuries " , ChatColor . WHITE + ChatColor . ITALIC . toString ( ) + " sustained while using this item. " ) ) ;
itemMeta . addEnchant ( Enchantment . BINDING_CURSE , 1 , false ) ;
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
2020-07-02 08:42:27 +00:00
public ItemStack getStackingPotato ( )
{
ItemStack itemStack = new ItemStack ( Material . POTATO ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-07-02 08:42:27 +00:00
itemMeta . setDisplayName ( ChatColor . YELLOW + " Stacking Potato " ) ;
2020-12-25 19:46:43 +00:00
itemMeta . setLore ( Collections . singletonList ( ChatColor . GREEN + " Left click to ride a mob, right click to put a mob on your head. " ) ) ;
2020-07-02 08:42:27 +00:00
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
2020-07-09 22:18:29 +00:00
public ItemStack getClownFish ( )
{
ItemStack itemStack = new ItemStack ( Material . TROPICAL_FISH ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-07-09 22:18:29 +00:00
itemMeta . setDisplayName ( ChatColor . GOLD + " Clown Fish " ) ;
2020-12-25 19:46:43 +00:00
itemMeta . setLore ( Collections . singletonList ( ChatColor . AQUA + " :clown: " ) ) ;
2020-07-09 22:18:29 +00:00
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
2020-07-26 02:59:37 +00:00
}
2020-04-08 02:20:01 +00:00
public boolean canAfford ( int price , int coins )
{
2020-12-25 19:46:43 +00:00
return coins > = price ;
2020-04-08 02:20:01 +00:00
}
public int amountNeeded ( int price , int coins )
{
return price - coins ;
}
2020-06-30 07:25:38 +00:00
public ItemStack shopGUIItem ( ShopItem item , PlayerData data )
2020-04-08 02:20:01 +00:00
{
ItemStack itemStack = new ItemStack ( item . getIcon ( ) ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
2020-12-25 19:46:43 +00:00
assert itemMeta ! = null ;
2020-04-08 02:20:01 +00:00
itemMeta . setDisplayName ( item . getColoredName ( ) ) ;
int price = item . getCost ( ) ;
int coins = data . getCoins ( ) ;
2020-12-25 19:46:43 +00:00
boolean canAfford = canAfford ( price , coins ) ;
List < String > lore = new ArrayList < > ( ) ;
2020-04-08 02:20:01 +00:00
if ( ! data . hasItem ( item ) )
{
lore . add ( ChatColor . GOLD + " Price: " + ( canAfford ? ChatColor . DARK_GREEN : ChatColor . RED ) + price ) ;
if ( ! canAfford )
2019-11-02 01:28:07 +00:00
{
2020-04-08 02:20:01 +00:00
lore . add ( ChatColor . RED + " You can not afford this item! " ) ;
lore . add ( ChatColor . RED + " You need " + amountNeeded ( price , coins ) + " more coins to buy this item. " ) ;
2019-11-02 01:28:07 +00:00
}
}
2020-04-08 02:20:01 +00:00
else
{
lore . add ( ChatColor . RED + " You already purchased this item. " ) ;
}
itemMeta . setLore ( lore ) ;
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
@EventHandler ( priority = EventPriority . HIGH )
2020-10-02 09:27:06 +00:00
public void onShopGUIClick ( InventoryClickEvent event )
2020-04-08 02:20:01 +00:00
{
if ( ! ( event . getWhoClicked ( ) instanceof Player ) )
{
return ;
}
Inventory inventory = event . getInventory ( ) ;
2020-07-07 05:21:35 +00:00
if ( inventory . getSize ( ) ! = 36 | | ! event . getView ( ) . getTitle ( ) . equals ( getShopTitle ( ) ) )
2020-04-08 02:20:01 +00:00
{
return ;
}
event . setCancelled ( true ) ;
ShopItem shopItem = getShopItem ( event . getSlot ( ) ) ;
if ( shopItem = = null )
{
return ;
}
2020-08-05 07:01:21 +00:00
Player player = ( Player ) event . getWhoClicked ( ) ;
2020-06-30 07:25:38 +00:00
PlayerData playerData = plugin . pl . getData ( player ) ;
2020-04-08 02:20:01 +00:00
int price = shopItem . getCost ( ) ;
2020-06-30 07:25:38 +00:00
int coins = playerData . getCoins ( ) ;
2020-04-08 02:20:01 +00:00
2020-07-07 05:21:35 +00:00
if ( playerData . hasItem ( shopItem ) | | ! canAfford ( price , coins ) )
2020-04-08 02:20:01 +00:00
{
return ;
}
2020-06-30 07:25:38 +00:00
playerData . giveItem ( shopItem ) ;
playerData . setCoins ( coins - price ) ;
plugin . pl . save ( playerData ) ;
2020-04-08 02:20:01 +00:00
player . closeInventory ( ) ;
2020-07-07 05:21:35 +00:00
player . sendMessage ( getShopPrefix ( ) + " " + ChatColor . GREEN + " Successfully purchased the \" " + shopItem . getColoredName ( ) + ChatColor . GREEN + " \" for " + ChatColor . GOLD + price + ChatColor . GREEN + " ! " ) ;
2020-04-08 02:20:01 +00:00
2020-07-07 08:36:29 +00:00
if ( shopItem . getCommand ( ) ! = null )
2020-04-08 02:20:01 +00:00
{
2020-07-07 08:36:29 +00:00
player . sendMessage ( ChatColor . GREEN + " Run " + shopItem . getCommand ( ) + " to get one! " ) ;
2020-07-02 08:42:27 +00:00
}
2020-04-08 02:20:01 +00:00
}
2020-10-02 09:27:06 +00:00
@EventHandler ( priority = EventPriority . HIGH )
public void onLoginMessageGUIClick ( InventoryClickEvent event )
{
if ( ! ( event . getWhoClicked ( ) instanceof Player ) )
{
return ;
}
Inventory inventory = event . getInventory ( ) ;
if ( inventory . getSize ( ) ! = 36 | | ! event . getView ( ) . getTitle ( ) . equals ( LOGIN_MESSAGE_GUI_TITLE ) )
{
return ;
}
event . setCancelled ( true ) ;
int slot = event . getSlot ( ) ;
Player player = ( Player ) event . getWhoClicked ( ) ;
PlayerData data = plugin . pl . getData ( player ) ;
if ( slot = = 35 )
{
data . setLoginMessage ( null ) ;
plugin . pl . save ( data ) ;
player . sendMessage ( ChatColor . GREEN + " Removed your login message " ) ;
}
else
{
String message = ConfigEntry . SHOP_LOGIN_MESSAGES . getStringList ( ) . get ( slot ) ;
data . setLoginMessage ( message ) ;
plugin . pl . save ( data ) ;
player . sendMessage ( ChatColor . GREEN + " Your login message is now the following: \ n " + plugin . rm . craftLoginMessage ( player , message ) ) ;
}
player . closeInventory ( ) ;
}
2020-04-08 02:20:01 +00:00
public ShopItem getShopItem ( int slot )
{
for ( ShopItem shopItem : ShopItem . values ( ) )
{
if ( shopItem . getSlot ( ) = = slot )
{
return shopItem ;
}
}
return null ;
2019-11-02 01:28:07 +00:00
}
}