Solved Close all doors in a region

Discussion in 'Plugin Development' started by MeJellyPelly, Nov 18, 2014.

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

    MeJellyPelly

    Is it possible to close doors programmatically? I am writing a plugin and want it to be able to reset the state of a small-ish area, including closing any open doors.

    I haven't quite figured out how / when the area will (be) reset yet, but I want to check if closing doors is even possible!

    (Ideally, I would like to reset everything when a player enters the region. If that isn't possible, I could have them enter through a door or over a pressure plate ... )
     
  2. Offline

    Skionz

    Iterate through every block in the "region" checking if it is a door then if it is shut it?
     
  3. Offline

    MeJellyPelly

    Sorry - it's the "shut it" bit I don't know how to do :)

    I am looping from the min to max points of the region, I am checking for Material.Wooden_Door ... but how do I tell it to close? Or even check if it is open or closed?
     
  4. Offline

    adam753

  5. Offline

    mythbusterma

  6. Offline

    MeJellyPelly

    Yikes, now you are testing my limited Java knowledge ... do you have an example of how I set the "third bit"?
     
  7. Offline

    mythbusterma

    MeJellyPelly

    I can't recall if Java lets you do bitwise with bytes, but you can do this with bit masking, something like this (again if Java lets you):

    Code:java
    1. byte data = doorBlock.getData();
    2. data | 0x4;
    3. doorBlock.setData(data);
     
  8. Offline

    adam753

  9. Offline

    MeJellyPelly

    OK, I will give that a go shortly ... currently struggling with another issue!

    I got it to work :) Thanks for your help.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  10. Offline

    MeJellyPelly

    In case anyone else needs to do this, here is the code I ended up with (world and regionName are defined elsewhere):

    Code:java
    1. RegionManager m = WGBukkit.getRegionManager(world);
    2. ProtectedRegion region = m.getRegion(regionName);
    3. if (region == null) {
    4. return;
    5. }
    6.  
    7. BlockVector minPoint = region.getMinimumPoint();
    8. BlockVector maxPoint = region.getMaximumPoint();
    9.  
    10. // loop through each block in the region and check if each one is correct
    11. for (int x = (int) minPoint.getX(); x <= (int) maxPoint.getX(); x++) {
    12. for (int y = (int) minPoint.getY(); y <= (int) maxPoint.getY(); y++) {
    13. for (int z = (int) minPoint.getZ(); z <= (int) maxPoint.getZ(); z++) {
    14.  
    15. Block block = world.getBlockAt(x, y, z);
    16.  
    17. if (block == null) {
    18. continue;
    19. }
    20. else if (block.getType() == Material.WOODEN_DOOR) {
    21. // it's a Door!
    22.  
    23. BlockState state = block.getState();
    24. Openable o = (Openable) state.getData();
    25. o.setOpen(false);
    26. state.setData((MaterialData) o);
    27. state.update();
    28. }
    29. }
    30. }
    31. }
    32.  
     
    Quackster and ChipDev like this.
Thread Status:
Not open for further replies.

Share This Page