Skip to content

Commit

Permalink
Customizable name and style for nova items and blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
NichtStudioCode committed Aug 3, 2023
1 parent 5e1f6bb commit 116a8b1
Show file tree
Hide file tree
Showing 27 changed files with 398 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.xenondevs.nova.api.block;

import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.xenondevs.nova.api.data.NamespacedId;
Expand All @@ -25,6 +26,23 @@ public interface NovaBlock {
* resource packs (e.g. en_us).
* @return The localized name of this block type.
*/
@NotNull String getLocalizedName(String locale);
@Deprecated
default @NotNull String getLocalizedName(String locale) {
return getPlaintextName(locale);
}

/**
* Gets the name of this block type.
* @return The name of this block type.
*/
@NotNull Component getName();

/**
* Gets the plaintext name of this block type.
* @param locale The locale to get the name in. Should be in the same format as the language file
* names in resource packs (e.g. en_us).
* @return The name of this {@link NovaBlock} in plaintext.
*/
@NotNull String getPlaintextName(@NotNull String locale);

}
27 changes: 26 additions & 1 deletion nova-api/src/main/java/xyz/xenondevs/nova/api/item/NovaItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package xyz.xenondevs.nova.api.item;

import net.kyori.adventure.text.Component;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -35,18 +36,40 @@ public interface NovaItem {
* @param locale The locale to get the name in . Should be in the same format as the language file
* names in resource packs (e.g. en_us).
* @return The localized name of this item for the specified locale.
* @deprecated Use {@link #getName()} or {@link #getPlaintextName(String)} instead.
*/
@NotNull String getLocalizedName(String locale);
@Deprecated
default @NotNull String getLocalizedName(String locale) {
return getPlaintextName(locale);
}

/**
* Gets the name of this {@link NovaItem}.
*
* @return The name of this {@link NovaItem}.
*/
@NotNull Component getName();

/**
* Gets the plaintext name of this {@link NovaItem}.
*
* @param locale The locale to get the name in. Should be in the same format as the language file
* names in resource packs (e.g. en_us).
* @return The name of this {@link NovaItem} in plaintext.
*/
@NotNull String getPlaintextName(@NotNull String locale);

/**
* Creates an {@link ItemStack} of this {@link NovaItem} with the specified amount.
*
* @param amount The amount of items in the stack.
* @return An {@link ItemStack} of this {@link NovaItem} with the specified amount.
*/
@NotNull ItemStack createItemStack(int amount);

/**
* Creates an {@link ItemStack} of this {@link NovaItem} with the amount of 1.
*
* @return An {@link ItemStack} of this {@link NovaItem} with the amount of 1.
*/
default @NotNull ItemStack createItemStack() {
Expand All @@ -55,13 +78,15 @@ public interface NovaItem {

/**
* Creates a client-side {@link ItemStack} of this {@link NovaItem} with the specified amount.
*
* @param amount The amount of items in the stack.
* @return A client-side {@link ItemStack} of this {@link NovaItem} with the specified amount.
*/
@NotNull ItemStack createClientsideItemStack(int amount);

/**
* Creates a client-side {@link ItemStack} of this {@link NovaItem} with the amount of 1.
*
* @return A client-side {@link ItemStack} of this {@link NovaItem} with the amount of 1.
*/
default @NotNull ItemStack createClientsideItemStack() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package xyz.xenondevs.nova.addon.registry

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.Style
import xyz.xenondevs.nova.item.NovaItem
import xyz.xenondevs.nova.item.NovaItemBuilder
import xyz.xenondevs.nova.item.behavior.ItemBehaviorHolder
Expand Down Expand Up @@ -27,7 +29,8 @@ interface ItemRegistry : AddonGetter {
): NovaItem {
val item = NovaItem(
ResourceLocation(addon, name),
localizedName,
Component.translatable(localizedName),
Style.empty(),
ItemLogic(*behaviors),
isHidden = isHidden
)
Expand All @@ -38,13 +41,14 @@ interface ItemRegistry : AddonGetter {
fun registerItem(
block: NovaBlock,
vararg behaviors: ItemBehaviorHolder,
localizedName: String = block.localizedName,
localizedName: String? = null,
isHidden: Boolean = false
): NovaItem {
require(block.id.namespace == addon.description.id) { "The block must be from the same addon (${block.id})!" }
val item = NovaItem(
block.id,
localizedName,
localizedName?.let(Component::translatable) ?: block.name,
Style.empty(),
ItemLogic(*behaviors),
isHidden = isHidden,
block = block
Expand All @@ -60,7 +64,8 @@ interface ItemRegistry : AddonGetter {
): NovaItem {
val item = NovaItem(
ResourceLocation(addon, name),
"",
Component.empty(),
Style.empty(),
ItemLogic(),
isHidden = isHidden
)
Expand All @@ -73,7 +78,8 @@ interface ItemRegistry : AddonGetter {
): NovaItem {
val item = NovaItem(
ResourceLocation(addon, name),
"",
Component.empty(),
Style.empty(),
ItemLogic(),
isHidden = true
)
Expand Down
10 changes: 4 additions & 6 deletions nova/src/main/kotlin/xyz/xenondevs/nova/api/ApiBlockWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package xyz.xenondevs.nova.api

import net.kyori.adventure.text.Component
import xyz.xenondevs.nova.api.data.NamespacedId
import xyz.xenondevs.nova.api.item.NovaItem
import xyz.xenondevs.nova.i18n.LocaleManager
import xyz.xenondevs.nova.util.component.adventure.toPlainText
import xyz.xenondevs.nova.util.namespacedId
import xyz.xenondevs.nova.world.block.NovaBlock
import xyz.xenondevs.nova.api.block.NovaBlock as INovaBlock

internal class ApiBlockWrapper(val block: NovaBlock): INovaBlock {

override fun getId(): NamespacedId = block.id.namespacedId

override fun getItem(): NovaItem? = block.item?.let(::ApiItemWrapper)

override fun getLocalizedName(locale: String): String {
return LocaleManager.getTranslation(locale, block.localizedName)
}
override fun getName(): Component = block.name
override fun getPlaintextName(locale: String): String = block.name.toPlainText(locale)

}
8 changes: 4 additions & 4 deletions nova/src/main/kotlin/xyz/xenondevs/nova/api/ApiItemWrapper.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package xyz.xenondevs.nova.api

import net.kyori.adventure.text.Component
import org.bukkit.inventory.ItemStack
import xyz.xenondevs.nova.api.block.NovaBlock
import xyz.xenondevs.nova.api.data.NamespacedId
import xyz.xenondevs.nova.i18n.LocaleManager
import xyz.xenondevs.nova.item.NovaItem
import xyz.xenondevs.nova.util.component.adventure.toPlainText
import xyz.xenondevs.nova.util.namespacedId
import xyz.xenondevs.nova.api.item.NovaItem as INovaItem

Expand All @@ -14,9 +15,8 @@ internal class ApiItemWrapper(private val item: NovaItem): INovaItem {
override fun getBlock(): NovaBlock? = item.block?.let(::ApiBlockWrapper)
override fun getMaxStackSize(): Int = item.maxStackSize

override fun getLocalizedName(locale: String): String {
return LocaleManager.getTranslation(locale, item.localizedName)
}
override fun getName(): Component = item.name
override fun getPlaintextName(locale: String): String = item.name.toPlainText(locale)

override fun createItemStack(amount: Int): ItemStack {
return item.createItemStack(amount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package xyz.xenondevs.nova.api

import com.mojang.datafixers.util.Either
import org.bukkit.inventory.ItemStack
import xyz.xenondevs.nova.i18n.LocaleManager
import xyz.xenondevs.nova.item.NovaItem
import xyz.xenondevs.nova.registry.NovaRegistries
import xyz.xenondevs.nova.util.component.adventure.toPlainText
import xyz.xenondevs.nova.util.get
import xyz.xenondevs.nova.util.item.novaItem
import xyz.xenondevs.nova.util.namespacedId
Expand All @@ -22,8 +22,8 @@ internal class LegacyMaterialWrapper(val material: Either<NovaItem, NovaBlock>)

@Deprecated("Use NovaBlockRegistry and NovaItemRegistry instead")
override fun getLocalizedName(locale: String): String {
val key = material.map(NovaItem::localizedName, NovaBlock::localizedName)
return LocaleManager.getTranslation(key, locale)
val component = material.map(NovaItem::name, NovaBlock::name)
return component.toPlainText(locale)
}

}
Expand Down
32 changes: 15 additions & 17 deletions nova/src/main/kotlin/xyz/xenondevs/nova/command/impl/NovaCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ import xyz.xenondevs.nova.tileentity.network.NetworkType
import xyz.xenondevs.nova.tileentity.vanilla.VanillaTileEntityManager
import xyz.xenondevs.nova.ui.menu.item.creative.ItemsWindow
import xyz.xenondevs.nova.ui.waila.WailaManager
import xyz.xenondevs.nova.util.BlockUtils
import xyz.xenondevs.nova.util.addItemCorrectly
import xyz.xenondevs.nova.util.bukkitMirror
import xyz.xenondevs.nova.util.getSurroundingChunks
import xyz.xenondevs.nova.util.item.localizedName
import xyz.xenondevs.nova.util.item.ItemUtils
import xyz.xenondevs.nova.util.item.novaCompoundOrNull
import xyz.xenondevs.nova.util.item.takeUnlessEmpty
import xyz.xenondevs.nova.util.runAsyncTask
Expand Down Expand Up @@ -239,8 +239,6 @@ internal object NovaCommand : Command("nova") {
giveTo(ctx, item, ctx["amount"])

private fun giveTo(ctx: CommandContext<CommandSourceStack>, item: NovaItem, amount: Int) {
val itemName = item.localizedName.ifBlank { item.id.toString() }

val targetPlayers = ctx.getArgument("player", EntitySelector::class.java).findPlayers(ctx.source)

if (targetPlayers.isNotEmpty()) {
Expand All @@ -252,7 +250,7 @@ internal object NovaCommand : Command("nova") {
"command.nova.give.success",
NamedTextColor.GRAY,
Component.text(amount).color(NamedTextColor.AQUA),
Component.translatable(itemName).color(NamedTextColor.AQUA),
item.name.color(NamedTextColor.AQUA),
Component.text(player.name).color(NamedTextColor.AQUA)
))
}
Expand All @@ -274,7 +272,7 @@ internal object NovaCommand : Command("nova") {
NamedTextColor.RED,
Component.translatable(enchantment.localizedName, NamedTextColor.AQUA),
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))

continue
Expand All @@ -293,7 +291,7 @@ internal object NovaCommand : Command("nova") {
NamedTextColor.RED,
Component.translatable(enchantment.localizedName, NamedTextColor.AQUA),
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))

continue
Expand All @@ -311,7 +309,7 @@ internal object NovaCommand : Command("nova") {
Component.translatable("enchantment.level.$level", NamedTextColor.AQUA),
),
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA))
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA))
)
}
} else ctx.source.sendFailure(Component.translatable("command.nova.no-players", NamedTextColor.RED))
Expand All @@ -330,7 +328,7 @@ internal object NovaCommand : Command("nova") {
"command.nova.unenchant_all.failure",
NamedTextColor.RED,
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))
}

Expand All @@ -339,7 +337,7 @@ internal object NovaCommand : Command("nova") {
"command.nova.unenchant_all.success",
NamedTextColor.GRAY,
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))
}

