Solved Get teams from onEnable()

Discussion in 'Spigot Plugin Development' started by Switch, May 28, 2016.

  1. Hello !
    I have register 2 Teams in the onEnable() method. Now i would like to get thes teams in my command "/ptf join <team>"
    What i have to do ?
    Thanks for your help :3

    Code:

    Code (Text):
    package com.switchmc.ptf;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.weather.WeatherChangeEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;


    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            System.out.println("PTF > Activé");
            getServer().getPluginManager().registerEvents(this, this);
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getMainScoreboard();
            Team skyr = board.registerNewTeam("SkyR");
            Team ct = board.registerNewTeam("CT");
            skyr.setPrefix("&3");
            ct.setPrefix("&c");
            skyr.setDisplayName("SkyR");
            skyr.setDisplayName("CT");
            ct.setAllowFriendlyFire(false);
            skyr.setAllowFriendlyFire(false);
            super.onEnable();
        }
       
        public void onDisable() {
            System.out.println("PTF > Desactivé");
            super.onEnable();
        }
       
        @EventHandler
        public void onFoodLevelChange (FoodLevelChangeEvent hunger){
            if(hunger.getEntityType() != EntityType.PLAYER)return;
                hunger.setCancelled(true);
           
        }
       
        @EventHandler
        public void onRainStart(WeatherChangeEvent weather){
            if (!weather.isCancelled()) {
                weather.setCancelled(true);
            }
        }
       
        @EventHandler
        public void onPlayerDamage(EntityDamageByEntityEvent e){
            e.setDamage(0.00);
            if(e.getCause() == DamageCause.VOID){
            }
                   
               
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("ptf")){
                if(args.length < 1){  
                    sender.sendMessage("§6--- §ePTF > 1.0 §6---");
                    sender.sendMessage("§e/ptf setlobby §f: §7§oPlacer le lobby");
                    sender.sendMessage("§e/ptf setspawn <SkyR/CT> §f: §7§oPlacer le spawn d'une équipe");
                    sender.sendMessage("§e/ptf join <SkyR/CT> §f: §7§oRejoindre une équipe");
                    return true;
                } else {
                    if (args[0].equalsIgnoreCase("join")){
                        if(args.length < 2){
                            sender.sendMessage("Essayez : /ptf join <SkyR/CT>");
                        } else {
                            if (args[1].equalsIgnoreCase("SkyR")){
                                sender.sendMessage("Tu as rejoint la SkyR !");
                            } else if (args[1].equalsIgnoreCase("CT")){
                                sender.sendMessage("Tu as rejoint la CT !");
                            } else {
                                sender.sendMessage("Veuillez entrer une team valide ! (CT/SkyR)");
                            }
                        }
                    } else {
                        sender.sendMessage("Sous-commande inconnue !");
                    }
                }
            }
            return false;
        }
       
       
    }

     
     
  2. Scoreboard board = Bukkit.getScoreboardManager();
    board.getTeam(String arg0);
     
  3. Thanks, but it don't work :/
    I have the error "An internal error occured..."

    And:

    org.bukkit.command.CommandException: Unhandled exception executing command 'ptf' in plugin PTF v0.1
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_91]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_91]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_91]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.scoreboard.CraftScoreboardManager cannot be cast to org.bukkit.scoreboard.Scoreboard
    at com.switchmc.ptf.Main.onCommand(Main.java:83) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more


    Code:
    Code (Text):
    package com.switchmc.ptf;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.FoodLevelChangeEvent;
    import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    import org.bukkit.event.weather.WeatherChangeEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;


    public class Main extends JavaPlugin implements Listener{
       
        public void onEnable() {
            System.out.println("PTF > Activé");
            getServer().getPluginManager().registerEvents(this, this);
            ScoreboardManager manager = Bukkit.getScoreboardManager();
            Scoreboard board = manager.getMainScoreboard();
            Team skyr = board.registerNewTeam("SkyR");
            Team ct = board.registerNewTeam("CT");
            skyr.setPrefix("&3");
            ct.setPrefix("&c");
            skyr.setDisplayName("SkyR");
            skyr.setDisplayName("CT");
            ct.setAllowFriendlyFire(false);
            skyr.setAllowFriendlyFire(false);
            super.onEnable();
        }
       
        public void onDisable() {
            System.out.println("PTF > Desactivé");
            super.onEnable();
        }
       
        @EventHandler
        public void onFoodLevelChange (FoodLevelChangeEvent hunger){
            if(hunger.getEntityType() != EntityType.PLAYER)return;
                hunger.setCancelled(true);
           
        }
       
        @EventHandler
        public void onRainStart(WeatherChangeEvent weather){
            if (!weather.isCancelled()) {
                weather.setCancelled(true);
            }
        }
       
        @EventHandler
        public void onPlayerDamage(EntityDamageByEntityEvent e){
            e.setDamage(0.00);
            if(e.getCause() == DamageCause.VOID){
            }
                   
               
           
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equalsIgnoreCase("ptf")){
                if(args.length < 1){  
                    sender.sendMessage("§6--- §ePTF > 1.0 §6---");
                    sender.sendMessage("§e/ptf setlobby §f: §7§oPlacer le lobby");
                    sender.sendMessage("§e/ptf setspawn <SkyR/CT> §f: §7§oPlacer le spawn d'une équipe");
                    sender.sendMessage("§e/ptf join <SkyR/CT> §f: §7§oRejoindre une équipe");
                    return true;
                } else {
                    if (args[0].equalsIgnoreCase("join")){
                        if(args.length < 2){
                            sender.sendMessage("Essayez : /ptf join <SkyR/CT>");
                        } else {
                            if (args[1].equalsIgnoreCase("SkyR")){
                                sender.sendMessage("Tu as rejoint la SkyR !");
                                Scoreboard board = (Scoreboard) Bukkit.getScoreboardManager();
                                Team skyr = board.getTeam("SkyR");
                                Player p = (Player) sender;
                                skyr.addPlayer(p);
                            } else if (args[1].equalsIgnoreCase("CT")){
                                sender.sendMessage("Tu as rejoint la CT !");
                                Scoreboard board = (Scoreboard) Bukkit.getScoreboardManager();
                                Team ct = board.getTeam("CT");
                                Player p = (Player) sender;
                                ct.addPlayer(p);
                               
                            } else {
                                sender.sendMessage("Veuillez entrer une team valide ! (CT/SkyR)");
                            }
                        }
                    } else {
                        sender.sendMessage("Sous-commande inconnue !");
                    }
                }
            }
            return false;
        }
       
       
    }

     
     
  4. 4chan

    Supporter

    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.scoreboard.CraftScoreboardManager cannot be cast to org.bukkit.scoreboard.Scoreboard
     
  5. Yes, but how can I resolve this problem ? :c
     
  6. 4chan

    Supporter

    you learn java
    then fix it
     
  7. Problem solved.