Jump to content

How to Register Entities using the Deferred Register?


kiou.23

Recommended Posts

I have an Item (DynamiteStickItem) that when used spawns an entity (DynamiteEntity) that extends ProjectileTileEntity.

if (!worldIn.isRemote) {
  DynamiteEntity dynamiteEntity = new DynamiteEntity(worldIn, player);
  dynamiteEntity.setItem(currentItemStack);
  dynamiteEntity.func_234612_a_(player, player.rotationPitch, player.rotationYaw, 0.0f, 1.5f, 1.0f);
  worldIn.addEntity(dynamiteEntity);
}

 

However, the Java compiler seems to have a problem with the constructors. At first, in the DynamiteEntity, I had the following constructor:

public DynamiteEntity(World world, PlayerEntity player) {
	super(EntityTypes.SNOWBALL, player, world);
}

 

Then when I used the Item, the summoned entity had the snowball texture. I figured I had to register my own EntityType, and I'm trying to do so with the Deferred Registries.

public static final RegistryObject<EntityType<DynamiteEntity>> DYNAMITE_ENTITY = RegistryHandler.ENTITIES.register("dynamite_entity", () ->
	EntityType.Builder.create(DynamiteEntity::new, EntityClassification.MISC)
		.size(0.25f, 0.25f)
		.build("dynamite_entity")
);

 

