设置块Bukkit性能友好

时间:2017-02-01 06:13:15

标签: java game-physics minecraft bukkit

我正在编写一个游戏,其中包括大量的块设置。 当玩家移动时,彩色玻璃方式会引导并按照他的方式行进,并在5秒后消失。

我已经检查过,如果未设置块,则只设置块,如果播放器实际上将其位置更改为另一个块,则仅检查块。

我也尝试用sendBlockChange做到这一点,但首先,这对性能没有帮助,因为sendBlockChange必须应用于每个在线播放器,其次,反作弊插件会发疯,我不会&#39有时间编写一个新的NCP。

所以我在这里发布了moveEvent期间的实际块设置,并希望在这里建议保持性能下降。

目前,服务器需要大约5 GB内存,可供3-5名玩家使用。

谢谢大家

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);
    }

}

注意:标记的注释中有一些代码括号。这些都是扩展,它们是目标,但已经不习惯于降低性能。

1 个答案:

答案 0 :(得分:1)

尝试在更新块之间实现一小段等待时间。例如,玩家第一次触发块更新时,将条目存储在地图中,键入其UUID并使用当前时间的值。然后,在下次激活事件时,检查当前时间与上次激活事件之间已经过了一定时间段。如果未经过指定的时间,请不要做任何事情。即使您将其更改为每秒更新一次,也会比每次更新一次更新20倍。