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    161

ChampionAsh5357

ChampionAsh5357    161

  • World Shaper
  • ChampionAsh5357
  • Members
  • 161
  • 1021 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    161

ChampionAsh5357

ChampionAsh5357    161

  • World Shaper
  • ChampionAsh5357
  • Members
  • 161
  • 1021 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    161

ChampionAsh5357

ChampionAsh5357    161

  • World Shaper
  • ChampionAsh5357
  • Members
  • 161
  • 1021 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

    • Jack Richard
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By Jack Richard · Posted 1 hour ago

      Thanks so much for your help! I removed the log4j dependency, I honesty don't remember when I put that there. Once I fixed the jar.finalizedBy line, everything worked! (One small issue where I was relocating all of "com.google" when I just should've relocated "com.google.common," too)   Thanks again!
    • Teedledee
      1.16.4 Failed to synchronize registry data with server LAN

      By Teedledee · Posted 1 hour ago

      Fixed the issue, one config didnt match, downloaded randompatches to solve badly compressed packet afterwards.
    • DaemonUmbra
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By DaemonUmbra · Posted 1 hour ago

      First of all, why are you shadowing log4j? Minecraft already provides log4j2. Second if build is running shadowjar instead of jar then you might need to tweak the line where it says jar.finalizedBy('reobfJar') to finalize shadowJar instead
    • DaemonUmbra
      "The game crashed whilst rendering overlay"

      By DaemonUmbra · Posted 1 hour ago

      What do you think you should do about this?
    • DaemonUmbra
      IntelliJ doesn't include resources folder while debugging

      By DaemonUmbra · Posted 1 hour ago

      What version of Minecraft are you using?
  • Topics

    • Jack Richard
      2
      [1.15.2] Minecraft Registry Problem When Loading from Jar

      By Jack Richard
      Started 2 hours ago

    • Teedledee
      2
      1.16.4 Failed to synchronize registry data with server LAN

      By Teedledee
      Started January 18

    • GuyWithTVHead
      1
      "The game crashed whilst rendering overlay"

      By GuyWithTVHead
      Started 1 hour ago

    • GermanBucket
      2
      IntelliJ doesn't include resources folder while debugging

      By GermanBucket
      Started 3 hours ago

    • Arthurmeade12
      8
      Error Conecting to My Minecraft Server

      By Arthurmeade12
      Started Saturday at 11:05 PM

  • Who's Online (See full list)

    • forgotendeath09
    • Inforno
    • brunoxd2
    • ChampionAsh5357
    • pupymk1
  • 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