Advertisement
jayhillx

spawn_egg register

Aug 9th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. =======================================================================================================================================
  2. ModItem
  3.  
  4. public static final RegistryObject<Item> STRAWBERRY_COW_SPAWN_EGG = ITEMS.register("strawberry_cow_spawn_egg", () -> new ModSpawnEgg(ModEntity.STRAWBERRY_COW, 0xF5ABC5, 0xFFD7E5, new Item.Properties().group(MysticsBiomes.TAB)));
  5. =======================================================================================================================================
  6. ModSpawnEgg
  7.  
  8. public class ModSpawnEgg extends SpawnEggItem {
  9.  
  10. protected final static List<ModSpawnEgg> UNADDED_EGGS = new ArrayList<>();
  11. private final Lazy<? extends EntityType<?>> entityTypeSupplier;
  12.  
  13. public ModSpawnEgg(final RegistryObject<? extends EntityType<?>> entityTypeSupplier, int primaryColorIn, int secondaryColorIn, Properties builder) {
  14. super(null, primaryColorIn, secondaryColorIn, builder);
  15. this.entityTypeSupplier = Lazy.of(entityTypeSupplier::get);
  16. UNADDED_EGGS.add(this);
  17. }
  18.  
  19. public static void initSpawnEgg() {
  20. final Map<EntityType<?>, SpawnEggItem> EGGS = ObfuscationReflectionHelper.getPrivateValue(SpawnEggItem.class, null, "field_195987_b");
  21. DefaultDispenseItemBehavior dispenseBehavior = new DefaultDispenseItemBehavior() {
  22.  
  23. @Override
  24. protected ItemStack dispenseStack(IBlockSource source, ItemStack stack) {
  25. Direction direction = source.getBlockState().get(DispenserBlock.FACING);
  26. EntityType<?> type = ((SpawnEggItem) stack.getItem()).getType(stack.getTag());
  27. type.spawn(source.getWorld(), stack, null, source.getBlockPos(),
  28. SpawnReason.DISPENSER, direction != Direction.UP, false);
  29. stack.shrink(1);
  30. return stack;
  31. }
  32. };
  33.  
  34. for (final SpawnEggItem spawnEgg : UNADDED_EGGS) {
  35. EGGS.put(spawnEgg.getType(null), spawnEgg);
  36. DispenserBlock.registerDispenseBehavior(spawnEgg, dispenseBehavior);
  37. }
  38. UNADDED_EGGS.clear();
  39. }
  40.  
  41. @Override
  42. public EntityType<?> getType(CompoundNBT nbt) {
  43. return this.entityTypeSupplier.get();
  44. }
  45. }
  46. =======================================================================================================================================
  47. ClientEventBusSubscriber
  48.  
  49. @SubscribeEvent
  50. public static void onRegisterEntities(final RegistryEvent.Register<EntityType<?>> event) {
  51. ModSpawnEgg.initSpawnEgg();
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement