Skip to content

Commit

Permalink
Added SizedIngredient.of(string), mapped all vanilla server registrie…
Browse files Browse the repository at this point in the history
…s, implemented RecipeComponentBuilder `{key?!: component}` syntax
  • Loading branch information
LatvianModder committed Jul 12, 2024
1 parent 8300ce7 commit afc5679
Show file tree
Hide file tree
Showing 22 changed files with 281 additions and 128 deletions.
61 changes: 58 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/BuiltinKubeJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
import dev.latvian.mods.kubejs.recipe.schema.minecraft.ShapelessKubeRecipe;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEvents;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.registry.ServerRegistryRegistry;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.DataComponentTypeInfoRegistry;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
Expand Down Expand Up @@ -160,6 +161,7 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.ChatType;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
Expand All @@ -170,22 +172,46 @@
import net.minecraft.util.valueproviders.FloatProvider;
import net.minecraft.util.valueproviders.IntProvider;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.entity.animal.WolfVariant;
import net.minecraft.world.entity.decoration.PaintingVariant;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.JukeboxSong;
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.Fireworks;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.ItemEnchantments;
import net.minecraft.world.item.enchantment.providers.EnchantmentProvider;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.MultiNoiseBiomeSourceParameterList;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BannerPattern;
import net.minecraft.world.level.block.state.properties.BlockSetType;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.dimension.DimensionType;
import net.minecraft.world.level.levelgen.DensityFunction;
import net.minecraft.world.level.levelgen.NoiseGeneratorSettings;
import net.minecraft.world.level.levelgen.carver.ConfiguredWorldCarver;
import net.minecraft.world.level.levelgen.feature.ConfiguredFeature;
import net.minecraft.world.level.levelgen.flat.FlatLevelGeneratorPreset;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.minecraft.world.level.levelgen.presets.WorldPreset;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.StructureSet;
import net.minecraft.world.level.levelgen.structure.pools.StructureTemplatePool;
import net.minecraft.world.level.levelgen.structure.templatesystem.RuleTest;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorList;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
import net.minecraft.world.level.levelgen.synth.NormalNoise;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.functions.CopyNameFunction;
Expand Down Expand Up @@ -266,20 +292,49 @@ public void registerBuilderTypes(BuilderTypeRegistry registry) {
});

registry.addDefault(NeoForgeRegistries.Keys.FLUID_TYPES, FluidTypeBuilder.class, FluidTypeBuilder::new);
// FIXME registry.addDefault(Registries.ENCHANTMENT, EnchantmentBuilder.class, EnchantmentBuilder::new);
registry.addDefault(Registries.MOB_EFFECT, BasicMobEffect.Builder.class, BasicMobEffect.Builder::new);
registry.addDefault(Registries.POTION, PotionBuilder.class, PotionBuilder::new);
registry.addDefault(Registries.PARTICLE_TYPE, ParticleTypeBuilder.class, ParticleTypeBuilder::new);
registry.addDefault(Registries.PAINTING_VARIANT, PaintingVariantBuilder.class, PaintingVariantBuilder::new);
registry.addDefault(Registries.CUSTOM_STAT, CustomStatBuilder.class, CustomStatBuilder::new);
registry.addDefault(Registries.POINT_OF_INTEREST_TYPE, PoiTypeBuilder.class, PoiTypeBuilder::new);
registry.addDefault(Registries.VILLAGER_TYPE, VillagerTypeBuilder.class, VillagerTypeBuilder::new);
registry.addDefault(Registries.VILLAGER_PROFESSION, VillagerProfessionBuilder.class, VillagerProfessionBuilder::new);
registry.addDefault(Registries.CREATIVE_MODE_TAB, CreativeTabBuilder.class, CreativeTabBuilder::new);
registry.addDefault(Registries.ARMOR_MATERIAL, ArmorMaterialBuilder.class, ArmorMaterialBuilder::new);

// FIXME registry.addDefault(Registries.ENCHANTMENT, EnchantmentBuilder.class, EnchantmentBuilder::new);
registry.addDefault(Registries.PAINTING_VARIANT, PaintingVariantBuilder.class, PaintingVariantBuilder::new);
registry.addDefault(Registries.JUKEBOX_SONG, JukeboxSongBuilder.class, JukeboxSongBuilder::new);
}

