2019-11-02 01:28:07 +00:00
package me.totalfreedom.totalfreedommod.shop ;
import com.google.common.collect.Maps ;
2020-04-08 02:20:01 +00:00
import com.vexsoftware.votifier.model.Vote ;
import com.vexsoftware.votifier.model.VotifierEvent ;
2019-11-02 01:28:07 +00:00
import java.io.File ;
2020-04-08 02:20:01 +00:00
import java.util.ArrayList ;
import java.util.Arrays ;
2019-11-02 01:28:07 +00:00
import java.util.Collection ;
2020-04-08 08:34:08 +00:00
import java.util.Date ;
2020-04-08 02:20:01 +00:00
import java.util.List ;
2019-11-02 01:28:07 +00:00
import java.util.Map ;
2020-04-08 02:20:01 +00:00
import java.util.UUID ;
2019-11-02 01:28:07 +00:00
import lombok.Getter ;
2020-04-09 03:08:37 +00:00
import me.rayzr522.jsonmessage.JSONMessage ;
2019-11-02 01:28:07 +00:00
import me.totalfreedom.totalfreedommod.FreedomService ;
import me.totalfreedom.totalfreedommod.TotalFreedomMod ;
import me.totalfreedom.totalfreedommod.config.ConfigEntry ;
import me.totalfreedom.totalfreedommod.util.FLog ;
import me.totalfreedom.totalfreedommod.util.FUtil ;
import net.pravian.aero.config.YamlConfig ;
import net.pravian.aero.util.Ips ;
import org.bukkit.ChatColor ;
2020-04-08 02:20:01 +00:00
import org.bukkit.Material ;
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-04-08 02:20:01 +00:00
import org.bukkit.event.inventory.InventoryClickEvent ;
2019-11-02 01:28:07 +00:00
import org.bukkit.event.player.PlayerQuitEvent ;
2020-04-08 02:20:01 +00:00
import org.bukkit.inventory.Inventory ;
import org.bukkit.inventory.ItemStack ;
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
{
@Getter
2020-04-09 03:08:37 +00:00
public final Map < UUID , ShopData > dataMap = Maps . newHashMap ( ) ;
2019-11-02 01:28:07 +00:00
@Getter
private final File configFolder ;
2020-04-08 08:34:08 +00:00
private BukkitTask reactions ;
public String reactionString = " " ;
public Date reactionStartTime ;
public final int coinsPerReactionWin = ConfigEntry . SHOP_REACTIONS_COINS_PER_WIN . getInteger ( ) ;
2019-11-02 01:28:07 +00:00
public Shop ( TotalFreedomMod plugin )
{
super ( plugin ) ;
this . configFolder = new File ( plugin . getDataFolder ( ) , " shopdata " ) ;
}
@Override
protected void onStart ( )
{
dataMap . clear ( ) ;
2020-04-08 08:34:08 +00:00
if ( ConfigEntry . SHOP_REACTIONS_ENABLED . getBoolean ( ) )
{
long interval = ConfigEntry . SHOP_REACTIONS_INTERVAL . getInteger ( ) * 20L ;
reactions = new BukkitRunnable ( )
{
@Override
public void run ( )
{
reactionString = FUtil . randomString ( ConfigEntry . SHOP_REACTIONS_STRING_LENGTH . getInteger ( ) ) ;
2020-04-09 03:08:37 +00:00
for ( Player player : server . getOnlinePlayers ( ) )
{
String reactionMessage = ChatColor . DARK_GRAY + " [ " + ChatColor . YELLOW + " Reaction " + ChatColor . DARK_GRAY + " ] "
+ ChatColor . AQUA + " Hover over this message or click on it and type the "
+ ChatColor . AQUA + " string to win " + ChatColor . GOLD + plugin . sh . coinsPerReactionWin + ChatColor . AQUA + " coins! " ;
JSONMessage . create ( reactionMessage )
. tooltip ( ChatColor . DARK_AQUA + reactionString )
. runCommand ( " /reactionbar " )
. send ( player ) ;
reactionStartTime = new Date ( ) ;
}
2020-04-08 08:34:08 +00:00
}
} . runTaskTimer ( plugin , interval , interval ) ;
}
2019-11-02 01:28:07 +00:00
}
@Override
protected void onStop ( )
{
for ( ShopData sd : dataMap . values ( ) )
{
save ( sd ) ;
}
2020-04-08 08:34:08 +00:00
if ( ConfigEntry . SHOP_REACTIONS_ENABLED . getBoolean ( ) )
{
reactions . cancel ( ) ;
}
2019-11-02 01:28:07 +00:00
}
public void save ( ShopData data )
{
YamlConfig config = getConfig ( data ) ;
data . saveTo ( config ) ;
config . save ( ) ;
2020-04-08 02:20:01 +00:00
dataMap . remove ( data . getUUID ( ) ) ;
dataMap . put ( data . getUUID ( ) , data ) ;
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
}
// May not return null
public ShopData getData ( Player player )
{
// Check already loaded
2020-04-08 02:20:01 +00:00
ShopData data = dataMap . get ( player . getUniqueId ( ) ) ;
2019-11-02 01:28:07 +00:00
if ( data ! = null )
{
return data ;
}
// Load data
2020-04-08 02:20:01 +00:00
data = getData ( player . getUniqueId ( ) ) ;
2019-11-02 01:28:07 +00:00
// Create data if nonexistent
if ( data = = null )
{
FLog . info ( " Creating new shop data entry for " + player . getName ( ) ) ;
// Create new player
data = new ShopData ( player ) ;
2020-04-08 02:20:01 +00:00
data . setUsername ( player . getName ( ) ) ;
2019-11-02 01:28:07 +00:00
// Set defaults
data . setCoins ( 0 ) ;
// Store player
2020-04-08 02:20:01 +00:00
dataMap . put ( player . getUniqueId ( ) , data ) ;
2019-11-02 01:28:07 +00:00
// Save player
YamlConfig config = getConfig ( data ) ;
data . saveTo ( config ) ;
config . save ( ) ;
}
2020-04-08 02:20:01 +00:00
dataMap . put ( player . getUniqueId ( ) , data ) ;
2019-11-02 01:28:07 +00:00
return data ;
}
public ShopData getData ( String username )
{
2020-04-08 02:20:01 +00:00
UUID uuid = FUtil . nameToUUID ( username ) ;
if ( uuid ! = null )
{
return getData ( uuid ) ;
}
return null ;
}
2019-11-02 01:28:07 +00:00
2020-04-08 02:20:01 +00:00
public ShopData getData ( UUID uuid )
{
2019-11-02 01:28:07 +00:00
// Check if the player is a known player
2020-04-08 02:20:01 +00:00
final File configFile = getConfigFile ( uuid ) ;
2019-11-02 01:28:07 +00:00
if ( ! configFile . exists ( ) )
{
return null ;
}
2020-04-08 02:20:01 +00:00
// Load entry
final ShopData data = new ShopData ( uuid ) ;
2019-11-02 01:28:07 +00:00
data . loadFrom ( getConfig ( data ) ) ;
2020-04-08 02:20:01 +00:00
return data ;
}
2019-11-02 01:28:07 +00:00
2020-04-08 02:20:01 +00:00
public Inventory generateShopGUI ( ShopData shopData )
{
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 ( ) ;
meta . setDisplayName ( " " ) ;
blank . setItemMeta ( meta ) ;
gui . setItem ( slot , blank ) ;
}
for ( ShopItem shopItem : ShopItem . values ( ) )
{
ItemStack item = shopGUIItem ( shopItem , shopData ) ;
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 ( ) ;
meta . setDisplayName ( FUtil . colorize ( " &c&lYou have &e&l " + shopData . getCoins ( ) + " &c&l coins " ) ) ;
coins . setItemMeta ( meta ) ;
gui . setItem ( 35 , coins ) ;
return gui ;
}
public boolean isRealItem ( ShopData data , ShopItem shopItem , ItemStack givenItem , ItemStack realItem )
{
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-04-08 02:20:01 +00:00
if ( givenMeta . getDisplayName ( ) . equals ( realMeta . getDisplayName ( ) ) & & givenMeta . getLore ( ) . equals ( realMeta . getLore ( ) ) )
2019-11-02 01:28:07 +00:00
{
2020-04-08 02:20:01 +00:00
return true ;
}
return false ;
}
public ItemStack getLightningRod ( )
{
ItemStack itemStack = new ItemStack ( Material . BLAZE_ROD ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
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 ( ) ;
itemMeta . setDisplayName ( ChatColor . YELLOW + " Grappling Hook " ) ;
itemMeta . setLore ( Arrays . asList ( ChatColor . GREEN + " be spider-man but ghetto " ) ) ;
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
public ItemStack getFireBall ( )
{
ItemStack itemStack = new ItemStack ( Material . FIRE_CHARGE ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
itemMeta . setDisplayName ( ChatColor . RED + " Fire Ball " ) ;
itemMeta . setLore ( Arrays . asList ( ChatColor . GOLD + " Yeet this at people " ) ) ;
itemStack . setItemMeta ( itemMeta ) ;
return itemStack ;
}
public ItemStack getRideablePearl ( )
{
ItemStack itemStack = new ItemStack ( Material . ENDER_PEARL ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
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 ;
}
public boolean canAfford ( int price , int coins )
{
if ( coins > = price )
{
return true ;
}
return false ;
}
public int amountNeeded ( int price , int coins )
{
return price - coins ;
}
public ItemStack shopGUIItem ( ShopItem item , ShopData data )
{
ItemStack itemStack = new ItemStack ( item . getIcon ( ) ) ;
ItemMeta itemMeta = itemStack . getItemMeta ( ) ;
itemMeta . setDisplayName ( item . getColoredName ( ) ) ;
int price = item . getCost ( ) ;
int coins = data . getCoins ( ) ;
Boolean canAfford = canAfford ( price , coins ) ;
List < String > lore = new ArrayList ( ) ;
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 )
public void onInventoryClick ( InventoryClickEvent event )
{
if ( ! ( event . getWhoClicked ( ) instanceof Player ) )
{
return ;
}
Inventory inventory = event . getInventory ( ) ;
if ( inventory . getSize ( ) ! = 36 | | ! event . getView ( ) . getTitle ( ) . equals ( plugin . sh . getShopTitle ( ) ) )
{
return ;
}
event . setCancelled ( true ) ;
ShopItem shopItem = getShopItem ( event . getSlot ( ) ) ;
if ( shopItem = = null )
{
return ;
}
Player player = ( Player ) event . getWhoClicked ( ) ;
ShopData shopData = plugin . sh . getData ( player ) ;
int price = shopItem . getCost ( ) ;
int coins = shopData . getCoins ( ) ;
if ( shopData . hasItem ( shopItem ) | | ! plugin . sh . canAfford ( price , coins ) )
{
return ;
}
shopData . giveItem ( shopItem ) ;
shopData . setCoins ( coins - price ) ;
save ( shopData ) ;
player . closeInventory ( ) ;
player . sendMessage ( plugin . sh . getShopPrefix ( ) + " " + ChatColor . GREEN + " Successfully purchased the \" " + shopItem . getColoredName ( ) + ChatColor . GREEN + " \" for " + ChatColor . GOLD + price + ChatColor . GREEN + " ! " ) ;
if ( shopItem . equals ( ShopItem . GRAPPLING_HOOK ) )
{
player . sendMessage ( ChatColor . GREEN + " Run /grapplinghook to get one! " ) ;
}
else if ( shopItem . equals ( ShopItem . LIGHTNING_ROD ) )
{
player . sendMessage ( ChatColor . GREEN + " Run /lightningrod to get one! " ) ;
}
else if ( shopItem . equals ( ShopItem . FIRE_BALL ) )
{
player . sendMessage ( ChatColor . GREEN + " Run /fireball to get one! " ) ;
}
else if ( shopItem . equals ( ShopItem . RIDEABLE_PEARL ) )
{
player . sendMessage ( ChatColor . GREEN + " Run /rideablepearl to get one! " ) ;
}
2019-11-02 01:28:07 +00:00
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
}
@EventHandler ( priority = EventPriority . MONITOR , ignoreCancelled = true )
public void onPlayerQuit ( PlayerQuitEvent event )
{
final String ip = Ips . getIp ( event . getPlayer ( ) ) ;
dataMap . remove ( ip ) ;
}
2020-04-08 02:20:01 +00:00
@EventHandler ( priority = EventPriority . NORMAL )
public void onPlayerVote ( VotifierEvent event )
{
Vote vote = event . getVote ( ) ;
String name = vote . getUsername ( ) ;
int coinsPerVote = ConfigEntry . SHOP_COINS_PER_VOTE . getInteger ( ) ;
Player player = server . getPlayer ( name ) ;
ShopData data = null ;
if ( player ! = null )
{
data = plugin . sh . getData ( player ) ;
}
else
{
data = plugin . sh . getData ( name ) ;
}
if ( data ! = null )
{
data . setCoins ( data . getCoins ( ) + coinsPerVote ) ;
data . setTotalVotes ( data . getTotalVotes ( ) + 1 ) ;
save ( data ) ;
FUtil . bcastMsg ( ChatColor . GREEN + name + ChatColor . AQUA + " has voted for us on " + ChatColor . GREEN + vote . getServiceName ( ) + ChatColor . AQUA + " ! " ) ;
}
if ( player ! = null )
{
player . sendMessage ( ChatColor . GREEN + " Thank you for voting for us! Here are " + coinsPerVote + " coins! " ) ;
}
}
2019-11-02 01:28:07 +00:00
public Collection < ShopData > getLoadedData ( )
{
return dataMap . values ( ) ;
}
2020-04-08 02:20:01 +00:00
protected File getConfigFile ( UUID uuid )
2019-11-02 01:28:07 +00:00
{
2020-04-08 02:20:01 +00:00
return new File ( getConfigFolder ( ) , uuid + " .yml " ) ;
2019-11-02 01:28:07 +00:00
}
protected YamlConfig getConfig ( ShopData data )
{
2020-04-08 02:20:01 +00:00
final YamlConfig config = new YamlConfig ( plugin , getConfigFile ( data . getUUID ( ) ) , false ) ;
2019-11-02 01:28:07 +00:00
config . load ( ) ;
return config ;
}
}