Coding question about Pistons

Discussion in 'Plugin Development' started by Specops343, Jun 30, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Specops343

    Hey I have been looking through the apidocs but haven't managed to find anything regarding this. So here is my question: Is there a way to get what material the piston is pushing?
     
  2. Offline

    Haias

    I'm not exactly sure how you can find the direction the piston is facing, but if you can manage that, you can get the part by doing this:
    block.getFace(BlockFace.EAST).getType()
     
  3. Offline

    Specops343

    Alright, ill give that a go.
     
  4. Offline

    feildmaster

    The code I use to get what face it's looking is the following:
    Code:
    int face = (int)(block.getData()^0x7);
            head = block.getFace((face==1?BlockFace.UP:(face==2?BlockFace.EAST:(face==3?BlockFace.WEST:(face==4?BlockFace.NORTH:BlockFace.SOUTH)))));
    BUT that will get you the piston head, you want to go another step with it. So you'd add
    Code:
    block = head.getFace((face==1?BlockFace.UP:(face==2?BlockFace.EAST:(face==3?BlockFace.WEST:(face==4?BlockFace.NORTH:BlockFace.SOUTH)))));
     
  5. Offline

    feildmaster

    Guga, block placement is bit 7, and you first have to get the BODY before you can get the HEAD, before you can get the BLOCK... (my answer above is the way to detect it)
     
  6. Offline

    Connor Mahaffey

    @Guga @feildmaster

    Hi, I've worked with .getData() before, but always as a decimal and never with bits. I want to know if a piston is pushed out or not, and getting direction wouldn't hurt. Do either of you know how to get the bit values to see if a piston is pushed out?
     
  7. Offline

    feildmaster

    @Connor Mahaffey It's not really "is pushed out" but it's "is powered," as manually turning the power on produces this, but use:
    Code:
    boolean turnedOn = (block.getData()&0x8)==8;
     
  8. Offline

    Connor Mahaffey

    Sorry "is pushed out" is probably the wrong way of saying it. But thank you so much. I really need to read up more on bits though, because I'm still not understanding the code.

    My program was going to allow for one piston to be powered, which would power the rest in a given set, but I see from your link above that that is glitched in this bukkit build, and that you seem to be working on a very similar program (correct me if I'm wrong).

    If you are doing something similar let me know, and I'll abandon my project now, seeing as you clearly know more about bits, etc. than I do.
     
  9. Offline

    feildmaster

  10. Its nothing hard, you can work with bits in really simple way.
    And honestly i dont really get why people trying to advice you something like
    boolean turnedOn = (block.getData()&0x8)==8; if its kinda easy to see, you dont know much about bits

    0x4 0x8 0x3 etc represent the decimal value in bits, so 0x8 = 8 , but for compiler its binary value of 8, which is 1000

    You can simply do this
    int data = block.getData();
    if (data>=8)
    {
    // eventually do something, if piston is pushed
    data -= 8; // if pistons data is equal or bigger than 8, then its pushed, so you just get rid of this bit so you've got only direction values
    }
    // now you just get the direction with few IFs or with some loop or something. Ill do example of IFs, since its easier to understand

    if (data == 5)
    {
    //blocks direction is south
    }
    else if (data == 4)
    {
    // North
    }
    // etc
    As you can see you dont have to understand the bits at all and bits are actually nothing complicated and binary numeral system is really simple.
     
  11. Offline

    Connor Mahaffey

    I see. Well it looks like what I was talking about could be done in Quantum Connectors quite easily, by chaining receivers to a lever (assuming the bug gets fixed and you complete it). So I think I'll drop my project.

    Thanks for your help, and good luck.

    Oh wow thanks for the explanation! I'll definitely bookmark this thread for future use. As you can see above I'm abandoning my project because something similar is being made (or exists) already. But thank you all the same, I might need it in the future, since most Minecraft items have some kind of data attached to them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  12. Offline

    Specops343

    Wow, am I confused. Xd
     
  13. Offline

    feildmaster

    Which part do you not understand?
     
  14. Offline

    Specops343

    Nvm, i think i got it. I'm going to start coding on my pistons plugin today. ;)
     
  15. Offline

    feildmaster

    awesome. I wish you luck.
     
  16. Offline

    Specops343

    @feildmaster Does this help me get the direction its facing, or what block it is pushing? Im trying to make a plugin that checks which block is being pushed.
     
  17. Offline

    feildmaster

    @Specops343 It gets the block it's pushing... Though it actually might not work 100% bug free. I believe this code assumes it's already "pushing" a block.

    Also, I hate conversing over forums, especially about coding. If you'd like I can converse with you on any messenger you have.
     
  18. Offline

    Specops343

    @feildmaster I'll send you a pm with my skype name, but i also have xfire if that is easier.
     
  19. Offline

    Altobot

    Hello there
    Well, I am coding at another Plugin that also does something with Pistons. I have to copy them. I also used the GetState() and just applied it to the new block, but the Piston sometimes faces in the wrong direction (not always!). Is there a way to get the Piston Base to see if it is copied in the wrong direction? How to even check if a block is a Piston base? block.getType() == Material.PISTON_BASE doesn't work...
    thanks for helping!

    Edit: [somehow solved] Well, I figured out where it comes from. It is the order in which you place the Extension and the Base. For me it worked just placing the base, but not extended. Then it keeps the direction. Anyway, somehow still a Piston extension is placed under the block sometimes...only at the first time copying. Can I somehow cancel it when I place the Block?
     
Thread Status:
Not open for further replies.

Share This Page