From 78cc8fc74c7421a8286a416bee6537a3409bd7f2 Mon Sep 17 00:00:00 2001 From: tobinio Date: Thu, 27 Mar 2025 16:44:49 +0100 Subject: [PATCH] projectile name based textures --- .gitignore | 4 + item-model-generator.py | 194 ++++++++++++++++++ .../tobinio/realarrowtip/RealArrowTip.java | 4 +- .../{charge => }/ChargeTypeProperty.java | 54 ++--- .../model/{tint => }/TintSource.java | 2 +- .../realarrowtip/model/charge/ChargeType.java | 29 --- .../resources/assets/minecraft/items/bow.json | 34 +-- .../assets/minecraft/items/crossbow.json | 18 +- 8 files changed, 243 insertions(+), 96 deletions(-) create mode 100644 item-model-generator.py rename src/main/java/tobinio/realarrowtip/model/{charge => }/ChargeTypeProperty.java (53%) rename src/main/java/tobinio/realarrowtip/model/{tint => }/TintSource.java (98%) delete mode 100644 src/main/java/tobinio/realarrowtip/model/charge/ChargeType.java diff --git a/.gitignore b/.gitignore index 3c37caf..1e1d556 100644 --- a/.gitignore +++ b/.gitignore @@ -116,3 +116,7 @@ run/ # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) !gradle-wrapper.jar + +# custom +generated_bow.json +generated_crossbow.json diff --git a/item-model-generator.py b/item-model-generator.py new file mode 100644 index 0000000..613ec2b --- /dev/null +++ b/item-model-generator.py @@ -0,0 +1,194 @@ +import os +import json + +MODEL_DIR = "./src/main/resources/assets/real_arrow_tip/models/item/" + +ARROW_TYPES = [ + {"item": "minecraft:spectral_arrow", "name": "spectral", "tint": None}, + {"item": "minecraft:tipped_arrow", "name": "tipped", "tint": -13083194}, +] + + +def generate_bow_arrow_json(arrow_data): + paths = [ + f"real_arrow_tip:item/bow_pulling_0_{arrow_data["name"]}", + f"real_arrow_tip:item/bow_pulling_1_{arrow_data["name"]}", + f"real_arrow_tip:item/bow_pulling_2_{arrow_data["name"]}", + ] + + range_dispatch = { + "type": "minecraft:range_dispatch", + "property": "minecraft:use_duration", + "scale": 0.05, + "entries": [ + { + "model": { + "type": "minecraft:model", + "model": paths[1], + }, + "threshold": 0.65, + }, + { + "model": { + "type": "minecraft:model", + "model": paths[2], + }, + "threshold": 0.9, + }, + ], + "fallback": { + "type": "minecraft:model", + "model": paths[0], + }, + } + + if arrow_data["tint"] is not None: + for entry in range_dispatch["entries"]: + entry["model"]["tints"] = [{"type": "real_arrow_tip:projectile", "default": arrow_data["tint"]}] + range_dispatch["fallback"]["tints"] = [{"type": "real_arrow_tip:projectile", "default": arrow_data["tint"]}] + + return { + "when": arrow_data["item"], + "model": range_dispatch, + } + + +def create_bow(): + json_structure = { + "model": { + "type": "minecraft:condition", + "property": "minecraft:using_item", + "on_false": { + "type": "minecraft:model", + "model": "minecraft:item/bow" + }, + "on_true": { + "type": "minecraft:select", + "property": "real_arrow_tip:charge_type", + "cases": [], + "fallback": { + "type": "minecraft:range_dispatch", + "property": "minecraft:use_duration", + "scale": 0.05, + "entries": [ + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/bow_pulling_1" + }, + "threshold": 0.65 + }, + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/bow_pulling_2" + }, + "threshold": 0.9 + } + ], + "fallback": { + "type": "minecraft:model", + "model": "minecraft:item/bow_pulling_0" + }, + }, + }, + } + } + + for arrow in ARROW_TYPES: + arrow_json = generate_bow_arrow_json(arrow) + if arrow_json: + json_structure["model"]["on_true"]["cases"].append(arrow_json) + + output_file = "generated_bow.json" + with open(output_file, "w", encoding="utf-8") as f: + json.dump(json_structure, f, indent=2) + + print(f"created: {output_file}") + + +def generate_crossbow_arrow_json(arrow_data): + model = { + "model": { + "type": "minecraft:model", + "model": f"real_arrow_tip:item/crossbow_{arrow_data["name"]}", + }, + "when": arrow_data["item"] + } + + if arrow_data["tint"] is not None: + model["model"]["tints"] = [{"type": "real_arrow_tip:projectile", "default": arrow_data["tint"]}] + + return model + + +def create_crossbow(): + json_structure = { + "model": { + "type": "minecraft:select", + "property": "real_arrow_tip:charge_type", + "cases": [ + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow_arrow" + }, + "when": "minecraft:arrow" + }, + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow_firework" + }, + "when": "minecraft:firework_rocket" + } + ], + "fallback": { + "type": "minecraft:condition", + "property": "minecraft:using_item", + "on_false": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow" + }, + "on_true": { + "type": "minecraft:range_dispatch", + "entries": [ + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow_pulling_1" + }, + "threshold": 0.58 + }, + { + "model": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow_pulling_2" + }, + "threshold": 1 + } + ], + "fallback": { + "type": "minecraft:model", + "model": "minecraft:item/crossbow_pulling_0" + }, + "property": "minecraft:crossbow/pull" + }, + }, + } + } + + for arrow in ARROW_TYPES: + arrow_json = generate_crossbow_arrow_json(arrow) + if arrow_json: + json_structure["model"]["cases"].append(arrow_json) + + output_file = "generated_crossbow.json" + with open(output_file, "w", encoding="utf-8") as f: + json.dump(json_structure, f, indent=2) + + print(f"created: {output_file}") + + +create_bow() +create_crossbow() diff --git a/src/main/java/tobinio/realarrowtip/RealArrowTip.java b/src/main/java/tobinio/realarrowtip/RealArrowTip.java index bbb37b2..313a4ea 100644 --- a/src/main/java/tobinio/realarrowtip/RealArrowTip.java +++ b/src/main/java/tobinio/realarrowtip/RealArrowTip.java @@ -10,8 +10,8 @@ import net.minecraft.util.Identifier; import net.minecraft.util.math.ColorHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import tobinio.realarrowtip.model.charge.ChargeTypeProperty; -import tobinio.realarrowtip.model.tint.TintSource; +import tobinio.realarrowtip.model.ChargeTypeProperty; +import tobinio.realarrowtip.model.TintSource; public class RealArrowTip implements ClientModInitializer { public static String modID = "real_arrow_tip"; diff --git a/src/main/java/tobinio/realarrowtip/model/charge/ChargeTypeProperty.java b/src/main/java/tobinio/realarrowtip/model/ChargeTypeProperty.java similarity index 53% rename from src/main/java/tobinio/realarrowtip/model/charge/ChargeTypeProperty.java rename to src/main/java/tobinio/realarrowtip/model/ChargeTypeProperty.java index 63fb87c..7d92883 100644 --- a/src/main/java/tobinio/realarrowtip/model/charge/ChargeTypeProperty.java +++ b/src/main/java/tobinio/realarrowtip/model/ChargeTypeProperty.java @@ -1,4 +1,4 @@ -package tobinio.realarrowtip.model.charge; +package tobinio.realarrowtip.model; import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; @@ -22,27 +22,28 @@ import org.jetbrains.annotations.Nullable; * @author Tobias Frischmann */ @Environment (EnvType.CLIENT) -public record ChargeTypeProperty() implements SelectProperty { - public static final SelectProperty.Type TYPE; - public static final Codec VALUE_CODEC; +public record ChargeTypeProperty() implements SelectProperty { + public static final SelectProperty.Type TYPE; + public static final Codec VALUE_CODEC; static { - VALUE_CODEC = ChargeType.CODEC; + VALUE_CODEC = Codec.STRING; TYPE = SelectProperty.Type.create( MapCodec.unit(new ChargeTypeProperty()), VALUE_CODEC ); } @Override - public ChargeType getValue(ItemStack stack, @Nullable ClientWorld world, @Nullable LivingEntity user, + public String getValue(ItemStack stack, @Nullable ClientWorld world, @Nullable LivingEntity user, int seed, ItemDisplayContext displayContext) { + if (stack.isOf(Items.BOW)) { return getChargeTypeBow(stack, user); } return getChargeTypeCrossbow(stack); } - private static @NotNull ChargeType getChargeTypeBow(ItemStack stack, @Nullable LivingEntity user) { + private static @NotNull String getChargeTypeBow(ItemStack stack, @Nullable LivingEntity user) { if (user != null) { ItemStack projectile = user.getProjectileType(stack); @@ -50,52 +51,29 @@ public record ChargeTypeProperty() implements SelectProperty { projectile = skeleton.createArrowProjectile(projectile, 0, stack).getItemStack(); } - if (projectile.isOf(Items.SPECTRAL_ARROW)) { - return ChargeType.SPECTRAL; - } - - if (projectile.isOf(Items.TIPPED_ARROW) || projectile.get(DataComponentTypes.POTION_CONTENTS) != null) { - return ChargeType.TIPPED; - } - - if (projectile.isOf(Items.FIREWORK_ROCKET)) { - return ChargeType.ROCKET; - } - - return ChargeType.ARROW; + return projectile.getRegistryEntry().getIdAsString(); } - return ChargeType.NONE; + return "none"; } - private static @NotNull ChargeType getChargeTypeCrossbow(ItemStack stack) { + private static String getChargeTypeCrossbow(ItemStack stack) { ChargedProjectilesComponent chargedProjectilesComponent = stack.get(DataComponentTypes.CHARGED_PROJECTILES); if (chargedProjectilesComponent == null || chargedProjectilesComponent.isEmpty()) { - return ChargeType.NONE; + return "none"; } else { - if (chargedProjectilesComponent.contains(Items.SPECTRAL_ARROW)) { - return ChargeType.SPECTRAL; - } - - if (chargedProjectilesComponent.contains(Items.TIPPED_ARROW)) { - return ChargeType.TIPPED; - } - - if (chargedProjectilesComponent.contains(Items.FIREWORK_ROCKET)) { - return ChargeType.ROCKET; - } - - return ChargeType.ARROW; + ItemStack projectile = chargedProjectilesComponent.getProjectiles().getFirst(); + return projectile.getRegistryEntry().getIdAsString(); } } @Override - public Codec valueCodec() { + public Codec valueCodec() { return VALUE_CODEC; } @Override - public SelectProperty.Type getType() { + public SelectProperty.Type getType() { return TYPE; } } \ No newline at end of file diff --git a/src/main/java/tobinio/realarrowtip/model/tint/TintSource.java b/src/main/java/tobinio/realarrowtip/model/TintSource.java similarity index 98% rename from src/main/java/tobinio/realarrowtip/model/tint/TintSource.java rename to src/main/java/tobinio/realarrowtip/model/TintSource.java index 385cb66..4815e38 100644 --- a/src/main/java/tobinio/realarrowtip/model/tint/TintSource.java +++ b/src/main/java/tobinio/realarrowtip/model/TintSource.java @@ -1,4 +1,4 @@ -package tobinio.realarrowtip.model.tint; +package tobinio.realarrowtip.model; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; diff --git a/src/main/java/tobinio/realarrowtip/model/charge/ChargeType.java b/src/main/java/tobinio/realarrowtip/model/charge/ChargeType.java deleted file mode 100644 index 769b451..0000000 --- a/src/main/java/tobinio/realarrowtip/model/charge/ChargeType.java +++ /dev/null @@ -1,29 +0,0 @@ -package tobinio.realarrowtip.model.charge; - -import com.mojang.serialization.Codec; -import net.minecraft.item.ItemStack; -import net.minecraft.item.Items; -import net.minecraft.util.StringIdentifiable; - -import java.util.List; - -/** - * Created: 18.12.24 - * - * @author Tobias Frischmann - */ -public enum ChargeType implements StringIdentifiable { - NONE("none"), ARROW("arrow"), SPECTRAL("spectral"), TIPPED("tipped"), ROCKET("rocket"); - - public static final Codec CODEC = StringIdentifiable.createCodec(ChargeType::values); - private final String name; - - ChargeType(final String name) { - this.name = name; - } - - @Override - public String asString() { - return this.name; - } -} diff --git a/src/main/resources/assets/minecraft/items/bow.json b/src/main/resources/assets/minecraft/items/bow.json index 1548556..42e8c43 100644 --- a/src/main/resources/assets/minecraft/items/bow.json +++ b/src/main/resources/assets/minecraft/items/bow.json @@ -1,16 +1,21 @@ { "model": { "type": "minecraft:condition", + "property": "minecraft:using_item", "on_false": { "type": "minecraft:model", "model": "minecraft:item/bow" }, "on_true": { "type": "minecraft:select", + "property": "real_arrow_tip:charge_type", "cases": [ { + "when": "minecraft:spectral_arrow", "model": { "type": "minecraft:range_dispatch", + "property": "minecraft:use_duration", + "scale": 0.05, "entries": [ { "model": { @@ -30,15 +35,15 @@ "fallback": { "type": "minecraft:model", "model": "real_arrow_tip:item/bow_pulling_0_spectral" - }, - "property": "minecraft:use_duration", - "scale": 0.05 - }, - "when": "spectral" + } + } }, { + "when": "minecraft:tipped_arrow", "model": { "type": "minecraft:range_dispatch", + "property": "minecraft:use_duration", + "scale": 0.05, "entries": [ { "model": { @@ -76,15 +81,14 @@ "default": -13083194 } ] - }, - "property": "minecraft:use_duration", - "scale": 0.05 - }, - "when": "tipped" + } + } } ], "fallback": { "type": "minecraft:range_dispatch", + "property": "minecraft:use_duration", + "scale": 0.05, "entries": [ { "model": { @@ -104,12 +108,8 @@ "fallback": { "type": "minecraft:model", "model": "minecraft:item/bow_pulling_0" - }, - "property": "minecraft:use_duration", - "scale": 0.05 - }, - "property": "real_arrow_tip:charge_type" - }, - "property": "minecraft:using_item" + } + } + } } } \ No newline at end of file diff --git a/src/main/resources/assets/minecraft/items/crossbow.json b/src/main/resources/assets/minecraft/items/crossbow.json index 9744fbe..9c2983f 100644 --- a/src/main/resources/assets/minecraft/items/crossbow.json +++ b/src/main/resources/assets/minecraft/items/crossbow.json @@ -1,27 +1,28 @@ { "model": { "type": "minecraft:select", + "property": "real_arrow_tip:charge_type", "cases": [ { "model": { "type": "minecraft:model", "model": "minecraft:item/crossbow_arrow" }, - "when": "arrow" + "when": "minecraft:arrow" }, { "model": { "type": "minecraft:model", "model": "minecraft:item/crossbow_firework" }, - "when": "rocket" + "when": "minecraft:firework_rocket" }, { "model": { "type": "minecraft:model", "model": "real_arrow_tip:item/crossbow_spectral" }, - "when": "spectral" + "when": "minecraft:spectral_arrow" }, { "model": { @@ -34,11 +35,12 @@ } ] }, - "when": "tipped" + "when": "minecraft:tipped_arrow" } ], "fallback": { "type": "minecraft:condition", + "property": "minecraft:using_item", "on_false": { "type": "minecraft:model", "model": "minecraft:item/crossbow" @@ -66,9 +68,7 @@ "model": "minecraft:item/crossbow_pulling_0" }, "property": "minecraft:crossbow/pull" - }, - "property": "minecraft:using_item" - }, - "property": "real_arrow_tip:charge_type" + } + } } -} +} \ No newline at end of file -- 2.51.2