Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.15.2] Throwable item not firing when used
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
NarwhelZhu

[1.15.2] Throwable item not firing when used

By NarwhelZhu, November 28, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

NarwhelZhu    0

NarwhelZhu

NarwhelZhu    0

  • Tree Puncher
  • NarwhelZhu
  • Members
  • 0
  • 12 posts
Posted November 28, 2020 (edited)

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 November 28, 2020 by NarwhelZhu
  • Quote

Share this post


Link to post
Share on other sites

ChampionAsh5357    160

ChampionAsh5357

ChampionAsh5357    160

  • World Shaper
  • ChampionAsh5357
  • Members
  • 160
  • 1019 posts
Posted November 28, 2020
1 hour ago, NarwhelZhu said:

public static final RegistryObject<Item> RICE_BULLET_ITEM = ITEMS.register("rice_bullet_item", () -> new Item(new Item.Properties().group(ItemGroup.COMBAT)));

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

  • Quote

Share this post


Link to post
Share on other sites

NarwhelZhu    0

NarwhelZhu

NarwhelZhu    0

  • Tree Puncher
  • NarwhelZhu
  • Members
  • 0
  • 12 posts
Posted November 28, 2020
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?

  • Quote

Share this post


Link to post
Share on other sites

ChampionAsh5357    160

ChampionAsh5357

ChampionAsh5357    160

  • World Shaper
  • ChampionAsh5357
  • Members
  • 160
  • 1019 posts
Posted November 28, 2020
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.

  • Quote

Share this post


Link to post
Share on other sites

NarwhelZhu    0

NarwhelZhu

NarwhelZhu    0

  • Tree Puncher
  • NarwhelZhu
  • Members
  • 0
  • 12 posts
Posted November 28, 2020 (edited)

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 November 28, 2020 by NarwhelZhu
  • Quote

Share this post


Link to post
Share on other sites

ChampionAsh5357    160

ChampionAsh5357

ChampionAsh5357    160

  • World Shaper
  • ChampionAsh5357
  • Members
  • 160
  • 1019 posts
Posted November 28, 2020
11 minutes ago, NarwhelZhu said:

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.

You need to register the dispenser behavior using DispenserBlock::registerDispenseBehavior.

  • Quote

Share this post


Link to post
Share on other sites

NarwhelZhu    0

NarwhelZhu

NarwhelZhu    0

  • Tree Puncher
  • NarwhelZhu
  • Members
  • 0
  • 12 posts
Posted November 28, 2020
31 minutes ago, ChampionAsh5357 said:

You need to register the dispenser behavior using DispenserBlock::registerDispenseBehavior.

It works now, thanks. I think that will be all for this particular thread. It renders and fires from the dispenser now. Thanks for the help.

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • diesieben07
      Minecraft forge not fully loading

      By diesieben07 · Posted 10 minutes ago

      1.8 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
    • BastouP
      [1.16.4] Get Overworld save directory

      By BastouP · Posted 10 minutes ago

      Yeah but I am creating a perpheral for computercraft that uses SQL. So i think using a sqlite file would be the easiest way. (You do not need to maintain a server.) However if there is another way I’d be glad to test it. Testing this this evening btw.
    • diesieben07
      [1.16.4] what difference between custom slots and EquipmentSlotType

      By diesieben07 · Posted 11 minutes ago

      All living entities already have an armor and held item inventory, you don't need to add it manually. Look at LivingEntity#getCapability, it will return you IItemHandler instances for those inventories.
    • diesieben07
      [1.15.2] Render as 2D icon in GUI, 3D model in hand

      By diesieben07 · Posted 13 minutes ago

      Good question. I don't know much about rendering, so nothing comes to mind immediately.
    • diesieben07
      Forge unable to find model

      By diesieben07 · Posted 15 minutes ago

      1.12 is no longer supported on this forum. Please update to a modern version of Minecraft to receive support.
  • Topics

    • Fars3O_
      1
      Minecraft forge not fully loading

      By Fars3O_
      Started 2 hours ago

    • BastouP
      3
      [1.16.4] Get Overworld save directory

      By BastouP
      Started 12 hours ago

    • Klarks
      1
      [1.16.4] what difference between custom slots and EquipmentSlotType

      By Klarks
      Started 2 hours ago

    • Woodside
      15
      [1.15.2] Render as 2D icon in GUI, 3D model in hand

      By Woodside
      Started Sunday at 08:26 PM

    • CatSack
      1
      Forge unable to find model

      By CatSack
      Started 10 hours ago

  • Who's Online (See full list)

    • XenoPyax
    • diesieben07
    • Choonster
    • Rhayzan
    • DevilishPoint
    • MrDuck20
    • Lucie
    • BastouP
    • Klarks
    • VoidTheFloof
    • Will11690
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • [1.15.2] Throwable item not firing when used
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community