Skip to content

Commit

Permalink
Fixed various tag issues in item registry, added few more type wrappe…
Browse files Browse the repository at this point in the history
…rs, updated MDG
  • Loading branch information
LatvianModder committed Aug 12, 2024
1 parent 3b7ed56 commit a495db5
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TagInstance> {
public enum Type {
BLOCK('B'),
ITEM('J'),
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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;
import net.minecraft.world.item.Item;
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;

Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public void add(String id, Consumer<MutableToolTier> tier) {
tiers.put(id, t);
}

public void addBasedOnExisting(String id, String existing, Consumer<MutableToolTier> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends BiFunction<Context, Object, T> {
Expand Down Expand Up @@ -96,4 +99,17 @@ public <T> void registerMapCodec(Class<T> target, MapCodec<T> codec, T defaultVa
public <T> void registerMapCodec(Class<T> target, MapCodec<T> codec) {
registerMapCodec(target, codec, null);
}
}

public <F, T> void registerAlias(Class<T> target, Class<F> from, Function<F, T> converter) {
registerAlias(target, TypeInfo.of(from), converter);
}

public <F, T> void registerAlias(Class<T> target, TypeInfo from, @Nullable Function<F, T> 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)));
});
}
}
}

0 comments on commit a495db5

Please sign in to comment.