Solved A Cocoa Facing bug

Discussion in 'Plugin Development' started by Kilorbine, Mar 26, 2016.

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

    Kilorbine

    Edit : Solution here https://bukkit.org/posts/3352492/

    Hi,

    I'm currently working on my plugin, AutoReplant, and 'm agaist a bug.
    When a player break a Cocoa, I replant one automaticly.

    The probleme is that the cocoa always face NORTH.

    So, I've try to fix that, here's the code


    Code:
    System.out.println("before");
    CocoaPlant cp = (CocoaPlant)e.getBlock().getState().getData(); // Here's, getting the materialData
    BlockFace f = cp.getFacing(); //saving the facing for the new cocoa
    System.out.println(f.toString()); //printing the face
    e.getBlock().breakNaturally(); //breaking the old cocoa
    e.setCancelled(true);
    e.getBlock().setType(tmp); //"replant" the cocoa
    cp = (CocoaPlant)e.getBlock().getState().getData();
    cp.setFacingDirection(f); //setting the face for the new cocoa
    e.getBlock().getState().setData(cp); //setting the data to the block
    cp = (CocoaPlant)e.getBlock().getState().getData(); //verifying
    f = cp.getFacing();                              // if
    System.out.println(f.toString());                 //the fancing is well set
    System.out.println("finish");

    This is what I got when i break a cocoa :

    I don't know what i Miss because i set the facing good.
    But,regardless I do, nothing work.

    Any help?
     
    Last edited: Mar 26, 2016
  2. Offline

    mcdorli

    I think cocoa doesn't work with setting the state's direction, You need to use blockData's

    Also, you should consider using variable names wich make at least some sense, for example cocoaPlant for the cocoaPlant, or facing for the facing (wich shouldn't even exist, because you could simply stick those 2 lines together)
     
  3. Offline

    Kilorbine

    name have sens for me ;)
    But why then it say the good direction?
     
  4. Offline

    mcdorli

    it doesn't, it first prints out SOUTH for the cocoa's direction, then NORTH, when you set it.
     
  5. Offline

    Kilorbine

    hum...
    Strange because it say the good direction when i broke the cocoa.
    Can you show me how to set the byte?
    Search on the web but don't see how to create it with the good value
     
  6. Offline

    mcdorli

    Block#setData(byte data)
     
  7. Offline

    Kilorbine

    Yes, I've seen that function.
    The problem is that I don't know how to build data
     
  8. Offline

    mcdorli

  9. Offline

    Kilorbine

    Aaah
    You don't get me :/
    The cocoa data is set with the cocoa size (0/1/2) and the cocoa facing (0/1/2/3)
    I don't know to set the two value in one variable.
    I will try by my own.
    Thx for your replies :)
     
  10. Offline

    mcdorli

    Just set the facng, the other one is something else
     
  11. Offline

    Kilorbine

    For people with the same problem, here's my solution :

    Code:
    BlockFace f = ((CocoaPlant)e.getBlock().getState().getData()).getFacing();
    e.getBlock().breakNaturally();
    e.setCancelled(true);
    e.getBlock().setType(tmp);
    e.getBlock().setData((byte) ((f.ordinal() + 2) % 4));
    
     
Thread Status:
Not open for further replies.

Share This Page