Expand Down Expand Up @@ -369,7 +367,7 @@ internal object NovaCommand : Command("nova") {
NamedTextColor.RED,
Component.translatable(enchantment.localizedName, NamedTextColor.AQUA),
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))
}

Expand All @@ -379,7 +377,7 @@ internal object NovaCommand : Command("nova") {
NamedTextColor.GRAY,
Component.translatable(enchantment.localizedName, NamedTextColor.AQUA),
Component.text(player.displayName, NamedTextColor.AQUA),
Component.translatable(itemStack.bukkitMirror.localizedName ?: "", NamedTextColor.AQUA)
ItemUtils.getName(itemStack).color(NamedTextColor.AQUA)
))
}

Expand Down Expand Up @@ -447,21 +445,21 @@ internal object NovaCommand : Command("nova") {
NamedTextColor.RED
))

fun sendSuccess(name: String, data: Compound) = ctx.source.sendSuccess(Component.translatable(
fun sendSuccess(name: Component, data: Compound) = ctx.source.sendSuccess(Component.translatable(
"command.nova.show_tile_entity_data.success",
NamedTextColor.GRAY,
Component.text(name).color(NamedTextColor.AQUA),
name.color(NamedTextColor.AQUA),
Component.text(data.toString(), NamedTextColor.WHITE)
))

val location = player.getTargetBlockExact(8)?.location
if (location != null) {
val tileEntity = TileEntityManager.getTileEntity(location, true)
if (tileEntity != null) {
sendSuccess(tileEntity.block.localizedName, tileEntity.data)
sendSuccess(tileEntity.block.name, tileEntity.data)
} else {
val vanillaTileEntity = VanillaTileEntityManager.getTileEntityAt(location)
if (vanillaTileEntity != null) sendSuccess(vanillaTileEntity.block.type.name, vanillaTileEntity.data)
if (vanillaTileEntity != null) sendSuccess(BlockUtils.getName(vanillaTileEntity.block), vanillaTileEntity.data)
else sendFailure()
}
} else sendFailure()
Expand Down Expand Up @@ -491,7 +489,7 @@ internal object NovaCommand : Command("nova") {
ctx.source.sendSuccess(Component.translatable(
"command.nova.show_item_data.success",
NamedTextColor.GRAY,
Component.translatable(item.localizedName ?: item.type.name.lowercase(), NamedTextColor.AQUA),
ItemUtils.getName(item).color(NamedTextColor.AQUA),
Component.text(novaCompound.toString(), NamedTextColor.WHITE)
))
} else ctx.source.sendFailure(Component.translatable("command.nova.show_item.no_data", NamedTextColor.RED))
Expand Down
11 changes: 0 additions & 11 deletions nova/src/main/kotlin/xyz/xenondevs/nova/i18n/LocaleManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import xyz.xenondevs.nova.data.resources.lookup.ResourceLookups
import xyz.xenondevs.nova.initialize.InitFun
import xyz.xenondevs.nova.initialize.InternalInit
import xyz.xenondevs.nova.initialize.InternalInitStage
import xyz.xenondevs.nova.item.NovaItem
import xyz.xenondevs.nova.util.formatSafely
import xyz.xenondevs.nova.util.runAsyncTask

Expand Down Expand Up @@ -107,16 +106,6 @@ object LocaleManager {
return getTranslation(player.locale, key, *args)
}

@Synchronized
fun getTranslatedName(lang: String, item: NovaItem): String {
return getTranslation(lang, item.localizedName)
}

@Synchronized
fun getTranslatedName(player: Player, item: NovaItem): String {
return getTranslation(player, item.localizedName)
}

private object NovaLanguage : Language() {

private val delegate = getInstance()
Expand Down
Loading

0 comments on commit 116a8b1

Please sign in to comment.