diff --git a/build.gradle b/build.gradle index 1eda91ec1..aa22424f5 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { id 'base' id 'java' id 'maven-publish' - id 'net.neoforged.moddev' version "1.0.14" + id 'net.neoforged.moddev' version "2.0.10-beta" // https://maven.architectury.dev/me/shedaniel/unified-publishing/maven-metadata.xml id "me.shedaniel.unified-publishing" version "0.1.13" } diff --git a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java index e881dca26..d98ef2424 100644 --- a/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java +++ b/src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java @@ -177,8 +177,10 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.stats.Stat; import net.minecraft.stats.Stats; +import net.minecraft.util.Unit; import net.minecraft.util.valueproviders.FloatProvider; import net.minecraft.util.valueproviders.IntProvider; +import net.minecraft.world.LockCode; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.damagesource.DamageType; import net.minecraft.world.entity.MobCategory; @@ -192,6 +194,9 @@ import net.minecraft.world.item.Tier; import net.minecraft.world.item.armortrim.TrimMaterial; import net.minecraft.world.item.armortrim.TrimPattern; +import net.minecraft.world.item.component.BlockItemStateProperties; +import net.minecraft.world.item.component.CustomData; +import net.minecraft.world.item.component.CustomModelData; import net.minecraft.world.item.component.Fireworks; import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.enchantment.Enchantment; @@ -608,6 +613,13 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) { // codecs registry.registerCodec(Fireworks.class, Fireworks.CODEC); + + // alias + registry.registerAlias(Unit.class, TypeInfo.NONE, o -> Unit.INSTANCE); + registry.registerAlias(CustomData.class, CompoundTag.class, CustomData::of); + registry.registerAlias(CustomModelData.class, TypeInfo.PRIMITIVE_INT, CustomModelData::new); + registry.registerAlias(LockCode.class, TypeInfo.STRING, LockCode::new); + registry.registerAlias(BlockItemStateProperties.class, TypeInfo.RAW_MAP.withParams(TypeInfo.STRING, TypeInfo.STRING), BlockItemStateProperties::new); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java index f802cde62..3a9a43ccb 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/KubeJSGameClientEventHandler.java @@ -222,8 +222,8 @@ public static void onItemTooltip(ItemTooltipEvent event) { } } - for (var instance : tempTagNames.values()) { - lines.add(instance.toText()); + if (!tempTagNames.isEmpty()) { + tempTagNames.values().stream().sorted().map(TagInstance::toText).forEach(lines::add); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/client/TagInstance.java b/src/main/java/dev/latvian/mods/kubejs/client/TagInstance.java index 8ea7f3efd..a3d1bedc7 100644 --- a/src/main/java/dev/latvian/mods/kubejs/client/TagInstance.java +++ b/src/main/java/dev/latvian/mods/kubejs/client/TagInstance.java @@ -5,13 +5,14 @@ import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; +import org.jetbrains.annotations.NotNull; import java.util.LinkedHashSet; import java.util.Map; import java.util.Set; import java.util.stream.Stream; -public class TagInstance { +public class TagInstance implements Comparable { public enum Type { BLOCK('B'), ITEM('J'), @@ -50,4 +51,9 @@ public Component toText() { .append(TextWrapper.darkGray(Component.literal("#" + tag))) .append(TextIcons.icon(Component.literal(sb.toString()))); } + + @Override + public int compareTo(@NotNull TagInstance o) { + return tag.compareNamespaced(o.tag); + } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java index b311691e3..8ce9cc833 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/custom/ArmorItemBuilder.java @@ -4,6 +4,7 @@ import dev.latvian.mods.rhino.util.ReturnsSelf; import net.minecraft.core.Holder; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.ItemTags; import net.minecraft.world.item.AnimalArmorItem; import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.ArmorMaterial; @@ -13,27 +14,46 @@ @ReturnsSelf public class ArmorItemBuilder extends ItemBuilder { public static class Helmet extends ArmorItemBuilder { + public static final ResourceLocation[] HELMET_TAGS = { + ItemTags.HEAD_ARMOR.location(), + }; + public Helmet(ResourceLocation i) { super(i, ArmorItem.Type.HELMET); + tag(HELMET_TAGS); } } public static class Chestplate extends ArmorItemBuilder { + public static final ResourceLocation[] CHESTPLATE_TAGS = { + ItemTags.CHEST_ARMOR.location(), + }; + public Chestplate(ResourceLocation i) { super(i, ArmorItem.Type.CHESTPLATE); + tag(CHESTPLATE_TAGS); } } public static class Leggings extends ArmorItemBuilder { + public static final ResourceLocation[] LEGGING_TAGS = { + ItemTags.LEG_ARMOR.location(), + }; public Leggings(ResourceLocation i) { super(i, ArmorItem.Type.LEGGINGS); + tag(LEGGING_TAGS); } } public static class Boots extends ArmorItemBuilder { + public static final ResourceLocation[] BOOT_TAGS = { + ItemTags.FOOT_ARMOR.location(), + }; + public Boots(ResourceLocation i) { super(i, ArmorItem.Type.BOOTS); + tag(BOOT_TAGS); } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/DiggerItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/DiggerItemBuilder.java index 68150d03f..52e6cf5c8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/DiggerItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/custom/DiggerItemBuilder.java @@ -1,6 +1,7 @@ package dev.latvian.mods.kubejs.item.custom; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.ItemTags; import net.minecraft.world.item.AxeItem; import net.minecraft.world.item.DiggerItem; import net.minecraft.world.item.HoeItem; @@ -8,6 +9,7 @@ import net.minecraft.world.item.PickaxeItem; import net.minecraft.world.item.ShovelItem; import net.minecraft.world.item.Tier; +import net.neoforged.neoforge.common.Tags; import java.util.function.BiFunction; @@ -26,30 +28,52 @@ public Item createObject() { } public static class Pickaxe extends DiggerItemBuilder { + public static final ResourceLocation[] PICKAXE_TAGS = { + ItemTags.PICKAXES.location(), + ItemTags.CLUSTER_MAX_HARVESTABLES.location(), + Tags.Items.MINING_TOOL_TOOLS.location(), + }; + public Pickaxe(ResourceLocation i) { super(i, 1F, -2.8F, PickaxeItem::new); parentModel = "minecraft:item/iron_pickaxe"; + tag(PICKAXE_TAGS); } } public static class Shovel extends DiggerItemBuilder { + public static final ResourceLocation[] SHOVEL_TAGS = { + ItemTags.SHOVELS.location(), + }; + public Shovel(ResourceLocation i) { super(i, 1.5F, -3F, ShovelItem::new); parentModel = "minecraft:item/iron_shovel"; + tag(SHOVEL_TAGS); } } public static class Axe extends DiggerItemBuilder { + public static final ResourceLocation[] AXE_TAGS = { + ItemTags.AXES.location(), + }; + public Axe(ResourceLocation i) { super(i, 6F, -3.1F, AxeItem::new); parentModel = "minecraft:item/iron_axe"; + tag(AXE_TAGS); } } public static class Hoe extends DiggerItemBuilder { + public static final ResourceLocation[] HOE_TAGS = { + ItemTags.HOES.location(), + }; + public Hoe(ResourceLocation i) { super(i, 0F, -3F, HoeItem::new); parentModel = "minecraft:item/iron_hoe"; + tag(HOE_TAGS); } } } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/ItemToolTierRegistryKubeEvent.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/ItemToolTierRegistryKubeEvent.java index 41da59048..80a266d13 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/ItemToolTierRegistryKubeEvent.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/custom/ItemToolTierRegistryKubeEvent.java @@ -20,6 +20,12 @@ public void add(String id, Consumer tier) { tiers.put(id, t); } + public void addBasedOnExisting(String id, String existing, Consumer tier) { + var t = new MutableToolTier(tiers.getOrDefault(existing, Tiers.IRON)); + tier.accept(t); + tiers.put(id, t); + } + public void addExisting(String id, Tier tier) { tiers.put(id, tier); } diff --git a/src/main/java/dev/latvian/mods/kubejs/item/custom/SwordItemBuilder.java b/src/main/java/dev/latvian/mods/kubejs/item/custom/SwordItemBuilder.java index acc9aee77..c18959cc8 100644 --- a/src/main/java/dev/latvian/mods/kubejs/item/custom/SwordItemBuilder.java +++ b/src/main/java/dev/latvian/mods/kubejs/item/custom/SwordItemBuilder.java @@ -1,14 +1,20 @@ package dev.latvian.mods.kubejs.item.custom; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.ItemTags; import net.minecraft.world.item.Item; import net.minecraft.world.item.SwordItem; public class SwordItemBuilder extends HandheldItemBuilder { + public static final ResourceLocation[] SWORD_TAGS = { + ItemTags.SWORDS.location(), + }; + public SwordItemBuilder(ResourceLocation i) { super(i, 3F, -2.4F); itemAttributeModifiers = SwordItem.createAttributes(toolTier, attackDamageBaseline, speedBaseline); parentModel = "minecraft:item/iron_sword"; + tag(SWORD_TAGS); } @Override diff --git a/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java b/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java index a09e6624b..a4d5ef471 100644 --- a/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java +++ b/src/main/java/dev/latvian/mods/kubejs/script/TypeWrapperRegistry.java @@ -7,13 +7,16 @@ import dev.latvian.mods.kubejs.util.RegistryAccessContainer; import dev.latvian.mods.rhino.Context; import dev.latvian.mods.rhino.Wrapper; +import dev.latvian.mods.rhino.type.TypeInfo; import dev.latvian.mods.rhino.util.wrap.DirectTypeWrapperFactory; import dev.latvian.mods.rhino.util.wrap.TypeWrapperFactory; import dev.latvian.mods.rhino.util.wrap.TypeWrapperValidator; import dev.latvian.mods.rhino.util.wrap.TypeWrappers; import net.minecraft.util.StringRepresentable; +import org.jetbrains.annotations.Nullable; import java.util.function.BiFunction; +import java.util.function.Function; public class TypeWrapperRegistry { public interface ContextFromFunction extends BiFunction { @@ -96,4 +99,17 @@ public void registerMapCodec(Class target, MapCodec codec, T defaultVa public void registerMapCodec(Class target, MapCodec codec) { registerMapCodec(target, codec, null); } -} + + public void registerAlias(Class target, Class from, Function converter) { + registerAlias(target, TypeInfo.of(from), converter); + } + + public void registerAlias(Class target, TypeInfo from, @Nullable Function converter) { + if (converter != null) { + typeWrappers.register(target, (cx, f, typeInfo) -> { + var o1 = cx.jsToJava(f, from); + return o1 == null ? null : Cast.to(converter.apply(Cast.to(o1))); + }); + } + } +} \ No newline at end of file