registry.serverRegistry(Registries.PAINTING_VARIANT, PaintingVariant.DIRECT_CODEC, PaintingVariant.class);
@Override
public void registerServerRegistries(ServerRegistryRegistry registry) {
// VanillaRegistries
registry.register(Registries.DIMENSION_TYPE, DimensionType.DIRECT_CODEC, DimensionType.class);
registry.register(Registries.CONFIGURED_CARVER, ConfiguredWorldCarver.DIRECT_CODEC, TypeInfo.of(ConfiguredWorldCarver.class));
registry.register(Registries.CONFIGURED_FEATURE, ConfiguredFeature.DIRECT_CODEC, TypeInfo.of(ConfiguredFeature.class));
registry.register(Registries.PLACED_FEATURE, PlacedFeature.DIRECT_CODEC, PlacedFeature.class);
registry.register(Registries.STRUCTURE, Structure.DIRECT_CODEC, Structure.class);
registry.register(Registries.STRUCTURE_SET, StructureSet.DIRECT_CODEC, StructureSet.class);
registry.register(Registries.PROCESSOR_LIST, StructureProcessorType.DIRECT_CODEC, StructureProcessorList.class);
registry.register(Registries.TEMPLATE_POOL, StructureTemplatePool.DIRECT_CODEC, StructureTemplatePool.class);
registry.register(Registries.BIOME, Biome.DIRECT_CODEC, Biome.class);
registry.register(Registries.MULTI_NOISE_BIOME_SOURCE_PARAMETER_LIST, MultiNoiseBiomeSourceParameterList.DIRECT_CODEC, MultiNoiseBiomeSourceParameterList.class);
registry.register(Registries.NOISE, NormalNoise.NoiseParameters.DIRECT_CODEC, NormalNoise.NoiseParameters.class);
registry.register(Registries.DENSITY_FUNCTION, DensityFunction.DIRECT_CODEC, DensityFunction.class);
registry.register(Registries.NOISE_SETTINGS, NoiseGeneratorSettings.DIRECT_CODEC, NoiseGeneratorSettings.class);
registry.register(Registries.WORLD_PRESET, WorldPreset.DIRECT_CODEC, WorldPreset.class);
registry.register(Registries.FLAT_LEVEL_GENERATOR_PRESET, FlatLevelGeneratorPreset.DIRECT_CODEC, FlatLevelGeneratorPreset.class);
registry.register(Registries.CHAT_TYPE, ChatType.DIRECT_CODEC, ChatType.class);
registry.register(Registries.TRIM_PATTERN, TrimPattern.DIRECT_CODEC, TrimPattern.class);
registry.register(Registries.TRIM_MATERIAL, TrimMaterial.DIRECT_CODEC, TrimMaterial.class);
registry.register(Registries.WOLF_VARIANT, WolfVariant.DIRECT_CODEC, WolfVariant.class);
registry.register(Registries.PAINTING_VARIANT, PaintingVariant.DIRECT_CODEC, PaintingVariant.class);
registry.register(Registries.DAMAGE_TYPE, DamageType.DIRECT_CODEC, DamageType.class);
registry.register(Registries.BANNER_PATTERN, BannerPattern.DIRECT_CODEC, BannerPattern.class);
registry.register(Registries.ENCHANTMENT, Enchantment.DIRECT_CODEC, Enchantment.class);
registry.register(Registries.ENCHANTMENT_PROVIDER, EnchantmentProvider.DIRECT_CODEC, EnchantmentProvider.class);
registry.register(Registries.JUKEBOX_SONG, JukeboxSong.DIRECT_CODEC, JukeboxSong.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
Expand Down Expand Up @@ -98,7 +97,11 @@ public static List<String> getTypeList() {
public static List<ResourceLocation> getTaggedIds(ResourceLocation tag) {
return Util.make(new LinkedList<>(), list -> {
for (var holder : BuiltInRegistries.BLOCK.getTagOrEmpty(Tags.block(tag))) {
holder.unwrapKey().map(ResourceKey::location).ifPresent(list::add);
var l = holder.getKey();

if (l != null) {
list.add(l.location());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public interface SizedIngredientWrapper {
@Info("An ingredient that matches everything")
SizedIngredient all = new SizedIngredient(IngredientWrapper.all, 1);

@Info("Returns a sized ingredient of the input")
static SizedIngredient of(SizedIngredient ingredient) {
return ingredient;
}

@Info("Returns a sized ingredient of the input")
static SizedIngredient of(Ingredient ingredient, int count) {
return new SizedIngredient(ingredient, count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public interface RegistryObjectKJS<T> {

default ResourceKey<T> kjs$getKey() {
try {
var h = kjs$asHolder();
return h instanceof Holder.Reference ref ? ref.key() : h.unwrapKey().orElseThrow();
return kjs$asHolder().getKey();
} catch (Exception ex) {
return kjs$getRegistry().getResourceKey((T) this).orElseThrow();
}
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/item/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,21 @@ public ItemBuilder name(NameCallback name) {
Set the food properties of the item.
""")
public ItemBuilder food(Consumer<FoodBuilder> b) {
foodBuilder = new FoodBuilder();
if (foodBuilder == null) {
foodBuilder = new FoodBuilder();
}

b.accept(foodBuilder);
return this;
}

@Info("""
Set the food nutrition and saturation of the item.
""")
public ItemBuilder food(int nutrition, float saturation) {
return food(b -> b.nutrition(nutrition).saturation(saturation));
}

@Info("Makes the item fire resistant like netherite tools (or not).")
public ItemBuilder fireResistant(boolean isFireResistant) {
fireResistant = isFireResistant;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ public EntityArrayList getPlayersInRadius() {
}

public ResourceLocation getBiomeId() {
return minecraftLevel.getBiome(pos).unwrapKey().orElse(Biomes.PLAINS).location();
var k = minecraftLevel.getBiome(pos).getKey();
return k == null ? Biomes.PLAINS.location() : k.location();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaRegistry;
import dev.latvian.mods.kubejs.recipe.viewer.RecipeViewerEntryType;
import dev.latvian.mods.kubejs.registry.BuilderTypeRegistry;
import dev.latvian.mods.kubejs.registry.ServerRegistryRegistry;
import dev.latvian.mods.kubejs.script.BindingRegistry;
import dev.latvian.mods.kubejs.script.DataComponentTypeInfoRegistry;
import dev.latvian.mods.kubejs.script.ScriptManager;
Expand Down Expand Up @@ -45,6 +46,9 @@ default void afterInit() {
default void registerBuilderTypes(BuilderTypeRegistry registry) {
}

default void registerServerRegistries(ServerRegistryRegistry registry) {
}

/**
* Call {@link EventGroupRegistry#register(EventGroup)} for event groups your mod adds
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import dev.latvian.mods.kubejs.core.RecipeLikeKJS;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.error.MissingComponentException;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentBuilderMap;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValue;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValueMap;
import dev.latvian.mods.kubejs.recipe.ingredientaction.ConsumeAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.CustomIngredientAction;
import dev.latvian.mods.kubejs.recipe.ingredientaction.DamageAction;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class KubeRecipe implements RecipeLikeKJS, CustomJavaToJsWrapper {
public SourceLine sourceLine = SourceLine.UNKNOWN;
public String modifyResult = "";

private RecipeComponentBuilderMap valueMap = RecipeComponentBuilderMap.EMPTY;
private RecipeComponentValueMap valueMap = RecipeComponentValueMap.EMPTY;
private RecipeComponentValue<?>[] inputValues;
private RecipeComponentValue<?>[] outputValues;

Expand Down Expand Up @@ -156,7 +156,7 @@ public void initValues(boolean created) {
}

if (!type.schemaType.schema.keys.isEmpty()) {
valueMap = new RecipeComponentBuilderMap(type.schemaType.schema.keys);
valueMap = new RecipeComponentValueMap(type.schemaType.schema.keys);

if (created) {
for (var v : valueMap.holders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,6 @@ public Either<H, L> replace(Context cx, KubeRecipe recipe, Either<H, L> original
}
}

@Override
public boolean checkValueHasChanged(Either<H, L> oldValue, Either<H, L> newValue) {
if (oldValue != null && newValue != null) {
var left = oldValue.left();

if (left.isPresent()) {
if (high.checkValueHasChanged(left.get(), newValue.left().get())) {
return true;
}
} else if (low.checkValueHasChanged(oldValue.right().get(), newValue.right().get())) {
return true;
}
}

return oldValue != newValue;
}

@Override
public void buildUniqueId(UniqueIdBuilder builder, Either<H, L> value) {
var left = value.left();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,12 @@
*/
@Nullable
public interface RecipeComponent<T> {
static RecipeComponentBuilder builder() {
return new RecipeComponentBuilder(4);
static RecipeComponentBuilder builder(List<RecipeComponentBuilder.Key> keys) {
return new RecipeComponentBuilder(keys);
}

static RecipeComponentBuilder builder(RecipeKey<?>... key) {
var b = new RecipeComponentBuilder(key.length);

for (var k : key) {
b.add(k);
}

return b;
static RecipeComponentBuilder builder(RecipeComponentBuilder.Key... keys) {
return new RecipeComponentBuilder(List.of(keys));
}

/**
Expand Down Expand Up @@ -196,10 +190,6 @@ default String checkEmpty(RecipeKey<T> key, T value) {
return "";
}

default boolean checkValueHasChanged(T oldValue, T newValue) {
return oldValue != newValue;
}

default void buildUniqueId(UniqueIdBuilder builder, T value) {
builder.append(value.toString());
}
Expand Down
Loading

0 comments on commit afc5679

Please sign in to comment.