Skip to content

Commit

Permalink
Replace getItemData with getItemBehaviors debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Sep 26, 2024
1 parent 2632fae commit 4b6d4c1
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 22 deletions.
31 changes: 15 additions & 16 deletions nova/src/main/kotlin/xyz/xenondevs/nova/command/impl/NovaCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ import xyz.xenondevs.nova.util.data.getStringOrNull
import xyz.xenondevs.nova.util.getSurroundingChunks
import xyz.xenondevs.nova.util.item.ItemUtils
import xyz.xenondevs.nova.util.item.customModelData
import xyz.xenondevs.nova.util.item.novaCompound
import xyz.xenondevs.nova.util.item.novaItem
import xyz.xenondevs.nova.util.item.takeUnlessEmpty
import xyz.xenondevs.nova.util.item.unsafeNovaTag
Expand Down Expand Up @@ -123,9 +122,9 @@ internal object NovaCommand : Command() {
.then(literal("getBlockModelData")
.requiresPlayer()
.executes0(::showBlockModelData))
.then(literal("getItemData")
.then(literal("getItemBehaviors")
.requiresPlayer()
.executes0(::showItemData))
.executes0(::showItemBehaviors))
.then(literal("getItemModelData")
.requiresPlayer()
.executes0(::showItemModelData))
Expand Down Expand Up @@ -478,22 +477,22 @@ internal object NovaCommand : Command() {
}
}

private fun showItemData(ctx: CommandContext<CommandSourceStack>) {
private fun showItemBehaviors(ctx: CommandContext<CommandSourceStack>) {
val player = ctx.player

val item = player.inventory.itemInMainHand.takeUnlessEmpty()
val itemStack = player.inventory.itemInMainHand.takeUnlessEmpty()
val novaItem = itemStack?.novaItem

if (item != null) {
val novaCompound = item.novaCompound
if (novaCompound != null) {
ctx.source.sender.sendMessage(Component.translatable(
"command.nova.show_item_data.success",
NamedTextColor.GRAY,
ItemUtils.getName(item).color(NamedTextColor.AQUA),
Component.text(novaCompound.toString(), NamedTextColor.WHITE)
))
} else ctx.source.sender.sendMessage(Component.translatable("command.nova.show_item.no_data", NamedTextColor.RED))
} else ctx.source.sender.sendMessage(Component.translatable("command.nova.show_item_data.no_item", NamedTextColor.RED))
if (novaItem != null) {
val behaviors = novaItem.behaviors
ctx.source.sender.sendMessage(Component.translatable(
"command.nova.show_item_behaviors.success",
NamedTextColor.GRAY,
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA),
Component.text(behaviors.size).color(NamedTextColor.AQUA),
Component.text(behaviors.joinToString("\n") { it.toString(itemStack) }, NamedTextColor.WHITE)
))
} else ctx.source.sender.sendMessage(Component.translatable("command.nova.show_item_behaviors.no_item", NamedTextColor.RED))
}

