Jump to content
  • Home
  • Files
  • Docs
  • Merch
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
  • Making a block disappear on right click with item error...
1.13 Update Notes for Mod Creators
Sign in to follow this  
Followers 0
StretchFre

Making a block disappear on right click with item error...

By StretchFre, August 2, 2015 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

StretchFre    0

StretchFre

StretchFre    0

  • Tree Puncher
  • StretchFre
  • Members
  • 0
  • 8 posts
Posted August 2, 2015

Hello. Nothing happens when i try to use this code i entered into my mod's block class file:

 

public boolean onBlockActivated(Block block, World world, EntityPlayer player, Item item, ItemStack itemstack, int x, int y, int z)

{

 

if(player.inventory.getCurrentItem().getItem() == Items.bone)

{

world.setBlock(x, y, z, Blocks.air);

System.out.println("rightclick worked");

return true;

}

return true;

 

}

 

nothing happens. What am i doing wrong? (also, my block file extends Block)

 

 

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45730 posts
Posted August 2, 2015

Well, what did you expect to happen? You just added a random method to your Block class, nobody will give a damn about it.

 

(Hint: you probably meant to override onBlockActivated, but you didn't. I highly suggest you do not override manually but use your IDE to do it).

  • Quote

Share this post


Link to post
Share on other sites

StretchFre    0

StretchFre

StretchFre    0

  • Tree Puncher
  • StretchFre
  • Members
  • 0
  • 8 posts
Posted August 2, 2015

Sorry... i am using eclipse, and i have no idea how to override automatically with it. How do i do this? I tried adding @Override annotation to it(above it) and it did not work, giving me a error.

I have no idea how to fix it. Please do not get angry, i am a beginner and have no idea how to

@Override, eventhough i know most of the basics.

EDIT: OKay.... wow. Yeah your right it was WAY too simple. Sorry to waste your time.

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45730 posts
Posted August 2, 2015

Sorry... i am using eclipse, and i have no idea how to override automatically with it. How do i do this? I tried adding @Override annotation to it(above it) and it did not work, giving me a error.

Exactly, it gave you an error because you are not overriding (that is the sole purpose of this annotation). If @Override gives you an error, the fix is almost never to remove the annotation.

 

To override automatically either do Source > Override / Implement Methods or just type the method name inside your class and press Ctrl-Space.

  • Quote

Share this post


Link to post
Share on other sites

StretchFre    0

StretchFre

StretchFre    0

  • Tree Puncher
  • StretchFre
  • Members
  • 0
  • 8 posts
Posted August 2, 2015

Okay. Now i got a different problem...i tried fixing it a bit, but it still crashes when i right click with nothing in my hand...

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int coords, float one, float two, float three)

{

   

Item item = player.getCurrentEquippedItem().getItem();

 

if(player.getHeldItem() != null)

{

if(item == MyMod.olKey)

{

world.setBlock(x, y, z, Blocks.air);

player.inventory.consumeInventoryItem(MyMod.olKey);

      System.out.println("Old Dungeon unlocked");

     

}

else

{

System.out.println("Are you trying to unlock it with THAT?");

}

 

}

else

{

System.out.println("Stpo trying to pry it open");

}

return super.onBlockActivated(world, x, y, z, player, coords, one, two, three);

 

}

  • Quote

Share this post


Link to post
Share on other sites

Choonster    1624

Choonster

Choonster    1624

  • Reality Controller
  • Choonster
  • Forge Modder
  • 1624
  • 5050 posts
Posted August 2, 2015

You're calling

ItemStack#getItem

before checking if the

ItemStack

(returned from

player.getCurrentEquippedItem()

) is

null

. When it's

null

, you'll get a

NullPointerException

because you tried to call a method of a

null

value.

 

getHeldItem

and

getCurrentEquippedItem

do the exact same thing, you should really only use one.

 

Don't use

System.out.println

for output that the player is supposed to see in-game. Use

EntityPlayer#addChatComponentMessage

to send a chat message to a player (make sure you only do it on the client or server, not both). For general logging output, use

FMLLog

or a wrapper around it.

  • Quote

Share this post


Link to post
Share on other sites

StretchFre    0

StretchFre

StretchFre    0

  • Tree Puncher
  • StretchFre
  • Members
  • 0
  • 8 posts
Posted August 2, 2015

You're calling

ItemStack#getItem

before checking if the

ItemStack

(returned from

player.getCurrentEquippedItem()

) is

null

. When it's

null

, you'll get a

NullPointerException

because you tried to call a method of a

null

value.

 

getHeldItem

and

getCurrentEquippedItem

do the exact same thing, you should really only use one.

 

Don't use

System.out.println

for output that the player is supposed to see in-game. Use

EntityPlayer#addChatComponentMessage

to send a chat message to a player (make sure you only do it on the client or server, not both). For general logging output, use

FMLLog

or a wrapper around it.

Okay, thanks for attempting to help. What i do not understand is i did exactly what you said to do(without the chat thing) but it crashes still. here is my code:

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int coords, float one, float two, float three)

