2023-07-25 11:39:40 +02:00
package com.craftaro.epicenchants.listeners.item ;
2019-02-19 14:23:20 +01:00
2024-01-11 16:09:04 +01:00
import com.craftaro.third_party.com.cryptomorin.xseries.XMaterial ;
2023-06-25 12:50:56 +02:00
import com.craftaro.core.third_party.de.tr7zw.nbtapi.NBTItem ;
2023-07-25 11:39:40 +02:00
import com.craftaro.epicenchants.events.EnchantApplyEvent ;
import com.craftaro.epicenchants.objects.Enchant ;
import com.craftaro.epicenchants.objects.Group ;
import com.craftaro.epicenchants.EpicEnchants ;
import com.craftaro.epicenchants.enums.EnchantResult ;
import com.craftaro.epicenchants.utils.Tuple ;
import com.craftaro.epicenchants.utils.single.GeneralUtils ;
2019-02-19 14:23:20 +01:00
import org.bukkit.Bukkit ;
2020-03-21 06:30:13 +01:00
import org.bukkit.entity.Player ;
2019-02-19 14:23:20 +01:00
import org.bukkit.event.inventory.InventoryClickEvent ;
import org.bukkit.event.player.PlayerInteractEvent ;
import org.bukkit.inventory.ItemStack ;
import java.util.Optional ;
2023-07-25 11:39:40 +02:00
import static com.craftaro.epicenchants.enums.EnchantResult.ALREADY_APPLIED ;
import static com.craftaro.epicenchants.enums.EnchantResult.BROKEN_FAILURE ;
import static com.craftaro.epicenchants.enums.EnchantResult.CONFLICT ;
import static com.craftaro.epicenchants.enums.EnchantResult.MAXED_OUT ;
2020-03-21 15:03:17 +01:00
import static java.util.concurrent.ThreadLocalRandom.current ;
2019-02-19 14:23:20 +01:00
public class BookListener extends ItemListener {
public BookListener ( EpicEnchants instance ) {
super ( instance ) ;
}
@Override
void onApply ( InventoryClickEvent event , NBTItem cursor , NBTItem current ) {
2023-06-25 13:50:27 +02:00
if ( ! cursor . hasTag ( " book-item " ) | | ! cursor . getBoolean ( " book-item " ) ) {
2019-02-19 14:23:20 +01:00
return ;
}
event . setCancelled ( true ) ;
ItemStack toApply = event . getCurrentItem ( ) ;
2023-06-25 13:50:27 +02:00
Enchant enchant = this . instance . getEnchantManager ( ) . getValue ( cursor . getString ( " enchant " ) ) . orElseThrow ( ( ) - > new IllegalStateException ( " Book without enchant! " ) ) ;
2019-02-19 14:23:20 +01:00
2023-07-25 11:39:40 +02:00
if ( ! enchant . getItemWhitelist ( ) . contains ( XMaterial . matchXMaterial ( current . getItem ( ) . getType ( ) ) ) ) {
2019-02-19 14:23:20 +01:00
return ;
}
2020-03-21 06:30:13 +01:00
// get total amount of enchantments on item
2023-06-25 13:50:27 +02:00
int currentEnchantmentTotal = this . instance . getEnchantUtils ( ) . getEnchants ( toApply ) . size ( ) ;
int maxAllowedOverride = this . instance . getEnchantUtils ( ) . getMaximumEnchantsCanApply ( ( Player ) event . getWhoClicked ( ) ) ;
int maxAllowedApply = this . instance . getEnchantUtils ( ) . getMaximumEnchantsCanApplyItem ( toApply , ( Player ) event . getWhoClicked ( ) ) ;
2021-01-19 22:29:49 +01:00
maxAllowedApply = Math . min ( maxAllowedApply , maxAllowedOverride ) ;
2020-03-21 06:30:13 +01:00
// item is at max enchantments
if ( currentEnchantmentTotal > = maxAllowedApply ) {
2023-06-25 13:50:27 +02:00
this . instance . getLocale ( ) . getMessage ( " enchants.maxallowed " ) . processPlaceholder ( " max_enchants " , maxAllowedApply ) . sendPrefixedMessage ( event . getWhoClicked ( ) ) ;
2020-03-21 06:30:13 +01:00
return ;
}
2022-03-18 20:35:14 +01:00
int level = cursor . getInteger ( " level " ) ;
int successRate = cursor . getInteger ( " success-rate " ) ;
int destroyRate = cursor . getInteger ( " destroy-rate " ) ;
2019-02-19 14:23:20 +01:00
EnchantApplyEvent enchantEvent = new EnchantApplyEvent ( toApply , enchant , level , successRate , destroyRate ) ;
Bukkit . getPluginManager ( ) . callEvent ( enchantEvent ) ;
if ( enchantEvent . isCancelled ( ) ) {
return ;
}
2023-06-25 13:50:27 +02:00
Tuple < ItemStack , EnchantResult > result = this . instance . getEnchantUtils ( ) . apply ( toApply , enchant , enchantEvent . getLevel ( ) , enchantEvent . getSuccessRate ( ) , enchantEvent . getDestroyRate ( ) ) ;
2019-02-19 14:23:20 +01:00
2023-06-25 13:50:27 +02:00
this . instance . getLocale ( ) . getMessage ( GeneralUtils . getMessageFromResult ( result . getRight ( ) ) )
2019-08-04 23:50:07 +02:00
. processPlaceholder ( " enchant " , enchant . getIdentifier ( ) )
. sendPrefixedMessage ( event . getWhoClicked ( ) ) ;
2019-02-19 14:23:20 +01:00
if ( result . getRight ( ) = = BROKEN_FAILURE ) {
event . getClickedInventory ( ) . clear ( event . getSlot ( ) ) ;
}
if ( result . getRight ( ) ! = CONFLICT & & result . getRight ( ) ! = MAXED_OUT & & result . getRight ( ) ! = ALREADY_APPLIED ) {
useItem ( event ) ;
}
event . getClickedInventory ( ) . setItem ( event . getSlot ( ) , result . getLeft ( ) ) ;
}
@Override
void onClick ( PlayerInteractEvent event , NBTItem clicked ) {
2023-06-25 13:50:27 +02:00
if ( ! clicked . hasTag ( " mystery-book " ) | | ! clicked . getBoolean ( " mystery-book " ) ) {
2019-02-19 14:23:20 +01:00
return ;
}
event . setCancelled ( true ) ;
2019-03-19 14:13:27 +01:00
if ( event . getItem ( ) . getAmount ( ) ! = 1 & & event . getPlayer ( ) . getInventory ( ) . firstEmpty ( ) = = - 1 ) {
2019-02-19 14:23:20 +01:00
return ;
}
2023-06-25 13:50:27 +02:00
Group group = this . instance . getGroupManager ( ) . getValue ( clicked . getString ( " group " ) ) . orElseThrow ( ( ) - > new IllegalStateException ( " Book without group! " ) ) ;
2019-03-21 15:35:12 +01:00
2023-06-25 13:50:27 +02:00
Optional < Enchant > enchant = this . instance . getEnchantManager ( ) . getRandomEnchant ( group ) ;
2019-02-19 14:23:20 +01:00
if ( ! enchant . isPresent ( ) ) {
2019-03-19 14:13:27 +01:00
throw new IllegalStateException ( " The " + group . getName ( ) + " group does not have any enchants. " ) ;
2019-02-19 14:23:20 +01:00
}
2020-03-21 15:03:17 +01:00
int level = current ( ) . nextInt ( enchant . get ( ) . getMaxLevel ( ) ) + 1 ;
2019-02-19 14:23:20 +01:00
useItem ( event ) ;
2020-03-21 15:03:17 +01:00
event . getPlayer ( ) . getInventory ( ) . addItem ( enchant . get ( ) . getBook ( ) . get ( enchant . get ( ) , level ) ) ;
2019-03-19 14:13:27 +01:00
2023-06-25 13:50:27 +02:00
event . getPlayer ( ) . sendMessage ( this . instance . getLocale ( ) . getMessage ( " book.discover " )
2019-08-04 23:50:07 +02:00
. processPlaceholder ( " group_name " , group . getName ( ) )
. processPlaceholder ( " group_color " , group . getColor ( ) )
. processPlaceholder ( " enchant_format " , enchant . get ( ) . getFormat ( ) )
2020-03-21 15:03:17 +01:00
. processPlaceholder ( " level " , level )
2021-01-19 23:35:39 +01:00
. getPrefixedMessage ( ) ) ;
2019-02-19 14:23:20 +01:00
}
}