(I don't know if I'm missing something when passing the Supplier using EntityType.Builder.create method, I haven't found much resources about registering entities)

However, The EntityType.Builder.create complained about the constructor, so I looked at the code for the Snowball constructors and added the following to the DynamiteEntity class:

public DynamiteEntity(EntityType<? extends DynamiteEntity> entity, World world) { 
	super(entity, world);
}

 

Then the EntityType.Builder.create is not being able to resolve the constructor for 'DynamiteEntity'. I Tried removing the initial constructor (and updating the code in DynamiteStickItem to use only the construcor that uses EntityType<? extends DynamiteEntity>), the EntityType.Builder.create then worked, but the Item wouldn't spawn the Entity, and It wasn't textures when I tried spawning it using commands...

What am I doing wrong?

 

Additionnaly, is there a Data Generator to generate Entity models?

Link to comment
Share on other sites

Howdy

 

>Additionnaly, is there a Data Generator to generate Entity models?

Try BlockBench, it is quite awesome 

https://blockbench.net/

 

I don't know about DeferredRegister for Entities; but this method should work for what you're trying to do:

  // register our entity types
  @SubscribeEvent
  public static void onEntityTypeRegistration(RegistryEvent.Register<EntityType<?>> entityTypeRegisterEvent) {
    emojiEntityType = EntityType.Builder.<EmojiEntity>create(EmojiEntity::new, EntityClassification.MISC)
            .size(0.25F, 0.25F)
            .build("minecraftbyexample:mbe81a_emoji_type_registry_name");
    emojiEntityType.setRegistryName("minecraftbyexample:mbe81a_emoji_type_registry_name");
    entityTypeRegisterEvent.getRegistry().register(emojiEntityType);
 }
      
/**
 * Created by TGG on 24/06/2020.
 *
 * Heavily based on the vanilla SnowballEntity
 */
public class EmojiEntity extends ProjectileItemEntity {
  public EmojiEntity(EntityType<? extends EmojiEntity> entityType, World world) {
    super(entityType, world);
  }

  public EmojiEntity(World world, LivingEntity livingEntity) {
    super(StartupCommon.emojiEntityType, livingEntity, world);
  }

  public EmojiEntity(World world, double x, double y, double z) {
    super(StartupCommon.emojiEntityType, x, y, z, world);
  }

  // If you forget to override this method, the default vanilla method will be called.
  // This sends a vanilla spawn packet, which is then silently discarded when it reaches the client.
  //  Your entity will be present on the server and can cause effects, but the client will not have a copy of the entity
  //    and hence it will not render.
  @Nonnull
  @Override
  public IPacket<?> createSpawnPacket() {
    return NetworkHooks.getEntitySpawningPacket(this);
  }

  // ProjectileItemEntity::setItem uses this to save storage space.  It only stores the itemStack if the itemStack is not
  //   the default item.
  @Override
  protected Item getDefaultItem() {
    return StartupCommon.emojiItemHappy;
  }

  // We hit something (entity or block).
  @Override
  protected void onImpact(RayTraceResult rayTraceResult) {
    // if we hit an entity, apply an effect to it depending on the emoji mood
    if (rayTraceResult.getType() == RayTraceResult.Type.ENTITY) {
      EntityRayTraceResult entityRayTraceResult = (EntityRayTraceResult)rayTraceResult;
      Entity entity = entityRayTraceResult.getEntity();
      if (entity instanceof LivingEntity) {
        LivingEntity livingEntity = (LivingEntity)entity;
        Optional<EmojiItem.EmojiMood> mood = getMoodFromMyItem();
        if (mood.isPresent()) {
          EffectInstance effect = (mood.get() == EmojiItem.EmojiMood.HAPPY) ?
                  new EffectInstance(Effects.REGENERATION, 100, 1) :
                  new EffectInstance(Effects.POISON, 10, 0);
          livingEntity.addPotionEffect(effect);
        }
      }
    }

    if (!this.world.isRemote) {
      this.world.setEntityState(this, VANILLA_IMPACT_STATUS_ID);  // calls handleStatusUpdate which tells the client to render particles
      this.remove();
    }
  }

  // not needed here, but can be useful as a breakpoint location to check whether the entity was spawned properly, and to debug the behaviour / lifetime
  @Override
  public void tick() {
    super.tick();
  }

  private static final byte VANILLA_IMPACT_STATUS_ID = 3;

  /*   see https://wiki.vg/Entity_statuses
       make a cloud of particles at the impact point
   */
  @Override
  public void handleStatusUpdate(byte statusID) {
    if (statusID == VANILLA_IMPACT_STATUS_ID) {
      IParticleData particleData = this.makeParticle();

      for(int i = 0; i < 8; ++i) {
        this.world.addParticle(particleData, this.getPosX(), this.getPosY(), this.getPosZ(), 0.0D, 0.0D, 0.0D);
      }
    }
  }

  private IParticleData makeParticle() {
    Optional<EmojiItem.EmojiMood> mood = getMoodFromMyItem();
    if (!mood.isPresent()) return ParticleTypes.SNEEZE;
    return (mood.get() == EmojiItem.EmojiMood.HAPPY) ? ParticleTypes.HEART : ParticleTypes.ANGRY_VILLAGER;
  }

  /** Look at the ItemStack stored by this entity and determine its mood
   * @return  The mood, or empty if no mood defined (for some unknown reason...)
   */
  private Optional<EmojiItem.EmojiMood> getMoodFromMyItem() {
    ItemStack itemStackForThisEntity = this.func_213882_k();  // returns air if the entity is storing the default item (HAPPY in this case)
    Item item = itemStackForThisEntity.isEmpty() ? getDefaultItem() : itemStackForThisEntity.getItem();
    if (item instanceof EmojiItem) {
      EmojiItem.EmojiMood mood = ((EmojiItem) item).getEmojiMood();
      return Optional.of(mood);
    }
    return Optional.empty();
  }
}      
      

 

-TGG

Link to comment
Share on other sites

32 minutes ago, TheGreyGhost said:

>Additionnaly, is there a Data Generator to generate Entity models?

Try BlockBench, it is quite awesome 

https://blockbench.net/

 

This is going to be REALy useful, thanks!!

 

But then... I know there are Deferred Registries for Entities, I'm just not getting EntityType.Builder.create to work I think... Still thanks tho

I'm going to stick sctrictly to deferred registries, they seem far easier to write

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.