2014-07-27 81 views
2

我不知道我怎麼會從不同的@EventHandler得到一個變量。我的變量在這個實例中是一個數組列表。Bukkit - 從其他@EventHandlers獲取變量?

我的代碼如下:

@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它無法解析變量「打造」這是我在以前的@EventHandler定義。那麼我怎麼才能做到這一點呢?

+0

的問題是變量的作用域。 – BitNinja

回答

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