Skip to content

Commit

Permalink
I forgot to push
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Aug 24, 2024
1 parent 3a6dc5c commit 4afbda8
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.fabricmc.loader.api.FabricLoader;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -22,6 +25,10 @@ public static ResourceLocation id(String path) {
return ResourceLocation.fromNamespaceAndPath(ID, path);
}

public static <T> ResourceKey<Registry<T>> key(String path) {
return ResourceKey.createRegistryKey(id(path));
}

public static ResourceLocation neo(String path) {
return ResourceLocation.fromNamespaceAndPath(NEO_ID, path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@
import com.google.common.collect.ImmutableList;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;

import io.github.fabricators_of_create.porting_lib.core.util.LamdbaExceptionUtils;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import io.github.fabricators_of_create.porting_lib.conditions.ICondition;
import io.github.fabricators_of_create.porting_lib.conditions.WithConditions;
import net.minecraft.core.HolderLookup;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;

/**
* Provider for forge's GlobalLootModifier system. See {@link LootModifier}
*
Expand All @@ -29,12 +30,15 @@
public abstract class GlobalLootModifierProvider implements DataProvider {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private final PackOutput output;
private final CompletableFuture<HolderLookup.Provider> registriesLookup;
protected HolderLookup.Provider registries;
private final String modid;
private final Map<String, JsonElement> toSerialize = new HashMap<>();
private final Map<String, WithConditions<IGlobalLootModifier>> toSerialize = new HashMap<>();
private boolean replace = false;

public GlobalLootModifierProvider(PackOutput output, String modid) {
public GlobalLootModifierProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries, String modid) {
this.output = output;
this.registriesLookup = registries;
this.modid = modid;
}

Expand All @@ -51,20 +55,27 @@ protected void replacing() {
protected abstract void start();

@Override
public CompletableFuture<?> run(CachedOutput cache) {
public final CompletableFuture<?> run(CachedOutput cache) {
return this.registriesLookup.thenCompose(registries -> this.run(cache, registries));
}

protected CompletableFuture<?> run(CachedOutput cache, HolderLookup.Provider registries) {
this.registries = registries;
start();

Path forgePath = this.output.getOutputFolder(PackOutput.Target.DATA_PACK).resolve("forge").resolve("loot_modifiers").resolve("global_loot_modifiers.json");
Path forgePath = this.output.getOutputFolder(PackOutput.Target.DATA_PACK).resolve("neoforge").resolve("loot_modifiers").resolve("global_loot_modifiers.json");
Path modifierFolderPath = this.output.getOutputFolder(PackOutput.Target.DATA_PACK).resolve(this.modid).resolve("loot_modifiers");
List<ResourceLocation> entries = new ArrayList<>();

ImmutableList.Builder<CompletableFuture<?>> futuresBuilder = new ImmutableList.Builder<>();

toSerialize.forEach(LamdbaExceptionUtils.rethrowBiConsumer((name, json) -> {
entries.add(new ResourceLocation(modid, name));
for (var entry : toSerialize.entrySet()) {
var name = entry.getKey();
var lootModifier = entry.getValue();
entries.add(ResourceLocation.fromNamespaceAndPath(modid, name));
Path modifierPath = modifierFolderPath.resolve(name + ".json");
futuresBuilder.add(DataProvider.saveStable(cache, json, modifierPath));
}));
futuresBuilder.add(DataProvider.saveStable(cache, registries, IGlobalLootModifier.CONDITIONAL_CODEC, Optional.of(lootModifier), modifierPath));
}

JsonObject forgeJson = new JsonObject();
forgeJson.addProperty("replace", this.replace);
Expand All @@ -78,12 +89,23 @@ public CompletableFuture<?> run(CachedOutput cache) {
/**
* Passes in the data needed to create the file without any extra objects.
*
* @param modifier The name of the modifier, which will be the file name.
* @param instance The instance to serialize
* @param modifier the name of the modifier, which will be the file name
* @param instance the instance to serialize
* @param conditions a list of conditions to add to the GLM file
*/
public <T extends IGlobalLootModifier> void add(String modifier, T instance, List<ICondition> conditions) {
this.toSerialize.put(modifier, new WithConditions<>(conditions, instance));
}

/**
* Passes in the data needed to create the file without any extra objects.
*
* @param modifier the name of the modifier, which will be the file name
* @param instance the instance to serialize
* @param conditions a list of conditions to add to the GLM file
*/
public <T extends IGlobalLootModifier> void add(String modifier, T instance) {
JsonElement json = IGlobalLootModifier.DIRECT_CODEC.encodeStart(JsonOps.INSTANCE, instance).getOrThrow(false, s -> {});
this.toSerialize.put(modifier, json);
public <T extends IGlobalLootModifier> void add(String modifier, T instance, ICondition... conditions) {
add(modifier, instance, Arrays.asList(conditions));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package io.github.fabricators_of_create.porting_lib.loot;

import java.util.List;
import java.util.function.Predicate;

import com.mojang.datafixers.Products;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.levelgen.feature.trunkplacers.BendingTrunkPlacer;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.predicates.AllOfCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditions;
import org.jetbrains.annotations.NotNull;

/**
* A base implementation of a Global Loot Modifier for modders to extend.
Expand All @@ -37,14 +35,14 @@ protected static <T extends LootModifier> Products.P1<RecordCodecBuilder.Mu<T>,

/**
* Constructs a LootModifier.
*
* @param conditionsIn the ILootConditions that need to be matched before the loot is modified.
*/
protected LootModifier(LootItemCondition[] conditionsIn) {
this.conditions = conditionsIn;
this.combinedConditions = LootItemConditions.andConditions(List.of(conditionsIn));
this.combinedConditions = AllOfCondition.allOf(List.of(conditionsIn));
}

@NotNull
@Override
public final ObjectArrayList<ItemStack> apply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
return this.combinedConditions.test(context) ? this.doApply(generatedLoot, context) : generatedLoot;
Expand All @@ -53,10 +51,11 @@ public final ObjectArrayList<ItemStack> apply(ObjectArrayList<ItemStack> generat
/**
* Applies the modifier to the generated loot (all loot conditions have already been checked
* and have returned true).
*
* @param generatedLoot the list of ItemStacks that will be dropped, generated by loot tables
* @param context the LootContext, identical to what is passed to loot tables
* @param context the LootContext, identical to what is passed to loot tables
* @return modified loot drops
*/
@NotNull

protected abstract ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.mojang.serialization.Codec;

import com.mojang.serialization.MapCodec;

import io.github.fabricators_of_create.porting_lib.core.PortingLib;
import io.github.fabricators_of_create.porting_lib.loot.extensions.LootTableBuilderExtensions;
import io.github.fabricators_of_create.porting_lib.util.RegistryBuilder;
Expand All @@ -21,8 +23,8 @@
import net.minecraft.world.level.storage.loot.LootContext;

public class PortingLibLoot implements ModInitializer {
public static final ResourceKey<Registry<Codec<? extends IGlobalLootModifier>>> GLOBAL_LOOT_MODIFIER_SERIALIZERS_KEY = ResourceKey.createRegistryKey(PortingLib.id("global_loot_modifier_serializers"));
public static final Registry<Codec<? extends IGlobalLootModifier>> GLOBAL_LOOT_MODIFIER_SERIALIZERS = new RegistryBuilder<>(GLOBAL_LOOT_MODIFIER_SERIALIZERS_KEY).create();
public static final ResourceKey<Registry<MapCodec<? extends IGlobalLootModifier>>> GLOBAL_LOOT_MODIFIER_SERIALIZERS_KEY = PortingLib.key("global_loot_modifier_serializers");
public static final Registry<MapCodec<? extends IGlobalLootModifier>> GLOBAL_LOOT_MODIFIER_SERIALIZERS = new RegistryBuilder<>(GLOBAL_LOOT_MODIFIER_SERIALIZERS_KEY).create();
public static final ResourceLocation LAST = PortingLib.id("last");

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package io.github.fabricators_of_create.porting_lib.models.geometry.extensions;

import com.mojang.math.Transformation;

import io.github.fabricators_of_create.porting_lib.core.PortingLib;
import io.github.fabricators_of_create.porting_lib.models.geometry.BlockGeometryBakingContext;
import io.github.fabricators_of_create.porting_lib.models.geometry.IUnbakedGeometry;
import io.github.fabricators_of_create.porting_lib.models.geometry.VisibilityData;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand Down

0 comments on commit 4afbda8

Please sign in to comment.