Skip to content

Commit

Permalink
v4.1.3 b68
Browse files Browse the repository at this point in the history
* fix nbt-data not working on items
* update nbt-data methods to use the latest API functions which should be more performant
  • Loading branch information
stumper66 committed Oct 1, 2024
1 parent a5697e9 commit 340d8c4
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 88 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
version = 4.1.3 b67
version = 4.1.3 b68
description = The Ultimate RPG Mob Levelling Solution
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import io.github.arcaneplugins.levelledmobs.misc.CachedModalList
import io.github.arcaneplugins.levelledmobs.misc.CustomUniversalGroups
import io.github.arcaneplugins.levelledmobs.debug.DebugType
import io.github.arcaneplugins.levelledmobs.misc.YmlParsingHelper
import io.github.arcaneplugins.levelledmobs.result.NBTApplyResult
import io.github.arcaneplugins.levelledmobs.rules.MinAndMax
import io.github.arcaneplugins.levelledmobs.util.Log
import io.github.arcaneplugins.levelledmobs.util.MessageUtils.colorizeAll
import io.github.arcaneplugins.levelledmobs.util.MiscUtils
import io.github.arcaneplugins.levelledmobs.util.Utils
import java.util.Locale
import java.util.SortedMap
Expand Down Expand Up @@ -571,17 +571,19 @@ class CustomDropsParser(
item.nbtData = ymlHelper.getString("nbt-data", defaults.nbtData)
if (item.material != Material.AIR && !item.nbtData.isNullOrEmpty()) {
if (ExternalCompatibilityManager.hasNbtApiInstalled) {
val result: NBTApplyResult = NBTManager.applyNBTDataItem(item, item.nbtData!!)
val result = NBTManager.applyNBTDataItem(item, item.nbtData!!)
if (result.hadException)
"custom drop ${item.material} for ${dropInstance.getMobOrGroupName()} has invalid NBT data: ${result.exceptionMessage}"
hadError("custom drop ${item.material} for ${dropInstance.getMobOrGroupName()} has invalid NBT data: ${result.exceptionMessage}")
else if (result.itemStack != null) {
item.itemStack = result.itemStack
this.dropsUtilizeNBTAPI = true

DebugManager.log(DebugType.NBT_APPLICATION) {
"Applied NBT data, ${MiscUtils.getNBTDebugMessage(mutableListOf(result))}"
}
}
} else if (!hasMentionedNBTAPIMissing) {
hadError(
"NBT Data has been specified in customdrops.yml but required plugin NBTAPI is not installed!"
)
hadError("NBT Data has been specified in customdrops.yml but required plugin NBTAPI is not installed!")
hasMentionedNBTAPIMissing = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import io.github.arcaneplugins.levelledmobs.rules.RulesManager
import io.github.arcaneplugins.levelledmobs.rules.strategies.RandomVarianceGenerator
import io.github.arcaneplugins.levelledmobs.rules.strategies.StrategyType
import io.github.arcaneplugins.levelledmobs.util.Log
import io.github.arcaneplugins.levelledmobs.util.MiscUtils
import io.github.arcaneplugins.levelledmobs.util.MythicMobUtils
import io.github.arcaneplugins.levelledmobs.util.Utils
import io.github.arcaneplugins.levelledmobs.wrappers.LivingEntityWrapper
Expand Down Expand Up @@ -68,8 +69,6 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataType
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.roundToInt

Expand Down Expand Up @@ -2095,69 +2094,11 @@ class LevelManager : LevelInterface2 {

if (hadSuccess) {
DebugManager.log(DebugType.NBT_APPLICATION, lmEntity, true) {
"Applied NBT data, ${getNBTDebugMessage(allResults)}"
"Applied NBT data, ${MiscUtils.getNBTDebugMessage(allResults)}"
}
}
}

private fun getNBTDebugMessage(
results: MutableList<NBTApplyResult>
): String {
val sb = StringBuilder()

for (result in results) {
if (result.objectsAdded == null) {
continue
}

for (i in 0 until result.objectsAdded!!.size) {
if (i > 0) {
sb.append(", ")
} else {
sb.append("added: ")
}

sb.append(result.objectsAdded!![i])
}
}

for (result in results) {
if (result.objectsUpdated == null) {
continue
}

for (i in 0 until result.objectsUpdated!!.size) {
if (i > 0 || sb.isNotEmpty()) {
sb.append(", ")
}
if (i == 0) {
sb.append("updated: ")
}

sb.append(result.objectsUpdated!![i])
}
}

for (result in results) {
if (result.objectsRemoved == null) {
continue
}

for (i in 0 until result.objectsRemoved!!.size) {
if (i > 0 || sb.isNotEmpty()) {
sb.append(", ")
}
if (i == 0) {
sb.append("removed: ")
}

sb.append(result.objectsRemoved!![i])
}
}

return if (sb.isEmpty()) "" else sb.toString()
}

/**
* Check if a LivingEntity is a levelled mob or not. This is determined *after*
* MobPreLevelEvent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.github.arcaneplugins.levelledmobs.debug.DebugType
import io.github.arcaneplugins.levelledmobs.misc.EvaluationException
import io.github.arcaneplugins.levelledmobs.misc.QueueItem
import io.github.arcaneplugins.levelledmobs.util.Log
import io.github.arcaneplugins.levelledmobs.wrappers.SchedulerWrapper
import java.util.UUID
import java.util.concurrent.atomic.AtomicInteger
import org.bukkit.Bukkit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package io.github.arcaneplugins.levelledmobs.managers

import com.google.gson.JsonParser
import de.tr7zw.nbtapi.NBTContainer
import de.tr7zw.nbtapi.NBTEntity
import de.tr7zw.nbtapi.NBTItem
import de.tr7zw.nbtapi.NBT
import de.tr7zw.nbtapi.iface.ReadWriteNBT
import io.github.arcaneplugins.levelledmobs.LevelledMobs
import io.github.arcaneplugins.levelledmobs.customdrops.CustomDropItem
import io.github.arcaneplugins.levelledmobs.debug.DebugType
Expand All @@ -20,11 +19,25 @@ object NBTManager {
nbtStuff: String
): NBTApplyResult {
val result = NBTApplyResult()
val nbtent = NBTItem(item.itemStack)
var itemNbt: ReadWriteNBT? = null
var jsonBefore: String? = null

if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(DebugType.NBT_APPLICATION)){
itemNbt = NBT.createNBTObject()
NBT.get(item.itemStack, itemNbt::mergeCompound)
jsonBefore = itemNbt.toString()
}

try {
nbtent.mergeCompound(NBTContainer(nbtStuff))
result.itemStack = nbtent.item
NBT.modify(item.itemStack){ nbt -> nbt.mergeCompound(NBT.parseNBT(nbtStuff)) }

if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(DebugType.NBT_APPLICATION)){
NBT.get(item.itemStack, itemNbt!!::mergeCompound)
result.itemStack = item.itemStack

val jsonAfter = itemNbt.toString()
formulateChangedJson(jsonBefore!!, jsonAfter, result)
}
} catch (e: Exception) {
result.exceptionMessage = e.message
}
Expand All @@ -37,22 +50,25 @@ object NBTManager {
nbtStuff: String
): NBTApplyResult {
val result = NBTApplyResult()
var jsonBefore: String? = null
var entityNbt: ReadWriteNBT? = null

try {
val nbtent = NBTEntity(lmEntity.livingEntity)
val jsonBefore = nbtent.toString()
nbtent.mergeCompound(NBTContainer(nbtStuff))
val jsonAfter = nbtent.toString()

if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(
DebugType.NBT_APPLICATION
)
) {
showChangedJson(jsonBefore, jsonAfter, result)
if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(DebugType.NBT_APPLICATION)){
entityNbt = NBT.createNBTObject()
NBT.get(lmEntity.livingEntity, entityNbt::mergeCompound)
jsonBefore = entityNbt.toString()
}

if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(DebugType.NBT_APPLICATION) && jsonBefore == jsonAfter) {
result.exceptionMessage = "No NBT data changed. Make sure you have used proper NBT strings"
NBT.modify(lmEntity.livingEntity){ nbt -> nbt.mergeCompound(NBT.parseNBT(nbtStuff)) }

if (LevelledMobs.instance.debugManager.isDebugTypeEnabled(DebugType.NBT_APPLICATION)){
NBT.get(lmEntity.livingEntity, entityNbt!!::mergeCompound)
val jsonAfter = entityNbt.toString()
formulateChangedJson(jsonBefore!!, jsonAfter, result)

if (jsonBefore == jsonAfter)
result.exceptionMessage = "No NBT data changed. Make sure you have used proper NBT strings"
}
} catch (e: Exception) {
result.exceptionMessage = e.message
Expand All @@ -61,7 +77,7 @@ object NBTManager {
return result
}

private fun showChangedJson(
private fun formulateChangedJson(
jsonBefore: String,
jsonAfter: String,
applyResult: NBTApplyResult
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.arcaneplugins.levelledmobs.util

import io.github.arcaneplugins.levelledmobs.LevelledMobs
import io.github.arcaneplugins.levelledmobs.result.NBTApplyResult
import org.bukkit.entity.LivingEntity

/**
Expand Down Expand Up @@ -114,4 +115,62 @@ object MiscUtils {

return results.toSortedMap()
}

fun getNBTDebugMessage(
results: MutableList<NBTApplyResult>
): String {
val sb = StringBuilder()

for (result in results) {
if (result.objectsAdded == null) {
continue
}

for (i in 0 until result.objectsAdded!!.size) {
if (i > 0) {
sb.append(", ")
} else {
sb.append("added: ")
}

sb.append(result.objectsAdded!![i])
}
}

for (result in results) {
if (result.objectsUpdated == null) {
continue
}

for (i in 0 until result.objectsUpdated!!.size) {
if (i > 0 || sb.isNotEmpty()) {
sb.append(", ")
}
if (i == 0) {
sb.append("updated: ")
}

sb.append(result.objectsUpdated!![i])
}
}

for (result in results) {
if (result.objectsRemoved == null) {
continue
}

for (i in 0 until result.objectsRemoved!!.size) {
if (i > 0 || sb.isNotEmpty()) {
sb.append(", ")
}
if (i == 0) {
sb.append("removed: ")
}

sb.append(result.objectsRemoved!![i])
}
}

return if (sb.isEmpty()) "" else sb.toString()
}
}

0 comments on commit 340d8c4

Please sign in to comment.