Search code examples
javabukkit

Splitting Code into Different Classes [Bukkit/Spigot]


So I created this small plugin and I want to know how to move the code from inside onCommand to another class and call/execute that class inside onCommand. Please Help. Thanks

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

Solution

  • you can create new Class Commands and there will be:

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

    And in your MainClass you have to set CommandExecutor:

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