Jump to content

[1.15.2] Throwable item not firing when used


NarwhelZhu

Recommended Posts

I'm trying to make a super basic throwable item that is essentially just a retextured snowball with virtually identical code just to understand the basics of thrown items, and almost everything works, except that the item won't appear when I right click it to throw it or be fired from a dispenser. The item and entity are registered, the rendering works just fine, I can summon the entity with /summon and it appears, and there are no errors in the console at any point. The problem seems to be that the onItemRightClick() method isn't even being called at all when I right click, as I put debug messages in the method that never get run. I'm guessing that there's just one little thing that I'm not adding that would solve the whole thing, but I have no idea what it might be. Most of the code for the item and entity is copied directly from the snowball class, so I'm not sure if it has anything to do with those classes. 

 

Edit: This problem has been solved. For any future readers who have the same problem, the code below is updated to be correct and can be used as a reference.

 

Here's the code for the mod.

 

Entity class:

public class RiceBulletEntity extends ProjectileItemEntity {
	
	public RiceBulletEntity(EntityType<? extends RiceBulletEntity> p_i50159_1_, World p_i50159_2_) {
		super(p_i50159_1_, p_i50159_2_);
	}
	
	public RiceBulletEntity(World worldIn, LivingEntity throwerIn) {
	    super(Registries.RICE_BULLET.get(), throwerIn, worldIn);
	}
	
	public RiceBulletEntity(World worldIn, double x, double y, double z) {
	      super(Registries.RICE_BULLET.get(), x, y, z, worldIn);
	}
	
	@Override
	protected Item getDefaultItem() {
		return Registries.RICE_BULLET_ITEM.get();
	}
	
	@Override
	public IPacket<?> createSpawnPacket() {
		return NetworkHooks.getEntitySpawningPacket(this);
	}

	@Override
	protected void onImpact(RayTraceResult result) {
		if (!this.world.isRemote) {
	         this.world.setEntityState(this, (byte)3);
	         this.remove();
	      }
	}
}

 

Item class:

public class RiceBulletItem extends Item {
	public RiceBulletItem(Item.Properties builder) {
	      super(builder);
	}

	@Override
	public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
		
		ItemStack itemstack = playerIn.getHeldItem(handIn);
		
	    if (!worldIn.isRemote) {
	        RiceBulletEntity riceBullet = new RiceBulletEntity(worldIn, playerIn);
	        riceBullet.setItem(itemstack);
	        riceBullet.shoot(playerIn, playerIn.rotationPitch, playerIn.rotationYaw, 0.0F, 1.5F, 1.0F);
	        worldIn.addEntity(riceBullet);
	    }

        playerIn.addStat(Stats.ITEM_USED.get(this));
        
        if (!playerIn.abilities.isCreativeMode) {
        	itemstack.shrink(1);
        }
        
        return ActionResult.resultSuccess(itemstack);
	}
}

 

Item and entity registries:

public class Registries {
	public static final DeferredRegister<Item> ITEMS = new DeferredRegister<>(ForgeRegistries.ITEMS, Minesokyo.MOD_ID);

	public static final DeferredRegister<EntityType<?>> ENTITIES = new DeferredRegister<>(ForgeRegistries.ENTITIES,
			Minesokyo.MOD_ID);

	
	public static void init() {
		ITEMS.register(FMLJavaModLoadingContext.get().getModEventBus());
        ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus());
    }
	
	
	public static final RegistryObject<Item> RICE_BULLET_ITEM = ITEMS.register("rice_bullet_item",
			() -> new RiceBulletItem(new Item.Properties().group(ItemGroup.COMBAT)));

	public static final RegistryObject<EntityType<RiceBulletEntity>> RICE_BULLET = ENTITIES.register("rice_bullet",
			() -> EntityType.Builder.<RiceBulletEntity>create(RiceBulletEntity::new, EntityClassification.MISC)
					.build("rice_bullet"));
}

 

Entity renderer:

public class RiceBulletRenderer extends SpriteRenderer<RiceBulletEntity> {

	public RiceBulletRenderer(EntityRendererManager renderManagerIn, ItemRenderer itemRendererIn) {
		super(renderManagerIn, itemRendererIn);
	}

	protected static final ResourceLocation TEXTURE = new ResourceLocation(Minesokyo.MOD_ID, "textures/items/rice_bullet.png");
	
	@Override
	public ResourceLocation getEntityTexture(RiceBulletEntity entity) {
		return TEXTURE;
	}



	public static class Factory implements IRenderFactory<RiceBulletEntity> {
		@Override
		public EntityRenderer<? super RiceBulletEntity> createRenderFor(EntityRendererManager manager) {
			return new RiceBulletRenderer(manager, Minecraft.getInstance().getItemRenderer());
		}	
	}
}

 

Renderer registries:

@Mod.EventBusSubscriber(modid = Minesokyo.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD, value = Dist.CLIENT)
public class ClientEventBusSubscriber {
	
	@SubscribeEvent
	public static void onClientSetup(FMLClientSetupEvent event) {
		RenderingRegistry.registerEntityRenderingHandler(Registries.RICE_BULLET.get(), new RiceBulletRenderer.Factory());
	}

}

 

Edited by NarwhelZhu
Link to comment
Share on other sites

57 minutes ago, ChampionAsh5357 said:

That would happen if you do not register the item using your extended item class.

Ah, thank you, I knew that it would be something really dumb like that. The entity does summon now, although it seems to be invisible. Is there some other obvious reason for that that I'm missing too?

Link to comment
Share on other sites

8 minutes ago, NarwhelZhu said:

The entity does summon now, although it seems to be invisible. Is there some other obvious reason for that that I'm missing too?

This is more or less a guess, but try checking the output of ProjectileItemEntity#getItem and where the entity is initially placed. It's either due to the fact that the position of the entity spawn is too low so it dies immediately or the item is not synchronized to the client for rendering.

Link to comment
Share on other sites

Alright, I figured it out. Once again, an idiot mistake. I forgot to put throwerIn in the second entity constructor's super call. It appears now and I believe it's working fine. I'll update my code blocks to be correct for any future coders who have the same problem. Thank you for your help, even by just pointing out my own idiocy.

 

One last problem though: it doesn't fire from a dispenser like a snowball does. It doesn't look like the snowball classes even have code for that though.

Edited by NarwhelZhu
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.

Announcements



×
×
  • Create New...

Important Information

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