Skip to content

Commit

Permalink
Wrote a global look modifier that I wound up not using -- I'll uncomm…
Browse files Browse the repository at this point in the history
…ent it if its necessary, but I can't do particles or tool damage with it..
  • Loading branch information
Direwolf20-MC committed Mar 10, 2024
1 parent 0acbeca commit dfc9a25
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.direwolf20.justdirethings.common.items.tools.utils;

import com.direwolf20.justdirethings.setup.Registration;
import com.google.common.base.Suppliers;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.neoforged.neoforge.common.loot.IGlobalLootModifier;
import net.neoforged.neoforge.common.loot.LootModifier;
import net.neoforged.neoforge.items.ItemHandlerHelper;

import java.util.function.Supplier;

/**
* An alternate way to smelt drops - I wrote all this, but went back to my old approach. Will re-implement if told its necessary
*/
public class AutoSmeltLootModifier extends LootModifier {
public static final Supplier<Codec<AutoSmeltLootModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, AutoSmeltLootModifier::new)));


public AutoSmeltLootModifier(LootItemCondition[] conditionsIn) {
super(conditionsIn);
}

@Override
protected ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
ItemStack tool = context.getParamOrNull(LootContextParams.TOOL);
if (tool != null && tool.getItem() instanceof ToggleableTool toggleableTool) {
if (toggleableTool.canUseAbility(tool, Ability.SMELTER)) {
ObjectArrayList<ItemStack> ret = new ObjectArrayList<ItemStack>();
generatedLoot.forEach((stack) -> ret.add(smelt(stack, context)));
return ret;
}
}
return generatedLoot;
}

private static ItemStack smelt(ItemStack stack, LootContext context) {
return context.getLevel().getRecipeManager().getRecipeFor(RecipeType.SMELTING, new SimpleContainer(stack), context.getLevel())
.map(smeltingRecipe -> smeltingRecipe.value().getResultItem(context.getLevel().registryAccess()))
.filter(itemStack -> !itemStack.isEmpty())
.map(itemStack -> ItemHandlerHelper.copyStackWithSize(itemStack, stack.getCount() * itemStack.getCount()))
.orElse(stack);
}

@Override
public Codec<? extends IGlobalLootModifier> codec() {
return Registration.AUTO_SMELT_LOOT_MODIFIER.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static void gatherData(GatherDataEvent event) {
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();

generator.addProvider(event.includeServer(), new JustDireRecipes(packOutput));
//generator.addProvider(event.includeServer(), new JustDireLootProviders(packOutput));
generator.addProvider(event.includeServer(), new LootTableProvider(packOutput, Collections.emptySet(),
List.of(new LootTableProvider.SubProviderEntry(JustDireLootTables::new, LootContextParamSets.BLOCK))));
JustDireBlockTags blockTags = new JustDireBlockTags(packOutput, lookupProvider, event.getExistingFileHelper());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.direwolf20.justdirethings.datagen;

import com.direwolf20.justdirethings.JustDireThings;
import com.direwolf20.justdirethings.common.items.tools.utils.AutoSmeltLootModifier;
import net.minecraft.data.PackOutput;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.neoforged.neoforge.common.data.GlobalLootModifierProvider;

/**
* An alternate way to smelt drops - I wrote all this, but went back to my old approach. Will re-implement if told its necessary
*/
public class JustDireLootProviders extends GlobalLootModifierProvider {

public JustDireLootProviders(PackOutput output) {
super(output, JustDireThings.MODID);
}

@Override
protected void start() {
this.add("auto_smelt", new AutoSmeltLootModifier(new LootItemCondition[]{}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.direwolf20.justdirethings.common.items.resources.RawBlazegold;
import com.direwolf20.justdirethings.common.items.resources.RawFerricore;
import com.direwolf20.justdirethings.common.items.tools.*;
import com.direwolf20.justdirethings.common.items.tools.utils.AutoSmeltLootModifier;
import com.direwolf20.justdirethings.datagen.recipes.GooSpreadRecipe;
import com.mojang.serialization.Codec;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.MenuType;
Expand All @@ -34,6 +36,7 @@
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.common.extensions.IMenuTypeExtension;
import net.neoforged.neoforge.common.loot.IGlobalLootModifier;
import net.neoforged.neoforge.energy.EnergyStorage;
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.registries.DeferredHolder;
Expand All @@ -59,6 +62,13 @@ public class Registration {
public static final DeferredRegister<RecipeSerializer<?>> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, JustDireThings.MODID);
public static final Supplier<GooSpreadRecipe.Serializer> GOO_SPREAD_RECIPE_SERIALIZER = RECIPE_SERIALIZERS.register("goospread", GooSpreadRecipe.Serializer::new);

/**
* An alternate way to smelt drops - I wrote all this, but went back to my old approach. Will re-implement if told its necessary
*/
private static final DeferredRegister<Codec<? extends IGlobalLootModifier>> GLOBAL_LOOT_MODIFIER_SERIALIZERS = DeferredRegister.create(NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, JustDireThings.MODID);
public static final DeferredHolder<Codec<? extends IGlobalLootModifier>, Codec<AutoSmeltLootModifier>> AUTO_SMELT_LOOT_MODIFIER = GLOBAL_LOOT_MODIFIER_SERIALIZERS.register("auto_smelt", AutoSmeltLootModifier.CODEC);


public static void init(IEventBus eventBus) {
BLOCKS.register(eventBus);
ITEMS.register(eventBus);
Expand All @@ -69,6 +79,7 @@ public static void init(IEventBus eventBus) {
RECIPE_SERIALIZERS.register(eventBus);
RECIPE_TYPES.register(eventBus);
PARTICLE_TYPES.register(eventBus);
//GLOBAL_LOOT_MODIFIER_SERIALIZERS.register(eventBus);
}

//Blocks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"entries": [
"justdirethings:auto_smelt"
]
}

0 comments on commit dfc9a25

Please sign in to comment.