Bukkit我的部分代码仅适用于第一人

时间:2014-08-04 18:51:02

标签: java minecraft bukkit

我的代码有问题,因为它不能正常工作。这部分代码在第一个播放器

上执行时有效
@EventHandler
public void onMove(PlayerMoveEvent event) {
    Player player = event.getPlayer();
    if (plugin.getPlayerEvent(player).equals("spleef")) {
        if (plugin.getInfo().eventstarted) {
            Material b = player.getLocation().getBlock().getType();
            if (b.equals(Material.STATIONARY_WATER) || b.equals(Material.WATER) || event.getPlayer().getLocation().getY() <= 0) {
                if (!lul.contains(player.getName()) && !ended) {
                    ended = true;
                    lul.add(player.getName());
                    plugin.getMethods().basicLose(player);
                }
            }
        }
    }
}

当第二个玩家接触水时没有任何反应。没有错误。这是完整的代码:

package Handlers.EventHandlers.SimpleEvents;

import Mains.MiniEvents;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerMoveEvent;

import java.util.ArrayList;
import java.util.HashSet;

public class Spleef implements Listener {
    public final ArrayList<Location> block = new ArrayList<>();
    public final ArrayList<Location> block2 = new ArrayList<>();
    public static final HashSet<String> lul = new HashSet<>();
    public MiniEvents plugin;

    public Spleef(MiniEvents plugin) {
        this.plugin = plugin;
    }

    @EventHandler
    public void onBreak(final BlockBreakEvent event) {
        final Player player = event.getPlayer();
        if (plugin.getPlayerEvent(player).equals("spleef")) {
            if (plugin.getInfo().sbefore.contains(event.getPlayer().getName())) {
                event.setCancelled(true);
            } else {
                final Block b = event.getBlock();
                if (player.getItemInHand() != null && !player.getItemInHand().getType().equals(Material.AIR)) {
                    if (b.getType().equals(Material.SNOW_BLOCK)) {
                        event.setCancelled(true);
                        player.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, 80);
                        b.setType(Material.AIR);
                        block.add(b.getLocation());
                        block2.add(b.getLocation());
                    } else {
                        event.setCancelled(true);
                    }
                } else {
                    event.setCancelled(true);
                }
            }
        }
    }

    public static boolean ended = false;

    @EventHandler
    public void onMove(PlayerMoveEvent event) {
        Player player = event.getPlayer();
        if (plugin.getPlayerEvent(player).equals("spleef")) {
            if (plugin.getInfo().eventstarted) {
                Material b = player.getLocation().getBlock().getType();
                if (b.equals(Material.STATIONARY_WATER) || b.equals(Material.WATER) || event.getPlayer().getLocation().getY() <= 0) {
                    if (!lul.contains(player.getName()) && !ended) {
                        ended = true;
                        lul.add(player.getName());
                        plugin.getMethods().basicLose(player);
                    }
                }
            }
        }
    }
}

我很确定'结束'在第二个玩家是真的,所以它不会这样做。

1 个答案:

答案 0 :(得分:0)

这是因为布尔ended,你设置为true,但永远不能重新设置为false。因此,代码的执行在此处停止:

if (!lul.contains(player.getName()) && !ended) {

因为ended现在是真的。

要解决此问题,您可以使用HashMapHashMap存储值来表示密钥。你必须确保你永远不会在HashMaps中存储Player对象,因为它会在玩家离开时导致内存泄漏,因此,你应该使用玩家的UUID(你也可以使用他们的名字,但它可能如果玩家的用户名在1.8)中发生变化,则会导致错误:

Map<UUID, Boolean> ended = new HashMap<UUID, Boolean>();

获得价值,你可以这样做:

ended.get(player.getUniqueId();

请确保在获取之前检查HashMap是否包含密钥,否则您可能会收到NullPointerException:

if(ended.containsKey(player.getUniqueId())

要将值放入地图,您可以使用:

ended.put(player.getUniqueId(), boolean);

所以,这就是你的活动应该是什么样的:

public static Map<UUID, Boolean> ended = new HashMap<UUID, Boolean>();

@EventHandler
public void onMove(PlayerMoveEvent event) {
    Player player = event.getPlayer();
    if (plugin.getPlayerEvent(player).equals("spleef")) {
        if (plugin.getInfo().eventstarted) {
            Material b = player.getLocation().getBlock().getType();
            if (b.equals(Material.STATIONARY_WATER) || b.equals(Material.WATER) || event.getPlayer().getLocation().getY() <= 0) {
                boolean end = false;
                if(ended.containsKey(player.getUniqueId()){
                    end = ended.get(player.getUniqueId();
                }
                //else{} we don't want to do anything if ended does not contain the player's UUID!
                if (!lul.contains(player.getName()) && !end){
                    ended.put(player.getUniqueId(), true);
                    lul.add(player.getName());
                    plugin.getMethods().basicLose(player);
                }
            }
        }
    }
}