1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-11-13 05:19:59 +00:00

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.
This commit is contained in:
Jonathan Coates 2024-04-28 20:07:15 +01:00
parent 0aca6a4dc9
commit 06ac373e83
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
15 changed files with 53 additions and 38 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<ITur
public final class ToolBuilder {
private final ResourceLocation id;
private final Item toolItem;
private String adjective;
private Component adjective;
private @Nullable Item craftingItem;
private float damageMultiplier = TurtleToolSpec.DEFAULT_DAMAGE_MULTIPLIER;
private @Nullable TagKey<Block> breakable;
@ -67,7 +68,7 @@ public abstract class TurtleUpgradeDataProvider extends UpgradeDataProvider<ITur
ToolBuilder(ResourceLocation id, Item toolItem) {
this.id = id;
adjective = UpgradeBase.getDefaultAdjective(id);
adjective = Component.translatable(UpgradeBase.getDefaultAdjective(id));
this.toolItem = toolItem;
craftingItem = null;
}
@ -78,7 +79,7 @@ public abstract class TurtleUpgradeDataProvider extends UpgradeDataProvider<ITur
* @param adjective The new adjective to use.
* @return The tool builder, for further use.
*/
public ToolBuilder adjective(String adjective) {
public ToolBuilder adjective(Component adjective) {
this.adjective = adjective;
return this;
}

View File

@ -11,6 +11,7 @@ import dan200.computercraft.api.turtle.ITurtleUpgrade;
import dan200.computercraft.api.turtle.TurtleSide;
import net.minecraft.Util;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
@ -26,13 +27,16 @@ public interface UpgradeBase {
UpgradeType<?> getType();
/**
* Return an unlocalised string to describe this type of computer in item names.
* A description of this upgrade for use in item names.
* <p>
* This should typically be a {@linkplain Component#translatable(String) translation key}, rather than a hard coded
* string.
* <p>
* 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";

View File

@ -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<Item> 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<TurtleToolSpec> 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),

View File

@ -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"
}

View File

@ -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"
}

View File

@ -1,5 +1,5 @@
{
"type": "computercraft:tool",
"adjective": "upgrade.minecraft.diamond_pickaxe.adjective",
"adjective": {"translate": "upgrade.minecraft.diamond_pickaxe.adjective"},
"item": "minecraft:diamond_pickaxe"
}

View File

@ -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"
}

View File

@ -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"

View File

@ -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<String> 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");

View File

@ -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);
}

View File

@ -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);
}

View File

@ -1,6 +1,6 @@
{
"type": "computercraft:tool",
"adjective": "upgrade.minecraft.diamond_pickaxe.adjective",
"adjective": "Netherite",
"item": "minecraft:netherite_pickaxe",
"allowEnchantments": true,
"consumeDurability": "when_enchanted"

View File

@ -1,6 +1,6 @@
{
"type": "computercraft:tool",
"adjective": "upgrade.minecraft.diamond_pickaxe.adjective",
"adjective": "Wooden",
"item": "minecraft:wooden_pickaxe",
"consumeDurability": "always"
}