Skip to content

Commit

Permalink
Removed ChancedIngredient, added type wrapped for ChancedItem
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Jul 3, 2024
1 parent 6aad24c commit fad66ff
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import dev.latvian.mods.kubejs.fluid.ThickFluidBuilder;
import dev.latvian.mods.kubejs.fluid.ThinFluidBuilder;
import dev.latvian.mods.kubejs.item.ArmorMaterialBuilder;
import dev.latvian.mods.kubejs.item.ChancedIngredient;
import dev.latvian.mods.kubejs.item.ChancedItem;
import dev.latvian.mods.kubejs.item.ItemEnchantmentsWrapper;
import dev.latvian.mods.kubejs.item.ItemPredicate;
Expand Down Expand Up @@ -528,6 +527,7 @@ public void registerTypeWrappers(TypeWrapperRegistry registry) {
registry.register(ItemTintFunction.class, ItemTintFunction::of);
registry.register(BlockTintFunction.class, BlockTintFunction::of);
registry.register(RegistryInfo.class, RegistryInfo::wrap);
registry.register(ChancedItem.class, ChancedItem::wrap);

// components //
registry.register(Component.class, TextWrapper::of);
Expand Down Expand Up @@ -583,7 +583,6 @@ public void registerRecipeComponents(RecipeComponentFactoryRegistry registry) {
registry.register(SizedFluidIngredientComponent.NESTED);

registry.register(ChancedItem.RECIPE_COMPONENT);
registry.register(ChancedIngredient.RECIPE_COMPONENT);

registry.register(BlockComponent.BLOCK);

Expand Down
6 changes: 0 additions & 6 deletions src/main/java/dev/latvian/mods/kubejs/core/IngredientKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import com.mojang.serialization.Codec;
import dev.latvian.mods.kubejs.bindings.SizedIngredientWrapper;
import dev.latvian.mods.kubejs.ingredient.WildcardIngredient;
import dev.latvian.mods.kubejs.item.ChancedIngredient;
import dev.latvian.mods.kubejs.item.ItemPredicate;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.kubejs.util.WithCodec;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.util.valueproviders.FloatProvider;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.crafting.CompoundIngredient;
Expand Down Expand Up @@ -54,10 +52,6 @@ public interface IngredientKJS extends ItemPredicate, Replaceable, WithCodec {
return new SizedIngredient(kjs$self(), count);
}

default ChancedIngredient kjs$withChance(FloatProvider chance) {
return new ChancedIngredient(kjs$self(), 1, chance);
}

@Override
default boolean kjs$isWildcard() {
return kjs$self().getCustomIngredient() == WildcardIngredient.INSTANCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package dev.latvian.mods.kubejs.core;

import dev.latvian.mods.kubejs.item.ChancedIngredient;
import dev.latvian.mods.kubejs.item.ingredient.IngredientJS;
import dev.latvian.mods.kubejs.recipe.match.Replaceable;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.rhino.Context;
import net.minecraft.util.valueproviders.FloatProvider;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.crafting.SizedIngredient;

Expand All @@ -25,10 +23,6 @@ default Object replaceThisWith(Context cx, Object with) {
return this;
}

default ChancedIngredient kjs$withChance(FloatProvider chance) {
return new ChancedIngredient(kjs$self().ingredient(), kjs$self().count(), chance);
}

@Override
default Ingredient kjs$asIngredient() {
return kjs$self().ingredient();
Expand Down
40 changes: 0 additions & 40 deletions src/main/java/dev/latvian/mods/kubejs/item/ChancedIngredient.java

This file was deleted.

18 changes: 17 additions & 1 deletion src/main/java/dev/latvian/mods/kubejs/item/ChancedItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponent;
import dev.latvian.mods.kubejs.recipe.component.SimpleRecipeComponent;
import dev.latvian.mods.kubejs.script.KubeJSContext;
import dev.latvian.mods.rhino.Context;
import dev.latvian.mods.rhino.type.RecordTypeInfo;
import dev.latvian.mods.rhino.type.TypeInfo;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
Expand All @@ -14,9 +17,12 @@
import net.minecraft.world.item.ItemStack;

public record ChancedItem(ItemStack item, FloatProvider chance) {
public static final RecordTypeInfo TYPE_INFO = (RecordTypeInfo) TypeInfo.of(ChancedItem.class);
public static final FloatProvider DEFAULT_CHANCE = ConstantFloat.of(1F);

public static final MapCodec<ChancedItem> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
ItemStack.CODEC.fieldOf("item").forGetter(ChancedItem::item),
FloatProvider.CODEC.optionalFieldOf("chance", ConstantFloat.of(1F)).forGetter(ChancedItem::chance)
FloatProvider.CODEC.optionalFieldOf("chance", DEFAULT_CHANCE).forGetter(ChancedItem::chance)
).apply(instance, ChancedItem::new));

public static final StreamCodec<RegistryFriendlyByteBuf, ChancedItem> STREAM_CODEC = StreamCodec.composite(
Expand All @@ -27,6 +33,16 @@ public record ChancedItem(ItemStack item, FloatProvider chance) {

public static final RecipeComponent<ChancedItem> RECIPE_COMPONENT = new SimpleRecipeComponent<>("chanced_item", CODEC.codec(), TypeInfo.of(ChancedItem.class));

public static ChancedItem wrap(Context cx, Object from) {
if (from instanceof ItemStack is) {
return new ChancedItem(is, DEFAULT_CHANCE);
} else if (from instanceof CharSequence) {
return new ChancedItem(ItemStackJS.wrap(((KubeJSContext) cx).getRegistries(), from), DEFAULT_CHANCE);
} else {
return (ChancedItem) TYPE_INFO.wrap(cx, from, TYPE_INFO);
}
}

public boolean test(RandomSource random) {
return random.nextFloat() < chance.sample(random);
}
Expand Down

0 comments on commit fad66ff

Please sign in to comment.