Guest User

SmelterEnchantment.class

a guest
Jun 15th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. package com.doggie.raremetalsrevised.enchantments;
  2.  
  3.  
  4. import com.doggie.raremetalsrevised.init.EnchantmentInit;
  5. import com.google.gson.JsonObject;
  6. import net.minecraft.block.AirBlock;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.Blocks;
  9. import net.minecraft.enchantment.Enchantment;
  10. import net.minecraft.enchantment.EnchantmentHelper;
  11. import net.minecraft.enchantment.EnchantmentType;
  12. import net.minecraft.entity.Entity;
  13. import net.minecraft.entity.LivingEntity;
  14. import net.minecraft.inventory.EquipmentSlotType;
  15. import net.minecraft.inventory.Inventory;
  16. import net.minecraft.item.Item;
  17. import net.minecraft.item.ItemStack;
  18. import net.minecraft.item.Rarity;
  19. import net.minecraft.item.crafting.FurnaceRecipe;
  20. import net.minecraft.item.crafting.IRecipeType;
  21. import net.minecraft.util.ResourceLocation;
  22. import net.minecraft.util.math.BlockPos;
  23. import net.minecraft.world.IWorld;
  24. import net.minecraft.world.World;
  25. import net.minecraft.world.storage.loot.LootContext;
  26. import net.minecraft.world.storage.loot.conditions.ILootCondition;
  27. import net.minecraftforge.common.loot.GlobalLootModifierSerializer;
  28. import net.minecraftforge.common.loot.LootModifier;
  29. import net.minecraftforge.event.world.BlockEvent;
  30. import net.minecraftforge.eventbus.api.SubscribeEvent;
  31. import net.minecraftforge.items.ItemHandlerHelper;
  32.  
  33. import javax.annotation.Nonnull;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import java.util.Map;
  37.  
  38.  
  39.  
  40. public class SmelterEnchantment extends Enchantment {
  41. public SmelterEnchantment(Rarity rarityIn, EnchantmentType typeIn, EquipmentSlotType... slots) {
  42. super(rarityIn, typeIn, slots);
  43. }
  44.  
  45.  
  46.  
  47. }
  48.  
  49.  
  50. /**
  51. * The smelting enchantment causes this modifier to be invoked, via the smelting loot_modifier json
  52. *
  53. */ class SmeltingEnchantmentModifier extends LootModifier {
  54. public SmeltingEnchantmentModifier(ILootCondition[] conditionsIn) {
  55. super(conditionsIn);
  56. }
  57.  
  58. @Nonnull
  59. @Override
  60. public List<ItemStack> doApply(List<ItemStack> generatedLoot, LootContext context) {
  61. ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
  62. generatedLoot.forEach((stack) -> ret.add(smelt(stack, context)));
  63. return ret;
  64. }
  65.  
  66. private static ItemStack smelt(ItemStack stack, LootContext context) {
  67. return context.getWorld().getRecipeManager().getRecipe(IRecipeType.SMELTING, new Inventory(stack), context.getWorld())
  68. .map(FurnaceRecipe::getRecipeOutput)
  69. .filter(itemStack -> !itemStack.isEmpty())
  70. .map(itemStack -> ItemHandlerHelper.copyStackWithSize(itemStack, stack.getCount() * itemStack.getCount()))
  71. .orElse(stack);
  72. }
  73.  
  74. private static class Serializer extends GlobalLootModifierSerializer<SmeltingEnchantmentModifier> {
  75. @Override
  76. public SmeltingEnchantmentModifier read(ResourceLocation name, JsonObject json, ILootCondition[] conditionsIn) {
  77. return new SmeltingEnchantmentModifier(conditionsIn);
  78. }
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment