mirror of
https://github.com/alexcrea/CustomAnvil.git
synced 2026-06-23 16:16:17 +02:00
paper lore edit partially works
This commit is contained in:
parent
b25e3961c2
commit
3217a9e4cc
3 changed files with 152 additions and 14 deletions
|
|
@ -27,6 +27,7 @@ import xyz.alexcrea.cuanvil.util.AnvilXpUtil
|
||||||
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
|
import xyz.alexcrea.cuanvil.util.CustomRecipeUtil
|
||||||
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.collections.ArrayList
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
class AnvilResultListener : Listener {
|
class AnvilResultListener : Listener {
|
||||||
|
|
@ -105,8 +106,8 @@ class AnvilResultListener : Listener {
|
||||||
// For lore edit
|
// For lore edit
|
||||||
if (handleBookLoreEdit(event, inventory, player, leftItem, rightItem, output)) {
|
if (handleBookLoreEdit(event, inventory, player, leftItem, rightItem, output)) {
|
||||||
return
|
return
|
||||||
} else if (Material.PAPER == rightItem.type) {
|
} else if (handlePaperLoreEdit(event, inventory, player, leftItem, rightItem, output)) {
|
||||||
//TODO
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Else there was no working situation somehow so we deny
|
// Else there was no working situation somehow so we deny
|
||||||
|
|
@ -315,14 +316,14 @@ class AnvilResultListener : Listener {
|
||||||
output: ItemStack,
|
output: ItemStack,
|
||||||
): Boolean {
|
): Boolean {
|
||||||
if (Material.WRITABLE_BOOK != rightItem.type) return false
|
if (Material.WRITABLE_BOOK != rightItem.type) return false
|
||||||
val bookMeta = rightItem.itemMeta as BookMeta
|
val bookMeta = rightItem.itemMeta as BookMeta? ?: return false
|
||||||
|
|
||||||
val editType = AnvilLoreEditUtil.bookLoreEditTypeAppend(leftItem, rightItem) ?: return false
|
val editType = AnvilLoreEditUtil.bookLoreEditIsAppend(leftItem, rightItem) ?: return false
|
||||||
|
|
||||||
if (editType) {
|
if (editType) {
|
||||||
if (output != AnvilLoreEditUtil.handleLoreAppendByBook(player, leftItem, bookMeta)) return false
|
if (output != AnvilLoreEditUtil.handleLoreAppendByBook(player, leftItem, bookMeta)) return false
|
||||||
|
|
||||||
// Remove pages to
|
// Remove pages to book
|
||||||
val bookCopy = rightItem.clone()
|
val bookCopy = rightItem.clone()
|
||||||
bookMeta.pages = Collections.emptyList()
|
bookMeta.pages = Collections.emptyList()
|
||||||
bookCopy.itemMeta = bookMeta
|
bookCopy.itemMeta = bookMeta
|
||||||
|
|
@ -338,8 +339,8 @@ class AnvilResultListener : Listener {
|
||||||
|
|
||||||
// remove lore
|
// remove lore
|
||||||
val leftCopy = leftItem.clone()
|
val leftCopy = leftItem.clone()
|
||||||
val leftMeta = leftCopy.itemMeta
|
val leftMeta = leftCopy.itemMeta!!
|
||||||
leftMeta!!.lore = null
|
leftMeta.lore = null
|
||||||
leftCopy.itemMeta = leftMeta
|
leftCopy.itemMeta = leftMeta
|
||||||
|
|
||||||
return extractAnvilResult(
|
return extractAnvilResult(
|
||||||
|
|
@ -351,6 +352,59 @@ class AnvilResultListener : Listener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun handlePaperLoreEdit(
|
||||||
|
event: InventoryClickEvent,
|
||||||
|
inventory: AnvilInventory,
|
||||||
|
player: Player,
|
||||||
|
leftItem: ItemStack,
|
||||||
|
rightItem: ItemStack,
|
||||||
|
output: ItemStack,
|
||||||
|
): Boolean {
|
||||||
|
if (Material.PAPER != rightItem.type) return false
|
||||||
|
val paperMeta = rightItem.itemMeta ?: return false
|
||||||
|
|
||||||
|
val editType = AnvilLoreEditUtil.paperLoreEditIsAppend(leftItem, rightItem) ?: return false
|
||||||
|
|
||||||
|
if (editType) {
|
||||||
|
if (output != AnvilLoreEditUtil.handleLoreAppendByPaper(player, leftItem, rightItem)) return false
|
||||||
|
|
||||||
|
// Remove custom name to paper
|
||||||
|
val paperCopy = rightItem.clone()
|
||||||
|
paperMeta.setDisplayName(null)
|
||||||
|
paperCopy.itemMeta = paperMeta
|
||||||
|
|
||||||
|
return extractAnvilResult(
|
||||||
|
event, player, inventory,
|
||||||
|
leftItem, 1,
|
||||||
|
paperCopy, 0,
|
||||||
|
output, 0
|
||||||
|
) //TODO DO REPAIR COST
|
||||||
|
} else {
|
||||||
|
if (output != AnvilLoreEditUtil.handleLoreRemoveByPaper(player, leftItem, rightItem)) return false
|
||||||
|
|
||||||
|
// remove lore line
|
||||||
|
val leftCopy = leftItem.clone()
|
||||||
|
val leftMeta = leftCopy.itemMeta!!
|
||||||
|
|
||||||
|
val removeEnd = ConfigOptions.paperLoreOrderIsEnd
|
||||||
|
val lore: ArrayList<String> = ArrayList(leftMeta.lore!!)
|
||||||
|
|
||||||
|
if(removeEnd) lore.removeAt(lore.size - 1)
|
||||||
|
else lore.removeAt(0)
|
||||||
|
|
||||||
|
leftMeta.lore = if(lore.isEmpty()) null else lore
|
||||||
|
leftCopy.itemMeta = leftMeta
|
||||||
|
|
||||||
|
return extractAnvilResult(
|
||||||
|
event, player, inventory,
|
||||||
|
leftCopy, 0,
|
||||||
|
rightItem, 1,
|
||||||
|
output, 0
|
||||||
|
) //TODO DO REPAIR COST
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the destination slot or "NO_SLOT" slot container if there is no slot available
|
* Get the destination slot or "NO_SLOT" slot container if there is no slot available
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ import io.delilaheve.util.ItemUtil.setEnchantmentsUnsafe
|
||||||
import io.delilaheve.util.ItemUtil.unitRepair
|
import io.delilaheve.util.ItemUtil.unitRepair
|
||||||
import org.bukkit.ChatColor
|
import org.bukkit.ChatColor
|
||||||
import org.bukkit.Material
|
import org.bukkit.Material
|
||||||
|
import org.bukkit.entity.AbstractVillager
|
||||||
import org.bukkit.entity.HumanEntity
|
import org.bukkit.entity.HumanEntity
|
||||||
import org.bukkit.event.EventHandler
|
import org.bukkit.event.EventHandler
|
||||||
import org.bukkit.event.EventPriority
|
import org.bukkit.event.EventPriority
|
||||||
|
|
@ -233,7 +234,7 @@ class PrepareAnvilListener : Listener {
|
||||||
result = AnvilLoreEditUtil.tryLoreEditByBook(player, first, second)
|
result = AnvilLoreEditUtil.tryLoreEditByBook(player, first, second)
|
||||||
}
|
}
|
||||||
else if(Material.PAPER == type) {
|
else if(Material.PAPER == type) {
|
||||||
|
result = AnvilLoreEditUtil.tryLoreEditByPaper(player, first, second)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(result == null || first == result) {
|
if(result == null || first == result) {
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,14 @@ object AnvilLoreEditUtil {
|
||||||
|
|
||||||
val result = first.clone()
|
val result = first.clone()
|
||||||
val meta = result.itemMeta
|
val meta = result.itemMeta
|
||||||
//TODO take into account previous lore
|
val lore = if (meta?.hasLore() == true) {
|
||||||
meta?.lore = book.pages[0].split("\n") //TODO check color if color is enabled
|
ArrayList<String>(meta.lore!!)
|
||||||
|
} else ArrayList()
|
||||||
|
|
||||||
|
//TODO check color if color if enabled
|
||||||
|
lore.addAll(book.pages[0].split("\n"))
|
||||||
|
|
||||||
|
meta?.lore = lore
|
||||||
result.itemMeta = meta
|
result.itemMeta = meta
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
@ -36,10 +42,13 @@ object AnvilLoreEditUtil {
|
||||||
|
|
||||||
val meta = first.itemMeta
|
val meta = first.itemMeta
|
||||||
if (meta == null || !meta.hasLore()) return null
|
if (meta == null || !meta.hasLore()) return null
|
||||||
|
val lore = meta.lore!!
|
||||||
|
if(lore.isEmpty()) return null
|
||||||
|
|
||||||
val bookPage = StringBuilder()
|
val bookPage = StringBuilder()
|
||||||
meta.lore!!.forEach {
|
lore.forEach {
|
||||||
if (bookPage.isNotEmpty()) bookPage.append('\n')
|
if (bookPage.isNotEmpty()) bookPage.append('\n')
|
||||||
|
//TODO check & do color
|
||||||
bookPage.append(it)
|
bookPage.append(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -54,9 +63,9 @@ object AnvilLoreEditUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return true if append, false if remove, null if neither
|
// Return true if append, false if remove, null if neither
|
||||||
fun bookLoreEditTypeAppend(first: ItemStack, second: ItemStack): Boolean? {
|
fun bookLoreEditIsAppend(first: ItemStack, second: ItemStack): Boolean? {
|
||||||
// Test if the book & quil contain content
|
// Test if the book & quil contain content
|
||||||
val meta = second.itemMeta as BookMeta
|
val meta = second.itemMeta as BookMeta? ?: return false
|
||||||
|
|
||||||
var hasContent = false
|
var hasContent = false
|
||||||
if (meta.hasPages() && meta.pageCount >= 1) {
|
if (meta.hasPages() && meta.pageCount >= 1) {
|
||||||
|
|
@ -85,11 +94,85 @@ object AnvilLoreEditUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun tryLoreEditByBook(player: HumanEntity, first: ItemStack, second: ItemStack): ItemStack? {
|
fun tryLoreEditByBook(player: HumanEntity, first: ItemStack, second: ItemStack): ItemStack? {
|
||||||
val bookType = bookLoreEditTypeAppend(first, second) ?: return null
|
val bookType = bookLoreEditIsAppend(first, second) ?: return null
|
||||||
|
|
||||||
val meta = second.itemMeta as BookMeta
|
val meta = second.itemMeta as BookMeta
|
||||||
return if (bookType) handleLoreAppendByBook(player, first, meta)
|
return if (bookType) handleLoreAppendByBook(player, first, meta)
|
||||||
else handleLoreRemoveByBook(player, first, second, meta)
|
else handleLoreRemoveByBook(player, first, second, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return true if append, false if remove, null if neither
|
||||||
|
fun paperLoreEditIsAppend(first: ItemStack, second: ItemStack): Boolean? {
|
||||||
|
// Test if the paper contain a display name
|
||||||
|
val meta = second.itemMeta ?: return false
|
||||||
|
|
||||||
|
val hasContent = meta.hasDisplayName()
|
||||||
|
if (hasContent) {
|
||||||
|
if (ConfigOptions.appendLoreBookAndQuil)
|
||||||
|
return true
|
||||||
|
} else if (ConfigOptions.removeLoreBookAndQuil) {
|
||||||
|
if (!first.hasItemMeta()) return null
|
||||||
|
|
||||||
|
val leftMeta = first.itemMeta!!
|
||||||
|
return if (leftMeta.hasLore() && leftMeta.lore!!.isNotEmpty()) false
|
||||||
|
else null
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handleLoreAppendByPaper(player: Permissible, first: ItemStack, second: ItemStack): ItemStack? {
|
||||||
|
if (!hasLoreEditByPaperPermission(player)) return null
|
||||||
|
|
||||||
|
val result = first.clone()
|
||||||
|
val meta = result.itemMeta
|
||||||
|
val lore = if (meta?.hasLore() == true) {
|
||||||
|
ArrayList<String>(meta.lore!!)
|
||||||
|
} else ArrayList()
|
||||||
|
|
||||||
|
val appendEnd = ConfigOptions.paperLoreOrderIsEnd
|
||||||
|
|
||||||
|
//TODO check color if color if enabled
|
||||||
|
val line = second.itemMeta!!.displayName
|
||||||
|
if(appendEnd)
|
||||||
|
lore.add(line)
|
||||||
|
else
|
||||||
|
lore.add(0, line)
|
||||||
|
|
||||||
|
meta?.lore = lore
|
||||||
|
result.itemMeta = meta
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handleLoreRemoveByPaper(player: Permissible, first: ItemStack, second: ItemStack): ItemStack? {
|
||||||
|
if (!hasLoreEditByPaperPermission(player)) return null
|
||||||
|
|
||||||
|
val meta = first.itemMeta
|
||||||
|
if (meta == null || !meta.hasLore()) return null
|
||||||
|
val lore = meta.lore!!
|
||||||
|
if(lore.isEmpty()) return null
|
||||||
|
|
||||||
|
val removeEnd = ConfigOptions.paperLoreOrderIsEnd
|
||||||
|
//TODO check & do color
|
||||||
|
val line = if(removeEnd) lore[lore.size-1]
|
||||||
|
else lore[0]
|
||||||
|
|
||||||
|
// Create result item
|
||||||
|
val result = second.clone()
|
||||||
|
result.amount = 1
|
||||||
|
|
||||||
|
val resultMeta = result.itemMeta ?: return null
|
||||||
|
resultMeta.setDisplayName(line)
|
||||||
|
result.itemMeta = resultMeta
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
fun tryLoreEditByPaper(player: HumanEntity, first: ItemStack, second: ItemStack): ItemStack? {
|
||||||
|
val bookType = paperLoreEditIsAppend(first, second) ?: return null
|
||||||
|
|
||||||
|
return if (bookType) handleLoreAppendByPaper(player, first, second)
|
||||||
|
else handleLoreRemoveByPaper(player, first, second)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue