mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-19 11:59:25 +00:00
Revive custom ingredient types.
This commit is contained in:
47 files changed
+1891
-1353
No files matched your search
@@ -25,6 +25,7 @@ import com.google.common.collect.Multimap;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.DataResult;
|
||||
@@ -34,6 +35,7 @@ import com.mojang.serialization.DynamicOps;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
import com.mojang.serialization.Lifecycle;
|
||||
|
||||
import io.netty.handler.codec.DecoderException;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
@@ -46,6 +48,7 @@ import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.particles.ParticleTypes;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.ClickEvent;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.MutableComponent;
|
||||
@@ -55,6 +58,7 @@ import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.tags.BlockTags;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.util.CrudeIncrementalIntIdentityHashBiMap;
|
||||
import net.minecraft.util.ExtraCodecs;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.util.datafix.fixes.StructuresBecomeConfiguredFix;
|
||||
@@ -118,6 +122,7 @@ import net.minecraft.world.level.biome.BiomeSpecialEffects;
|
||||
import net.minecraft.world.level.biome.BiomeGenerationSettings;
|
||||
import net.minecraft.world.level.biome.MobSpawnSettings;
|
||||
import net.minecraftforge.common.crafting.conditions.ICondition;
|
||||
import net.minecraftforge.common.crafting.ingredients.IIngredientSerializer;
|
||||
import net.minecraftforge.common.util.BlockSnapshot;
|
||||
import net.minecraftforge.common.util.BrainBuilder;
|
||||
import net.minecraftforge.common.util.Lazy;
|
||||
@@ -856,7 +861,7 @@ public class ForgeHooks {
|
||||
EntityDataSerializer<?> serializer = vanilla.byId(id);
|
||||
if (serializer == null) {
|
||||
// ForgeRegistries.DATA_SERIALIZERS is a deferred register now, so if this method is called too early, the registry will be null
|
||||
ForgeRegistry<EntityDataSerializer<?>> registry = (ForgeRegistry<EntityDataSerializer<?>>) ForgeRegistries.ENTITY_DATA_SERIALIZERS.get();
|
||||
var registry = (ForgeRegistry<EntityDataSerializer<?>>)ForgeRegistries.ENTITY_DATA_SERIALIZERS.get();
|
||||
if (registry != null)
|
||||
serializer = registry.getValue(id);
|
||||
}
|
||||
@@ -867,7 +872,7 @@ public class ForgeHooks {
|
||||
int id = vanilla.getId(serializer);
|
||||
if (id < 0) {
|
||||
// ForgeRegistries.DATA_SERIALIZERS is a deferred register now, so if this method is called too early, the registry will be null
|
||||
ForgeRegistry<EntityDataSerializer<?>> registry = (ForgeRegistry<EntityDataSerializer<?>>) ForgeRegistries.ENTITY_DATA_SERIALIZERS.get();
|
||||
var registry = (ForgeRegistry<EntityDataSerializer<?>>)ForgeRegistries.ENTITY_DATA_SERIALIZERS.get();
|
||||
if (registry != null)
|
||||
id = registry.getID(serializer);
|
||||
}
|
||||
@@ -1313,4 +1318,44 @@ public class ForgeHooks {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static Codec<Ingredient> enhanceIngredientCodec(Codec<Ingredient> vanilla) {
|
||||
return ExtraCodecs.lazyInitializedCodec(() ->
|
||||
ExtraCodecs.<Ingredient, Ingredient>either(
|
||||
ForgeRegistries.INGREDIENT_SERIALIZERS.get().getCodec().dispatch(Ingredient::serializer, IIngredientSerializer::codec),
|
||||
vanilla
|
||||
)
|
||||
.flatComapMap(
|
||||
i -> i.left().isPresent() ? i.left().get() : i.right().get(),
|
||||
i -> DataResult.success(i.isVanilla() ? Either.right(i) : Either.left(i))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
public static <T extends Ingredient> boolean ingredientToNetwork(FriendlyByteBuf buffer, T ingredient) {
|
||||
if (ingredient.isVanilla())
|
||||
return true;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
var serializer = (IIngredientSerializer<T>)ingredient.serializer();
|
||||
var key = ForgeRegistries.INGREDIENT_SERIALIZERS.get().getKey(ingredient.serializer());
|
||||
if (key == null)
|
||||
throw new IllegalArgumentException("Tried to write unregistered Ingredient to network: " + ingredient);
|
||||
|
||||
buffer.writeVarInt(-1); // Our Marker
|
||||
buffer.writeResourceLocation(key);
|
||||
serializer.write(buffer, ingredient);
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Ingredient ingredientFromNetwork(FriendlyByteBuf buffer) {
|
||||
// We have already read our marker
|
||||
var key = buffer.readResourceLocation();
|
||||
var serializer = ForgeRegistries.INGREDIENT_SERIALIZERS.get().getValue(key);
|
||||
if (serializer == null)
|
||||
throw new DecoderException("Could not read ingredient of type: " + key);
|
||||
return serializer.read(buffer);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import net.minecraft.commands.synchronization.ArgumentTypeInfos;
|
||||
import net.minecraft.commands.synchronization.SingletonArgumentInfo;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.data.metadata.PackMetadataGenerator;
|
||||
@@ -31,15 +32,9 @@ import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.item.crafting.RecipeSerializer;
|
||||
import net.minecraft.world.level.BlockAndTintGetter;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.MobSpawnSettings.SpawnerData;
|
||||
import net.minecraft.world.level.levelgen.GenerationStep.Decoration;
|
||||
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
||||
import net.minecraft.world.level.material.Fluid;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.sounds.SoundEvent;
|
||||
import net.minecraft.util.ExtraCodecs;
|
||||
import net.minecraft.core.RegistryCodecs;
|
||||
import net.minecraft.world.level.material.FluidState;
|
||||
import net.minecraft.world.level.pathfinder.BlockPathTypes;
|
||||
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;
|
||||
@@ -93,6 +88,7 @@ import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
import net.minecraft.data.DataGenerator;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.ConditionalRecipe;
|
||||
import net.minecraftforge.common.crafting.conditions.AndCondition;
|
||||
@@ -104,6 +100,12 @@ import net.minecraftforge.common.crafting.conditions.NotCondition;
|
||||
import net.minecraftforge.common.crafting.conditions.OrCondition;
|
||||
import net.minecraftforge.common.crafting.conditions.TagEmptyCondition;
|
||||
import net.minecraftforge.common.crafting.conditions.TrueCondition;
|
||||
import net.minecraftforge.common.crafting.ingredients.CompoundIngredient;
|
||||
import net.minecraftforge.common.crafting.ingredients.DifferenceIngredient;
|
||||
import net.minecraftforge.common.crafting.ingredients.IIngredientSerializer;
|
||||
import net.minecraftforge.common.crafting.ingredients.IntersectionIngredient;
|
||||
import net.minecraftforge.common.crafting.ingredients.PartialNBTIngredient;
|
||||
import net.minecraftforge.common.crafting.ingredients.StrictNBTIngredient;
|
||||
import net.minecraftforge.common.data.ForgeBlockTagsProvider;
|
||||
import net.minecraftforge.common.data.ForgeEntityTypeTagsProvider;
|
||||
import net.minecraftforge.common.data.ForgeItemTagsProvider;
|
||||
@@ -112,15 +114,12 @@ import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@Mod("forge")
|
||||
@@ -129,20 +128,24 @@ public class ForgeMod {
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
private static final Marker FORGEMOD = MarkerManager.getMarker("FORGEMOD");
|
||||
|
||||
private static final DeferredRegister<Attribute> ATTRIBUTES = DeferredRegister.create(ForgeRegistries.Keys.ATTRIBUTES, "forge");
|
||||
private static final DeferredRegister<ArgumentTypeInfo<?, ?>> COMMAND_ARGUMENT_TYPES = DeferredRegister.create(Registries.COMMAND_ARGUMENT_TYPE, "forge");
|
||||
private static final DeferredRegister<Codec<? extends BiomeModifier>> BIOME_MODIFIER_SERIALIZERS = DeferredRegister.create(ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS, "forge");
|
||||
private static final DeferredRegister<Codec<? extends StructureModifier>> STRUCTURE_MODIFIER_SERIALIZERS = DeferredRegister.create(ForgeRegistries.Keys.STRUCTURE_MODIFIER_SERIALIZERS, "forge");
|
||||
private static final DeferredRegister<HolderSetType> HOLDER_SET_TYPES = DeferredRegister.create(ForgeRegistries.Keys.HOLDER_SET_TYPES, "forge");
|
||||
private static final List<DeferredRegister<?>> registries = new ArrayList<>();
|
||||
private static <T> DeferredRegister<T> deferred(ResourceKey<Registry<T>> key) {
|
||||
return deferred(key, "forge");
|
||||
}
|
||||
private static <T> DeferredRegister<T> deferred(ResourceKey<Registry<T>> key, String modid) {
|
||||
var ret = DeferredRegister.create(key, modid);
|
||||
registries.add(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
|
||||
private static final RegistryObject<EnumArgument.Info> ENUM_COMMAND_ARGUMENT_TYPE = COMMAND_ARGUMENT_TYPES.register("enum", () ->
|
||||
ArgumentTypeInfos.registerByClass(EnumArgument.class, new EnumArgument.Info()));
|
||||
@SuppressWarnings("unused")
|
||||
private static final RegistryObject<SingletonArgumentInfo<ModIdArgument>> MODID_COMMAND_ARGUMENT_TYPE = COMMAND_ARGUMENT_TYPES.register("modid", () ->
|
||||
ArgumentTypeInfos.registerByClass(ModIdArgument.class,
|
||||
SingletonArgumentInfo.contextFree(ModIdArgument::modIdArgument)));
|
||||
private static final DeferredRegister<ArgumentTypeInfo<?, ?>> COMMAND_ARGUMENT_TYPES = deferred(Registries.COMMAND_ARGUMENT_TYPE);
|
||||
static {
|
||||
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
|
||||
var v = COMMAND_ARGUMENT_TYPES.register("enum", () -> ArgumentTypeInfos.registerByClass(EnumArgument.class, new EnumArgument.Info()));
|
||||
COMMAND_ARGUMENT_TYPES.register("modid", () -> ArgumentTypeInfos.registerByClass(ModIdArgument.class, SingletonArgumentInfo.contextFree(ModIdArgument::modIdArgument)));
|
||||
}
|
||||
|
||||
private static final DeferredRegister<Attribute> ATTRIBUTES = deferred(ForgeRegistries.Keys.ATTRIBUTES);
|
||||
public static final RegistryObject<Attribute> SWIM_SPEED = ATTRIBUTES.register("swim_speed", () -> new RangedAttribute("forge.swim_speed", 1.0D, 0.0D, 1024.0D).setSyncable(true));
|
||||
public static final RegistryObject<Attribute> NAMETAG_DISTANCE = ATTRIBUTES.register("nametag_distance", () -> new RangedAttribute("forge.name_tag_distance", 64.0D, 0.0D, 64.0).setSyncable(true));
|
||||
public static final RegistryObject<Attribute> ENTITY_GRAVITY = ATTRIBUTES.register("entity_gravity", () -> new RangedAttribute("forge.entity_gravity", 0.08D, -8.0D, 8.0D).setSyncable(true));
|
||||
@@ -169,64 +172,22 @@ public class ForgeMod {
|
||||
*/
|
||||
public static final RegistryObject<Attribute> STEP_HEIGHT_ADDITION = ATTRIBUTES.register("step_height_addition", () -> new RangedAttribute("forge.step_height", 0.0D, -512.0D, 512.0D).setSyncable(true));
|
||||
|
||||
/**
|
||||
* Noop biome modifier. Can be used in a biome modifier json with "type": "forge:none".
|
||||
*/
|
||||
public static final RegistryObject<Codec<NoneBiomeModifier>> NONE_BIOME_MODIFIER_TYPE = BIOME_MODIFIER_SERIALIZERS.register("none", () -> Codec.unit(NoneBiomeModifier.INSTANCE));
|
||||
|
||||
/**
|
||||
* Stock biome modifier for adding features to biomes.
|
||||
*/
|
||||
public static final RegistryObject<Codec<AddFeaturesBiomeModifier>> ADD_FEATURES_BIOME_MODIFIER_TYPE = BIOME_MODIFIER_SERIALIZERS.register("add_features", () ->
|
||||
RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(AddFeaturesBiomeModifier::biomes),
|
||||
PlacedFeature.LIST_CODEC.fieldOf("features").forGetter(AddFeaturesBiomeModifier::features),
|
||||
Decoration.CODEC.fieldOf("step").forGetter(AddFeaturesBiomeModifier::step)
|
||||
).apply(builder, AddFeaturesBiomeModifier::new))
|
||||
);
|
||||
private static final DeferredRegister<Codec<? extends BiomeModifier>> BIOME_MODIFIER_SERIALIZERS = deferred(ForgeRegistries.Keys.BIOME_MODIFIER_SERIALIZERS);
|
||||
static {
|
||||
BIOME_MODIFIER_SERIALIZERS.register("none", () -> NoneBiomeModifier.CODEC);
|
||||
BIOME_MODIFIER_SERIALIZERS.register("add_features", () -> AddFeaturesBiomeModifier.CODEC);
|
||||
BIOME_MODIFIER_SERIALIZERS.register("remove_features", () -> RemoveFeaturesBiomeModifier.CODEC);
|
||||
BIOME_MODIFIER_SERIALIZERS.register("add_spawns", () -> AddSpawnsBiomeModifier.CODEC);
|
||||
BIOME_MODIFIER_SERIALIZERS.register("remove_spawns", () -> RemoveSpawnsBiomeModifier.CODEC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stock biome modifier for removing features from biomes.
|
||||
*/
|
||||
public static final RegistryObject<Codec<RemoveFeaturesBiomeModifier>> REMOVE_FEATURES_BIOME_MODIFIER_TYPE = BIOME_MODIFIER_SERIALIZERS.register("remove_features", () ->
|
||||
RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(RemoveFeaturesBiomeModifier::biomes),
|
||||
PlacedFeature.LIST_CODEC.fieldOf("features").forGetter(RemoveFeaturesBiomeModifier::features),
|
||||
new ExtraCodecs.EitherCodec<List<Decoration>, Decoration>(Decoration.CODEC.listOf(), Decoration.CODEC).<Set<Decoration>>xmap(
|
||||
either -> either.map(Set::copyOf, Set::of), // convert list/singleton to set when decoding
|
||||
set -> set.size() == 1 ? Either.right(set.toArray(Decoration[]::new)[0]) : Either.left(List.copyOf(set))
|
||||
).optionalFieldOf("steps", EnumSet.allOf(Decoration.class)).forGetter(RemoveFeaturesBiomeModifier::steps)
|
||||
).apply(builder, RemoveFeaturesBiomeModifier::new))
|
||||
);
|
||||
private static final DeferredRegister<Codec<? extends StructureModifier>> STRUCTURE_MODIFIER_SERIALIZERS = deferred(ForgeRegistries.Keys.STRUCTURE_MODIFIER_SERIALIZERS);
|
||||
static {
|
||||
STRUCTURE_MODIFIER_SERIALIZERS.register("none", () -> NoneStructureModifier.CODEC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stock biome modifier for adding mob spawns to biomes.
|
||||
*/
|
||||
public static final RegistryObject<Codec<AddSpawnsBiomeModifier>> ADD_SPAWNS_BIOME_MODIFIER_TYPE = BIOME_MODIFIER_SERIALIZERS.register("add_spawns", () ->
|
||||
RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(AddSpawnsBiomeModifier::biomes),
|
||||
// Allow either a list or single spawner, attempting to decode the list format first.
|
||||
// Uses the better EitherCodec that logs both errors if both formats fail to parse.
|
||||
new ExtraCodecs.EitherCodec<>(SpawnerData.CODEC.listOf(), SpawnerData.CODEC).xmap(
|
||||
either -> either.map(Function.identity(), List::of), // convert list/singleton to list when decoding
|
||||
list -> list.size() == 1 ? Either.right(list.get(0)) : Either.left(list) // convert list to singleton/list when encoding
|
||||
).fieldOf("spawners").forGetter(AddSpawnsBiomeModifier::spawners)
|
||||
).apply(builder, AddSpawnsBiomeModifier::new))
|
||||
);
|
||||
|
||||
/**
|
||||
* Stock biome modifier for removing mob spawns from biomes.
|
||||
*/
|
||||
public static final RegistryObject<Codec<RemoveSpawnsBiomeModifier>> REMOVE_SPAWNS_BIOME_MODIFIER_TYPE = BIOME_MODIFIER_SERIALIZERS.register("remove_spawns", () ->
|
||||
RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(RemoveSpawnsBiomeModifier::biomes),
|
||||
RegistryCodecs.homogeneousList(ForgeRegistries.Keys.ENTITY_TYPES).fieldOf("entity_types").forGetter(RemoveSpawnsBiomeModifier::entityTypes)
|
||||
).apply(builder, RemoveSpawnsBiomeModifier::new))
|
||||
);
|
||||
/**
|
||||
* Noop structure modifier. Can be used in a structure modifier json with "type": "forge:none".
|
||||
*/
|
||||
public static final RegistryObject<Codec<NoneStructureModifier>> NONE_STRUCTURE_MODIFIER_TYPE = STRUCTURE_MODIFIER_SERIALIZERS.register("none", () -> Codec.unit(NoneStructureModifier.INSTANCE));
|
||||
private static final DeferredRegister<HolderSetType> HOLDER_SET_TYPES = deferred(ForgeRegistries.Keys.HOLDER_SET_TYPES);
|
||||
|
||||
/**
|
||||
* Stock holder set type that represents any/all values in a registry. Can be used in a holderset object with {@code { "type": "forge:any" }}
|
||||
@@ -249,7 +210,7 @@ public class ForgeMod {
|
||||
*/
|
||||
public static final RegistryObject<HolderSetType> NOT_HOLDER_SET = HOLDER_SET_TYPES.register("not", () -> NotHolderSet::codec);
|
||||
|
||||
private static final DeferredRegister<FluidType> VANILLA_FLUID_TYPES = DeferredRegister.create(ForgeRegistries.Keys.FLUID_TYPES, "minecraft");
|
||||
private static final DeferredRegister<FluidType> VANILLA_FLUID_TYPES = deferred(ForgeRegistries.Keys.FLUID_TYPES, "minecraft");
|
||||
|
||||
public static final RegistryObject<FluidType> EMPTY_TYPE = VANILLA_FLUID_TYPES.register("empty", () ->
|
||||
new FluidType(FluidType.Properties.create()
|
||||
@@ -374,13 +335,13 @@ public class ForgeMod {
|
||||
}
|
||||
});
|
||||
|
||||
private static final DeferredRegister<LootItemConditionType> LOOT_CONDITION_TYPES = DeferredRegister.create(Registries.LOOT_CONDITION_TYPE, "forge");
|
||||
private static final DeferredRegister<LootItemConditionType> LOOT_CONDITION_TYPES = deferred(Registries.LOOT_CONDITION_TYPE);
|
||||
static {
|
||||
LOOT_CONDITION_TYPES.register("loot_table_id", () -> LootTableIdCondition.TYPE);
|
||||
LOOT_CONDITION_TYPES.register("can_tool_perform_action", () -> CanToolPerformAction.TYPE);
|
||||
}
|
||||
|
||||
private static final DeferredRegister<Codec<? extends ICondition>> CONDITION_SERIALIZERS = DeferredRegister.create(ForgeRegistries.Keys.CONDITION_SERIALIZERS, "forge");
|
||||
private static final DeferredRegister<Codec<? extends ICondition>> CONDITION_SERIALIZERS = deferred(ForgeRegistries.Keys.CONDITION_SERIALIZERS);
|
||||
static {
|
||||
CONDITION_SERIALIZERS.register("and", () -> AndCondition.CODEC);
|
||||
CONDITION_SERIALIZERS.register("false", () -> FalseCondition.CODEC);
|
||||
@@ -392,11 +353,20 @@ public class ForgeMod {
|
||||
CONDITION_SERIALIZERS.register("tag_empty", () -> TagEmptyCondition.CODEC);
|
||||
}
|
||||
|
||||
private static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = DeferredRegister.create(ForgeRegistries.Keys.RECIPE_SERIALIZERS, "forge");
|
||||
private static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = deferred(ForgeRegistries.Keys.RECIPE_SERIALIZERS);
|
||||
static {
|
||||
RECIPE_SERIALIZERS.register("conditional", () -> ConditionalRecipe.SERIALZIER);
|
||||
}
|
||||
|
||||
private static final DeferredRegister<IIngredientSerializer<?>> INGREDIENT_SERIALIZERS = deferred(ForgeRegistries.Keys.INGREDIENT_SERIALIZERS);
|
||||
static {
|
||||
INGREDIENT_SERIALIZERS.register("compound", () -> CompoundIngredient.SERIALIZER);
|
||||
INGREDIENT_SERIALIZERS.register("nbt", () -> StrictNBTIngredient.SERIALIZER);
|
||||
INGREDIENT_SERIALIZERS.register("partial_nbt", () -> PartialNBTIngredient.SERIALIZER);
|
||||
INGREDIENT_SERIALIZERS.register("difference", () -> DifferenceIngredient.SERIALIZER);
|
||||
INGREDIENT_SERIALIZERS.register("intersection", () -> IntersectionIngredient.SERIALIZER);
|
||||
}
|
||||
|
||||
|
||||
private static boolean enableMilkFluid = false;
|
||||
public static final RegistryObject<SoundEvent> BUCKET_EMPTY_MILK = RegistryObject.create(new ResourceLocation("item.bucket.empty_milk"), ForgeRegistries.SOUND_EVENTS);
|
||||
@@ -444,17 +414,9 @@ public class ForgeMod {
|
||||
modEventBus.addListener(this::loadComplete);
|
||||
modEventBus.addListener(this::registerFluids);
|
||||
modEventBus.addListener(this::registerVanillaDisplayContexts);
|
||||
modEventBus.addListener(this::registerRecipeSerializers);
|
||||
modEventBus.register(this);
|
||||
ATTRIBUTES.register(modEventBus);
|
||||
COMMAND_ARGUMENT_TYPES.register(modEventBus);
|
||||
BIOME_MODIFIER_SERIALIZERS.register(modEventBus);
|
||||
STRUCTURE_MODIFIER_SERIALIZERS.register(modEventBus);
|
||||
HOLDER_SET_TYPES.register(modEventBus);
|
||||
VANILLA_FLUID_TYPES.register(modEventBus);
|
||||
LOOT_CONDITION_TYPES.register(modEventBus);
|
||||
CONDITION_SERIALIZERS.register(modEventBus);
|
||||
RECIPE_SERIALIZERS.register(modEventBus);
|
||||
registries.forEach(r -> r.register(modEventBus));
|
||||
|
||||
MinecraftForge.EVENT_BUS.addListener(this::serverStopping);
|
||||
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ForgeConfig.clientSpec);
|
||||
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ForgeConfig.serverSpec);
|
||||
@@ -577,22 +539,6 @@ public class ForgeMod {
|
||||
}
|
||||
}
|
||||
|
||||
public void registerRecipeSerializers(RegisterEvent event) {
|
||||
if (event.getRegistryKey().equals(ForgeRegistries.Keys.RECIPE_SERIALIZERS)) {
|
||||
|
||||
/*
|
||||
CraftingHelper.register(new ResourceLocation("forge", "compound"), CompoundIngredient.Serializer.INSTANCE);
|
||||
CraftingHelper.register(new ResourceLocation("forge", "nbt"), StrictNBTIngredient.Serializer.INSTANCE);
|
||||
CraftingHelper.register(new ResourceLocation("forge", "partial_nbt"), PartialNBTIngredient.Serializer.INSTANCE);
|
||||
CraftingHelper.register(new ResourceLocation("forge", "difference"), DifferenceIngredient.Serializer.INSTANCE);
|
||||
CraftingHelper.register(new ResourceLocation("forge", "intersection"), IntersectionIngredient.Serializer.INSTANCE);
|
||||
CraftingHelper.register(new ResourceLocation("minecraft", "item"), VanillaIngredientSerializer.INSTANCE);
|
||||
*/
|
||||
|
||||
//event.register(ForgeRegistries.Keys.RECIPE_SERIALIZERS, new ResourceLocation("forge", "conditional"), ConditionalRecipe.Serializer::new);
|
||||
}
|
||||
}
|
||||
|
||||
public static final PermissionNode<Boolean> USE_SELECTORS_PERMISSION = new PermissionNode<>("forge", "use_entity_selectors",
|
||||
PermissionTypes.BOOLEAN, (player, uuid, contexts) -> player != null && player.hasPermissions(Commands.LEVEL_GAMEMASTERS));
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ package net.minecraftforge.common;
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.DataResult;
|
||||
import net.minecraft.util.ExtraCodecs;
|
||||
import net.minecraft.util.StringRepresentable;
|
||||
|
||||
import java.util.function.Function;
|
||||
@@ -34,8 +33,7 @@ import java.util.function.Supplier;
|
||||
* {@code create} methods <strong>can</strong> be defined as long as each
|
||||
* matches a constructor.
|
||||
*/
|
||||
public interface IExtensibleEnum
|
||||
{
|
||||
public interface IExtensibleEnum {
|
||||
/**
|
||||
* Called by generated factory code to do any post-constructor setup required by
|
||||
* the enum. Should not be called manually.
|
||||
|
||||
@@ -5,128 +5,25 @@
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraftforge.common.crafting.conditions.ICondition;
|
||||
import net.minecraft.nbt.TagParser;
|
||||
import net.minecraft.Util;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.Marker;
|
||||
import org.apache.logging.log4j.MarkerManager;
|
||||
|
||||
public class CraftingHelper {
|
||||
@SuppressWarnings("unused")
|
||||
private static final Logger LOGGER = LogManager.getLogger();
|
||||
@SuppressWarnings("unused")
|
||||
private static final Marker CRAFTHELPER = MarkerManager.getMarker("CRAFTHELPER");
|
||||
private static Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
|
||||
/* TODO: Custom Ingredients
|
||||
private static final BiMap<ResourceLocation, IIngredientSerializer<?>> ingredients = HashBiMap.create();
|
||||
|
||||
|
||||
public static <T extends Ingredient> IIngredientSerializer<T> register(ResourceLocation key, IIngredientSerializer<T> serializer) {
|
||||
if (ingredients.containsKey(key))
|
||||
throw new IllegalStateException("Duplicate recipe ingredient serializer: " + key);
|
||||
if (ingredients.containsValue(serializer))
|
||||
throw new IllegalStateException("Duplicate recipe ingredient serializer: " + key + " " + serializer);
|
||||
ingredients.put(key, serializer);
|
||||
return serializer;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static ResourceLocation getID(IIngredientSerializer<?> serializer) {
|
||||
return ingredients.inverse().get(serializer);
|
||||
}
|
||||
|
||||
public static <T extends Ingredient> void write(FriendlyByteBuf buffer, T ingredient) {
|
||||
@SuppressWarnings("unchecked") //I wonder if there is a better way generic wise...
|
||||
IIngredientSerializer<T> serializer = (IIngredientSerializer<T>)ingredient.getSerializer();
|
||||
ResourceLocation key = ingredients.inverse().get(serializer);
|
||||
if (key == null)
|
||||
throw new IllegalArgumentException("Tried to serialize unregistered Ingredient: " + ingredient + " " + serializer);
|
||||
if (serializer != VanillaIngredientSerializer.INSTANCE) {
|
||||
buffer.writeVarInt(-1); //Marker to know there is a custom ingredient
|
||||
buffer.writeResourceLocation(key);
|
||||
}
|
||||
serializer.write(buffer, ingredient);
|
||||
}
|
||||
|
||||
public static Ingredient getIngredient(ResourceLocation type, FriendlyByteBuf buffer) {
|
||||
IIngredientSerializer<?> serializer = ingredients.get(type);
|
||||
if (serializer == null)
|
||||
throw new IllegalArgumentException("Can not deserialize unknown Ingredient type: " + type);
|
||||
return serializer.parse(buffer);
|
||||
}
|
||||
|
||||
public static Ingredient getIngredient(JsonElement json, boolean allowEmpty) {
|
||||
if (json == null || json.isJsonNull())
|
||||
throw new JsonSyntaxException("Json cannot be null");
|
||||
|
||||
if (json.isJsonArray()) {
|
||||
List<Ingredient> ingredients = Lists.newArrayList();
|
||||
List<Ingredient> vanilla = Lists.newArrayList();
|
||||
json.getAsJsonArray().forEach((ele) -> {
|
||||
Ingredient ing = CraftingHelper.getIngredient(ele, allowEmpty);
|
||||
|
||||
if (ing.getClass() == Ingredient.class) //Vanilla, Due to how we read it splits each itemstack, so we pull out to re-merge later
|
||||
vanilla.add(ing);
|
||||
else
|
||||
ingredients.add(ing);
|
||||
});
|
||||
|
||||
if (!vanilla.isEmpty())
|
||||
ingredients.add(Ingredient.merge(vanilla));
|
||||
|
||||
if (ingredients.size() == 0) {
|
||||
if (allowEmpty)
|
||||
return Ingredient.EMPTY;
|
||||
throw new JsonSyntaxException("Item array cannot be empty, at least one item must be defined");
|
||||
}
|
||||
|
||||
if (ingredients.size() == 1)
|
||||
return ingredients.get(0);
|
||||
|
||||
return new CompoundIngredient(ingredients);
|
||||
}
|
||||
|
||||
if (!json.isJsonObject())
|
||||
throw new JsonSyntaxException("Expcted ingredient to be a object or array of objects");
|
||||
|
||||
JsonObject obj = (JsonObject)json;
|
||||
|
||||
String type = GsonHelper.getAsString(obj, "type", "minecraft:item");
|
||||
if (type.isEmpty())
|
||||
throw new JsonSyntaxException("Ingredient type can not be an empty string");
|
||||
|
||||
IIngredientSerializer<?> serializer = ingredients.get(new ResourceLocation(type));
|
||||
if (serializer == null)
|
||||
throw new JsonSyntaxException("Unknown ingredient type: " + type);
|
||||
|
||||
return serializer.parse(obj);
|
||||
}
|
||||
*/
|
||||
|
||||
public static ItemStack getItemStack(JsonObject json, boolean readNBT) {
|
||||
return getItemStack(json, readNBT, false);
|
||||
|
||||
@@ -1,178 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
/*
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.world.entity.player.StackedContents;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Ingredient that matches if all child ingredients match * /
|
||||
public class IntersectionIngredient extends AbstractIngredient
|
||||
{
|
||||
private final List<Ingredient> children;
|
||||
private final boolean isSimple;
|
||||
private ItemStack[] intersectedMatchingStacks = null;
|
||||
private IntList packedMatchingStacks = null;
|
||||
protected IntersectionIngredient(List<Ingredient> children)
|
||||
{
|
||||
if (children.size() < 2)
|
||||
throw new IllegalArgumentException("Cannot create an IntersectionIngredient with one or no children");
|
||||
this.children = Collections.unmodifiableList(children);
|
||||
this.isSimple = children.stream().allMatch(Ingredient::isSimple);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an intersection ingredient
|
||||
* @param ingredients List of ingredients to match
|
||||
* @return Ingredient that only matches if all the passed ingredients match
|
||||
* /
|
||||
public static Ingredient of(Ingredient... ingredients)
|
||||
{
|
||||
if (ingredients.length == 0)
|
||||
throw new IllegalArgumentException("Cannot create an IntersectionIngredient with no children, use Ingredient.of() to create an empty ingredient");
|
||||
if (ingredients.length == 1)
|
||||
return ingredients[0];
|
||||
|
||||
return new IntersectionIngredient(Arrays.asList(ingredients));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack stack)
|
||||
{
|
||||
if (stack == null || stack.isEmpty())
|
||||
return false;
|
||||
|
||||
for (Ingredient ingredient : children)
|
||||
if (!ingredient.test(stack))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] getItems()
|
||||
{
|
||||
if (this.intersectedMatchingStacks == null)
|
||||
{
|
||||
this.intersectedMatchingStacks = Arrays
|
||||
.stream(children.get(0).getItems())
|
||||
.filter(stack ->
|
||||
{
|
||||
// the first ingredient is treated as a base, filtered by the second onwards
|
||||
for (int i = 1; i < children.size(); i++)
|
||||
if (!children.get(i).test(stack))
|
||||
return false;
|
||||
return true;
|
||||
})
|
||||
.toArray(ItemStack[]::new);
|
||||
}
|
||||
return intersectedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return children.stream().anyMatch(Ingredient::isEmpty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple()
|
||||
{
|
||||
return isSimple;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidate()
|
||||
{
|
||||
super.invalidate();
|
||||
this.intersectedMatchingStacks = null;
|
||||
this.packedMatchingStacks = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntList getStackingIds()
|
||||
{
|
||||
if (this.packedMatchingStacks == null || checkInvalidation())
|
||||
{
|
||||
markValid();
|
||||
ItemStack[] matchingStacks = getItems();
|
||||
this.packedMatchingStacks = new IntArrayList(matchingStacks.length);
|
||||
for(ItemStack stack : matchingStacks)
|
||||
this.packedMatchingStacks.add(StackedContents.getStackingIndex(stack));
|
||||
this.packedMatchingStacks.sort(IntComparators.NATURAL_COMPARATOR);
|
||||
}
|
||||
return packedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson()
|
||||
{
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("type", CraftingHelper.getID(Serializer.INSTANCE).toString());
|
||||
JsonArray array = new JsonArray();
|
||||
for (Ingredient ingredient : children)
|
||||
array.add(ingredient.toJson());
|
||||
|
||||
json.add("children", array);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<IntersectionIngredient> getSerializer()
|
||||
{
|
||||
return Serializer.INSTANCE;
|
||||
}
|
||||
|
||||
public static class Serializer implements IIngredientSerializer<IntersectionIngredient>
|
||||
{
|
||||
public static final IIngredientSerializer<IntersectionIngredient> INSTANCE = new Serializer();
|
||||
|
||||
@Override
|
||||
public IntersectionIngredient parse(JsonObject json)
|
||||
{
|
||||
JsonArray children = GsonHelper.getAsJsonArray(json, "children");
|
||||
if (children.size() < 2)
|
||||
throw new JsonSyntaxException("Must have at least two children for an intersection ingredient");
|
||||
|
||||
return new IntersectionIngredient(
|
||||
IntStream.range(0, children.size())
|
||||
.mapToObj(i -> Ingredient.fromJson(children.get(i), false))
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntersectionIngredient parse(FriendlyByteBuf buffer)
|
||||
{
|
||||
return new IntersectionIngredient(Stream.generate(() -> Ingredient.fromNetwork(buffer)).limit(buffer.readVarInt()).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, IntersectionIngredient intersection)
|
||||
{
|
||||
buffer.writeVarInt(intersection.children.size());
|
||||
for (Ingredient ingredient : intersection.children)
|
||||
ingredient.toNetwork(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -1,157 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
/*
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import net.minecraft.advancements.critereon.NbtPredicate;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.util.GsonHelper;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/** Ingredient that matches the given items, performing a partial NBT match. Use {@link StrictNBTIngredient} if you want exact match on NBT * /
|
||||
public class PartialNBTIngredient extends AbstractIngredient
|
||||
{
|
||||
private final Set<Item> items;
|
||||
private final CompoundTag nbt;
|
||||
private final NbtPredicate predicate;
|
||||
protected PartialNBTIngredient(Set<Item> items, CompoundTag nbt)
|
||||
{
|
||||
super(items.stream().map(item ->
|
||||
{
|
||||
ItemStack stack = new ItemStack(item);
|
||||
// copy NBT to prevent the stack from modifying the original, as capabilities or vanilla item durability will modify the tag
|
||||
stack.setTag(nbt.copy());
|
||||
return new Ingredient.ItemValue(stack);
|
||||
}));
|
||||
if (items.isEmpty())
|
||||
{
|
||||
throw new IllegalArgumentException("Cannot create a PartialNBTIngredient with no items");
|
||||
}
|
||||
this.items = Collections.unmodifiableSet(items);
|
||||
this.nbt = nbt;
|
||||
this.predicate = new NbtPredicate(nbt);
|
||||
}
|
||||
|
||||
/** Creates a new ingredient matching any item from the list, containing the given NBT * /
|
||||
public static PartialNBTIngredient of(CompoundTag nbt, ItemLike... items)
|
||||
{
|
||||
return new PartialNBTIngredient(Arrays.stream(items).map(ItemLike::asItem).collect(Collectors.toSet()), nbt);
|
||||
}
|
||||
|
||||
/** Creates a new ingredient matching the given item, containing the given NBT * /
|
||||
public static PartialNBTIngredient of(ItemLike item, CompoundTag nbt)
|
||||
{
|
||||
return new PartialNBTIngredient(Set.of(item.asItem()), nbt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack input)
|
||||
{
|
||||
if (input == null)
|
||||
return false;
|
||||
return items.contains(input.getItem()) && predicate.matches(input.getShareTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<? extends Ingredient> getSerializer()
|
||||
{
|
||||
return Serializer.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson()
|
||||
{
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("type", CraftingHelper.getID(Serializer.INSTANCE).toString());
|
||||
if (items.size() == 1)
|
||||
{
|
||||
json.addProperty("item", ForgeRegistries.ITEMS.getKey(items.iterator().next()).toString());
|
||||
}
|
||||
else
|
||||
{
|
||||
JsonArray items = new JsonArray();
|
||||
// ensure the order of items in the set is deterministic when saved to JSON
|
||||
this.items.stream().map(ForgeRegistries.ITEMS::getKey).sorted().forEach(name -> items.add(name.toString()));
|
||||
json.add("items", items);
|
||||
}
|
||||
json.addProperty("nbt", nbt.toString());
|
||||
return json;
|
||||
}
|
||||
|
||||
public static class Serializer implements IIngredientSerializer<PartialNBTIngredient>
|
||||
{
|
||||
public static final Serializer INSTANCE = new Serializer();
|
||||
|
||||
@Override
|
||||
public PartialNBTIngredient parse(JsonObject json)
|
||||
{
|
||||
// parse items
|
||||
Set<Item> items;
|
||||
if (json.has("item"))
|
||||
items = Set.of(CraftingHelper.getItem(GsonHelper.getAsString(json, "item"), true));
|
||||
else if (json.has("items"))
|
||||
{
|
||||
ImmutableSet.Builder<Item> builder = ImmutableSet.builder();
|
||||
JsonArray itemArray = GsonHelper.getAsJsonArray(json, "items");
|
||||
for (int i = 0; i < itemArray.size(); i++)
|
||||
{
|
||||
builder.add(CraftingHelper.getItem(GsonHelper.convertToString(itemArray.get(i), "items[" + i + ']'), true));
|
||||
}
|
||||
items = builder.build();
|
||||
}
|
||||
else
|
||||
throw new JsonSyntaxException("Must set either 'item' or 'items'");
|
||||
|
||||
// parse NBT
|
||||
if (!json.has("nbt"))
|
||||
throw new JsonSyntaxException("Missing nbt, expected to find a String or JsonObject");
|
||||
CompoundTag nbt = CraftingHelper.getNBT(json.get("nbt"));
|
||||
|
||||
return new PartialNBTIngredient(items, nbt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PartialNBTIngredient parse(FriendlyByteBuf buffer)
|
||||
{
|
||||
Set<Item> items = Stream.generate(() -> buffer.readRegistryIdUnsafe(ForgeRegistries.ITEMS)).limit(buffer.readVarInt()).collect(Collectors.toSet());
|
||||
CompoundTag nbt = buffer.readNbt();
|
||||
return new PartialNBTIngredient(items, Objects.requireNonNull(nbt));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, PartialNBTIngredient ingredient)
|
||||
{
|
||||
buffer.writeVarInt(ingredient.items.size());
|
||||
for (Item item : ingredient.items)
|
||||
buffer.writeRegistryIdUnsafe(ForgeRegistries.ITEMS, item);
|
||||
buffer.writeNbt(ingredient.nbt);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* Extension of {@link Ingredient} which makes most methods custom ingredients need to implement abstract, and removes the static constructors
|
||||
* Mods are encouraged to extend this class for their custom ingredients
|
||||
*/
|
||||
public abstract class AbstractIngredient extends Ingredient {
|
||||
/** Empty constructor, for the sake of dynamic ingredients */
|
||||
protected AbstractIngredient() {
|
||||
super(Stream.of());
|
||||
}
|
||||
|
||||
/** Value constructor, for ingredients that have some vanilla representation */
|
||||
protected AbstractIngredient(Stream<? extends Value> values) {
|
||||
super(values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public abstract boolean isSimple();
|
||||
|
||||
@Override
|
||||
public abstract IIngredientSerializer<? extends Ingredient> serializer();
|
||||
|
||||
|
||||
/* Hide vanilla ingredient static constructors to reduce errors with constructing custom ingredients */
|
||||
|
||||
/** @deprecated use {@link Ingredient#fromValues(Stream)} */
|
||||
@Deprecated
|
||||
public static Ingredient fromValues(Stream<? extends Ingredient.Value> values) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.fromValues()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#of()} */
|
||||
@Deprecated
|
||||
public static Ingredient of() {
|
||||
throw new UnsupportedOperationException("Use Ingredient.of()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#of(ItemLike...)} (Stream)} */
|
||||
@Deprecated
|
||||
public static Ingredient of(ItemLike... items) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.of()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#of(ItemStack...)} (Stream)} */
|
||||
@Deprecated
|
||||
public static Ingredient of(ItemStack... stacks) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.of()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#of(Stream)} (Stream)} */
|
||||
@Deprecated
|
||||
public static Ingredient of(Stream<ItemStack> stacks) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.of()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#of(TagKey)} (Stream)} */
|
||||
@Deprecated
|
||||
public static Ingredient of(TagKey<Item> tag) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.of()");
|
||||
}
|
||||
|
||||
/** @deprecated use {@link Ingredient#fromNetwork(FriendlyByteBuf)} */
|
||||
@Deprecated
|
||||
public static Ingredient fromNetwork(FriendlyByteBuf buffer) {
|
||||
throw new UnsupportedOperationException("Use Ingredient.fromNetwork()");
|
||||
}
|
||||
}
|
||||
+51
-46
@@ -3,20 +3,15 @@
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
/*
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
@@ -28,45 +23,27 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/** Ingredient that matches if any of the child ingredients match */
|
||||
/*
|
||||
public class CompoundIngredient extends Ingredient {
|
||||
public class CompoundIngredient extends AbstractIngredient {
|
||||
public static Ingredient of(Ingredient... children) {
|
||||
if (children.length == 0)
|
||||
throw new IllegalArgumentException("Cannot create a compound ingredient with no children, use Ingredient.of() to create an empty ingredient");
|
||||
|
||||
if (children.length == 1)
|
||||
return children[0];
|
||||
|
||||
return new CompoundIngredient(Arrays.asList(children));
|
||||
}
|
||||
|
||||
private List<Ingredient> children;
|
||||
private ItemStack[] stacks;
|
||||
private IntList itemIds;
|
||||
private final boolean isSimple;
|
||||
|
||||
protected CompoundIngredient(List<Ingredient> children)
|
||||
{
|
||||
private CompoundIngredient(List<Ingredient> children) {
|
||||
this.children = Collections.unmodifiableList(children);
|
||||
this.isSimple = children.stream().allMatch(Ingredient::isSimple);
|
||||
}
|
||||
|
||||
/** Creates a compound ingredient from the given list of ingredients */
|
||||
/*
|
||||
public static Ingredient of(Ingredient... children)
|
||||
{
|
||||
// if 0 or 1 ingredient, can save effort
|
||||
if (children.length == 0)
|
||||
throw new IllegalArgumentException("Cannot create a compound ingredient with no children, use Ingredient.of() to create an empty ingredient");
|
||||
if (children.length == 1)
|
||||
return children[0];
|
||||
|
||||
// need to merge vanilla ingredients, as otherwise the JSON produced by this ingredient could be invalid
|
||||
List<Ingredient> vanillaIngredients = new ArrayList<>();
|
||||
List<Ingredient> allIngredients = new ArrayList<>();
|
||||
for (Ingredient child : children) {
|
||||
if (child.getSerializer() == VanillaIngredientSerializer.INSTANCE)
|
||||
vanillaIngredients.add(child);
|
||||
else
|
||||
allIngredients.add(child);
|
||||
}
|
||||
if (!vanillaIngredients.isEmpty())
|
||||
allIngredients.add(merge(vanillaIngredients));
|
||||
if (allIngredients.size() == 1)
|
||||
return allIngredients.get(0);
|
||||
return new CompoundIngredient(allIngredients);
|
||||
}
|
||||
|
||||
@Override
|
||||
@NotNull
|
||||
public ItemStack[] getItems() {
|
||||
@@ -110,14 +87,42 @@ public class CompoundIngredient extends Ingredient {
|
||||
return isSimple;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Collection<Ingredient> getChildren() {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return children.stream().allMatch(Ingredient::isEmpty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<? extends Ingredient> serializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
public static final Codec<CompoundIngredient> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Ingredient.CODEC_NONEMPTY.listOf().fieldOf("children").forGetter(i -> i.children)
|
||||
).apply(builder, CompoundIngredient::new)
|
||||
);
|
||||
|
||||
public static final IIngredientSerializer<CompoundIngredient> SERIALIZER = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public Codec<CompoundIngredient> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, CompoundIngredient value) {
|
||||
buffer.writeVarInt(value.children.size());
|
||||
for (var child : value.children)
|
||||
child.toNetwork(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundIngredient read(FriendlyByteBuf buffer) {
|
||||
int size = buffer.readVarInt();
|
||||
var children = new ArrayList<Ingredient>(size);
|
||||
for (int x = 0; x < size; x++)
|
||||
children.add(Ingredient.fromNetwork(buffer));
|
||||
return new CompoundIngredient(children);
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
+41
-62
@@ -3,10 +3,11 @@
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
/*
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
@@ -18,42 +19,37 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/** Ingredient that matches everything from the first ingredient that is not included in the second ingredient * /
|
||||
public class DifferenceIngredient extends AbstractIngredient
|
||||
{
|
||||
/** Ingredient that matches everything from the first ingredient that is not included in the second ingredient */
|
||||
public class DifferenceIngredient extends AbstractIngredient {
|
||||
/**
|
||||
* Gets the difference from the two ingredients
|
||||
* @param base Ingredient the item must match
|
||||
* @param subtracted Ingredient the item must not match
|
||||
* @return Ingredient that matches anything in {@code base} that is not in {@code subtracted}
|
||||
*/
|
||||
public static DifferenceIngredient of(Ingredient base, Ingredient subtracted) {
|
||||
return new DifferenceIngredient(base, subtracted);
|
||||
}
|
||||
|
||||
private final Ingredient base;
|
||||
private final Ingredient subtracted;
|
||||
private ItemStack[] filteredMatchingStacks;
|
||||
private IntList packedMatchingStacks;
|
||||
|
||||
protected DifferenceIngredient(Ingredient base, Ingredient subtracted)
|
||||
{
|
||||
private DifferenceIngredient(Ingredient base, Ingredient subtracted) {
|
||||
this.base = base;
|
||||
this.subtracted = subtracted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the difference from the two ingredients
|
||||
* @param base Ingredient the item must match
|
||||
* @param subtracted Ingredient the item must not match
|
||||
* @return Ingredient that {@code base} anything in base that is not in {@code subtracted}
|
||||
* /
|
||||
public static DifferenceIngredient of(Ingredient base, Ingredient subtracted)
|
||||
{
|
||||
return new DifferenceIngredient(base, subtracted);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack stack)
|
||||
{
|
||||
public boolean test(@Nullable ItemStack stack) {
|
||||
if (stack == null || stack.isEmpty())
|
||||
return false;
|
||||
return base.test(stack) && !subtracted.test(stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] getItems()
|
||||
{
|
||||
public ItemStack[] getItems() {
|
||||
if (this.filteredMatchingStacks == null)
|
||||
this.filteredMatchingStacks = Arrays.stream(base.getItems())
|
||||
.filter(stack -> !subtracted.test(stack))
|
||||
@@ -62,34 +58,29 @@ public class DifferenceIngredient extends AbstractIngredient
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
public boolean isEmpty() {
|
||||
return base.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple()
|
||||
{
|
||||
public boolean isSimple() {
|
||||
return base.isSimple() && subtracted.isSimple();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidate()
|
||||
{
|
||||
protected void invalidate() {
|
||||
super.invalidate();
|
||||
this.filteredMatchingStacks = null;
|
||||
this.packedMatchingStacks = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntList getStackingIds()
|
||||
{
|
||||
if (this.packedMatchingStacks == null || checkInvalidation())
|
||||
{
|
||||
public IntList getStackingIds() {
|
||||
if (this.packedMatchingStacks == null || checkInvalidation()) {
|
||||
markValid();
|
||||
ItemStack[] matchingStacks = getItems();
|
||||
var matchingStacks = getItems();
|
||||
this.packedMatchingStacks = new IntArrayList(matchingStacks.length);
|
||||
for (ItemStack stack : matchingStacks)
|
||||
for (var stack : matchingStacks)
|
||||
this.packedMatchingStacks.add(StackedContents.getStackingIndex(stack));
|
||||
|
||||
this.packedMatchingStacks.sort(IntComparators.NATURAL_COMPARATOR);
|
||||
@@ -98,46 +89,34 @@ public class DifferenceIngredient extends AbstractIngredient
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement toJson(boolean allowEmpty)
|
||||
{
|
||||
JsonObject json = new JsonObject();
|
||||
json.addProperty("type", CraftingHelper.getID(Serializer.INSTANCE).toString());
|
||||
json.add("base", base.toJson(allowEmpty));
|
||||
json.add("subtracted", subtracted.toJson(allowEmpty));
|
||||
return json;
|
||||
public IIngredientSerializer<DifferenceIngredient> serializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<DifferenceIngredient> getSerializer()
|
||||
{
|
||||
return Serializer.INSTANCE;
|
||||
}
|
||||
|
||||
public static class Serializer implements IIngredientSerializer<DifferenceIngredient> {
|
||||
public static final IIngredientSerializer<DifferenceIngredient> INSTANCE = new Serializer();
|
||||
public static final Codec<DifferenceIngredient> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Ingredient.CODEC.fieldOf("base").forGetter(i -> i.base),
|
||||
Ingredient.CODEC.fieldOf("subtracted").forGetter(i -> i.subtracted)
|
||||
).apply(builder, DifferenceIngredient::new)
|
||||
);
|
||||
|
||||
public static final IIngredientSerializer<DifferenceIngredient> SERIALIZER = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public DifferenceIngredient parse(JsonObject json)
|
||||
{
|
||||
Ingredient base = Ingredient.fromJson(json.get("base"), false);
|
||||
Ingredient without = Ingredient.fromJson(json.get("subtracted"), false);
|
||||
return new DifferenceIngredient(base, without);
|
||||
public Codec<? extends DifferenceIngredient> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DifferenceIngredient parse(FriendlyByteBuf buffer)
|
||||
{
|
||||
public DifferenceIngredient read(FriendlyByteBuf buffer) {
|
||||
Ingredient base = Ingredient.fromNetwork(buffer);
|
||||
Ingredient without = Ingredient.fromNetwork(buffer);
|
||||
return new DifferenceIngredient(base, without);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, DifferenceIngredient ingredient)
|
||||
{
|
||||
public void write(FriendlyByteBuf buffer, DifferenceIngredient ingredient) {
|
||||
ingredient.base.toNetwork(buffer);
|
||||
ingredient.subtracted.toNetwork(buffer);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
|
||||
public interface IIngredientBuilder {
|
||||
default PartialNBTIngredient.Builder partialNbt() {
|
||||
return PartialNBTIngredient.builder();
|
||||
}
|
||||
|
||||
default Ingredient strictNbt(ItemStack value) {
|
||||
return StrictNBTIngredient.of(value);
|
||||
}
|
||||
|
||||
default Ingredient compound(Ingredient... values) {
|
||||
return CompoundIngredient.of(values);
|
||||
}
|
||||
|
||||
default Ingredient intersection(@SuppressWarnings("rawtypes") TagKey... values) {
|
||||
return intersection(Arrays.stream(values).map(Ingredient::of).toArray(Ingredient[]::new));
|
||||
}
|
||||
|
||||
default Ingredient intersection(Ingredient... values) {
|
||||
return IntersectionIngredient.of(values);
|
||||
}
|
||||
|
||||
default Ingredient difference(TagKey<Item> base, TagKey<Item> subtracted) {
|
||||
return difference(Ingredient.of(base), Ingredient.of(subtracted));
|
||||
}
|
||||
|
||||
default Ingredient difference(Ingredient base, Ingredient subtracted) {
|
||||
return DifferenceIngredient.of(base, subtracted);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
public interface IIngredientSerializer<T extends Ingredient> {
|
||||
Codec<? extends T> codec();
|
||||
void write(FriendlyByteBuf buffer, T value);
|
||||
T read(FriendlyByteBuf buffer);
|
||||
|
||||
IIngredientSerializer<Ingredient> VANILLA = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public Codec<? extends Ingredient> codec() {
|
||||
return Ingredient.VANILLA_CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, Ingredient value) {
|
||||
value.toNetwork(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ingredient read(FriendlyByteBuf buffer) {
|
||||
return Ingredient.fromNetwork(buffer);
|
||||
}
|
||||
};
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntComparators;
|
||||
import it.unimi.dsi.fastutil.ints.IntList;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.entity.player.StackedContents;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/** Ingredient that matches if all child ingredients match */
|
||||
public class IntersectionIngredient extends AbstractIngredient {
|
||||
/**
|
||||
* Gets an intersection ingredient
|
||||
* @param ingredients List of ingredients to match
|
||||
* @return Ingredient that only matches if all the passed ingredients match
|
||||
*/
|
||||
public static Ingredient of(Ingredient... ingredients) {
|
||||
if (ingredients.length == 0)
|
||||
throw new IllegalArgumentException("Cannot create an IntersectionIngredient with no children, use Ingredient.of() to create an empty ingredient");
|
||||
if (ingredients.length == 1)
|
||||
return ingredients[0];
|
||||
|
||||
return new IntersectionIngredient(Arrays.asList(ingredients));
|
||||
}
|
||||
|
||||
private final List<Ingredient> children;
|
||||
private final boolean isSimple;
|
||||
private ItemStack[] intersectedMatchingStacks = null;
|
||||
private IntList packedMatchingStacks = null;
|
||||
|
||||
private IntersectionIngredient(List<Ingredient> children) {
|
||||
if (children.size() < 2)
|
||||
throw new IllegalArgumentException("Cannot create an IntersectionIngredient with one or no children");
|
||||
this.children = Collections.unmodifiableList(children);
|
||||
this.isSimple = children.stream().allMatch(Ingredient::isSimple);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack stack) {
|
||||
if (stack == null || stack.isEmpty())
|
||||
return false;
|
||||
|
||||
for (Ingredient ingredient : children)
|
||||
if (!ingredient.test(stack))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack[] getItems() {
|
||||
if (this.intersectedMatchingStacks == null) {
|
||||
this.intersectedMatchingStacks = Arrays
|
||||
.stream(children.get(0).getItems())
|
||||
.filter(stack -> {
|
||||
// the first ingredient is treated as a base, filtered by the second onwards
|
||||
for (int i = 1; i < children.size(); i++)
|
||||
if (!children.get(i).test(stack))
|
||||
return false;
|
||||
return true;
|
||||
})
|
||||
.toArray(ItemStack[]::new);
|
||||
}
|
||||
return intersectedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return children.stream().anyMatch(Ingredient::isEmpty);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple() {
|
||||
return isSimple;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void invalidate() {
|
||||
super.invalidate();
|
||||
this.intersectedMatchingStacks = null;
|
||||
this.packedMatchingStacks = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntList getStackingIds() {
|
||||
if (this.packedMatchingStacks == null || checkInvalidation()) {
|
||||
markValid();
|
||||
var matchingStacks = getItems();
|
||||
this.packedMatchingStacks = new IntArrayList(matchingStacks.length);
|
||||
for (var stack : matchingStacks)
|
||||
this.packedMatchingStacks.add(StackedContents.getStackingIndex(stack));
|
||||
this.packedMatchingStacks.sort(IntComparators.NATURAL_COMPARATOR);
|
||||
}
|
||||
return packedMatchingStacks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<IntersectionIngredient> serializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
public static final Codec<IntersectionIngredient> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Ingredient.CODEC.listOf().fieldOf("children").forGetter(i -> i.children)
|
||||
)
|
||||
.apply(builder, IntersectionIngredient::new)
|
||||
);
|
||||
|
||||
public static final IIngredientSerializer<IntersectionIngredient> SERIALIZER = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public Codec<? extends IntersectionIngredient> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntersectionIngredient read(FriendlyByteBuf buffer) {
|
||||
return new IntersectionIngredient(buffer.readList(Ingredient::fromNetwork));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, IntersectionIngredient value) {
|
||||
buffer.writeCollection(value.children, (b, i) -> i.toNetwork(b));
|
||||
}
|
||||
};
|
||||
}
|
||||
+139
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import net.minecraft.advancements.critereon.NbtPredicate;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.TagParser;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import net.minecraft.world.level.ItemLike;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/** Ingredient that matches the given items, performing a partial NBT match. Use {@link StrictNBTIngredient} if you want exact match on NBT */
|
||||
public class PartialNBTIngredient extends AbstractIngredient {
|
||||
public static Builder builder() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
private final List<Item> items;
|
||||
private final CompoundTag nbt;
|
||||
private final NbtPredicate predicate;
|
||||
|
||||
private PartialNBTIngredient(List<Item> items, CompoundTag nbt) {
|
||||
super(items.stream().map(item -> {
|
||||
var stack = new ItemStack(item);
|
||||
// copy NBT to prevent the stack from modifying the original, as capabilities or vanilla item durability will modify the tag
|
||||
stack.setTag(nbt.copy());
|
||||
return new Ingredient.ItemValue(stack);
|
||||
}));
|
||||
|
||||
if (items.isEmpty())
|
||||
throw new IllegalArgumentException("Cannot create a PartialNBTIngredient with no items");
|
||||
|
||||
this.items = Collections.unmodifiableList(items);
|
||||
this.nbt = nbt;
|
||||
this.predicate = new NbtPredicate(nbt);
|
||||
}
|
||||
|
||||
/** Creates a new ingredient matching any item from the list, containing the given NBT */
|
||||
public static PartialNBTIngredient of(CompoundTag nbt, ItemLike... items) {
|
||||
return new PartialNBTIngredient(Arrays.stream(items).map(ItemLike::asItem).toList(), nbt);
|
||||
}
|
||||
|
||||
/** Creates a new ingredient matching the given item, containing the given NBT */
|
||||
public static PartialNBTIngredient of(ItemLike item, CompoundTag nbt) {
|
||||
return new PartialNBTIngredient(List.of(item.asItem()), nbt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack input) {
|
||||
if (input == null)
|
||||
return false;
|
||||
return items.contains(input.getItem()) && predicate.matches(input.getShareTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSimple() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<? extends Ingredient> serializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
public static final Codec<PartialNBTIngredient> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
ForgeRegistries.ITEMS.getCodec().listOf().fieldOf("items").forGetter(i -> i.items),
|
||||
TagParser.AS_CODEC.fieldOf("nbt").forGetter(i -> i.nbt)
|
||||
).apply(builder, PartialNBTIngredient::new)
|
||||
);
|
||||
|
||||
public static final IIngredientSerializer<PartialNBTIngredient> SERIALIZER = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public Codec<? extends PartialNBTIngredient> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PartialNBTIngredient read(FriendlyByteBuf buffer) {
|
||||
var items = buffer.readList(b -> b.readRegistryIdUnsafe(ForgeRegistries.ITEMS));
|
||||
var nbt = buffer.readNbt();
|
||||
return new PartialNBTIngredient(items, Objects.requireNonNull(nbt));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, PartialNBTIngredient value) {
|
||||
buffer.writeCollection(value.items, (b, item) -> b.writeRegistryIdUnsafe(ForgeRegistries.ITEMS, item));
|
||||
buffer.writeNbt(value.nbt);
|
||||
}
|
||||
};
|
||||
|
||||
public static class Builder {
|
||||
private List<ItemLike> items = new ArrayList<>();
|
||||
private CompoundTag nbt;
|
||||
|
||||
public Builder nbt(CompoundTag value) {
|
||||
if (this.nbt != null)
|
||||
throw new IllegalStateException("NBT Tag already set");
|
||||
this.nbt = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder item(ItemLike item) {
|
||||
this.items.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder items(ItemLike... values) {
|
||||
for (var item : values)
|
||||
this.items.add(item);
|
||||
return this;
|
||||
}
|
||||
|
||||
public PartialNBTIngredient build() {
|
||||
if (nbt == null)
|
||||
throw new IllegalStateException("NBT Data not set");
|
||||
if (items.isEmpty())
|
||||
throw new IllegalStateException("No items added");
|
||||
|
||||
return PartialNBTIngredient.of(nbt, items.stream().toArray(ItemLike[]::new));
|
||||
}
|
||||
}
|
||||
}
|
||||
+35
-13
@@ -3,10 +3,11 @@
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.crafting;
|
||||
package net.minecraftforge.common.crafting.ingredients;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.crafting.Ingredient;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -15,22 +16,17 @@ import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
/** Ingredient that matches the given stack, performing an exact NBT match. Use {@link PartialNBTIngredient} if you need partial match. */
|
||||
public class StrictNBTIngredient extends Ingredient {
|
||||
public static final Codec<StrictNBTIngredient> CODEC = RecordCodecBuilder.create(b -> b.group(
|
||||
ItemStack.CODEC.fieldOf("stack").forGetter(i -> i.stack)
|
||||
).apply(b, StrictNBTIngredient::new));
|
||||
|
||||
private final ItemStack stack;
|
||||
protected StrictNBTIngredient(ItemStack stack) {
|
||||
super(Stream.of(new Ingredient.ItemValue(stack)));
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
/** Creates a new ingredient matching the given stack and tag */
|
||||
public class StrictNBTIngredient extends AbstractIngredient {
|
||||
public static StrictNBTIngredient of(ItemStack stack) {
|
||||
return new StrictNBTIngredient(stack);
|
||||
}
|
||||
|
||||
private final ItemStack stack;
|
||||
private StrictNBTIngredient(ItemStack stack) {
|
||||
super(Stream.of(new Ingredient.ItemValue(stack)));
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(@Nullable ItemStack input) {
|
||||
if (input == null)
|
||||
@@ -43,4 +39,30 @@ public class StrictNBTIngredient extends Ingredient {
|
||||
public boolean isSimple() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IIngredientSerializer<? extends Ingredient> serializer() {
|
||||
return SERIALIZER;
|
||||
}
|
||||
|
||||
public static final Codec<StrictNBTIngredient> CODEC = RecordCodecBuilder.create(b -> b.group(
|
||||
ItemStack.CODEC.fieldOf("stack").forGetter(i -> i.stack)
|
||||
).apply(b, StrictNBTIngredient::new));
|
||||
|
||||
public static final IIngredientSerializer<StrictNBTIngredient> SERIALIZER = new IIngredientSerializer<>() {
|
||||
@Override
|
||||
public Codec<? extends StrictNBTIngredient> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf buffer, StrictNBTIngredient value) {
|
||||
buffer.writeItem(value.stack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StrictNBTIngredient read(FriendlyByteBuf buffer) {
|
||||
return new StrictNBTIngredient(buffer.readItem());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -8,7 +8,6 @@ package net.minecraftforge.common.data;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.data.PackOutput;
|
||||
import net.minecraft.data.tags.ItemTagsProvider;
|
||||
import net.minecraft.data.tags.TagsProvider;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.ItemTags;
|
||||
import net.minecraft.tags.TagKey;
|
||||
|
||||
@@ -437,7 +437,7 @@ public interface IForgeItem {
|
||||
* @param damage the new damage value
|
||||
*/
|
||||
default void setDamage(ItemStack stack, int damage) {
|
||||
stack.getOrCreateTag().putInt("Damage", Math.max(0, damage));
|
||||
stack.getOrCreateTag().putInt(ItemStack.TAG_DAMAGE, Math.max(0, damage));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) Forge Development LLC and contributors
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
public interface INBTBuilder {
|
||||
default Builder nbt() {
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
public static class Builder implements INBTBuilder {
|
||||
private final CompoundTag tag = new CompoundTag();
|
||||
|
||||
public CompoundTag build() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public Builder tag(String key, Tag value) {
|
||||
tag.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putByte(String key, byte value) {
|
||||
tag.putByte(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putShort(String key, short value) {
|
||||
tag.putShort(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putInt(String key, int value) {
|
||||
tag.putInt(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putLong(String key, long value) {
|
||||
tag.putLong(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putFloat(String key, float value) {
|
||||
tag.putFloat(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putDouble(String key, double value) {
|
||||
tag.putDouble(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putByteArray(String key, byte... value) {
|
||||
tag.putByteArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putByteArray(String key, List<Byte> value) {
|
||||
tag.putByteArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putIntArray(String key, int... value) {
|
||||
tag.putIntArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putIntArray(String key, List<Integer> value) {
|
||||
tag.putIntArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putLongArray(String key, long... value) {
|
||||
tag.putLongArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder putLongArray(String key, List<Long> value) {
|
||||
tag.putLongArray(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder put(String key, boolean value) {
|
||||
tag.putBoolean(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder put(String key, String value) {
|
||||
tag.putString(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder put(String key, UUID value) {
|
||||
tag.putUUID(key, value);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
@@ -21,32 +20,27 @@ import java.util.TreeSet;
|
||||
* This is implemented very basically, and thus is not a speedy system.
|
||||
* This is not recommended for used in high traffic areas, and is mainly intended for writing to disc.
|
||||
*/
|
||||
public class SortedProperties extends Properties
|
||||
{
|
||||
public class SortedProperties extends Properties {
|
||||
private static final long serialVersionUID = -8913480931455982442L;
|
||||
|
||||
@Override
|
||||
public Set<Map.Entry<Object, Object>> entrySet()
|
||||
{
|
||||
public Set<Map.Entry<Object, Object>> entrySet() {
|
||||
Set<Map.Entry<Object, Object>> ret = new TreeSet<>((left, right) -> left.getKey().toString().compareTo(right.getKey().toString()));
|
||||
ret.addAll(super.entrySet());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Object> keySet()
|
||||
{
|
||||
public Set<Object> keySet() {
|
||||
return new TreeSet<>(super.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized Enumeration<Object> keys()
|
||||
{
|
||||
public synchronized Enumeration<Object> keys() {
|
||||
return Collections.enumeration(new TreeSet<>(super.keySet()));
|
||||
}
|
||||
|
||||
public static void store(Properties props, Writer stream, String comment) throws IOException
|
||||
{
|
||||
public static void store(Properties props, Writer stream, String comment) throws IOException {
|
||||
SortedProperties sorted = new SortedProperties();
|
||||
sorted.putAll(props);
|
||||
sorted.store(stream, comment);
|
||||
|
||||
@@ -8,23 +8,26 @@ package net.minecraftforge.common.world;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import com.mojang.datafixers.util.Either;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.HolderSet;
|
||||
import net.minecraft.core.RegistryCodecs;
|
||||
import net.minecraft.util.ExtraCodecs;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.MobCategory;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.MobSpawnSettings.SpawnerData;
|
||||
import net.minecraft.world.level.levelgen.GenerationStep.Decoration;
|
||||
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.common.world.ModifiableBiomeInfo.BiomeInfo.Builder;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
public final class ForgeBiomeModifiers
|
||||
{
|
||||
public final class ForgeBiomeModifiers {
|
||||
private ForgeBiomeModifiers() {} // Utility class.
|
||||
|
||||
/**
|
||||
@@ -43,22 +46,24 @@ public final class ForgeBiomeModifiers
|
||||
* @param features PlacedFeatures to add to biomes.
|
||||
* @param step Decoration step to run features in.
|
||||
*/
|
||||
public static record AddFeaturesBiomeModifier(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features, Decoration step) implements BiomeModifier
|
||||
{
|
||||
public static record AddFeaturesBiomeModifier(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features, Decoration step) implements BiomeModifier {
|
||||
public static final Codec<AddFeaturesBiomeModifier> CODEC = RecordCodecBuilder.create(builder -> builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(AddFeaturesBiomeModifier::biomes),
|
||||
PlacedFeature.LIST_CODEC.fieldOf("features").forGetter(AddFeaturesBiomeModifier::features),
|
||||
Decoration.CODEC.fieldOf("step").forGetter(AddFeaturesBiomeModifier::step)
|
||||
).apply(builder, AddFeaturesBiomeModifier::new));
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.ADD && this.biomes.contains(biome))
|
||||
{
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder) {
|
||||
if (phase == Phase.ADD && this.biomes.contains(biome)) {
|
||||
BiomeGenerationSettingsBuilder generationSettings = builder.getGenerationSettings();
|
||||
this.features.forEach(holder -> generationSettings.addFeature(this.step, holder));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends BiomeModifier> codec()
|
||||
{
|
||||
return ForgeMod.ADD_FEATURES_BIOME_MODIFIER_TYPE.get();
|
||||
public Codec<? extends BiomeModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,35 +82,38 @@ public final class ForgeBiomeModifiers
|
||||
* @param features PlacedFeatures to remove from biomes.
|
||||
* @param steps Decoration steps to remove features from.
|
||||
*/
|
||||
public static record RemoveFeaturesBiomeModifier(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features, Set<Decoration> steps) implements BiomeModifier
|
||||
{
|
||||
public static record RemoveFeaturesBiomeModifier(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features, Set<Decoration> steps) implements BiomeModifier {
|
||||
public static final Codec<RemoveFeaturesBiomeModifier> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(RemoveFeaturesBiomeModifier::biomes),
|
||||
PlacedFeature.LIST_CODEC.fieldOf("features").forGetter(RemoveFeaturesBiomeModifier::features),
|
||||
ExtraCodecs.either(Decoration.CODEC.listOf(), Decoration.CODEC).<Set<Decoration>>xmap(
|
||||
either -> either.map(Set::copyOf, Set::of), // convert list/singleton to set when decoding
|
||||
set -> set.size() == 1 ? Either.right(set.toArray(Decoration[]::new)[0]) : Either.left(List.copyOf(set))
|
||||
).optionalFieldOf("steps", EnumSet.allOf(Decoration.class)).forGetter(RemoveFeaturesBiomeModifier::steps)
|
||||
).apply(builder, RemoveFeaturesBiomeModifier::new));
|
||||
|
||||
/**
|
||||
* Creates a modifier that removes the given features from all decoration steps in the given biomes.
|
||||
* @param biomes Biomes to remove features from.
|
||||
* @param features PlacedFeatures to remove from biomes.
|
||||
*/
|
||||
public static RemoveFeaturesBiomeModifier allSteps(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features)
|
||||
{
|
||||
public static RemoveFeaturesBiomeModifier allSteps(HolderSet<Biome> biomes, HolderSet<PlacedFeature> features) {
|
||||
return new RemoveFeaturesBiomeModifier(biomes, features, EnumSet.allOf(Decoration.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.REMOVE && this.biomes.contains(biome))
|
||||
{
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder) {
|
||||
if (phase == Phase.REMOVE && this.biomes.contains(biome)) {
|
||||
BiomeGenerationSettingsBuilder generationSettings = builder.getGenerationSettings();
|
||||
for (Decoration step : this.steps)
|
||||
{
|
||||
generationSettings.getFeatures(step).removeIf(this.features::contains);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends BiomeModifier> codec()
|
||||
{
|
||||
return ForgeMod.REMOVE_FEATURES_BIOME_MODIFIER_TYPE.get();
|
||||
public Codec<? extends BiomeModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,37 +155,39 @@ public final class ForgeBiomeModifiers
|
||||
* @param biomes Biomes to add mob spawns to.
|
||||
* @param spawners List of SpawnerDatas specifying EntityType, weight, and pack size.
|
||||
*/
|
||||
public record AddSpawnsBiomeModifier(HolderSet<Biome> biomes, List<SpawnerData> spawners) implements BiomeModifier
|
||||
{
|
||||
public record AddSpawnsBiomeModifier(HolderSet<Biome> biomes, List<SpawnerData> spawners) implements BiomeModifier {
|
||||
public static final Codec<AddSpawnsBiomeModifier> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(AddSpawnsBiomeModifier::biomes),
|
||||
// Allow either a list or single spawner, attempting to decode the list format first.
|
||||
ExtraCodecs.either(SpawnerData.CODEC.listOf(), SpawnerData.CODEC).xmap(
|
||||
either -> either.map(Function.identity(), List::of), // convert list/singleton to list when decoding
|
||||
list -> list.size() == 1 ? Either.right(list.get(0)) : Either.left(list) // convert list to singleton/list when encoding
|
||||
).fieldOf("spawners").forGetter(AddSpawnsBiomeModifier::spawners)
|
||||
).apply(builder, AddSpawnsBiomeModifier::new));
|
||||
|
||||
/**
|
||||
* Convenience method for using a single spawn data.
|
||||
* @param biomes Biomes to add mob spawns to.
|
||||
* @param spawner SpawnerData specifying EntityTYpe, weight, and pack size.
|
||||
* @return AddSpawnsBiomeModifier that adds a single spawn entry to the specified biomes.
|
||||
*/
|
||||
public static AddSpawnsBiomeModifier singleSpawn(HolderSet<Biome> biomes, SpawnerData spawner)
|
||||
{
|
||||
public static AddSpawnsBiomeModifier singleSpawn(HolderSet<Biome> biomes, SpawnerData spawner) {
|
||||
return new AddSpawnsBiomeModifier(biomes, List.of(spawner));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.ADD && this.biomes.contains(biome))
|
||||
{
|
||||
MobSpawnSettingsBuilder spawns = builder.getMobSpawnSettings();
|
||||
for (SpawnerData spawner : this.spawners)
|
||||
{
|
||||
EntityType<?> type = spawner.type;
|
||||
spawns.addSpawn(type.getCategory(), spawner);
|
||||
}
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder) {
|
||||
if (phase == Phase.ADD && this.biomes.contains(biome)) {
|
||||
var spawns = builder.getMobSpawnSettings();
|
||||
for (var spawner : this.spawners)
|
||||
spawns.addSpawn(spawner.type.getCategory(), spawner);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends BiomeModifier> codec()
|
||||
{
|
||||
return ForgeMod.ADD_SPAWNS_BIOME_MODIFIER_TYPE.get();
|
||||
public Codec<? extends BiomeModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,26 +204,25 @@ public final class ForgeBiomeModifiers
|
||||
* @param biomes Biomes to add mob spawns to.
|
||||
* @param entityTypes EntityTypes to remove from spawn lists.
|
||||
*/
|
||||
public record RemoveSpawnsBiomeModifier(HolderSet<Biome> biomes, HolderSet<EntityType<?>> entityTypes) implements BiomeModifier
|
||||
{
|
||||
public record RemoveSpawnsBiomeModifier(HolderSet<Biome> biomes, HolderSet<EntityType<?>> entityTypes) implements BiomeModifier {
|
||||
public static final Codec<RemoveSpawnsBiomeModifier> CODEC = RecordCodecBuilder.create(builder ->
|
||||
builder.group(
|
||||
Biome.LIST_CODEC.fieldOf("biomes").forGetter(RemoveSpawnsBiomeModifier::biomes),
|
||||
RegistryCodecs.homogeneousList(ForgeRegistries.Keys.ENTITY_TYPES).fieldOf("entity_types").forGetter(RemoveSpawnsBiomeModifier::entityTypes)
|
||||
).apply(builder, RemoveSpawnsBiomeModifier::new));
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
if (phase == Phase.REMOVE && this.biomes.contains(biome))
|
||||
{
|
||||
MobSpawnSettingsBuilder spawnBuilder = builder.getMobSpawnSettings();
|
||||
for (MobCategory category : MobCategory.values())
|
||||
{
|
||||
List<SpawnerData> spawns = spawnBuilder.getSpawner(category);
|
||||
spawns.removeIf(spawnerData -> this.entityTypes.contains(ForgeRegistries.ENTITY_TYPES.getHolder(spawnerData.type).get()));
|
||||
}
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder) {
|
||||
if (phase == Phase.REMOVE && this.biomes.contains(biome)) {
|
||||
var spawns = builder.getMobSpawnSettings();
|
||||
for (var category : MobCategory.values())
|
||||
spawns.getSpawner(category).removeIf(data -> this.entityTypes.contains(ForgeRegistries.ENTITY_TYPES.getHolder(data.type).get()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends BiomeModifier> codec()
|
||||
{
|
||||
return ForgeMod.REMOVE_SPAWNS_BIOME_MODIFIER_TYPE.get();
|
||||
public Codec<? extends BiomeModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,22 +8,21 @@ package net.minecraftforge.common.world;
|
||||
import com.mojang.serialization.Codec;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.common.world.ModifiableBiomeInfo.BiomeInfo.Builder;
|
||||
|
||||
public class NoneBiomeModifier implements BiomeModifier
|
||||
{
|
||||
/**
|
||||
* Noop biome modifier. Can be used in a biome modifier json with "type": "forge:none".
|
||||
*/
|
||||
public class NoneBiomeModifier implements BiomeModifier {
|
||||
public static final NoneBiomeModifier INSTANCE = new NoneBiomeModifier();
|
||||
public static final Codec<NoneBiomeModifier> CODEC = Codec.unit(NoneBiomeModifier.INSTANCE);
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder)
|
||||
{
|
||||
// NOOP - intended for datapack makers who want to disable a biome modifier
|
||||
public void modify(Holder<Biome> biome, Phase phase, Builder builder) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Codec<? extends BiomeModifier> codec()
|
||||
{
|
||||
return ForgeMod.NONE_BIOME_MODIFIER_TYPE.get();
|
||||
public Codec<? extends BiomeModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
@@ -8,22 +8,21 @@ package net.minecraftforge.common.world;
|
||||
import com.mojang.serialization.Codec;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.world.level.levelgen.structure.Structure;
|
||||
import net.minecraftforge.common.ForgeMod;
|
||||
import net.minecraftforge.common.world.ModifiableStructureInfo.StructureInfo.Builder;
|
||||
|
||||
public class NoneStructureModifier implements StructureModifier
|
||||
{
|
||||
/**
|
||||
* Noop structure modifier. Can be used in a structure modifier json with "type": "forge:none".
|
||||
* intended for datapack makers who want to disable a structure modifier
|
||||
*/
|
||||
public class NoneStructureModifier implements StructureModifier {
|
||||
public static final NoneStructureModifier INSTANCE = new NoneStructureModifier();
|
||||
public static final Codec<NoneStructureModifier> CODEC = Codec.unit(NoneStructureModifier.INSTANCE);
|
||||
|
||||
@Override
|
||||
public void modify(Holder<Structure> structure, Phase phase, Builder builder)
|
||||
{
|
||||
// NOOP - intended for datapack makers who want to disable a structure modifier
|
||||
}
|
||||
public void modify(Holder<Structure> structure, Phase phase, Builder builder) {}
|
||||
|
||||
@Override
|
||||
public Codec<? extends StructureModifier> codec()
|
||||
{
|
||||
return ForgeMod.NONE_STRUCTURE_MODIFIER_TYPE.get();
|
||||
public Codec<? extends StructureModifier> codec() {
|
||||
return CODEC;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user