Jump to content

[Solved] Opening a door through code


tunasushi

Recommended Posts

I have an item that I want to open doors when used (iron ones too, obviously), but I can't figure out how to do it. I'm not sure how the metadata works with doors (orientation, open/close state, and whether the top or bottom half matters), but I did find the method onPoweredBlockChange in BlockDoor.java.

*Puts on Java newbie hat*

The method is public, but how do I call it? I have tried just onPoweredBlockChange(some args);

This tells me to change the method to static though, and so I tried:

new BlockDoor(71, Material.iron).onPoweredBlockChange(w, x, y, z, true);

The problem with this is that BlockDoor is protected. I am sure there is some super easy way to do this, but because I am self-taught and still very new to Java, could someone please enlighten me? Thanks

Link to comment
Share on other sites

I tried your suggestions and I realized that onBlockActivated won't change an iron door, even if the player is null (the method only uses the player for playing a sound effect). Does anyone know how to call onPoweredBlockChange? It isn't a method defined in Blocks.java so Block.blocksList[].onBlockActivated doesn't work (at least I think that's how it works, gave me an error when I tried it). Thanks for the help!

Link to comment
Share on other sites

That's why GotoLink said Block.blockList[id], not Block.blockList[]. Small but important difference. When you do Block.blockList[block.doorIron.blockID] you'll get the instance of the Iron Door block. Java thinks however that you're just dealing with a Block, so you'll need a typecast:

((BlockDoor)Block.blockList[block.doorIron.blockID]).onPoweredBlockChange(params)

Author of PneumaticCraft, MineChess, Minesweeper Mod and Sokoban Mod. Visit www.minemaarten.com to take a look at them.

Link to comment
Share on other sites

That's why GotoLink said Block.blockList[id], not Block.blockList[]. Small but important difference. When you do Block.blockList[block.doorIron.blockID] you'll get the instance of the Iron Door block. Java thinks however that you're just dealing with a Block, so you'll need a typecast:

((BlockDoor)Block.blockList[block.doorIron.blockID]).onPoweredBlockChange(params)

 

Thanks for the help, it's working now! I actually did give it the item id (though just as a number, not using .blockId. Sue me.), but I was missing the typecast. I actually have no idea what a typecast is (as I mentioned in the original post, I am soooo new to this), so could someone explain it to me? Thanks!

Link to comment
Share on other sites

Normally I would say, go look it up, it's a basic Java question. However, as this example is such a good example to explain typecasting I'll explain it to you right now:

 

When you refer to an element of Block.blockList[], you'll get a Block, as blockList[] is an array of Blocks. And when you want to execute methods that are defined in the Block class this is fine. For example, if you want to mimic the right click on a door, you can just use

Block.blockList[block.doorWood.blockID].onBlockActivated(world, x, y, z, player, side, f1, f2, f3);

That is because the onBlockActivated() IS defined in the Block class (it is overriden though, to give it its opening door properties).

When you wanted to use the onPoweredBlockChange() defined in the BlockDoor class, you'd tried this:

Block.blockList[block.doorIron.blockID].onPoweredBlockChange(params)

which gave you the following error:

The method onPoweredBlockChange() is undefined for the type Block

And this is true, there's no method in the Block class that is called onPoweredBlockChange(). And Java searches this method in the Block class, because blockList[] was like I said an array of Blocks. So Java thinks you're just dealing with a Block. We are sure that blockList[block.doorIron.blockID] is an instance of a BlockDoor. When you have this situation, you can use a typecast to tell Java that you're not just dealing with a Block.

BlockDoor door = (BlockDoor)Block.blockList[block.doorIron.blockID];

 

I recommend that you learn more Java. When you do, you know what tools you can use to solve a problem and you understand what's going on :).

Author of PneumaticCraft, MineChess, Minesweeper Mod and Sokoban Mod. Visit www.minemaarten.com to take a look at them.

Link to comment
Share on other sites

I actually did give it the item id (though just as a number, not using .blockId. Sue me.)

in this case no one cares and it shouldn't make a difference but in another example typing manually (also refered as "hardcoded") the id mean that everytime the iron door id changes you will have to manually change it.

 

in this case the iron door id will probably never change so it technicly makes no difference.

 

but hey if you don't want to follow convention and write crappy code, nobody is going to stop you...

how to debug 101:http://www.minecraftforge.net/wiki/Debug_101

-hydroflame, author of the forge revolution-

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.