java - 将代码分成不同的类[Bukkit/Spigot]

原文 标签 java bukkit

因此,我创建了这个小插件,我想知道如何将代码从onCommand内部移动到另一个类,并在onCommand内部调用/执行该类。
请帮忙。谢谢

public class SkinStandoff extends JavaPlugin {
Block bEnd;
Location End;
Block ZeroBlock;
Location Zero;
Location ZeroEnd;

@Override
public void onEnable(){
}

public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
    if (cmd.getName().equalsIgnoreCase("build") && sender instanceof Player) {
        Player player = (Player) sender;
        Location start;
        int Count;
        Count = 1;

        start = player.getLocation();
        End = start.add(3, -1, 3);
        Zero = getEnd().add(1,0,0);

        bEnd = End.getBlock();
        bEnd.setType(Material.REDSTONE_BLOCK);
        do {
            Zero= Zero.add(1,0,0);
            ZeroBlock = Zero.getBlock();
            ZeroBlock.setType(Material.REDSTONE_BLOCK);
            Count++;
        } while (Count != 10);
        return true;
    }
    return false;
}

public Location getEnd(){
    return End;
}
public Location getZeroEnd(Location ZeroEnd){
    ZeroEnd = this.Zero.add(10,0,0);
    return ZeroEnd;
}
}

最佳答案

您可以创建新的类命令,并且将有:

public class Commands implements CommandExecutor {

    private MainClass plugin;

    public Commands(MainClass core) {
        this.plugin = core;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
your commands here
return true;
}
}


在MainClass中,必须设置CommandExecutor:

getCommand("command").setExecutor(new Commands(this));

关于java - 将代码分成不同的类[Bukkit/Spigot],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39434215/

相关文章:

java - "Exception in thread "主要 "java.lang.IndexOutOfBoundsException: Index: 0, Size: 0"与 ArrayList?

java - 红石颗粒不会改变颜色

java - Bukkit Java插件中的ClassNotFoundException

java - Bukkit-EventHandler方法出错

java - 子弹无限烟火

java - Bukkit-健康的名义

java - Hibernate管理@ManyToMany表的插入和顺序

java - 获取错误 : . ... 嵌套异常是 java.lang.NoSuchMethodError : org. apache.catalina.Context.addServletMapping(Ljava/lang/Stri

java - 将netbeans升级到7.2后出现错误

java - sql命令未正确结束(我的错误在哪里)