Possibly fix tool issues

This commit is contained in:
TomTom 2024-05-28 17:38:40 +02:00
parent 3e6b86d7df
commit 9ba7a44ebd

View File

@ -7,7 +7,6 @@ import com.artillexstudios.axapi.events.PacketEntityInteractEvent
import com.artillexstudios.axapi.hologram.Hologram import com.artillexstudios.axapi.hologram.Hologram
import com.artillexstudios.axapi.hologram.HologramLine import com.artillexstudios.axapi.hologram.HologramLine
import com.artillexstudios.axapi.scheduler.Scheduler import com.artillexstudios.axapi.scheduler.Scheduler
import com.artillexstudios.axapi.serializers.Serializers
import com.artillexstudios.axapi.utils.EquipmentSlot import com.artillexstudios.axapi.utils.EquipmentSlot
import com.artillexstudios.axapi.utils.ItemBuilder import com.artillexstudios.axapi.utils.ItemBuilder
import com.artillexstudios.axapi.utils.RotationType import com.artillexstudios.axapi.utils.RotationType
@ -27,10 +26,6 @@ import com.artillexstudios.axminions.api.utils.fastFor
import com.artillexstudios.axminions.api.warnings.Warning import com.artillexstudios.axminions.api.warnings.Warning
import com.artillexstudios.axminions.api.warnings.Warnings import com.artillexstudios.axminions.api.warnings.Warnings
import com.artillexstudios.axminions.listeners.LinkingListener import com.artillexstudios.axminions.listeners.LinkingListener
import java.text.NumberFormat
import java.util.Locale
import java.util.UUID
import java.util.concurrent.atomic.AtomicBoolean
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import org.bukkit.Bukkit import org.bukkit.Bukkit
import org.bukkit.Location import org.bukkit.Location
@ -46,6 +41,9 @@ import org.bukkit.inventory.meta.Damageable
import org.bukkit.inventory.meta.ItemMeta import org.bukkit.inventory.meta.ItemMeta
import org.bukkit.persistence.PersistentDataType import org.bukkit.persistence.PersistentDataType
import org.bukkit.util.EulerAngle import org.bukkit.util.EulerAngle
import java.text.NumberFormat
import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
class Minion( class Minion(
private var location: Location, private var location: Location,
@ -324,7 +322,10 @@ class Minion(
} else if (it.equals("charge")) { } else if (it.equals("charge")) {
if (Config.CHARGE_ENABLED()) { if (Config.CHARGE_ENABLED()) {
val charge = Placeholder.parsed("charge", TimeUtils.format(charge - System.currentTimeMillis())) val charge = Placeholder.parsed("charge", TimeUtils.format(charge - System.currentTimeMillis()))
item = ItemBuilder(AxMinionsAPI.INSTANCE.getConfig().getConfig().getSection("gui.items.$it"), charge).get() item = ItemBuilder(
AxMinionsAPI.INSTANCE.getConfig().getConfig().getSection("gui.items.$it"),
charge
).get()
val meta = item.itemMeta!! val meta = item.itemMeta!!
meta.persistentDataContainer.set(Keys.GUI, PersistentDataType.STRING, it) meta.persistentDataContainer.set(Keys.GUI, PersistentDataType.STRING, it)
@ -337,7 +338,9 @@ class Minion(
val linked = Placeholder.unparsed( val linked = Placeholder.unparsed(
"linked", when (linkedChest) { "linked", when (linkedChest) {
null -> "---" null -> "---"
else -> Messages.LOCATION_FORMAT().replace("<world>", location.world!!.name).replace("<x>", location.blockX.toString()).replace("<y>", location.blockY.toString()).replace("<z>", location.blockZ.toString()) else -> Messages.LOCATION_FORMAT().replace("<world>", location.world!!.name)
.replace("<x>", location.blockX.toString()).replace("<y>", location.blockY.toString())
.replace("<z>", location.blockZ.toString())
} }
) )
item = ItemBuilder( item = ItemBuilder(
@ -649,113 +652,138 @@ class Minion(
dirty = true dirty = true
} }
override fun damageTool(amount: Int) { override fun damageTool(amount: Int) {
if (!Config.USE_DURABILITY()) return if (!Config.USE_DURABILITY()) return
val tool = tool ?: return
val toolMeta = toolMeta as? Damageable ?: return
if (notDurable.contains(tool?.type) && !(tool?.type?.isAir ?: return)) { if (!tool.type.isAir && notDurable.contains(tool.type)) {
return return
} }
val meta = toolMeta as? Damageable ?: return val maxDurability = tool.type.maxDurability
val damage = toolMeta.damage
val remaining = maxDurability - damage
if (toolMeta?.isUnbreakable == true) return if (Math.random() > 1f / (toolMeta.getEnchantLevel(Enchantment.DURABILITY) + 1)) return
if (Math.random() > 1f / (meta.getEnchantLevel(Enchantment.DURABILITY) + 1)) return if (remaining > 1) {
// We can damage the tool
if ((tool?.type?.maxDurability ?: return) <= meta.damage + amount) { toolMeta.damage += amount
tool.itemMeta = toolMeta
updateInventories()
return
} else if (remaining == 1) {
// Tool is breaking
if (Config.CAN_BREAK_TOOLS()) { if (Config.CAN_BREAK_TOOLS()) {
if (Config.PULL_FROM_CHEST()) { if (Config.PULL_FROM_CHEST()) {
val allowedTools = arrayListOf<Material>() val item = pullFromChest()
getType().getConfig().getStringList("tool.material").fastFor { setTool(item)
allowedTools.add(Material.matchMaterial(it) ?: return@fastFor)
}
linkedInventory?.contents?.fastFor { if (!tool.type.isAir && notDurable.contains(tool.type)) {
if (it == null || it.type !in allowedTools) return@fastFor return
}
setTool(it)
linkedInventory?.remove(it) if (!item.type.isAir && (item.itemMeta as? Damageable ?: return).damage + 1 > item.type.maxDurability) {
return return
} }
setTool(ItemStack(Material.AIR))
} else { } else {
setTool(ItemStack(Material.AIR)) setTool(ItemStack(Material.AIR))
return
} }
} else if (Config.PULL_FROM_CHEST()) { } else {
val allowedTools = arrayListOf<Material>() if (Config.PULL_FROM_CHEST()) {
getType().getConfig().getStringList("tool.material").fastFor { val item = pullFromChest()
allowedTools.add(Material.matchMaterial(it) ?: return@fastFor) setTool(item)
if (!tool.type.isAir && notDurable.contains(tool.type)) {
return
} }
linkedInventory?.contents?.fastFor { if (!item.type.isAir && (item.itemMeta as? Damageable ?: return).damage + 1 > item.type.maxDurability) {
if (it == null || it.type !in allowedTools) return@fastFor
linkedInventory?.addItem(getTool())
setTool(it)
linkedInventory?.remove(it)
return return
} }
} }
} else {
meta.damage += amount return
tool?.itemMeta = meta
updateInventories()
} }
} }
}
override fun canUseTool(): Boolean { override fun canUseTool(): Boolean {
if (notDurable.contains(tool?.type) && !(tool?.type?.isAir ?: return false)) { val tool = tool ?: return false
val toolMeta = toolMeta as? Damageable ?: return false
if (!tool.type.isAir && notDurable.contains(tool.type)) {
return true return true
} }
val meta = toolMeta ?: return false if (!Config.USE_DURABILITY()) {
if (!Config.USE_DURABILITY() && meta is Damageable) {
return true return true
} }
if (meta is Damageable) { val maxDurability = tool.type.maxDurability
if ((tool?.type?.maxDurability ?: return false) <= meta.damage + 1) { val damage = toolMeta.damage
val remaining = maxDurability - damage
if (remaining > 1) {
// We can damage the tool
return true
} else if (remaining == 1) {
// Tool is breaking
if (Config.CAN_BREAK_TOOLS()) { if (Config.CAN_BREAK_TOOLS()) {
if (Config.PULL_FROM_CHEST()) { if (Config.PULL_FROM_CHEST()) {
val allowedTools = arrayListOf<Material>() val item = pullFromChest()
getType().getConfig().getStringList("tool.material").fastFor { setTool(item)
allowedTools.add(Material.matchMaterial(it) ?: return@fastFor)
}
linkedInventory?.contents?.fastFor { if (!tool.type.isAir && notDurable.contains(tool.type)) {
if (it == null || it.type !in allowedTools) return@fastFor
setTool(it)
linkedInventory?.remove(it)
return true return true
} }
setTool(ItemStack(Material.AIR))
if (!item.type.isAir && (item.itemMeta as? Damageable ?: return false).damage + 1 > item.type.maxDurability) {
return canUseTool()
}
} else { } else {
setTool(ItemStack(Material.AIR)) setTool(ItemStack(Material.AIR))
}
} else if (Config.PULL_FROM_CHEST()) {
val allowedTools = arrayListOf<Material>()
getType().getConfig().getStringList("tool.material").fastFor {
allowedTools.add(Material.matchMaterial(it) ?: return@fastFor)
}
linkedInventory?.contents?.fastFor {
if (it == null || it.type !in allowedTools) return@fastFor
linkedInventory?.addItem(getTool())
setTool(it)
linkedInventory?.remove(it)
return true
}
}
return false return false
}
} else { } else {
if (Config.PULL_FROM_CHEST()) {
val item = pullFromChest()
setTool(item)
if (!tool.type.isAir && notDurable.contains(tool.type)) {
return true return true
} }
if (!item.type.isAir && (item.itemMeta as? Damageable ?: return false).damage + 1 > item.type.maxDurability) {
return canUseTool()
}
} }
return true return false
}
}
return false
}
private fun pullFromChest(): ItemStack {
val allowedTools = arrayListOf<Material>()
getType().getConfig().getStringList("tool.material").fastFor {
allowedTools.add(Material.matchMaterial(it) ?: return@fastFor)
}
linkedInventory?.contents?.fastFor {
if (it == null || it.type !in allowedTools) return@fastFor
linkedInventory?.remove(it)
return it
}
return ItemStack(Material.AIR)
} }
override fun isOwnerOnline(): Boolean { override fun isOwnerOnline(): Boolean {