2023-07-27 03:28:26 +02:00
package com.craftaro.ultimatekits.kit ;
import com.craftaro.core.compatibility.CompatibleHand ;
import com.craftaro.core.configuration.Config ;
import com.craftaro.core.gui.Gui ;
import com.craftaro.core.gui.GuiManager ;
import com.craftaro.core.hooks.EconomyManager ;
import com.craftaro.core.third_party.de.tr7zw.nbtapi.NBTItem ;
import com.craftaro.core.utils.ItemUtils ;
import com.craftaro.core.utils.TextUtils ;
2023-08-25 12:10:37 +02:00
import com.craftaro.core.utils.TimeUtils ;
2024-01-13 22:26:07 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XSound ;
2023-07-27 03:28:26 +02:00
import com.craftaro.ultimatekits.UltimateKits ;
import com.craftaro.ultimatekits.category.Category ;
import com.craftaro.ultimatekits.crate.Crate ;
import com.craftaro.ultimatekits.gui.AnimatedKitGui ;
import com.craftaro.ultimatekits.gui.ConfirmBuyGui ;
2023-08-25 12:10:37 +02:00
import com.craftaro.ultimatekits.gui.PreviewKitGui ;
import com.craftaro.ultimatekits.key.Key ;
import com.craftaro.ultimatekits.kit.type.KitContentCommand ;
import com.craftaro.ultimatekits.kit.type.KitContentEconomy ;
2023-07-27 03:28:26 +02:00
import com.craftaro.ultimatekits.settings.Settings ;
import com.craftaro.ultimatekits.utils.ArmorType ;
2018-05-06 01:32:21 +02:00
import org.bukkit.Bukkit ;
import org.bukkit.ChatColor ;
import org.bukkit.Material ;
import org.bukkit.entity.Player ;
import org.bukkit.inventory.ItemStack ;
import org.bukkit.inventory.meta.ItemMeta ;
2019-10-12 20:45:24 +02:00
2020-09-08 17:35:18 +02:00
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import java.util.Objects ;
2018-05-06 01:32:21 +02:00
/ * *
* Created by songoda on 2 / 24 / 2017 .
* /
2021-03-08 16:54:49 +01:00
public class Kit implements Cloneable {
2018-05-06 01:32:21 +02:00
2021-03-08 16:54:49 +01:00
private String key ;
2021-03-10 17:23:48 +01:00
private String name ;
2020-01-20 03:30:51 +01:00
private Category category = null ;
2019-10-12 20:45:24 +02:00
private static UltimateKits plugin ;
private double price = 0 ;
private String link , title = null ;
private long delay = 0L ;
private boolean hidden = false ;
2021-03-10 17:23:48 +01:00
private ItemStack displayItem = null ;
2019-10-12 20:45:24 +02:00
private List < KitItem > contents = new ArrayList < > ( ) ;
private KitAnimation kitAnimation = KitAnimation . NONE ;
2018-05-06 01:32:21 +02:00
2020-01-20 03:30:51 +01:00
public Kit ( String key ) {
2023-08-25 12:10:37 +02:00
if ( plugin = = null ) {
2019-10-12 20:45:24 +02:00
plugin = UltimateKits . getInstance ( ) ;
2023-08-25 12:10:37 +02:00
}
2020-01-20 03:30:51 +01:00
this . key = key ;
this . name = TextUtils . formatText ( key , true ) ;
2018-05-06 01:32:21 +02:00
}
2024-03-11 23:35:40 +01:00
public boolean hasRoom ( Player player , int itemAmount ) {
2018-05-06 01:32:21 +02:00
int space = 0 ;
2024-03-11 23:35:40 +01:00
for ( ItemStack content : player . getInventory ( ) . getContents ( ) )
if ( content = = null )
2018-05-06 01:32:21 +02:00
space + + ;
2020-05-01 00:25:11 +02:00
// Since roulette only gives one item, we don't need to check if the user has room for the whole kit.
2023-08-25 12:10:37 +02:00
if ( this . kitAnimation = = KitAnimation . ROULETTE & & space > = 1 ) {
2020-05-01 00:25:11 +02:00
return true ;
2023-08-25 12:10:37 +02:00
}
2018-05-17 08:40:59 +02:00
2020-04-19 11:38:26 +02:00
return space > = itemAmount ;
2018-05-06 01:32:21 +02:00
}
public void saveKit ( List < ItemStack > items ) {
2019-07-21 04:45:10 +02:00
List < KitItem > list = new ArrayList < > ( ) ;
for ( ItemStack is : items ) {
if ( is ! = null & & is . getType ( ) ! = Material . AIR ) {
if ( is . getItemMeta ( ) . hasLore ( ) ) {
ItemMeta meta = is . getItemMeta ( ) ;
List < String > newLore = new ArrayList < > ( ) ;
for ( String line : meta . getLore ( ) ) {
2023-08-25 12:10:37 +02:00
if ( line . equals ( TextUtils . formatText ( " &8---- " ) ) ) {
break ;
}
2019-07-21 04:45:10 +02:00
newLore . add ( line ) ;
2018-10-24 23:56:23 +02:00
}
2019-07-21 04:45:10 +02:00
meta . setLore ( newLore ) ;
is . setItemMeta ( meta ) ;
}
2018-10-24 23:56:23 +02:00
2019-11-28 02:54:43 +01:00
if ( is . getType ( ) = = Material . PAPER & & is . getItemMeta ( ) . getDisplayName ( )
. equals ( UltimateKits . getInstance ( ) . getLocale ( ) . getMessage ( " general.type.command " )
. getMessage ( ) ) ) {
2019-07-21 04:45:10 +02:00
StringBuilder command = new StringBuilder ( ) ;
for ( String line : is . getItemMeta ( ) . getLore ( ) ) {
command . append ( line ) ;
2018-05-06 01:32:21 +02:00
}
2019-07-21 04:45:10 +02:00
list . add ( new KitItem ( is , ChatColor . stripColor ( command . toString ( ) ) ) ) ;
2019-11-28 02:54:43 +01:00
} else if ( is . getType ( ) = = Material . PAPER & & is . getItemMeta ( ) . getDisplayName ( )
. equals ( UltimateKits . getInstance ( ) . getLocale ( ) . getMessage ( " general.type.money " )
. getMessage ( ) ) ) {
2019-07-21 04:45:10 +02:00
String money = is . getItemMeta ( ) . getLore ( ) . get ( 0 ) ;
list . add ( new KitItem ( is , ChatColor . stripColor ( money ) ) ) ;
} else {
list . add ( new KitItem ( is ) ) ;
2018-05-06 01:32:21 +02:00
}
}
}
2023-08-25 12:10:37 +02:00
this . contents = list ;
2019-11-05 12:10:18 +01:00
plugin . saveKits ( false ) ;
2018-05-06 01:32:21 +02:00
}
2018-10-26 05:07:47 +02:00
public List < ItemStack > getReadableContents ( Player player , boolean preview , boolean commands , boolean moveable ) {
2018-05-06 01:32:21 +02:00
List < ItemStack > stacks = new ArrayList < > ( ) ;
2019-07-21 04:45:10 +02:00
for ( KitItem item : getContents ( ) ) {
2019-09-12 00:04:29 +02:00
if ( ( ! item . getSerialized ( ) . startsWith ( " / " ) & & ! item . getSerialized ( ) . startsWith ( Settings . CURRENCY_SYMBOL . getString ( ) ) ) | | commands ) { //ToDO: I doubt this is correct.
2019-07-21 04:45:10 +02:00
ItemStack stack = moveable ? item . getMoveableItem ( ) : item . getItem ( ) ;
2023-08-25 12:10:37 +02:00
if ( preview ) {
2024-04-01 01:06:12 +02:00
stack = item . getItemForDisplay ( this ) ;
2023-08-25 12:10:37 +02:00
}
if ( stack = = null ) {
continue ;
}
2019-07-21 04:45:10 +02:00
ItemStack fin = stack ;
if ( Bukkit . getPluginManager ( ) . isPluginEnabled ( " PlaceholderAPI " ) & & stack . getItemMeta ( ) . getLore ( ) ! = null ) {
ArrayList < String > lore2 = new ArrayList < > ( ) ;
ItemMeta meta2 = stack . getItemMeta ( ) ;
for ( String lor : stack . getItemMeta ( ) . getLore ( ) ) {
2019-09-16 16:07:33 +02:00
lor = me . clip . placeholderapi . PlaceholderAPI . setPlaceholders ( player , lor . replace ( " " , " _ " ) ) . replace ( " _ " , " " ) ;
2019-07-21 04:45:10 +02:00
lore2 . add ( lor ) ;
2018-05-06 01:32:21 +02:00
}
2019-07-21 04:45:10 +02:00
meta2 . setLore ( lore2 ) ;
fin . setItemMeta ( meta2 ) ;
2018-05-06 01:32:21 +02:00
}
2019-07-21 04:45:10 +02:00
stacks . add ( fin ) ;
2018-05-06 01:32:21 +02:00
}
}
return stacks ;
}
public void updateDelay ( Player player ) {
2023-08-25 12:10:37 +02:00
plugin . getDataFile ( ) . set ( " Kits. " + this . key + " .delays. " + player . getUniqueId ( ) , System . currentTimeMillis ( ) ) ;
2018-05-06 01:32:21 +02:00
}
public Long getNextUse ( Player player ) {
2023-08-25 12:10:37 +02:00
String configSectionPlayer = " Kits. " + this . key + " .delays. " + player . getUniqueId ( ) ;
2019-09-12 00:04:29 +02:00
Config config = plugin . getDataFile ( ) ;
2018-05-06 01:32:21 +02:00
if ( ! config . contains ( configSectionPlayer ) ) {
return 0L ;
2023-08-25 12:10:37 +02:00
} else if ( this . delay = = - 1 ) {
return - 1L ;
}
2018-05-06 01:32:21 +02:00
long last = config . getLong ( configSectionPlayer ) ;
2020-04-30 18:23:28 +02:00
long delay = this . delay * 1000 ;
2018-05-06 01:32:21 +02:00
return ( last + delay ) > = System . currentTimeMillis ( ) ? ( last + delay ) - System . currentTimeMillis ( ) : 0L ;
}
2020-04-30 18:23:28 +02:00
public boolean hasPermissionToClaim ( Player player ) {
2023-08-25 12:10:37 +02:00
return player . hasPermission ( " ultimatekits.claim. " + this . key . toLowerCase ( ) ) ;
2020-04-30 18:23:28 +02:00
}
public boolean hasPermissionToPreview ( Player player ) {
2023-08-25 12:10:37 +02:00
return player . hasPermission ( " ultimatekits.preview. " + this . key . toLowerCase ( ) ) ;
2020-04-30 18:23:28 +02:00
}
public boolean hasPermissionToBuy ( Player player ) {
2023-08-25 12:10:37 +02:00
return player . hasPermission ( " ultimatekits.buy. " + this . key . toLowerCase ( ) ) ;
2018-05-06 01:32:21 +02:00
}
public double getPrice ( ) {
2023-08-25 12:10:37 +02:00
return this . price ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setPrice ( double price ) {
2018-05-06 01:32:21 +02:00
this . price = price ;
2019-10-12 20:45:24 +02:00
return this ;
2018-05-06 01:32:21 +02:00
}
public String getLink ( ) {
2023-08-25 12:10:37 +02:00
return this . link ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setLink ( String link ) {
2018-05-06 01:32:21 +02:00
this . link = link ;
2019-10-12 20:45:24 +02:00
return this ;
2018-05-06 01:32:21 +02:00
}
public String getTitle ( ) {
2023-08-25 12:10:37 +02:00
return this . title ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setTitle ( String title ) {
2018-05-06 01:32:21 +02:00
this . title = title ;
2019-10-12 20:45:24 +02:00
return this ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:09:05 +02:00
public long getDelay ( ) {
2023-08-25 12:10:37 +02:00
return this . delay ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setDelay ( long delay ) {
2018-05-06 01:32:21 +02:00
this . delay = delay ;
2019-10-12 20:45:24 +02:00
return this ;
2018-05-06 01:32:21 +02:00
}
2020-01-20 03:30:51 +01:00
public Category getCategory ( ) {
2023-08-25 12:10:37 +02:00
return this . category ;
2020-01-20 03:30:51 +01:00
}
public Kit setCategory ( Category category ) {
this . category = category ;
return this ;
}
2018-10-24 23:56:23 +02:00
public List < KitItem > getContents ( ) {
2024-03-11 23:35:40 +01:00
return Collections . unmodifiableList ( this . contents ) ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setContents ( List < KitItem > contents ) {
2018-10-22 23:41:15 +02:00
this . contents = contents ;
2019-10-12 20:45:24 +02:00
return this ;
2018-10-22 23:41:15 +02:00
}
2020-01-20 03:30:51 +01:00
public String getKey ( ) {
2023-08-25 12:10:37 +02:00
return this . key ;
2018-05-06 01:32:21 +02:00
}
2020-01-20 03:30:51 +01:00
public String getName ( ) {
2023-08-25 12:10:37 +02:00
return this . name ;
2018-05-06 01:32:21 +02:00
}
2024-04-01 01:06:12 +02:00
public boolean all100Percent ( ) {
for ( KitItem item : this . contents ) {
if ( item . getChance ( ) ! = 100 ) {
return false ;
}
}
return true ;
}
2021-03-10 17:23:48 +01:00
public ItemStack getDisplayItem ( ) {
2023-08-25 12:10:37 +02:00
return this . displayItem ;
2018-05-06 01:32:21 +02:00
}
2021-03-10 17:23:48 +01:00
public Kit setDisplayItem ( ItemStack item ) {
this . displayItem = item ;
2019-10-12 20:45:24 +02:00
return this ;
}
2018-05-06 01:32:21 +02:00
public boolean isHidden ( ) {
2023-08-25 12:10:37 +02:00
return this . hidden ;
2018-05-06 01:32:21 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setHidden ( boolean hidden ) {
2018-05-06 01:32:21 +02:00
this . hidden = hidden ;
2019-10-12 20:45:24 +02:00
return this ;
2018-05-06 01:32:21 +02:00
}
2018-10-24 23:56:23 +02:00
public KitAnimation getKitAnimation ( ) {
2023-08-25 12:10:37 +02:00
return this . kitAnimation ;
2018-10-24 23:56:23 +02:00
}
2019-10-12 20:45:24 +02:00
public Kit setKitAnimation ( KitAnimation kitAnimation ) {
2018-10-24 23:56:23 +02:00
this . kitAnimation = kitAnimation ;
2019-10-12 20:45:24 +02:00
return this ;
2018-10-24 23:56:23 +02:00
}
2021-03-08 16:54:49 +01:00
public Kit clone ( String key ) {
try {
Kit newKit = ( Kit ) super . clone ( ) ;
List < KitItem > contents = new ArrayList < > ( ) ;
2023-08-25 12:10:37 +02:00
for ( KitItem item : newKit . contents ) {
2021-03-08 16:54:49 +01:00
contents . add ( item . clone ( ) ) ;
2023-08-25 12:10:37 +02:00
}
2021-03-08 16:54:49 +01:00
newKit . setContents ( contents ) ;
newKit . key = key ;
newKit . name = TextUtils . formatText ( key , true ) ;
return newKit ;
} catch ( CloneNotSupportedException e ) {
return null ;
}
}
2018-05-06 01:32:21 +02:00
@Override
public int hashCode ( ) {
2023-08-25 12:10:37 +02:00
return 31 * ( this . key ! = null ? this . key . hashCode ( ) : 0 ) ;
2018-05-06 01:32:21 +02:00
}
@Override
2023-08-25 12:10:37 +02:00
public boolean equals ( Object obj ) {
if ( this = = obj ) {
return true ;
}
if ( ! ( obj instanceof Kit ) ) {
return false ;
}
2018-05-06 01:32:21 +02:00
2023-08-25 12:10:37 +02:00
Kit kit = ( Kit ) obj ;
return Objects . equals ( this . key , kit . key ) ;
2018-05-06 01:32:21 +02:00
}
}