{

   

 

 

if(player.getHeldItem().getItem() != null)

{

if(player.getHeldItem().getItem() == MyMod.olKey)

{

world.setBlock(x, y, z, Blocks.air);

player.inventory.consumeInventoryItem(MyMod.olKey);

     

}

else

{

System.out.println("Are you trying to unlock it with THAT?");

}

 

}

else

{

System.out.println("Stpo trying to pry it open");

}

return super.onBlockActivated(world, x, y, z, player, coords, one, two, three);

 

}

  • Quote

Share this post


Link to post
Share on other sites

diesieben07    6692

diesieben07

diesieben07    6692

  • Reality Controller
  • diesieben07
  • Forum Team
  • 6692
  • 45730 posts
Posted August 2, 2015

This will crash if the player has an empty hand (because getHeldItem() is null):

 

player.getHeldItem().getItem()

  • Quote

Share this post


Link to post
Share on other sites

EverythingGames    19

EverythingGames

EverythingGames    19

  • Diamond Finder
  • EverythingGames
  • Forge Modder
  • 19
  • 388 posts
Posted August 2, 2015

You null check the itemstack not the item.

 

  • Quote

Share this post


Link to post
Share on other sites

StretchFre    0

StretchFre

StretchFre    0

  • Tree Puncher
  • StretchFre
  • Members
  • 0
  • 8 posts
Posted August 3, 2015

Thanks... But how do i make it so getHeldItem() is not null?

  • Quote

Share this post


Link to post
Share on other sites

Cerandior    17

Cerandior

Cerandior    17

  • Diamond Finder
  • Cerandior
  • Members
  • 17
  • 299 posts
Posted August 3, 2015

 

@Override

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {

 

if(player.getHeldItem().getItem() != null && player.getHeldItem().getItem() == Items.bone){

world.setBlockToAir(x, y, z);

}

 

return super.onBlockActivated(world, x, y, z, player, p_149727_6_, p_149727_7_, p_149727_8_, p_149727_9_);

}

 

 

First you check if the item is null and if it's not then you check if that item is your item. Then you execute your code. Your code is similiar to that right? The problem is that you are putting a condition in the if statement. The above code will work but if a player tries to right click that block with nothing in it's hand it will crash!

  • Quote

Share this post


Link to post
Share on other sites

coolAlias    745

coolAlias

coolAlias    745

  • Reality Controller
  • coolAlias
  • Members
  • 745
  • 2805 posts
Posted August 3, 2015

Thanks... But how do i make it so getHeldItem() is not null?

You DON'T. You can't help it if a player clicks with nothing in their hand, but you CAN check for it. Null-checking is a very basic concept which you seem to sort of understand, but you are checking the wrong object for null:

object.method() <-- this will crash if the object is null

player.getHeldItem() <-- returns an ItemStack, or possibly null if not holding anything; would crash if player was null (null.getHeldItem() fails)

player.getHeldItem().getItem() <-- returns the Item contained in the ItemStack, thus it crashes if getHeldItem() returns null instead of an actual ItemStack (player.null.getItem() ... yeah, nope)

player.getHeldItem().getItem().method() <-- would crash if Item returned from getItem() was null, but that should never be the case

If at any time an object (such as player, or the result of a method such as getHeldItem()) is possible to be null, you need to null-check or you WILL crash when that object is null and you try to access a class field or method using it.

  • 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 0
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • DragonITA
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA · Posted 30 minutes ago

      But i cant modify it.
    • Draco18s
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Draco18s · Posted 38 minutes ago

      There's only two options: FORGE and MOD. You're using the wrong one.
    • DaemonUmbra
      pointing to MCP folder instead of MDK ???

      By DaemonUmbra · Posted 53 minutes ago

      This entire issue is apparently surrounding your workspace apparently utilizing some mcp folder which shouldn't exist. Can you show me where your IDE mentions this mcp folder?
    • DaemonUmbra
      Error with running modpack on twitch

      By DaemonUmbra · Posted 54 minutes ago

      Please restart your launcher, reproduce this error, close your launcher, and share launcher_log.txt via one of the paste sites in my signature
    • DaemonUmbra
      Minecraft Modded Server jar doesn't like having more Memory...

      By DaemonUmbra · Posted 56 minutes ago

      I'm not 100% of what you're saying because you don't appear to be using English as I understand it. It sounds like you're using Hamachi and trying to set your server to use a specific port that Hamachi opens?
  • Topics

    • DragonITA
      41
      [1.14.4] How to get Minecraft Horse model/texture to make a custom unicorn?

      By DragonITA
      Started Monday at 05:06 AM

    • Oliviafrostpaw
      11
      [1.14.4] Injecting into Existing Loot Tables, Blocks

      By Oliviafrostpaw
      Started December 8

    • JMAS
      10
      pointing to MCP folder instead of MDK ???

      By JMAS
      Started Thursday at 04:21 PM

    • bluey418
      1
      Error with running modpack on twitch

      By bluey418
      Started 1 hour ago

    • Misterboy64
      1
      Minecraft Modded Server jar doesn't like having more Memory...

      By Misterboy64
      Started 3 hours ago

  • Who's Online (See full list)

    • PrinceRaiden
    • mr_ten_fan
    • plugsmustard
    • Oliviafrostpaw
    • Electrodynamite12
    • Crack3dC0d3
    • J0WAY
    • Choco
    • Pyre540
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Making a block disappear on right click with item error...
  • Theme
  • Contact Us
  • Discord

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