mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
book lore edit start
This commit is contained in:
parent
8d558a62f0
commit
20f9de084d
2 changed files with 121 additions and 4 deletions
|
|
@ -11,6 +11,7 @@ import io.delilaheve.util.ItemUtil.repairFrom
|
|||
import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
|
||||
import io.delilaheve.util.ItemUtil.unitRepair
|
||||
import org.bukkit.ChatColor
|
||||
import org.bukkit.Material
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.event.EventHandler
|
||||
import org.bukkit.event.EventPriority
|
||||
|
|
@ -18,11 +19,9 @@ import org.bukkit.event.Listener
|
|||
import org.bukkit.event.inventory.PrepareAnvilEvent
|
||||
import org.bukkit.inventory.AnvilInventory
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.BookMeta
|
||||
import xyz.alexcrea.cuanvil.dependency.DependencyManager
|
||||
import xyz.alexcrea.cuanvil.util.AnvilColorUtil
|
||||
import xyz.alexcrea.cuanvil.util.AnvilUseType
|
||||
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
|
||||
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
|
||||
import xyz.alexcrea.cuanvil.util.*
|
||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||
/**
|
||||
* Listener for anvil events
|
||||
|
|
@ -72,6 +71,9 @@ class PrepareAnvilListener : Listener {
|
|||
// Test for unit repair
|
||||
if(testUnitRepair(event, inventory, player, first, second)) return
|
||||
|
||||
// Test for lore edit
|
||||
if(testLoreEdit(event, inventory, player, first, second)) return
|
||||
|
||||
CustomAnvil.log("no anvil fuse type found")
|
||||
event.result = null
|
||||
|
||||
|
|
@ -222,4 +224,29 @@ class PrepareAnvilListener : Listener {
|
|||
return true
|
||||
}
|
||||
|
||||
private fun testLoreEdit(event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity,
|
||||
first: ItemStack, second: ItemStack): Boolean {
|
||||
val type = second.type
|
||||
var result: ItemStack? = null
|
||||
|
||||
if(Material.WRITABLE_BOOK == type) {
|
||||
result = AnvilLoreEditUtil.tryLoreEditByBook(player, first, second)
|
||||
}
|
||||
else if(Material.PAPER == type) {
|
||||
|
||||
}
|
||||
|
||||
if(result == null || first == result) {
|
||||
CustomAnvil.log("lore edit, But input is same as output")
|
||||
event.result = null
|
||||
return false
|
||||
}
|
||||
|
||||
event.result = result
|
||||
|
||||
// TODO forgot about xp config & logic
|
||||
// AnvilXpUtil.setAnvilInvXp(inventory, event.view, player, anvilCost)
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
package xyz.alexcrea.cuanvil.util
|
||||
|
||||
import io.delilaheve.util.ConfigOptions
|
||||
import org.bukkit.entity.HumanEntity
|
||||
import org.bukkit.inventory.ItemStack
|
||||
import org.bukkit.inventory.meta.BookMeta
|
||||
import org.bukkit.permissions.Permissible
|
||||
|
||||
object AnvilLoreEditUtil {
|
||||
|
||||
private const val LORE_BY_BOOK: String = "ca.lore_edit.book"
|
||||
private const val LORE_BY_PAPER: String = "ca.lore_edit.paper"
|
||||
|
||||
private fun hasLoreEditByBookPermission(player: Permissible): Boolean {
|
||||
return ConfigOptions.BookLoreEditNeedPermission && player.hasPermission(LORE_BY_BOOK)
|
||||
}
|
||||
|
||||
private fun hasLoreEditByPaperPermission(player: Permissible): Boolean {
|
||||
return ConfigOptions.PaperLoreEditNeedPermission && player.hasPermission(LORE_BY_PAPER)
|
||||
}
|
||||
|
||||
private fun handleLoreAppendByBook(player: Permissible, first: ItemStack, book: BookMeta): ItemStack? {
|
||||
if(!hasLoreEditByBookPermission(player)) return null
|
||||
|
||||
val result = first.clone()
|
||||
val meta = result.itemMeta
|
||||
meta?.lore = book.pages[0].split("\n") //TODO check color if color is enabled
|
||||
result.itemMeta = meta
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun handleLoreRemoveByBook(player: Permissible, first: ItemStack, second: ItemStack, book: BookMeta): ItemStack? {
|
||||
if(!hasLoreEditByBookPermission(player)) return null
|
||||
|
||||
val meta = first.itemMeta
|
||||
if(meta == null || !meta.hasLore()) return null
|
||||
|
||||
val bookPage = StringBuilder()
|
||||
meta.lore!!.forEach {
|
||||
if(bookPage.isNotEmpty()) bookPage.append('\n')
|
||||
bookPage.append(it)
|
||||
}
|
||||
|
||||
val resultPage = bookPage.toString()
|
||||
//TODO maybe check page size ? bc it may be too big ???
|
||||
|
||||
val result = second.clone()
|
||||
book.setPages(resultPage)
|
||||
result.itemMeta = book
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun bookLoreEditType(second: ItemStack) : Boolean? {
|
||||
// Test if the book & quil contain content
|
||||
val meta = second.itemMeta as BookMeta
|
||||
|
||||
var hasContent = false
|
||||
if(meta.hasPages() && meta.pageCount >= 1){
|
||||
// Test if the pages is ok
|
||||
for (page in meta.pages) {
|
||||
if(page.isNotEmpty()) {
|
||||
hasContent = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// We don't want to "add" the first page is there is content and the first page is empty
|
||||
if(hasContent){
|
||||
if(meta.pages[0].isEmpty()) return null
|
||||
if(ConfigOptions.appendLoreBookAndQuil)
|
||||
return true
|
||||
}
|
||||
else if(ConfigOptions.removeLoreBookAndQuil) {
|
||||
return false
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
fun tryLoreEditByBook(player: HumanEntity, first: ItemStack, second: ItemStack): ItemStack? {
|
||||
val bookType = bookLoreEditType(second) ?: return null
|
||||
|
||||
val meta = second.itemMeta as BookMeta
|
||||
return if(bookType) handleLoreAppendByBook(player, first, meta)
|
||||
else handleLoreRemoveByBook(player, first, second, meta)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue