Jump to content

[SOLVED]Help with onItemRightClick method


DrSteam1111

Recommended Posts

Hello i am getting an error when right clicking in game

 

[16:58:12] [server thread/FATAL]: Error executing task

java.util.concurrent.ExecutionException: java.lang.NullPointerException

at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[?:1.8.0_112]

at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[?:1.8.0_112]

at net.minecraft.util.Util.runTask(Util.java:26) [util.class:?]

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:742) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:687) [MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) [integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:536) [MinecraftServer.class:?]

at java.lang.Thread.run(Thread.java:745) [?:1.8.0_112]

Caused by: java.lang.NullPointerException

at com.drsteam1111.drbit.item.ItemNecklessOfDeath.onItemRightClick(ItemNecklessOfDeath.java:35) ~[itemNecklessOfDeath.class:?]

at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:176) ~[itemStack.class:?]

at net.minecraft.server.management.PlayerInteractionManager.processRightClick(PlayerInteractionManager.java:391) ~[PlayerInteractionManager.class:?]

at net.minecraft.network.NetHandlerPlayServer.processPlayerBlockPlacement(NetHandlerPlayServer.java:740) ~[NetHandlerPlayServer.class:?]

at net.minecraft.network.play.client.CPacketPlayerTryUseItem.processPacket(CPacketPlayerTryUseItem.java:43) ~[CPacketPlayerTryUseItem.class:?]

at net.minecraft.network.play.client.CPacketPlayerTryUseItem.processPacket(CPacketPlayerTryUseItem.java:9) ~[CPacketPlayerTryUseItem.class:?]

at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:15) ~[PacketThreadUtil$1.class:?]

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[?:1.8.0_112]

at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[?:1.8.0_112]

at net.minecraft.util.Util.runTask(Util.java:25) ~[util.class:?]

... 5 more

 

Heres my Code:

 

    @Override

    public ActionResult<ItemStack> onItemRightClick(ItemStack itemStack, World world, EntityPlayer player, EnumHand hand) {

        if(!world.isRemote) {

            if(player.getHeldItemOffhand().getItem() == itemStack.getItem() && player.getHeldItemMainhand().getItem() == itemStack.getItem()) {

                } else if (player.getHeldItemOffhand().getItem() == itemStack.getItem() || player.getHeldItemMainhand().getItem() == itemStack.getItem()){

                    player.addPotionEffect(new PotionEffect(Potion.getPotionById(6), 1, 2, false, false));

                    player.addChatComponentMessage(new TextComponentString("You Drain Strength From The Dead"));

                    player.setHealth(player.getHealth() - 4f);

                    return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack);

            }

        }

        return super.onItemRightClick(itemStack, world, player, hand);

    }

 

Does any one know what the problem is?

Link to comment
Share on other sites

What if one of the hands contains nothing? Hint: it returns

null

.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

What if one of the hands contains nothing? Hint: it returns

null

.

 

changed the code to

 

            if(player.getHeldItemOffhand().getItem() == ModItems.neckless_of_death && player.getHeldItemMainhand().getItem() == null || player.getHeldItemOffhand().getItem() == null && player.getHeldItemMainhand().getItem() == ModItems.neckless_of_death) {

                player.addPotionEffect(new PotionEffect(Potion.getPotionById(6), 1, 2, false, false));

                player.addChatComponentMessage(new TextComponentString("You Drain Strength From The Dead"));

                player.setHealth(player.getHealth() - 4f);

                return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack);

            }

 

Link to comment
Share on other sites

No.

getHeldItem*hand()

returns

null

, not

getItem()

. Check if

getHeldItem*hand()

is not equal to

null

, then try to get the

Item

from it.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

No.

getHeldItem*hand()

returns

null

, not

getItem()

. Check if

getHeldItem*hand()

is not equal to

null

, then try to get the

Item

from it.

 

got it working with

 

            if(player.getHeldItemMainhand() != null && player.getHeldItemMainhand().getItem() == itemStack.getItem() && player.getHeldItemOffhand() == null) {

                player.addPotionEffect(new PotionEffect(Potion.getPotionById(5), 1200, 2, false, false));

                player.addChatComponentMessage(new TextComponentString("You Drain Strength From The Dead"));

                player.setHealth(player.getHealth() - 4f);

                return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack);

            } else if (player.getHeldItemOffhand() != null && player.getHeldItemOffhand().getItem() == itemStack.getItem() &&    player.getHeldItemMainhand() == null) {

                player.addPotionEffect(new PotionEffect(Potion.getPotionById(5), 1200, 2, false, false));

                player.addChatComponentMessage(new TextComponentString("You Drain Strength From The Dead"));

                player.setHealth(player.getHealth() - 4f);

                return ActionResult.newResult(EnumActionResult.SUCCESS, itemStack);

 

Thank You!

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.