From 06ac373e830298935ca58330e0c49661d457f53f Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Sun, 28 Apr 2024 20:07:15 +0100 Subject: [PATCH] Use components for upgrade adjectives This makes quick-and-dirty datapacks a little easier, as you can now use a hard-coded string rather than adding a language key. --- .../api/pocket/AbstractPocketUpgrade.java | 11 ++++++++--- .../api/turtle/AbstractTurtleUpgrade.java | 11 ++++++++--- .../api/turtle/TurtleUpgradeDataProvider.java | 7 ++++--- .../computercraft/api/upgrades/UpgradeBase.java | 12 ++++++++---- .../computercraft/impl/upgrades/TurtleToolSpec.java | 6 ++++-- .../computercraft/turtle_upgrade/diamond_axe.json | 2 +- .../computercraft/turtle_upgrade/diamond_hoe.json | 2 +- .../turtle_upgrade/diamond_pickaxe.json | 2 +- .../turtle_upgrade/diamond_shovel.json | 2 +- .../computercraft/turtle_upgrade/diamond_sword.json | 2 +- .../dan200/computercraft/data/LanguageProvider.java | 13 ++++++++++--- .../shared/pocket/items/PocketComputerItem.java | 4 +--- .../shared/turtle/items/TurtleItem.java | 13 +++---------- .../turtle_upgrade/netherite_pickaxe.json | 2 +- .../turtle_upgrade/wooden_pickaxe.json | 2 +- 15 files changed, 53 insertions(+), 38 deletions(-) diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/pocket/AbstractPocketUpgrade.java b/projects/common-api/src/main/java/dan200/computercraft/api/pocket/AbstractPocketUpgrade.java index d18891dbe..bc6291919 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/pocket/AbstractPocketUpgrade.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/pocket/AbstractPocketUpgrade.java @@ -4,6 +4,7 @@ package dan200.computercraft.api.pocket; +import net.minecraft.network.chat.Component; import net.minecraft.world.item.ItemStack; @@ -13,16 +14,20 @@ import net.minecraft.world.item.ItemStack; * One does not have to use this, but it does provide a convenient template. */ public abstract class AbstractPocketUpgrade implements IPocketUpgrade { - private final String adjective; + private final Component adjective; private final ItemStack stack; - protected AbstractPocketUpgrade(String adjective, ItemStack stack) { + protected AbstractPocketUpgrade(Component adjective, ItemStack stack) { this.adjective = adjective; this.stack = stack; } + protected AbstractPocketUpgrade(String adjective, ItemStack stack) { + this(Component.translatable(adjective), stack); + } + @Override - public final String getUnlocalisedAdjective() { + public final Component getAdjective() { return adjective; } diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java b/projects/common-api/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java index 8ee5a65dd..409f54cbc 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/turtle/AbstractTurtleUpgrade.java @@ -4,6 +4,7 @@ package dan200.computercraft.api.turtle; +import net.minecraft.network.chat.Component; import net.minecraft.world.item.ItemStack; @@ -14,17 +15,21 @@ import net.minecraft.world.item.ItemStack; */ public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade { private final TurtleUpgradeType type; - private final String adjective; + private final Component adjective; private final ItemStack stack; - protected AbstractTurtleUpgrade(TurtleUpgradeType type, String adjective, ItemStack stack) { + protected AbstractTurtleUpgrade(TurtleUpgradeType type, Component adjective, ItemStack stack) { this.type = type; this.adjective = adjective; this.stack = stack; } + protected AbstractTurtleUpgrade(TurtleUpgradeType type, String adjective, ItemStack stack) { + this(type, Component.translatable(adjective), stack); + } + @Override - public final String getUnlocalisedAdjective() { + public final Component getAdjective() { return adjective; } diff --git a/projects/common-api/src/main/java/dan200/computercraft/api/turtle/TurtleUpgradeDataProvider.java b/projects/common-api/src/main/java/dan200/computercraft/api/turtle/TurtleUpgradeDataProvider.java index e227a3fef..b403c54bd 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/api/turtle/TurtleUpgradeDataProvider.java +++ b/projects/common-api/src/main/java/dan200/computercraft/api/turtle/TurtleUpgradeDataProvider.java @@ -13,6 +13,7 @@ import dan200.computercraft.impl.upgrades.TurtleToolSpec; import net.minecraft.core.component.DataComponents; import net.minecraft.data.DataGenerator; import net.minecraft.data.PackOutput; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import net.minecraft.world.entity.ai.attributes.Attributes; @@ -58,7 +59,7 @@ public abstract class TurtleUpgradeDataProvider extends UpgradeDataProvider breakable; @@ -67,7 +68,7 @@ public abstract class TurtleUpgradeDataProvider extends UpgradeDataProvider getType(); /** - * Return an unlocalised string to describe this type of computer in item names. + * A description of this upgrade for use in item names. + *

+ * This should typically be a {@linkplain Component#translatable(String) translation key}, rather than a hard coded + * string. *

* Examples of built-in adjectives are "Wireless", "Mining" and "Crafty". * - * @return The localisation key for this upgrade's adjective. + * @return The text component for this upgrade's adjective. */ - String getUnlocalisedAdjective(); + Component getAdjective(); /** * Return an item stack representing the type of item that a computer must be crafted @@ -107,7 +111,7 @@ public interface UpgradeBase { * * @param id The upgrade ID. * @return The generated adjective. - * @see #getUnlocalisedAdjective() + * @see #getAdjective() */ static String getDefaultAdjective(ResourceLocation id) { return Util.makeDescriptionId("upgrade", id) + ".adjective"; diff --git a/projects/common-api/src/main/java/dan200/computercraft/impl/upgrades/TurtleToolSpec.java b/projects/common-api/src/main/java/dan200/computercraft/impl/upgrades/TurtleToolSpec.java index 53532a629..60935d5a9 100644 --- a/projects/common-api/src/main/java/dan200/computercraft/impl/upgrades/TurtleToolSpec.java +++ b/projects/common-api/src/main/java/dan200/computercraft/impl/upgrades/TurtleToolSpec.java @@ -10,6 +10,8 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import dan200.computercraft.api.turtle.TurtleToolDurability; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.core.registries.Registries; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.ComponentSerialization; import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; @@ -28,7 +30,7 @@ import java.util.Optional; * @param breakable The items breakable by this tool. */ public record TurtleToolSpec( - String adjective, + Component adjective, Optional craftItem, Item toolItem, float damageMultiplier, @@ -39,7 +41,7 @@ public record TurtleToolSpec( public static final float DEFAULT_DAMAGE_MULTIPLIER = 3.0f; public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( - Codec.STRING.fieldOf("adjective").forGetter(TurtleToolSpec::adjective), + ComponentSerialization.CODEC.fieldOf("adjective").forGetter(TurtleToolSpec::adjective), BuiltInRegistries.ITEM.byNameCodec().optionalFieldOf("craftingItem").forGetter(TurtleToolSpec::craftItem), BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(TurtleToolSpec::toolItem), Codec.FLOAT.optionalFieldOf("damageMultiplier", DEFAULT_DAMAGE_MULTIPLIER).forGetter(TurtleToolSpec::damageMultiplier), diff --git a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_axe.json b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_axe.json index c94316b76..1bed1fcf5 100644 --- a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_axe.json +++ b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_axe.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_axe.adjective", + "adjective": {"translate": "upgrade.minecraft.diamond_axe.adjective"}, "damageMultiplier": 6.0, "item": "minecraft:diamond_axe" } diff --git a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_hoe.json b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_hoe.json index 66e945689..a94e9c1df 100644 --- a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_hoe.json +++ b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_hoe.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_hoe.adjective", + "adjective": {"translate": "upgrade.minecraft.diamond_hoe.adjective"}, "breakable": "computercraft:turtle_hoe_harvestable", "item": "minecraft:diamond_hoe" } diff --git a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_pickaxe.json b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_pickaxe.json index 6d5793315..77b3ed1c6 100644 --- a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_pickaxe.json +++ b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_pickaxe.json @@ -1,5 +1,5 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_pickaxe.adjective", + "adjective": {"translate": "upgrade.minecraft.diamond_pickaxe.adjective"}, "item": "minecraft:diamond_pickaxe" } diff --git a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_shovel.json b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_shovel.json index 0584f4e97..0e7a8855a 100644 --- a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_shovel.json +++ b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_shovel.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_shovel.adjective", + "adjective": {"translate": "upgrade.minecraft.diamond_shovel.adjective"}, "breakable": "computercraft:turtle_shovel_harvestable", "item": "minecraft:diamond_shovel" } diff --git a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_sword.json b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_sword.json index 5cdae267d..79e3cd7b8 100644 --- a/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_sword.json +++ b/projects/common/src/generated/resources/data/minecraft/computercraft/turtle_upgrade/diamond_sword.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_sword.adjective", + "adjective": {"translate": "upgrade.minecraft.diamond_sword.adjective"}, "breakable": "computercraft:turtle_sword_harvestable", "damageMultiplier": 9.0, "item": "minecraft:diamond_sword" diff --git a/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java b/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java index 12780478d..6d6a22c4d 100644 --- a/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java +++ b/projects/common/src/main/java/dan200/computercraft/data/LanguageProvider.java @@ -9,7 +9,6 @@ import dan200.computercraft.api.ComputerCraftAPI; import dan200.computercraft.api.ComputerCraftTags; import dan200.computercraft.api.pocket.PocketUpgradeDataProvider; import dan200.computercraft.api.turtle.TurtleUpgradeDataProvider; -import dan200.computercraft.api.upgrades.UpgradeBase; import dan200.computercraft.core.metrics.Metric; import dan200.computercraft.core.metrics.Metrics; import dan200.computercraft.shared.ModRegistry; @@ -22,6 +21,8 @@ import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.data.CachedOutput; import net.minecraft.data.DataProvider; import net.minecraft.data.PackOutput; +import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.contents.TranslatableContents; import net.minecraft.tags.TagKey; import net.minecraft.world.item.Item; @@ -287,8 +288,8 @@ public final class LanguageProvider implements DataProvider { BuiltInRegistries.ITEM.holders() .filter(x -> x.key().location().getNamespace().equals(ComputerCraftAPI.MOD_ID)) .map(x -> x.value().getDescriptionId()), - turtleUpgrades.getGeneratedUpgrades().values().stream().map(UpgradeBase::getUnlocalisedAdjective), - pocketUpgrades.getGeneratedUpgrades().values().stream().map(UpgradeBase::getUnlocalisedAdjective), + turtleUpgrades.getGeneratedUpgrades().values().stream().flatMap(x -> getTranslationKeys(x.getAdjective())), + pocketUpgrades.getGeneratedUpgrades().values().stream().flatMap(x -> getTranslationKeys(x.getAdjective())), Metric.metrics().values().stream().map(x -> AggregatedMetric.TRANSLATION_PREFIX + x.name() + ".name"), ConfigSpec.serverSpec.entries().map(ConfigFile.Entry::translationKey), ConfigSpec.clientSpec.entries().map(ConfigFile.Entry::translationKey), @@ -296,6 +297,12 @@ public final class LanguageProvider implements DataProvider { ).flatMap(x -> x); } + private static Stream getTranslationKeys(Component component) { + if (component.getContents() instanceof TranslatableContents contents) return Stream.of(contents.getKey()); + + return component.getSiblings().stream().flatMap(LanguageProvider::getTranslationKeys); + } + private void add(String id, String text) { Objects.requireNonNull(id, "id cannot be null"); Objects.requireNonNull(text, "text cannot be null"); diff --git a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java index 9512ae4d9..6bc675687 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/pocket/items/PocketComputerItem.java @@ -127,9 +127,7 @@ public class PocketComputerItem extends Item implements IMedia { var baseString = getDescriptionId(stack); var upgrade = getUpgrade(stack); if (upgrade != null) { - return Component.translatable(baseString + ".upgraded", - Component.translatable(upgrade.getUnlocalisedAdjective()) - ); + return Component.translatable(baseString + ".upgraded", upgrade.getAdjective()); } else { return super.getName(stack); } diff --git a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java index 7e24218c7..2c26ac23c 100644 --- a/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java +++ b/projects/common/src/main/java/dan200/computercraft/shared/turtle/items/TurtleItem.java @@ -34,18 +34,11 @@ public class TurtleItem extends AbstractComputerItem { var left = getUpgrade(stack, TurtleSide.LEFT); var right = getUpgrade(stack, TurtleSide.RIGHT); if (left != null && right != null) { - return Component.translatable(baseString + ".upgraded_twice", - Component.translatable(right.getUnlocalisedAdjective()), - Component.translatable(left.getUnlocalisedAdjective()) - ); + return Component.translatable(baseString + ".upgraded_twice", right.getAdjective(), left.getAdjective()); } else if (left != null) { - return Component.translatable(baseString + ".upgraded", - Component.translatable(left.getUnlocalisedAdjective()) - ); + return Component.translatable(baseString + ".upgraded", left.getAdjective()); } else if (right != null) { - return Component.translatable(baseString + ".upgraded", - Component.translatable(right.getUnlocalisedAdjective()) - ); + return Component.translatable(baseString + ".upgraded", right.getAdjective()); } else { return Component.translatable(baseString); } diff --git a/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/netherite_pickaxe.json b/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/netherite_pickaxe.json index c385abddd..f90d6af39 100644 --- a/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/netherite_pickaxe.json +++ b/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/netherite_pickaxe.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_pickaxe.adjective", + "adjective": "Netherite", "item": "minecraft:netherite_pickaxe", "allowEnchantments": true, "consumeDurability": "when_enchanted" diff --git a/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/wooden_pickaxe.json b/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/wooden_pickaxe.json index 5da827ea0..12601c097 100644 --- a/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/wooden_pickaxe.json +++ b/projects/common/src/testMod/resources/data/cctest/computercraft/turtle_upgrade/wooden_pickaxe.json @@ -1,6 +1,6 @@ { "type": "computercraft:tool", - "adjective": "upgrade.minecraft.diamond_pickaxe.adjective", + "adjective": "Wooden", "item": "minecraft:wooden_pickaxe", "consumeDurability": "always" }