How can I prevent the server, from being overloaded?

Discussion in 'Plugin Development' started by LukeSFT, Oct 23, 2012.

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

    LukeSFT

    Icyene: Can you explain the code please.
     
  2. Offline

    LukeSFT

    Icyene: Why aren't you answering?
     
  3. Offline

    Icyene

    LukeSFT

    Truly sorry for the late answer; I had (and still do) a huge amount of homework.

    Basically, that code emulates what MineCraft does in net.minecraft.server.World. It uses reflection to gain access to private fields in the class.

    Key method:
    World.g: gets the highest blockY at the specified coordinates. Basically org.bukkit.world.World.getHighestBlockYAt, but faster.

    In essence, it iterates through all the blocks within 7 blocks from ever player. If the chunk that location is in is loaded, and is not on the unloadQue, then it adds its coordinates to a ChunkCoordIntPair. Basically, gets a list of random chunks. Then adds an offset from 0-16 x and z, resulting in random blocks.
     
    LukeSFT likes this.
  4. Offline

    LukeSFT

    I didn't understand all of your explation, because I'm relatively new in coding, but:

    Can I use this code for my Plugin?
    And how can I use it to for example let it snow goldblocks?
     
  5. Offline

    LukeSFT

    Icyene: can you answer please?
     
  6. Offline

    Icyene

  7. Offline

    LukeSFT

    Icyene:
    I forgot to tag for you.

    Thank you, that I can use your code.

    Where shall I put something like: block.set(material.GOLD);
    to let it snow gold?
     
  8. Offline

    Icyene

    LukeSFT

    Replace everything inside the for loop with "b.setType(Material.GOLD)". If you need any further assistance just let me know.
     
  9. Offline

    LukeSFT

  10. Offline

    Icyene

    LukeSFT The outer for loop. Anything inside the first for loop can be deleted, and replaced with b.setType(Marerial.GOLD_BLOCK). The "for (Block b : ticker.getRandomTickedBlocks()) {" for loop.
     
  11. Offline

    LukeSFT

    Icyene: Do you mean this for loop:
    Code:
    for (List<String> trans : glob.Acid__Rain_Dissolver_Block__Transformations) {
                transformations.add(new BlockTransformer(new IDBlock(trans.get(0)), new IDBlock(trans.get(1))));
            }
     
  12. Offline

    Icyene

    LukeSFT
    ...
    Does that look like it is the correct for loop? No. I told you specifically which for loop.

    The code should look like this:

    Code:Java
    1.  
    2. public class GoldBlockSnower{
    3. private int id;
    4. private final Plugin plugin;
    5. private BlockTickSelector ticker;
    6.  
    7. public GoldBlockSnower(Plugin plug, String affectedWorld) {
    8. this.plugin = plug;7
    9. try {
    10. ticker = new BlockTickSelector(Bukkit.getWorld(affectedWorld),
    11. 100);
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. /**
    17. * Starts the task.
    18. */
    19. public void run() {
    20. id = Bukkit.getScheduler()
    21. .scheduleSyncRepeatingTask(
    22. storm,
    23. new Runnable() {
    24. @Override
    25. public void run() {
    26. try {
    27. for (Block b : ticker.getRandomTickedBlocks()) {
    28. Block tran = b.getRelative(BlockFace.DOWN);
    29. if (tran.getTypeId() != 0) {
    30. tran.setType(Material.GOLD);
    31. }
    32. }
    33. }
    34. } catch (Exception e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. },
    39. 0,
    40. 10);
    41. }
    42. /**
    43. * Ends the task.
    44. */
    45. public void stop() {
    46. Bukkit.getScheduler().cancelTask(id);
    47. }
    48. }
    49.  
     
  13. Offline

    LukeSFT

    Icyene:
    First: I know, but this was the first for loop.
    Second: Aren't you with this code changing the blocks where it snows upon into a goldblock?
    Third: Do I only have to copy this class into my project and execute it or do I need some other classes?
     
  14. Offline

    LukeSFT

    Icyene: Can you answer???
     
  15. Offline

    Icyene

    LukeSFT
    2: No, I am emulating the way Minecraft snows. Basically it does what your code did, but which MUCH less load on the server.
    3: The GoldBlockSnower class and the BlockTickSelector class.
     
  16. Offline

    LukeSFT

    Icyene: Thank you very much. I'll test it.
     
  17. Offline

    LukeSFT

    Icyene: can you give me the thinks you imported in the GoldBlockSnower class and the BlockTickSelector class?
     
  18. Offline

    BMX_ATVMAN14

    LukeSFT Stop spamming him please, PM him
     
  19. Offline

    LukeSFT

    BMX_ATVMAN14:Sorry. I'll do this. I forgot about this ability.
     
  20. Offline

    Icyene

    LukeSFT

    I'm not that active at the moment on Bukkit anymore: school and other projects take alot of my time, so I can't answer as quickly as I used to.

    Anyways, the BlockTickSelector imports:

    Code:Java
    1.  
    2. import net.minecraft.server.Chunk;
    3. import net.minecraft.server.ChunkCoordIntPair;
    4. import net.minecraft.server.EntityHuman;
    5. import net.minecraft.server.WorldServer;
    6. import org.bukkit.World;
    7. import org.bukkit.block.Block;
    8. import org.bukkit.craftbukkit.CraftWorld;
    9. import java.lang.reflect.InvocationTargetException;
    10. import java.lang.reflect.Method;
    11. import java.util.*;
    12.  


    The Snower:

    Code:Java
    1.  
    2. import org.bukkit.Bukkit;
    3. import org.bukkit.block.Block;
    4. import org.bukkit.block.BlockFace;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
     
Thread Status:
Not open for further replies.

Share This Page