set blocks performance friendly

Discussion in 'Plugin Development' started by ScorixEar, Jan 31, 2017.

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

    ScorixEar

    Hi guys, I am currently programming a game, which includes massive block setting.
    When a player moves, a stained glass way will lead and follow his way and will pass aways after 5 seconds.
    I already checked, that only blocks are set if they are not set, that only blocks are checked if the player is actually changing his location to another block.
    I also tried to do that with sendBlockChange, but first, this doesn't help the performance because the sendBlockChange has to be applied to every online player and second, anti cheat-Plugins will go crazy and I don't have time to program a new NCP.
    So I post here the actual block setting during the moveEvent and would love to here suggestions to keep the performance down.
    At the moment, a server needs about 5 GB RAM for 3-5 players.

    Thanks Guys


    Code:
    public class MoveListener implements Listener {
    
    
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
    
    
                Location l = e.getTo();
              if(Nyanfighters.getInstance().getSpawnpointsConfi().isSet("SpielSpawn.pos1.X"))
              {
                  if(e.getFrom().getBlockY()<254&& YmlMethods.isInArea(e.getPlayer(),e.getPlayer().getLocation()) && !(e.getFrom().getBlockX() == e.getTo().getBlockX() && e.getFrom().getBlockY() == e.getTo().getBlockY() && e.getFrom().getBlockZ() == e.getTo().getBlockZ())){
                      if ((l.getYaw() <= -45 && l.getYaw() > -135.0) || (l.getYaw() <= 305 && l.getYaw() > 215)) {           
                          setArea2(-1, 1, l, e.getPlayer(), e.getPlayer().isSneaking());
                      } else if ((l.getYaw() <= -305 || (l.getYaw() > -45 && l.getYaw() <= 0)) || ((l.getYaw() <= 45 && l.getYaw() >= 0) || l.getYaw() > 305)) {
                          setArea(1, 1, l, e.getPlayer(), e.getPlayer().isSneaking());
                      } else if ((l.getYaw() <= -215 && l.getYaw() > -305) || (l.getYaw() <= 135 && l.getYaw() > 45)) {
                          setArea2(-1, 1, l, e.getPlayer(), e.getPlayer().isSneaking());
                      } else {
                          setArea(-1, -1, l, e.getPlayer(), e.getPlayer().isSneaking());
                      }
                  }
              }
    
    
    
    
        }
    
        private void setArea(int x, int z, Location start, Player p, boolean sneak) {                                                      
            if (p.getLocation().getPitch()>75) {                                    
                setAir("start", 0, 0, start, 4);                                                                           
                setAir("left", x, 0, start, 14);
                //setAir("lefttwice", x * 2, 0, start, 14);
                setAir("right", -x, 0, start, 9);
                //setAir("righttwice", (-x) * 2, 0, start, 9);
                setAir("front", 0, z, start, 4);
                setAir("back", 0, -z, start, 4);
                setAir("leftfront", x, z, start, 14);
                //setAir("lefttwicefront", x * 2, z, start, 14);
                setAir("rightfront", -x, z, start, 9);
                //setAir("righttwicefront", (-x) * 2, z, start, 9);
                setAir("leftback", x, -z, start, 14);
                //setAir("lefttwiceback", x * 2, -z, start, 14);
                setAir("rightback", -x, -z, start, 9);
                //setAir("righttwiceback", (-x) * 2, -z, start, 9);
            } else {                                                                 
                setBlocks("start", 0, 0, start, 4);
                setBlocks("left", x, 0, start, 14);
                //setBlocks("lefttwice", x * 2, 0, start, 14);
                setBlocks("right", -x, 0, start, 9);
                //setBlocks("righttwice", (-x) * 2, 0, start, 9);
                setBlocks("front", 0, z, start, 4);
                setBlocks("back", 0, -z, start, 4);
                setBlocks("leftfront", x, z, start, 14);
                //setBlocks("lefttwicefront", x * 2, z, start, 14);
                setBlocks("rightfront", -x, z, start, 9);
                //setBlocks("righttwicefront", (-x) * 2, z, start, 9);
                setBlocks("leftback", x, -z, start, 14);
                //setBlocks("lefttwiceback", x * 2, -z, start, 14);
                setBlocks("rightback", -x, -z, start, 9);
                //setBlocks("righttwiceback", (-x) * 2, -z, start, 9);
            }
        }
    
        private void setArea2(int x, int z, Location start, Player p, boolean sneak) {                                                     
            if (p.getLocation().getPitch()>75) {                                                                       
                setAir("start", 0, 0, start, 4);                                                                       
                setAir("left", 0, x, start, 14);
                //setAir("lefttwice", 0, x * 2, start, 14);
                setAir("right", 0, -x, start, 9);
                //setAir("righttwice", 0, (-x) * 2, start, 9);
                setAir("front", z, 0, start, 4);
                setAir("back", -z, 0, start, 4);
                setAir("leftfront", z, x, start, 14);
                //setAir("lefttwicefront", z, x * 2, start, 14);
                setAir("rightfront", z, -x, start, 9);
                //setAir("righttwicefront", z, (-x) * 2, start, 9);
                setAir("leftback", -z, x, start, 14);
                //setAir("lefttwiceback", -z, x * 2, start, 14);
                setAir("rightback", -z, -x, start, 9);
                //setAir("righttwiceback", -z, (-x) * 2, start, 9);
            } else {
                setBlocks("start", 0, 0, start, 4);
                setBlocks("left", 0, x, start, 14);
                //setBlocks("lefttwice", 0, x * 2, start, 14);
                setBlocks("right", 0, -x, start, 9);
                //setBlocks("righttwice", 0, (-x) * 2, start, 9);
                setBlocks("front", z, 0, start, 4);
                setBlocks("back", -z, 0, start, 4);
                setBlocks("leftfront", z, x, start, 14);
                //setBlocks("lefttwicefront", z, x * 2, start, 14);
                setBlocks("rightfront", z, -x, start, 9);
                //setBlocks("righttwicefront", z, (-x) * 2, start, 9);
                setBlocks("leftback", -z, x, start, 14);
                //setBlocks("lefttwiceback", -z, x * 2, start, 14);
                setBlocks("rightback", -z, -x, start, 9);
                //setBlocks("righttwiceback", -z, (-x) * 2, start, 9);
            }
        }
    
        private void setBlocks(String s, int x, int z, Location start, int data) {                                            
            Location left = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() - 1, start.getBlockZ() + z);
            //Location leftair = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY(), start.getBlockZ() + z);
            /*if (leftair.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair, Material.AIR, (byte) id);
            }
            Location leftair2 = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() + 1, start.getBlockZ() + z);
            if (leftair2.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair2, Material.AIR, (byte) id);
            }
    
            Location leftair3 = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() + 2, start.getBlockZ());
            if (leftair3.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair3, Material.AIR, (byte) id);
            }*/
            Material block = left.getBlock().getType();
            if (block == Material.AIR) {
                left.getBlock().setType(Material.STAINED_GLASS);
                BlockState bs=  left.getBlock().getState();
                bs.setRawData((byte)data);
                bs.update();
                removeBlock(left);
            }
        }
    
        private void setAir(String s, int x, int z, Location start, int data) {                                               
    
            Location left = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() - 1, start.getBlockZ() + z);
            if (left.getBlock().getType() == Material.STAINED_GLASS)
                left.getBlock().setType(Material.AIR);
            /*Location leftair = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY(), start.getBlockZ() + z);
            if (leftair.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair, Material.AIR, (byte) id);
            }
            Location leftair2 = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() + 1, start.getBlockZ() + z);
            if (leftair2.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair2, Material.AIR, (byte) id);
            }
            Location leftair3 = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() + 2, start.getBlockZ());
            if (leftair3.getBlock().getType() == Material.AIR) {
                for (Player p : Bukkit.getOnlinePlayers())
                    p.sendBlockChange(leftair3, Material.AIR, (byte) id);
            }*/
            Location right = new Location(start.getWorld(), start.getBlockX() + x, start.getBlockY() - 3, start.getBlockZ() + z);
            Material block = right.getBlock().getType();
            if (block == Material.AIR) {
                right.getBlock().setType(Material.STAINED_GLASS);
                BlockState bs=  right.getBlock().getState();
                bs.setRawData((byte)data);
                bs.update();
                removeBlock(right);
            }
        }
    
        private void removeBlock(Location remove) {        
            Bukkit.getScheduler().scheduleSyncDelayedTask(Nyanfighters.getInstance(), () -> remove.getBlock().setType(Material.AIR), 20 * 5);
        }
    
    }

    Notice: There are some code brackets as comments marked. those are all Extensions, which are the target, but already not used to set peerformance down.
     
    Last edited: Jan 31, 2017
  2. Offline

    kameronn

    @ScorixEar
    Not sure if you want to do this but you can separate the blocks and check if they are in the same chunk or not. If they arent in the same chunk store them in maybe an ordered list and place only blocks that are in the same chunk at a time, then place the rest after the first task in done.

    TL;DR : Set the blocks based on chunk over a few ticks and in a runnable .runTask().

    If the next problem is that it doesnt look smooth, you can do a loading effect to make it look cool.

    Not sure if this will help though, just giving my thoughts, I heard that for time a block is changed in the world it updates the whole chunk
     
Thread Status:
Not open for further replies.

Share This Page