Solved How to replace iron doors with iron doors

Discussion in 'Plugin Development' started by ProSl3nderMan, Jul 25, 2015.

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

    ProSl3nderMan

    Alright, I've set it to when a player right clicks a redstone block, they win the game. The player is then kick and I need to set all the iron doors back to normal in the map they were just in because some doors are opened with a switch and I wish for them not to be open next game.

    So how do I replace all iron doors in a map with iron doors?
     
  2. Offline

    khave

    Why would you need to replace iron doors with iron doors? It seems really redundant.
     
  3. Offline

    Zombie_Striker

    @ProSl3nderMan
    What you mean to say is instead of "replacing iron doors with iron doors", you want to say "resetting iron doors"

    To do this, You need to dig into the block metadata and set a value that controls if the door is opened or not. Just debug by looping through all the metadata, and once you found the right tag, use Block.setMetaData(tag,value);
     
  4. Offline

    ProSl3nderMan

    What your saying all seems way to complicated to do, is there any other way?
    Alright, tell me how to close the iron doors each game instead, cause I did research and I can't seem to figure it out.
     
  5. Offline

    khave

    Get the iron door, get it's state, cast the state's data to Openable, use Openable.setOpen(false), set the state's data to the openable, update the state.
     
  6. Offline

    _Filip

    ((Openable) door.getState()).setOpen(false);
     
  7. Offline

    ProSl3nderMan

    Alright, I tried that. It doesn't seem to be working. Code:

    Code:
    Block irondoor = Bukkit.getWorld("cr1").getBlockAt(1635, 68, 1428);
                        if (irondoor == Material.IRON_DOOR_BLOCK) {
                            Openable.setOpen(false);
                        }
    Am I doing my code right?
     
  8. Offline

    DoggyCode™

    Why don't you just get the block's (in this case it's a iron_door_block) location and replace it with a CLOSED iron door? Doesn't that work?
     
  9. Offline

    khave

    No use something like this:

    Code:
    if(irondoor == Material.IRON_DOOR_BLCOK){
    BlockState state = block.getState();
    Openable openable = (Openable) state.getData();
    openable.setOpen(false);
    state.setData((MaterialData) openable);
    state.update();
    }
    
     
  10. Offline

    ProSl3nderMan

    Alright, I got this for my code:
    Code:
                        Block irondoor = Bukkit.getWorld("cr1").getBlockAt(1635, 68, 1428);
                        if (irondoor == Material.IRON_DOOR_BLOCK) {
                            BlockState state = block.getState();
                            Openable openable = (Openable) state.getData();
                            openable.setOpen(false);
                            state.setData((MaterialData) openable);
                            state.update();
                        }
    I get this error code "Incompatible operand types Block and Material" for the if command.
     
  11. irondoor is a Block and not a Material. Check if (block.getType() == Material....
     
    Shortninja66 likes this.
  12. Offline

    ProSl3nderMan

    So instead of the material.blahblahblah, I would maybe put the id number just alone? Would that work? (On the go, can't test lol).
     
  13. Ids are deprecated and soon no longer supported
     
  14. Offline

    Zombie_Striker

    @ProSl3nderMan
    You do realize that this line
    will never be true because a Block is not the same as Material. You would have to use irondoor.getType() == Material....
     
  15. Offline

    ProSl3nderMan

    @Zombie_Striker Yeah, I added that 3 hours after my post. Thanks for reassuring.

    Now, the code seems to be getting no errors and everything looks correct. The only problem I'm having now is the door is not changing lol! Here's my updated code:

    Code:
                        Block irondoor = Bukkit.getWorld("cr1").getBlockAt(1635, 67, 1431);
                        if (irondoor.getType() == Material.IRON_DOOR_BLOCK) {
                            BlockState state = block.getState();
                            Openable openable = (Openable) state.getData();
                            openable.setOpen(false);
                            state.setData((MaterialData) openable);
                            state.update();
                        }
    Did I type something in wrong? I'm partially new to this area in coding.
     
  16. Offline

    Zombie_Striker

    @ProSl3nderMan
    Normally, doors are two blocks high. Wouldn't you have to set both blocks? Also, are you sure that the location and world name are correct?

    Are there any errors? Are there glitches in the door texture?
     
  17. Offline

    plobnob

    @ProSl3nderMan Not being funny, but if its opened by a switch, surely you just need to turn the switch off? Whats the point in mucking about with the actual door if you can just turn a switch off? If the switch is still on and you close a door, will it not just open again?
     
  18. Offline

    ProSl3nderMan

    @Zombie_Striker
    Hm, I shall try to figure out how to get both. And yes, it's the right location. I've tped me to the spot a couple times.


    @plobnob
    I was thinking this too, so I took away the switch and made it to where the door should be opened. Didn't work sadly.

    Alright, I think I found out the problem, I just don't know how to fix it:
    Code:
                        Block irondoor = Bukkit.getWorld("cr1").getBlockAt(1635, 67, 1431);
                        Block irondoor2 = Bukkit.getWorld("cr1").getBlockAt(1635, 68, 1431);
                        if ((irondoor.getType() == Material.IRON_DOOR_BLOCK) && (irondoor2.getType() == Material.IRON_DOOR_BLOCK)) {
                            BlockState state = block.getState();
                            Openable openable = (Openable) state.getData();
                            openable.setOpen(true);
                            state.setData((MaterialData) openable);
                            state.update();
                        }
    I think maybe I'm suppose to declare what I'm changing? It seems to be focusing on the player interact event, which is right clicking a sponge. How can I declare I'm changing the iron door?

    EDIT by Timtower: merged posts
     
    Last edited by a moderator: Jul 28, 2015
  19. Shouldn't this be irondoor.getState(); ??
     
  20. Offline

    ProSl3nderMan

    I thought the same thing, I tried it but no difference.
     
  21. Offline

    ProSl3nderMan

    Still no work ;P A little help please haha.

    Figured it out, the if command wasn't working apparently, took it out and runs smoothly. Thanks ya'll for the help.

    Code for those who would like it:
    Code:
                        Block irondoor = Bukkit.getWorld("cr1").getBlockAt(1635, 68, 1431);
                        BlockState state = irondoor.getState();
                        Openable openable = (Openable) state.getData();
                        openable.setOpen(false);
                        state.setData((MaterialData) state.getData());
                        state.update();
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 11, 2016
  22. Offline

    ProSl3nderMan

    Hey, I tried doing multiple iron door things, but the code only works for one? Can someone tell me how to add 10 doors that all open? Code:
    Code:
                        int x1 = AccessControl.plugin.getConfig().getInt("map1.cell1.door1.x");
                        int y1 = AccessControl.plugin.getConfig().getInt("map1.cell1.door1.y");
                        int z1 = AccessControl.plugin.getConfig().getInt("map1.cell1.door1.z");
                        Block irondoor = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x1, y1, z1);
                        BlockState state = irondoor.getState();
                        Openable openable = (Openable) state.getData();
                        openable.setOpen(false);
                        state.setData((MaterialData) state.getData());
                        state.update();
                      
                        int x2 = AccessControl.plugin.getConfig().getInt("map1.cell1.door2.x");
                        int y2 = AccessControl.plugin.getConfig().getInt("map1.cell1.door2.y");
                        int z2 = AccessControl.plugin.getConfig().getInt("map1.cell1.door2.z");
                        Block irondoor2 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x2, y2, z2);
                        BlockState state2 = irondoor2.getState();
                        Openable openable2 = (Openable) state2.getData();
                        openable2.setOpen(false);
                        state2.setData((MaterialData) state2.getData());
                        state2.update();
                      
                        int x3 = AccessControl.plugin.getConfig().getInt("map1.cell1.door3.x");
                        int y3 = AccessControl.plugin.getConfig().getInt("map1.cell1.door3.y");
                        int z3 = AccessControl.plugin.getConfig().getInt("map1.cell1.door3.z");
                        Block irondoor3 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x3, y3, z3);
                        BlockState state3 = irondoor3.getState(); Openable openable3 = (Openable) state3.getData(); openable3.setOpen(false); state3.setData((MaterialData) state3.getData());state3.update();
                      
                        int x4 = AccessControl.plugin.getConfig().getInt("map1.cell1.door4.x");
                        int y4 = AccessControl.plugin.getConfig().getInt("map1.cell1.door4.y");
                        int z4 = AccessControl.plugin.getConfig().getInt("map1.cell1.door4.z");
                        Block irondoor4 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x4, y4, z4);
                        BlockState state4 = irondoor4.getState(); Openable openable4 = (Openable) state4.getData(); openable4.setOpen(false); state4.setData((MaterialData) state4.getData());state4.update();
                      
                        int x5 = AccessControl.plugin.getConfig().getInt("map1.cell1.door5.x");
                        int y5 = AccessControl.plugin.getConfig().getInt("map1.cell1.door5.y");
                        int z5 = AccessControl.plugin.getConfig().getInt("map1.cell1.door5.z");
                        Block irondoor5 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x5, y5, z5);
                        BlockState state5 = irondoor5.getState(); Openable openable5 = (Openable) state5.getData(); openable5.setOpen(false); state5.setData((MaterialData) state5.getData());state5.update();
                      
                        int x6 = AccessControl.plugin.getConfig().getInt("map1.cell1.door6.x");
                        int y6 = AccessControl.plugin.getConfig().getInt("map1.cell1.door6.y");
                        int z6 = AccessControl.plugin.getConfig().getInt("map1.cell1.door6.z");
                        Block irondoor6 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x6, y6, z6);
                        BlockState state6 = irondoor6.getState(); Openable openable6 = (Openable) state6.getData(); openable6.setOpen(false); state6.setData((MaterialData) state6.getData());state6.update();
                      
                        int x7 = AccessControl.plugin.getConfig().getInt("map1.cell1.door7.x");
                        int y7 = AccessControl.plugin.getConfig().getInt("map1.cell1.door7.y");
                        int z7 = AccessControl.plugin.getConfig().getInt("map1.cell1.door7.z");
                        Block irondoor7 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x7, y7, z7);
                        BlockState state7 = irondoor7.getState(); Openable openable7 = (Openable) state7.getData(); openable7.setOpen(false); state7.setData((MaterialData) state7.getData());state7.update();
                      
                        int x8 = AccessControl.plugin.getConfig().getInt("map1.cell1.door8.x");
                        int y8 = AccessControl.plugin.getConfig().getInt("map1.cell1.door8.y");
                        int z8 = AccessControl.plugin.getConfig().getInt("map1.cell1.door8.z");
                        Block irondoor8 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x8, y8, z8);
                        BlockState state8 = irondoor8.getState(); Openable openable8 = (Openable) state8.getData(); openable8.setOpen(false); state8.setData((MaterialData) state8.getData());state8.update();
                      
                        int x9 = AccessControl.plugin.getConfig().getInt("map1.cell1.door9.x");
                        int y9 = AccessControl.plugin.getConfig().getInt("map1.cell1.door9.y");
                        int z9 = AccessControl.plugin.getConfig().getInt("map1.cell1.door9.z");
                        Block irondoor9 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x9, y9, z9);
                        BlockState state9 = irondoor9.getState(); Openable openable9 = (Openable) state9.getData(); openable9.setOpen(false); state9.setData((MaterialData) state9.getData());state9.update();
                      
                        int x10 = AccessControl.plugin.getConfig().getInt("map1.cell1.door10.x");
                        int y10 = AccessControl.plugin.getConfig().getInt("map1.cell1.door10.y");
                        int z10 = AccessControl.plugin.getConfig().getInt("map1.cell1.door10.z");
                        Block irondoor10 = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x10, y10, z10);
                        BlockState state10 = irondoor10.getState(); Openable openable10 = (Openable) state10.getData(); openable10.setOpen(false); state10.setData((MaterialData) state10.getData());state10.update();
    I get this error code: "at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.ClassCastException: org.bukkit.material.MaterialData cannot be cast to org.bukkit.material.Openable" in the console for line 17.
     
  23. Offline

    teej107

    You seriously need to use a for-loop. It will reduce your redundant code by a lot. As for your error, don't cast a MaterialData object to a Openable because they are obviously not related in anyway.
     
  24. Offline

    ProSl3nderMan

    Well, it seems to be related in the first bit of code which doesn't make sense haha. I'll look into the for-loop thing though, that possibly could fix the problem I got atm.
     
  25. Offline

    ProSl3nderMan

    @teej107
    Can you help me with for-looping through a config? Idk how to go about this. Code:
    Code:
                        for(String key : AccessControl.plugin.getConfig().getConfigurationSection("map1.doors").getKeys(false)) {
                            int x = AccessControl.plugin.getConfig().getInt(".x");
                            int y = AccessControl.plugin.getConfig().getInt(".y");
                            int z = AccessControl.plugin.getConfig().getInt(".z");
                            Block irondoor = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x, y, z);
                            BlockState state = irondoor.getState();
                            Openable openable = (Openable) state.getData();
                            openable.setOpen(false);
                            state.setData((MaterialData) state.getData());
                            state.update();
                        }
    Idk know where to insert the "key" variable for the x, y, z. Thanks in advance.
     
  26. Offline

    teej107

    @ProSl3nderMan From the looks of how you set your config, you paths are like
    right?

    So don't use a for-each loop. Use a regular for-loop and get each key like
    Code:
    AccessControl.plugin.getConfig().getInt("map1.cell1.door" + i + ".x");
    where "i" is the number declared in the for-loop initialization.

    You can use a for-each loop like you tried but you are getting the wrong configuration section from the looks of it.
     
  27. Offline

    ProSl3nderMan

    @teej107
    My config is like this:

    Code:
    map1:
       doors:
         door1:
            x: 1635.430709496315
            y: 68.0
            z: 1431.4539155137643
         door2:
            x: 1624.385234447479
            y: 68.0
            z: 1431.389009344763
         door3:
            x: 1613.4017723517186
            y: 68.0
            z: 1431.3590368088426
         door4:
            x: 1602.35153197333
            y: 68.0
            z: 1431.5282716179433
         door5:
            x: 1591.512499988079
            y: 68.0
            z: 1431.40315612407
         door6:
            x: 1598.5303114761286
            y: 68.0
            z: 1413.4592004185604
         door7:
            x: 1609.5117751470095
            y: 68.0
            z: 1413.5012703839361
         door8:
            x: 1620.5727401387576
            y: 68.0
            z: 1413.5620796118035
         door9:
            x: 1631.5416817981818
            y: 68.0
            z: 1413.3884268759232
         door10:
            x: 1642.5139078915333
            y: 68.0
            z: 1413.46735955258
    I need to get every single door's coord and plug it into the for-loop so that they all close on an event. I tried your code, did I enter it right?:
    Code:
                    for(String i : AccessControl.plugin.getConfig().getConfigurationSection("map1.doors.").getKeys(false)) {
                           
    for(String i : AccessControl.plugin.getConfig().getConfigurationSection("map1.doors").getKeys(false)) {
                            int x = AccessControl.plugin.getConfig().getInt("map1.door" + i + ".x");
                            int y = AccessControl.plugin.getConfig().getInt("map1.door" + i + ".y");
                            int z = AccessControl.plugin.getConfig().getInt("map1.door" + i + ".z");
                            Block irondoor = Bukkit.getWorld((AccessControl.plugin.getConfig().getString("map1.world"))).getBlockAt(x, y, z);
                            BlockState state = irondoor.getState();
                            Openable openable = (Openable) state.getData();
                            openable.setOpen(false);
                            state.setData((MaterialData) state.getData());
                            state.update();
                   }
    
     
    Last edited: Aug 1, 2015
  28. Offline

    teej107

    No.
    If you plan on using a defined amount of doors then a regular for-loop will be easiest. However if you plan on having an infinite amount theoretically than looping through the configuration sections will be better rather than just referencing a path directly.
     
  29. Offline

    ProSl3nderMan

    Alright, I got everything down. The only problem now is the code isn't working for closing doors? It was just working, now I'm getting the MaterialData object to a Openable again. For some reason it was related but isn't anymore?

    Hm, only works when I don't get the coords for the iron door from the config...
     
    Last edited by a moderator: Aug 1, 2015
  30. Offline

    ProSl3nderMan

    *cough cough* I'm a fail xP Config didn't have the right coords, when I did the /set cell cr1 door 1, it took the wrong negative number. Works fine now, if anyone needs the code that works just msg me for it. Thanks all for the help! Really appreciate it :)
     
Thread Status:
Not open for further replies.

Share This Page