事件错误

时间:2014-05-25 20:20:20

标签: bukkit eventhandler

使用CraftBukkit 1.7.9

您好,

每当我输入“Minigame”标志时,它就无法工作。

这是我的代码:

package me.pvpcraftnetwork.customjoinsigns;


import java.util.HashMap;

import org.bukkit.Location;
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.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;


public class Main extends JavaPlugin implements Listener {
        public final HashMap<Location, String> signs =  new HashMap<Location, String>();

        public static Main plugin;

        public void onEnable(){
            plugin = this;
            getLogger().info("[PvpCraft Network] {Custom Join Signs} has been enabled!");
        }

        public void onDisable() {
            plugin = null;
            getLogger().info("[PvpCraft Network] {Custom Join Signs} has been disabled!");
        } 
        @EventHandler
        public void onSignChange(SignChangeEvent event){
            if(event.getLine(0).equalsIgnoreCase("Minigame")) {
                signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
                event.getPlayer().sendMessage("[PvpCraft Network] {Custom Join Signs} Custom Join Sign created! Now type in: /cjs [Custom Join Sign Arena Number] setarenaname");
            }
        }

        @EventHandler
        public void onBlockBreak(BlockBreakEvent event){
            Player player = event.getPlayer();  
            if(signs.containsKey(event.getBlock().getLocation()) && !signs.containsValue(event.getPlayer().getName()) || !player.isOp()) {
                event.setCancelled(true);   
            } else {
                signs.remove(event.getBlock().getLocation());
            }
        }

        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(signs.containsKey(event.getClickedBlock().getLocation())) {
                player.teleport(player.getWorld().getSpawnLocation());
            }
        }
    }

在第一个事件中,我想要这样:

每当我输入“Minigame”符号时,它都会发送消息。

但是,我也想要它“if(player.isOp());”(如果播放器是服务器上的操作员),我不知道放在哪里。

任何帮助?

1 个答案:

答案 0 :(得分:0)

这是因为你没有注册活动。在Main班级(extends JavaPlugin)中,您需要在onEnable()中添加此内容:

this.getServer().getPluginManager().registerEvents(this, new <listener class>());

因此,在您的情况下,您应该添加:

this.getServer().getPluginManager().registerEvents(this, this);

因此,您的onEnable()onDisable()方法应如下所示:

public void onEnable(){
  plugin = this;

  this.getServer().getPluginManager().registerEvents(this, this);

  getLogger().info("[PvpCraft Network] {Custom Join Signs} has been enabled!");
}

public void onDisable() {
  plugin = null;
  getLogger().info("[PvpCraft Network] {Custom Join Signs} has been disabled!");
}

你应该像这样if(player.isOp())

@EventHandler
public void onSignChange(SignChangeEvent event){
  boolean isOp = event.getPlayer().isOp();//I don't know what you want to do with this, so, you could just use this boolean to check if the player is an op or not
  if(event.getLine(0).equalsIgnoreCase("Minigame")) {
    signs.put(event.getBlock().getLocation(), event.getPlayer().getName());
    event.getPlayer().sendMessage("[PvpCraft Network] {Custom Join Signs} Custom Join Sign created! Now type in: /cjs [Custom Join Sign Arena Number] setarenaname");
  }
}