将代码拆分为不同的类[Bukkit / Spigot]

时间:2016-09-11 08:25:31

标签: 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;
}
}

1 个答案:

答案 0 :(得分:1)

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

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