Jump to content

[1.17.1] Custom Fishing Rod doesn't cast


Marcelstrike

Recommended Posts

So I'm trying to make a custom fishing rod by extending FishingRodItem. Everything seems alright in the code and the texture, but whenever I right click with it in hand I see the fishing hook entity shoot out, but it disappears instantly. I'm thinking that it's only alive for about 1 tick until it leaves, the rod never changes to the casted texture either. I tried making to make my own custom FishingHook entity instead of the default one to shoot out, but a lot of the fields are private and the custom fishing hook class I made didn't seem to change anything (Was having the same problem) so I'm not sure what could be the issue. Just need a push in the right direction.

Here's the code for my fishing rod, thank you in advance!

 

public class ItemIronRod extends FishingRodItem {


    public ItemIronRod(Properties props) {
        super(props);
    }



    private static final String NAME = "ironrod";

    public static final Item SELF = new ItemIronRod(ModItemList.defaultItemProperties()).setRegistryName(NAME);

    @Override
    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
        ItemStack currentstack = player.getItemInHand(hand);
        int lurespeed;
        //if player is fishing
        if (player.fishing != null) {
            if (!level.isClientSide) {
                lurespeed = player.fishing.retrieve(currentstack);
                currentstack.hurtAndBreak(lurespeed, player, (p) -> {
                    p.broadcastBreakEvent(hand);
                });
            }

            level.playSound((Player)null, player.getX(), player.getY(), player.getZ(), SoundEvents.FISHING_BOBBER_RETRIEVE, SoundSource.NEUTRAL, 1.0F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
            level.gameEvent(player, GameEvent.FISHING_ROD_REEL_IN, player);
        }
        else //if player is not fishing
        {

             if (!level.isClientSide)
             {

                 lurespeed = EnchantmentHelper.getFishingSpeedBonus(currentstack);
                 int luck = EnchantmentHelper.getFishingLuckBonus(currentstack);
                 level.addFreshEntity(new FishingHook(player, level, luck, lurespeed));
                 //level.playSound((Player)null, player.getX(), player.getY(), player.getZ(), SoundEvents.ENDERMAN_TELEPORT, SoundSource.NEUTRAL, 0.5F, 0.4F / (level.getRandom().nextFloat() * 0.4F + 0.8F));
             }

            player.awardStat(Stats.ITEM_USED.get(this));
            level.gameEvent(player, GameEvent.FISHING_ROD_CAST, player);
        }

        return InteractionResultHolder.sidedSuccess(currentstack, level.isClientSide());
    }


}

 

Link to comment
Share on other sites

25 minutes ago, diesieben07 said:

Do not create registry entries in static initializers.

Note taken! Is there any specific reason for this or is it just for good practice?

 

Just tried using the debugger for a bit specifically on the tick function and I found this

   private boolean shouldStopFishing(Player p_37137_) {
      ItemStack itemstack = p_37137_.getMainHandItem();
      ItemStack itemstack1 = p_37137_.getOffhandItem();
      boolean flag = itemstack.is(Items.FISHING_ROD);
      boolean flag1 = itemstack1.is(Items.FISHING_ROD);
      if (!p_37137_.isRemoved() && p_37137_.isAlive() && (flag || flag1) && !(this.distanceToSqr(p_37137_) > 1024.0D)) {
         return false;
      } else {
         this.discard();
         return true;
      }
   }

So I made a CustomFishingHook, added this function and changed the item to my iron rod with the fixed reference but it still didn't work. Same behavior. Do I have to register my new CustomFishingHook entity or is it probably something else as well that I'd have to override or remake inside of FishingHook?

Link to comment
Share on other sites

7 minutes ago, diesieben07 said:

It can cause the item to be crated at the wrong time, which can make it so the registry name is wrong.

I don't know :D

Ok, so If I override the tick function in my CustomFishingHook, it's using a ton of private fields that are in FishingHook. Is there any way to make them protected or make them usable in the child class, or am I going to have to make new versions or literally every field and function? is there any way to change the FishingHook class directly? Just want to make sure that I'm not missing anything obvious

Link to comment
Share on other sites

On 8/21/2021 at 1:40 PM, diesieben07 said:

You have to make the function protected using an access transformer, otherwise you obviously cannot override it.

Had no idea about transformers, they're just what I needed. One more question, so I managed to register my hook entity, and the renderer for it but right now whenever I cast my custom rod, the bobber is invisible, although it functions perfectly. I'm getting the following error

[Render thread/FATAL]: Error executing task on Client
java.lang.IndexOutOfBoundsException: readerIndex(55) + length(8) exceeds writerIndex(55): UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 55, widx: 55, cap: 256)
	at io.netty.buffer.AbstractByteBuf.checkReadableBytes0(AbstractByteBuf.java:1405) ~[netty-all-4.1.25.Final.jar%2334!:4.1.25.Final]
	at io.netty.buffer.AbstractByteBuf.readLong(AbstractByteBuf.java:812) ~[netty-all-4.1.25.Final.jar%2334!:4.1.25.Final]
	at net.minecraft.network.FriendlyByteBuf.readLong(FriendlyByteBuf.java:965) ~[forge-1.17.1-37.0.34_mapped_official_1.17.1-recomp.jar%2374!:?]
	at net.minecraft.network.FriendlyByteBuf.readUUID(FriendlyByteBuf.java:377) ~[forge-1.17.1-37.0.34_mapped_official_1.17.1-recomp.jar%2374!:?]
	at com.marcel.piscis.common.entity.CustomFishingHook.<init>(CustomFishingHook.java:30) ~[%2378!:?]

Because of how one of my fishing hook constructors are

    public CustomFishingHook(FMLPlayMessages.SpawnEntity sp, Level world) {
        super(world.getPlayerByUUID(sp.getAdditionalData().readUUID()), world, 0, 0);
    }

I've tried to see if there's any other way I can get player but it would give me null. My whole custom fishing hook entity currently looks like this https://hastebin.com/yebagaxejo.java Thanks for the help so far!

Link to comment
Share on other sites

20 hours ago, diesieben07 said:
  • Do not send the UUID, send the plain ID instead, it exists specifically for networking purposes.
  • To send additional data you have to implement IEntityAdditionalSpawnData on your entity. Right now you are just trying to read something from the additional data which you never write.

Thank you, solved!

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.