gpt4 book ai didi

java - 设置 Blocks Bukkit 性能友好

转载 作者:行者123 更新时间:2023-11-30 10:33:53 25 4
gpt4 key购买 nike

我目前正在编写一个游戏,其中包括大量的 block 设置。当玩家移动时,一条彩色玻璃路会引导并跟随他的路,并在 5 秒后消失。

我已经检查过,如果没有设置方 block ,则只设置方 block ,如果玩家实际上将他的位置更改到另一个方 block ,则只检查方 block 。

我也尝试用 sendBlockChange 做到这一点,但首先,这对性能没有帮助,因为 sendBlockChange 必须应用于每个在线玩家,其次,反作弊插件会变得疯狂,我没有时间编写新的 NCP。

所以我在这里发布了 moveEvent 期间的实际 block 设置,并且很乐意在这里提出建议以降低性能。

目前,一台服务器需要大约 5 GB RAM 供 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);
}

}

注意:注释中有部分代码括号。这些都是扩展,它们是目标,但尚未用于降低性能。

最佳答案

尝试在更新 block 之间实现一个小的等待时间。例如,当玩家第一次触发 block 更新时,在 map 中存储一个条目,以他们的 UUID 为键并使用当前时间的值。然后在下次激活事件时,检查当前时间和上次激活事件之间是否经过了某个时间段。如果指定的时间还没有过去,则不要执行任何操作。即使您将其更改为仍然每秒更新一次,这也会比每刻更新一次提高 20 倍。

关于java - 设置 Blocks Bukkit 性能友好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com