private fun showItemModelData(ctx: CommandContext<CommandSourceStack>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ interface Chargeable {
itemStack.novaCompound = compound
}

override fun toString(itemStack: ItemStack): String {
return "Chargeable(" +
"energy=${getEnergy(itemStack)}," +
" maxEnergy=$maxEnergy," +
" affectsItemDurability=$affectsItemDurability" +
")"
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import xyz.xenondevs.commons.provider.immutable.map
import xyz.xenondevs.commons.provider.immutable.mapNonNull
import xyz.xenondevs.commons.provider.immutable.orElse
import xyz.xenondevs.commons.provider.immutable.provider
import xyz.xenondevs.nova.world.item.NovaItem
import xyz.xenondevs.nova.util.data.getInputStacks
import xyz.xenondevs.nova.util.unwrap
import xyz.xenondevs.nova.world.item.NovaItem
import java.util.*

/**
Expand Down Expand Up @@ -69,6 +69,17 @@ class Consumable(
)).build()
}

override fun toString(itemStack: ItemStack): String {
return "Consumable(" +
"nutrition=$nutrition, " +
"saturation=$saturation, " +
"canAlwaysEat=$canAlwaysEat, " +
"consumeTime=$consumeTime, " +
"remains=$remains, " +
"possibleEffects=$possibleEffects" +
")"
}

companion object : ItemBehaviorFactory<Consumable> {

override fun create(item: NovaItem): Consumable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package xyz.xenondevs.nova.world.item.behavior

import net.minecraft.core.component.DataComponentMap
import net.minecraft.core.component.DataComponents
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.RecipeChoice
import xyz.xenondevs.commons.provider.Provider
import xyz.xenondevs.commons.provider.immutable.map
import xyz.xenondevs.commons.provider.immutable.orElse
import xyz.xenondevs.commons.provider.immutable.provider
import xyz.xenondevs.nova.util.unwrap
import xyz.xenondevs.nova.world.item.NovaItem

/**
Expand Down Expand Up @@ -43,6 +45,16 @@ class Damageable(
.build()
}

override fun toString(itemStack: ItemStack): String {
return "Damageable(" +
"damage=${itemStack.unwrap().get(DataComponents.DAMAGE) ?: 0}, " +
"maxDurability=$maxDurability, " +
"itemDamageOnAttackEntity=$itemDamageOnAttackEntity, " +
"itemDamageOnBreakBlock=$itemDamageOnBreakBlock, " +
"repairIngredient=$repairIngredient" +
")"
}

companion object : ItemBehaviorFactory<Damageable> {

override fun create(item: NovaItem): Damageable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.minecraft.core.component.DataComponentMap
import net.minecraft.core.component.DataComponents
import net.minecraft.world.item.enchantment.ItemEnchantments
import org.bukkit.enchantments.Enchantment
import org.bukkit.inventory.ItemStack
import xyz.xenondevs.commons.provider.Provider
import xyz.xenondevs.commons.provider.immutable.orElse
import xyz.xenondevs.commons.provider.immutable.provider
Expand Down Expand Up @@ -47,6 +48,14 @@ class Enchantable(
.build()
}

override fun toString(itemStack: ItemStack): String {
return "Enchantable(" +
"enchantmentValue=$enchantmentValue, " +
"primaryEnchantments=${primaryEnchantments.joinToString { it.key.toString() }}, " +
"supportedEnchantments=${primaryEnchantments.joinToString { it.key.toString() }}" +
")"
}

companion object : ItemBehaviorFactory<Enchantable> {

override fun create(item: NovaItem): Enchantable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import org.bukkit.craftbukkit.util.CraftMagicNumbers
import xyz.xenondevs.commons.collections.enumMap
import xyz.xenondevs.commons.provider.Provider
import xyz.xenondevs.commons.provider.immutable.provider
import xyz.xenondevs.nova.world.item.NovaItem
import xyz.xenondevs.nova.util.item.novaItem
import xyz.xenondevs.nova.world.item.NovaItem
import net.minecraft.world.item.ItemStack as MojangStack
import org.bukkit.inventory.ItemStack as BukkitStack

Expand All @@ -31,6 +31,10 @@ class Fuel(burnTime: Provider<Int>) : ItemBehavior {
*/
constructor(burnTime: Int) : this(provider(burnTime))

override fun toString(itemStack: BukkitStack): String {
return "Fuel(burnTime=$burnTime)"
}

companion object : ItemBehaviorFactory<Fuel> {

private val NMS_VANILLA_FUELS: Map<Item, Int> = AbstractFurnaceBlockEntity.getFuel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ interface ItemBehavior : ItemBehaviorHolder {
*/
fun modifyClientSideStack(player: Player?, itemStack: ItemStack, data: NamespacedCompound) = itemStack

/**
* Creates a string representation of this [ItemBehavior] and its data in [itemStack].
*/
fun toString(itemStack: ItemStack): String = this.javaClass.simpleName

}

interface ItemBehaviorFactory<T : ItemBehavior> : ItemBehaviorHolder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import xyz.xenondevs.nova.world.block.tileentity.network.type.item.ItemFilter
/**
* Interface for item behaviors that contain an [ItemFilter] of type [T].
*/
interface ItemFilterContainer<T : ItemFilter<T>> {
interface ItemFilterContainer<T : ItemFilter<T>> : ItemBehavior {

/**
* Reads the [ItemFilter] from the given [itemStack].
Expand All @@ -18,4 +18,8 @@ interface ItemFilterContainer<T : ItemFilter<T>> {
*/
fun setFilter(itemStack: ItemStack, filter: T?)

override fun toString(itemStack: ItemStack): String {
return "ItemFilterContainer(filter=${getFilter(itemStack)})"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation
import net.minecraft.world.entity.ai.attributes.Attributes
import net.minecraft.world.item.Item
import net.minecraft.world.item.component.ItemAttributeModifiers
import org.bukkit.inventory.ItemStack
import xyz.xenondevs.commons.provider.Provider
import xyz.xenondevs.commons.provider.immutable.combinedProvider
import xyz.xenondevs.commons.provider.immutable.map
Expand Down Expand Up @@ -149,6 +150,19 @@ class Tool(
.build()
}

override fun toString(itemStack: ItemStack): String {
return "Tool(" +
"tier=$tier, " +
"categories=$categories, " +
"breakSpeed=$breakSpeed, " +
"attackDamage=$attackDamage, " +
"attackSpeed=$attackSpeed, " +
"knockbackBonus=$knockbackBonus, " +
"canSweepAttack=$canSweepAttack, " +
"canBreakBlocksInCreative=$canBreakBlocksInCreative" +
")"
}

companion object : ItemBehaviorFactory<Tool> {

override fun create(item: NovaItem): Tool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ internal object UnknownItemFilterBehavior : ItemBehavior, ItemFilterContainer<Un
return itemStack
}

override fun toString(itemStack: ItemStack): String {
return "UnknownItemFilterBehavior(" +
"id=${itemStack.novaCompound?.get<ResourceLocation>(ID_KEY)}, " +
"data=${itemStack.novaCompound?.get<Compound>(DATA_KEY)})"
")"
}

}

internal class UnknownItemFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ class Wearable(
return itemStack
}

override fun toString(itemStack: BukkitStack): String {
return "Wearable(" +
"slot=$slot, " +
"armor=$armor, " +
"armorToughness=$armorToughness, " +
"knockbackResistance=$knockbackResistance" +
")"
}

companion object {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ open class ToolCategory internal constructor(
val id: ResourceLocation
) {

override fun toString(): String {
return id.toString()
}

companion object {

fun hasCorrectToolCategory(block: Block, tool: ItemStack?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class ToolTier(
return levelValue.compareTo(other.levelValue)
}

override fun toString(): String {
return id.toString()
}

companion object {

/**
Expand Down
5 changes: 2 additions & 3 deletions nova/src/main/resources/assets/nova/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@
"command.nova.show_network_node_info.network": "Type: %s\nID: %s\nNodes: %s (%s bridge(s), %s end point(s))",
"command.nova.show_network_node_info.node": "Name: %s\nworld: %s, x: %s, y: %s, z: %s",
"command.nova.show_network_node_info.failure": "The block at world: %s, x: %s, y: %s, z: %s is not a network node.",
"command.nova.show_item_data.success": "This %s has the following item data:\n%s",
"command.nova.show_item_data.no_item": "Please hold an item in your main hand and try again.",
"command.nova.show_item_data.no_data": "This item has no data.",
"command.nova.show_item_behaviors.success": "This %s has %s item behavior(s):\n%s",
"command.nova.show_item_behaviors.no_item": "Please hold an item in your main hand and try again.",
"command.nova.show_item_model_data.success": "This %s with model %s uses the item type %s with custom model data %s.",
"command.nova.show_item_model_data.no_item": "Please hold a Nova item in your main hand and try again.",
"command.nova.modeldata.item": "The item %s uses %s and has the following custom model data: %s.",
Expand Down

0 comments on commit 4b6d4c1

Please sign in to comment.