Solved I keep overloading the server

Discussion in 'Plugin Development' started by cfil360, Mar 25, 2014.

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

    cfil360

    I keep overloading the server when i try to replace all these blocks. Any suggestions.
    Code:java
    1. public void spawnBlocks(Player p) {
    2. Random rand = new Random();
    3. World w = p.getWorld();
    4. int x = p.getLocation().getBlockX();
    5. int y = p.getLocation().getBlockY();
    6. int z = p.getLocation().getBlockZ();
    7. for(int i : blocks) {
    8. for(int k = 0; k < 50; k++) {
    9. x = x + rand.nextInt(1000);
    10. y = y + rand.nextInt(50);
    11. z = z + rand.nextInt(1000);
    12. //find the block from the random location
    13. Block b = w.getBlockAt(new Location(w, x, y, z));
    14. //modify the block then repeat process
    15. b.setTypeId(i);
    16. }
    17. }
    18.  
    19. p.sendMessage(ChatColor.GREEN + "World Generated");
    20. }


    That is no help at all.

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

    RawCode

    well, answer two simple questions:

    1) Are chunk you want to use loaded?
    2) Is reading from hdd instant?
     
  3. cfil360
    A better question is, what exactly are you trying to do? There's probably a better way to go about doing it.
     
  4. Offline

    cfil360

    The Gaming Grunts I am trying to spawn blocks like sponge, and jukeboxes randomly throughout the world.
     
  5. cfil360
    You were kinda on the right track. Using the Random class, generate random x, y, and z coordinates. Then get the block at that location and set it to whatever you want. For example:

    Code:java
    1. Random r = new Random();
    2. int x, y, z;
    3.  
    4. x = r.nextInt();
    5. y = r.nextInt(256);
    6. z = r.nextInt();
    7.  
    8. World w = player.getWorld();
    9. Block b = w.getBlockAt(x, y, z);
    10. b.setType(Material.SPONGE);
     
  6. Offline

    cfil360

    The Gaming Grunts please read my original post. I am not having trouble placing the block, only I am attempting to place too many and I was looking for a way to quit crashing the server.
     
  7. cfil360
    Try putting everything into a queue of sorts. Basically requires putting all the blocks into a list and as you place them, remove them from the list.
     
  8. Offline

    cfil360


    The Gaming Grunts Can you please elaborate on this more. Are you saying enter the locations into a list?
     
  9. Offline

    Barinade

    Make sure the chunk at the given location is loaded, load if not (if that's necessary)
     
    cfil360 likes this.
  10. Offline

    cfil360

    Barinade so if I make sure all chunks are loaded it should prevent the crashing, got any ideas to cut back on lag. Like how to space out the block replacement or something.
     
  11. Offline

    amhokies

    You can figure out a way to use scheduled task to place x amount of blocks every y amount of time.
     
    cfil360 likes this.
  12. Offline

    Barinade

    Have you considered not doing it? lol
    What are you trying to achieve?
     
  13. cfil360
    To elaborate on what I said earlier, add the blocks you want to replace to a List<Material> and schedule a syncRepeatingTask() to place x amount of blocks from the list in a given time period and after the blocks are placed, remove them from the list.
     
    cfil360 likes this.
  14. Offline

    cfil360

    amhokies I was originally going to do that but I didn't want to make it take minutes just to spawn some blocks. Ill see what I can do tomorrow. Thanks for all your help.

    Barinade Someone wanted to make the idea of custom blocks. So they set the I'd, the lore, the crafting recipes etc. So blocks with little use would be naturally spawned and repurposed.

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

    Barinade

  16. Offline

    amhokies

    cfil360 It doesn't even really have to be over a large amount of time, you can set the task for every few ticks, and have it done in a second or two. Maybe play around with it a bit and find whatever works best.
     
  17. Offline

    hexaan

    cfil360
    I'm not sure if it loads the chunks you are changing a block in but this part of the code
    Code:java
    1. x = x + rand.nextInt(1000);
    2. y = y + rand.nextInt(50);
    3. z = z + rand.nextInt(1000);

    with the 'right' odds could potentially change blocks in a x = 50000 by y = 2500 by z = 50000 area since you keep adding this up tot he original value. (If the array blocks only has 1 block in it. If that array has more in it this number will go up by a lot)

    If you change your code to do a smaller area by removing the addition of the original value I think you would not overload the server.

    Code:java
    1. x = rand.nextInt(1000);
    2. y = rand.nextInt(50);
    3. z = rand.nextInt(1000);
     
  18. Offline

    RawCode

    answer is chunk populators
     
  19. Offline

    ZeusAllMighty11



    Sounds like you need to use a BlockPopulator
     
Thread Status:
Not open for further replies.

Share This Page