From dfc9a25a27395559575e8b2e0598d9ce75766f19 Mon Sep 17 00:00:00 2001 From: direwolf20 <39863894+Direwolf20-MC@users.noreply.github.com> Date: Sat, 9 Mar 2024 21:17:23 -0500 Subject: [PATCH] Wrote a global look modifier that I wound up not using -- I'll uncomment it if its necessary, but I can't do particles or tool damage with it.. --- .../tools/utils/AutoSmeltLootModifier.java | 56 +++++++++++++++++++ .../datagen/DataGenerators.java | 1 + .../datagen/JustDireLootProviders.java | 23 ++++++++ .../justdirethings/setup/Registration.java | 11 ++++ .../loot_modifiers/global_loot_modifiers.old | 6 ++ 5 files changed, 97 insertions(+) create mode 100644 src/main/java/com/direwolf20/justdirethings/common/items/tools/utils/AutoSmeltLootModifier.java create mode 100644 src/main/java/com/direwolf20/justdirethings/datagen/JustDireLootProviders.java create mode 100644 src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.old diff --git a/src/main/java/com/direwolf20/justdirethings/common/items/tools/utils/AutoSmeltLootModifier.java b/src/main/java/com/direwolf20/justdirethings/common/items/tools/utils/AutoSmeltLootModifier.java new file mode 100644 index 00000000..54558db0 --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/common/items/tools/utils/AutoSmeltLootModifier.java @@ -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 = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).apply(inst, AutoSmeltLootModifier::new))); + + + public AutoSmeltLootModifier(LootItemCondition[] conditionsIn) { + super(conditionsIn); + } + + @Override + protected ObjectArrayList doApply(ObjectArrayList generatedLoot, LootContext context) { + ItemStack tool = context.getParamOrNull(LootContextParams.TOOL); + if (tool != null && tool.getItem() instanceof ToggleableTool toggleableTool) { + if (toggleableTool.canUseAbility(tool, Ability.SMELTER)) { + ObjectArrayList ret = new ObjectArrayList(); + 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 codec() { + return Registration.AUTO_SMELT_LOOT_MODIFIER.get(); + } +} \ No newline at end of file diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/DataGenerators.java b/src/main/java/com/direwolf20/justdirethings/datagen/DataGenerators.java index 81091e92..e8a8a21a 100644 --- a/src/main/java/com/direwolf20/justdirethings/datagen/DataGenerators.java +++ b/src/main/java/com/direwolf20/justdirethings/datagen/DataGenerators.java @@ -23,6 +23,7 @@ public static void gatherData(GatherDataEvent event) { CompletableFuture 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()); diff --git a/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLootProviders.java b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLootProviders.java new file mode 100644 index 00000000..1ef739e0 --- /dev/null +++ b/src/main/java/com/direwolf20/justdirethings/datagen/JustDireLootProviders.java @@ -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[]{})); + } + +} diff --git a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java index e246582c..5d48f5f0 100644 --- a/src/main/java/com/direwolf20/justdirethings/setup/Registration.java +++ b/src/main/java/com/direwolf20/justdirethings/setup/Registration.java @@ -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; @@ -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; @@ -59,6 +62,13 @@ public class Registration { public static final DeferredRegister> RECIPE_SERIALIZERS = DeferredRegister.create(Registries.RECIPE_SERIALIZER, JustDireThings.MODID); public static final Supplier 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> GLOBAL_LOOT_MODIFIER_SERIALIZERS = DeferredRegister.create(NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS, JustDireThings.MODID); + public static final DeferredHolder, Codec> 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); @@ -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 diff --git a/src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.old b/src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.old new file mode 100644 index 00000000..b857833a --- /dev/null +++ b/src/main/resources/data/forge/loot_modifiers/global_loot_modifiers.old @@ -0,0 +1,6 @@ +{ + "replace": false, + "entries": [ + "justdirethings:auto_smelt" + ] +} \ No newline at end of file