Solved Problem setting block at coordinates on enable

Discussion in 'Plugin Development' started by TheYajrab, Aug 19, 2014.

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

    TheYajrab

    I am making a mine plugin where the ore acts like Runescape where you mine and the ore regenerates after a certain amount of time. The ore will be set to stone if it is preset in a config file. The problem I have found is that if the server is reloaded/restarted, the ore will no longer regenerate if it is still stone. Here is my code on enable code which is supposed to search the config file for the block location and the block id:

    Code:java
    1. for (World world : Bukkit.getWorlds()) {
    2. for (int i = 0; i < i + 1;) {
    3. Config OreLocations = new Config(this, "OreLocations"
    4. + File.separator + "OreLocations");
    5. if (OreLocations.getString(world.getName() + "." + i
    6. + ".OreName") == null) {
    7. return;
    8. } else {
    9. int x = OreLocations.getInt(world.getName() + i
    10. + ".LocationX");
    11. int y = OreLocations.getInt(world.getName() + i
    12. + ".LocationY");
    13. int z = OreLocations.getInt(world.getName() + i
    14. + ".LocationZ");
    15. int blockid = OreLocations.getInt(world.getName() + i
    16. + ".Ore");
    17. world.getBlockAt(x, y, z).setTypeId(blockid);
    18. i++;
    19. }
    20. }
    21. }
     
  2. Offline

    Zupsub

    Wow, this works? If I'm thinking right, it's an infinite loop, isn't it?
     
  3. You need to get the chunk the block is in and check if it's loaded, if it isn't loaded load it. Else you can also just make a copy of the world and in the plugin.yml you add a line that says that you want your plugin to start before the world gets loaded (can't remember the name) and remove the world and copy your backup world.
     
  4. Offline

    TheYajrab


    It is an infinite loop as I want it to get the numbers from a config, I cannot just see the first and the last value so I did that and broke the loop when the name of the ore at them coords is null.


    I will have a look and see if I can do that now.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. And Zupsub is also right your for loop is wrong (from what I have learned)
    for (int i = 0; i < i + 1; ) {

    It needs to be

    for (int i = 0; i < i + 1; i++) {

    Or


    do{
    //Code
    while(true);

    And the return is either break or just return.
     
  6. Offline

    TheYajrab

    Tried that before but it gives an error saying that the i++ is dead code or is not used. Hence the reason I put the i++ somewhere else. Anyway, I loaded the chunks and also noticed I was leading to the wrong path. After correcting, it still does not actually place the block for some odd reason. Any suggestions?
     
  7. You can place System.out.println in the for loop and check if it's even called correctly.

    Or you can use my second idea of using a backup.

    Or place the block place code in a join event and have it only triggered if a static boolean is equal to false (what it will be by default) and set it to true.
     
  8. Offline

    TheYajrab


    Here look at my code, I am not sure if I have done it right:

    Code:java
    1. for (World world : Bukkit.getWorlds()) {
    2. for (int i = 0; i < i + 1;) {
    3. Config OreLocations = new Config(this, "OreLocations"
    4. + File.separator + "OreLocations");
    5. if (OreLocations.getString(world.getName() + "." + i
    6. + ".OreName") == null) {
    7. return;
    8. } else {
    9. int x = OreLocations.getInt(world.getName() + "." + i
    10. + ".LocationX");
    11. int y = OreLocations.getInt(world.getName() + "." + i
    12. + ".LocationY");
    13. int z = OreLocations.getInt(world.getName() + "." + i
    14. + ".LocationZ");
    15. int blockid = OreLocations.getInt(world.getName() + "." + i
    16. + ".Ore");
    17. world.getBlockAt(x, y, z).getChunk().load(true);
    18. if (world.getBlockAt(x, y, z).getChunk().isLoaded()) {
    19. logger.info("Chunk is loaded!");
    20. world.getBlockAt(x, y, z).setType(Material.getMaterial(blockid));
    21. world.
    22. world.getBlockAt(x, y, z).getChunk().unload(true);
    23. } else {
    24. logger.info("Chunk is not loaded!");
    25. world.getBlockAt(x, y, z).getChunk().load(true);
    26. }
    27. logger.info("Placed block " + blockid + " in " + world.getName() + " at " + x + ", " + y + ", " + z + "!");
    28. i++;
    29. }
    30. }
    31. }
     
  9. Offline

    stormneo7

    Last time I checked, you don't do return; You do break;
     
    Lactem likes this.
  10. Try using the logger or system.out.println to check what doesn't work
     
  11. Offline

    TheYajrab


    I have. It says it is placing the blocks but it wont place it. There is something wrong with the line
    Code:java
    1. world.getBlockAt(x, y, z).setType(Material.getMaterial(blockid));


    Figured it out, I loaded the chunks wrong as I did this:

    Code:java
    1. world.getBlockAt(x, y, z).getChunk().load(true);


    When I was supposed to do this:

    Code:java
    1. world.loadChunk(world.getBlockAt(x, y, z).getChunk());


    Issue is now resolved, thank you to everyone that helped.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page