我想知道如何从不同的@EventHandler获取变量。我在这个实例中的变量是一个数组列表。
我的代码如下:
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
ArrayList<String> build = new ArrayList();
Player player = event.getPlayer();
if(!player.isOp()) {
event.setCancelled(true);
}else if(!build.contains(player)) {
event.setCancelled(true);
}else if(build.contains(player)) {
event.setCancelled(false);
}
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
event.setJoinMessage("§4§l>>§a§l " + player + " §b§ljoined! §4§l<<");
if(build.contains(player)) {
build.remove(player);
现在,在第二个@EventHandler上,它无法解析变量&#34; build&#34;这是我在之前的@EventHandler中定义的内容。那我怎么能这样做呢?
答案 0 :(得分:2)
build
是一个局部变量,仅在onBlockBreak(BlockBreakEvent event)
方法中可用。
你想要做的是在方法之外声明一个变量,并用它的onEnable()
方法初始化它。您还应该执行KingFaris10所提到的并存储播放器的UUID。您还需要将玩家添加到ArrayList。
private ArrayList<UUID> build;
@Override
public void onEnable () {
build = new ArrayList<UUID>();
}
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
Player player = event.getPlayer();
//Following derived from KingFaris10
if (!player.isOp() || !build.contains(player.getUUID())) event.setCancelled(true);
else event.setCancelled(false);
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
event.setJoinMessage("§4§l>>§a§l " + player + " §b§ljoined! §4§l<<");
UUID id = player.getUUID();
if(build.contains(id)) {
build.remove(id);