Guest User

Untitled

a guest
Nov 16th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.70 KB | None | 0 0
  1. package pl.faxu.cobblestonedrops;
  2.  
  3. import com.google.common.base.Suppliers;
  4. import com.mojang.logging.LogUtils;
  5. import com.mojang.serialization.Codec;
  6. import com.mojang.serialization.codecs.RecordCodecBuilder;
  7. import it.unimi.dsi.fastutil.objects.ObjectArrayList;
  8. import net.minecraft.advancements.critereon.ItemPredicate;
  9. import net.minecraft.client.Minecraft;
  10. import net.minecraft.data.DataGenerator;
  11. import net.minecraft.world.item.*;
  12. import net.minecraft.world.level.block.Block;
  13. import net.minecraft.world.level.block.Blocks;
  14. import net.minecraft.world.level.storage.loot.LootContext;
  15. import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
  16. import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
  17. import net.minecraft.world.level.storage.loot.predicates.MatchTool;
  18. import net.minecraftforge.api.distmarker.Dist;
  19. import net.minecraftforge.common.MinecraftForge;
  20. import net.minecraftforge.common.data.GlobalLootModifierProvider;
  21. import net.minecraftforge.common.loot.IGlobalLootModifier;
  22. import net.minecraftforge.common.loot.LootModifier;
  23. import net.minecraftforge.data.event.GatherDataEvent;
  24. import net.minecraftforge.eventbus.api.IEventBus;
  25. import net.minecraftforge.eventbus.api.SubscribeEvent;
  26. import net.minecraftforge.fml.common.Mod;
  27. import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
  28. import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
  29. import net.minecraftforge.event.server.ServerStartingEvent;
  30. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  31. import net.minecraftforge.registries.DeferredRegister;
  32. import net.minecraftforge.registries.ForgeRegistries;
  33. import org.jetbrains.annotations.NotNull;
  34. import org.slf4j.Logger;
  35.  
  36. import java.util.function.Supplier;
  37.  
  38. // The value here should match an entry in the META-INF/mods.toml file
  39. @Mod(CobblestoneDrops.MODID)
  40. public class CobblestoneDrops
  41. {
  42.     // Define mod id in a common place for everything to reference
  43.     public static final String MODID = "cobblestonedrops";
  44.     // Directly reference a slf4j logger
  45.     private static final Logger LOGGER = LogUtils.getLogger();
  46.     // Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace
  47.     public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, MODID);
  48.     // Create a Deferred Register to hold Items which will all be registered under the "examplemod" namespace
  49.     public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, MODID);
  50.  
  51.     public CobblestoneDrops()
  52.     {
  53.         LOGGER.info("[CD_DEBUG] Add listeners");
  54.         IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
  55.  
  56.         // Register the commonSetup method for modloading
  57.         modEventBus.addListener(this::commonSetup);
  58.  
  59.         // Register the Deferred Register to the mod event bus so blocks get registered
  60.         BLOCKS.register(modEventBus);
  61.         // Register the Deferred Register to the mod event bus so items get registered
  62.         ITEMS.register(modEventBus);
  63.  
  64.         // Register ourselves for server and other game events we are interested in
  65.         MinecraftForge.EVENT_BUS.register(this);
  66.     }
  67.  
  68.     private void commonSetup(final FMLCommonSetupEvent event)
  69.     {
  70.         // Some common setup code
  71.         LOGGER.info("[CD_DEBUG] HELLO FROM COMMON SETUP");
  72.         LOGGER.info("[CD_DEBUG] DIRT BLOCK >> {}", ForgeRegistries.BLOCKS.getKey(Blocks.DIRT));
  73.     }
  74.  
  75.     // You can use SubscribeEvent and let the Event Bus discover methods to call
  76.     @SubscribeEvent
  77.     public void onServerStarting(ServerStartingEvent event)
  78.     {
  79.         // Do something when the server starts
  80.         LOGGER.info("[CD_DEBUG] HELLO from server starting");
  81.     }
  82.  
  83.     // You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
  84.     @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
  85.     public static class ClientModEvents
  86.     {
  87.         @SubscribeEvent
  88.         public static void onClientSetup(FMLClientSetupEvent event)
  89.         {
  90.             // Some client setup code
  91.             LOGGER.info("[CD_DEBUG] HELLO FROM CLIENT SETUP");
  92.             LOGGER.info("[CD_DEBUG] MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName());
  93.         }
  94.     }
  95.  
  96.     @Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
  97.     public static class ServerModEvents
  98.     {
  99.         @SubscribeEvent
  100.         public static void runData(GatherDataEvent event)
  101.         {
  102.             LOGGER.info("[CD_DEBUG] Event happened");
  103.             event.getGenerator().addProvider(event.includeServer(), new DataProvider(event.getGenerator(), MODID));
  104.         }
  105.  
  106.         private static class DataProvider extends GlobalLootModifierProvider
  107.         {
  108.             public DataProvider(DataGenerator gen, String modid)
  109.             {
  110.                 super(gen, modid);
  111.             }
  112.  
  113.             @Override
  114.             protected void start()
  115.             {
  116.                 LOGGER.info("[CD_DEBUG] Add events");
  117.                 add("wheat_harvest", new WheatSeedsConverterModifier(
  118.                         new LootItemCondition[] {
  119.                                 MatchTool.toolMatches(ItemPredicate.Builder.item().of(Items.DIAMOND_PICKAXE)).build(),
  120.                                 LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.COBBLESTONE).build()
  121.                         },
  122.                         1, Items.COBBLESTONE, Items.IRON_BLOCK)
  123.                 );
  124.  
  125.             }
  126.  
  127.             private static class WheatSeedsConverterModifier extends LootModifier {
  128.                 public static final Supplier<Codec<WheatSeedsConverterModifier>> CODEC = Suppliers.memoize(() -> RecordCodecBuilder.create(inst -> codecStart(inst).and(
  129.                         inst.group(
  130.                                 Codec.INT.fieldOf("numSeeds").forGetter(m -> m.numSeedsToConvert),
  131.                                 ForgeRegistries.ITEMS.getCodec().fieldOf("seedItem").forGetter(m -> m.itemToCheck),
  132.                                 ForgeRegistries.ITEMS.getCodec().fieldOf("replacement").forGetter(m -> m.itemReward)
  133.                         )).apply(inst, WheatSeedsConverterModifier::new)
  134.                 ));
  135.  
  136.                 private final int numSeedsToConvert;
  137.                 private final Item itemToCheck;
  138.                 private final Item itemReward;
  139.                 public WheatSeedsConverterModifier(LootItemCondition[] conditionsIn, int numSeeds, Item itemCheck, Item reward) {
  140.                     super(conditionsIn);
  141.                     numSeedsToConvert = numSeeds;
  142.                     itemToCheck = itemCheck;
  143.                     itemReward = reward;
  144.                     LOGGER.info("[CD_DEBUG] Does it even happen?");
  145.                 }
  146.  
  147.                 @NotNull
  148.                 @Override
  149.                 public ObjectArrayList<ItemStack> doApply(ObjectArrayList<ItemStack> generatedLoot, LootContext context) {
  150.                     //
  151.                     // Additional conditions can be checked, though as much as possible should be parameterized via JSON data.
  152.                     // It is better to write a new ILootCondition implementation than to do things here.
  153.                     //
  154.                     LOGGER.info("[CD_DEBUG] Loot changed");
  155.                     generatedLoot.add(new ItemStack(Items.IRON_BLOCK, 1));
  156.                     return generatedLoot;
  157.                 }
  158.  
  159.                 @Override
  160.                 public Codec<? extends IGlobalLootModifier> codec() {
  161.                     return CODEC.get();
  162.                 }
  163.             }
  164.         }
  165.  
  166.     }
  167. }
  168.  
Add Comment
Please, Sign In to add comment