running basic code

Discussion in 'Plugin Development' started by Cool_Nick_Name, May 1, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Cool_Nick_Name

    Hello,
    I need to make a teams in my plugin, but I want the players to be added when they join the server (onPlayerJoin event) and that all works, but when i put the creating of the teams in the event it will re-create every time a player joins. so i tried putting in into onEnable, but the i couldn't call the teams from the onPlayerJoin event. How can i fix this?
     
  2. Online

    timtower Administrator Administrator Moderator

  3. Offline

    teej107

    Obtain a reference of your teams. If the event listener is in a different class, pass the team instance through the constructor of the listener class and assign the value to a field.
     
  4. Offline

    Cool_Nick_Name

    @teej107 no i don't like to make too much different classes so i do as most as possible in the main class.

    @timtower the code for the playerjoin event, the code for the teams, or just all the code?
     
  5. Online

    timtower Administrator Administrator Moderator

  6. Offline

    Cool_Nick_Name

    @timtower
    code (open)

    Code:
    package nl.yildri.BlockHunt;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.scoreboard.DisplaySlot;
    import org.bukkit.scoreboard.Objective;
    import org.bukkit.scoreboard.Score;
    import org.bukkit.scoreboard.Scoreboard;
    import org.bukkit.scoreboard.ScoreboardManager;
    import org.bukkit.scoreboard.Team;
    import org.kitteh.tag.AsyncPlayerReceiveNameTagEvent;
    
    
    public class BlockHunt extends JavaPlugin{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static BlockHunt plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            plugin = this;
            this.logger.info(pdfFile.getName() + " Has Been Disabled!");
        }
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            plugin = null;
            this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Been Enabled!");
           
           
        }
           
           
            @SuppressWarnings("deprecation")
            public boolean onCommand(org.bukkit.command.CommandSender sender, org.bukkit.command.Command cmd, String commandLabel, String[] args){
                Player player = (Player) sender;       
               
                if(commandLabel.equalsIgnoreCase("heal") || commandLabel.equalsIgnoreCase("h")){
                    if(args.length == 0){
                        player.sendMessage(ChatColor.GREEN + "So do you feel better now?!");
                    }else if(args.length == 1){
                        Player targetPlayer = player.getServer().getPlayer(args [0]);
                        player.sendMessage(ChatColor.GREEN + "So do you feel better now?!");
                    }
                }
                return false;
           
        }
            @SuppressWarnings("deprecation")
            @EventHandler
            (priority = EventPriority.HIGHEST)
           
            public void onPlayerJoin(PlayerJoinEvent event) {
                Player eplayer = event.getPlayer();
               
                ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Team Seeker = board.registerNewTeam("Seekers");
                Team Hider = board.registerNewTeam("Hiders");
                Seeker.setPrefix(ChatColor.RED + "Seeker ");
                Hider.setPrefix(ChatColor.AQUA + "Hider ");
                Hider.setAllowFriendlyFire(false);
                Seeker.setAllowFriendlyFire(false);
               
                Objective Teams = board.registerNewObjective("Test", "Players:");
                Teams.setDisplaySlot(DisplaySlot.SIDEBAR);
                Teams.setDisplayName(ChatColor.GREEN + "Players:");
               
                Score score = Teams.getScore(eplayer.getName());
                score.setScore(0);
               
                eplayer.setScoreboard(board);
               
                int oplayer = this.getServer().getOnlinePlayers().length;
               
                if (oplayer % 2 == 0) {
                      // even
                    Hider.addPlayer(eplayer);
                    eplayer.sendMessage(ChatColor.YELLOW  + "Hello" + ChatColor.BLUE + eplayer.getName() + ChatColor.YELLOW + "!");
                    eplayer.sendMessage(ChatColor.AQUA + "You joined Hiders team!");
                    } else {
                      // odd
                    Seeker.addPlayer(eplayer);
                    eplayer.sendMessage(ChatColor.YELLOW  + "Hello" + ChatColor.BLUE + eplayer.getName() + ChatColor.YELLOW + "!");
                    eplayer.sendMessage(ChatColor.RED + "You joined Seekers team!");
            }
            }
           
             @EventHandler
             public void onKill(PlayerDeathEvent e){
             String killed = e.getEntity().getName();
             String killer = e.getEntity().getKiller().getName();
             e.setDeathMessage(killed + ChatColor.YELLOW +" was slain by " + ChatColor.RESET + killer);
             }
           
    }
    


    At this point the teams load whenever a players join at the top of the PlayerJoinEvent
     
  7. @Cool_Nick_Name No need to log messages on enable and disable.

    Use bukkit's logger rather than Java's #getLogger()

    on disable and
    in on enable will cause memory leaks and any classes using the plugin variable to throw an NPE. If you want a lecture on it ask @nverdier he loves to give lectures ;)

    Use cmd.getName() instead of commandLabel, that gets the command, alias' etc. But commandLabel just gets the command.

    Check before casting player.

    Check if target player is not equal to null before using that player.
     
  8. Offline

    Cool_Nick_Name

    @bwfcwalshy thanks, i'll use that. but it doesn't solve my problem right? and i don't use the commands yet i just put that there so i wouln't forget.

    EDIT: btw how would I use that logger? should I edit
    Code:
    public final Logger logger = Logger.getLogger("Minecraft");
    ?
     
  9. @Cool_Nick_Name Another thing, I wouldn't create a new scoreboard everytime a player joins, create a scoreboard on enable.

    Doing that will fix your issue, also follow naming conventions when possible. This will save a lot of errors.

    #createNewTeam and #Team.addPlayer() will help you.
     
  10. Offline

    Evaluations

    Stop watching BCBroz.
     
  11. Offline

    Cool_Nick_Name

    @Evaluations tssss it was a long time ago, I just copy that code in evry new project becuase i'm lazy:p

    @bwfcwalshy I know I need to do that, but I started this topic because I don't know how!

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: May 1, 2015
  12. @Cool_Nick_Name
     
  13. Offline

    Cool_Nick_Name

    @bwfcwalshy
    that doesn't sound like java. and how does that work with the parts i already have? because that's new to me.
     
  14. Offline

    mythbusterma

    @Cool_Nick_Name

    He was using the notation incorrectly.

    A hashtag is used to indicate a member function of a class, so that it can be easily distinguished from a static method of a class in writing. E.g.

    ChatColor.translateAlternateColorCodes(...) refers to a static method
    Player#setHealth(...) refers to a member function

    Also,

    This goes against fundamental Object-Oriented design. You need different classes to represent things. Java is not a functional language.

    You need to learn Java before you can start programming for Bukkit.

    Here's the tutorial: The Java™ Tutorials
     
  15. Offline

    Cool_Nick_Name

    @mythbusterma

    I know my basic Java, and as i said
    I just didn't needed different classes so far, because i just started this project.
     
  16. Offline

    mythbusterma

  17. Offline

    Cool_Nick_Name

    @mythbusterma

    I want to create teams onEnable but i'm no able to call them back form my onPlayerJoinEvent
     
  18. Online

    timtower Administrator Administrator Moderator

  19. Offline

    Cool_Nick_Name

  20. Online

    timtower Administrator Administrator Moderator

    @Cool_Nick_Name A variable in the class instead of in a method. Then you can store and access the teams through there
     
  21. Offline

    Cool_Nick_Name

    @timtower I know it sounds realy stupid, but have no idea what you mean, except this one:
    should i make the
    Code:
    Scoreboard board = manager.getNewScoreboard();
    to something like:
    Code:
    public Scoreboard board = manager.getNewScoreboard();
    ? becuase I already tried that, didn't work:/
     
  22. Offline

    1Rogue

    no, it won't
     
    nverdier likes this.
  23. @1Rogue Sorry I did that at night. Ignore that part, the point of it was to say it would throw an NPE for all classes using it.
     
  24. Offline

    Cool_Nick_Name

    Okay, but back to the point:

    I have this code that creates the teams and stuff
    Code:
    ScoreboardManager manager = Bukkit.getScoreboardManager();
                Scoreboard board = manager.getNewScoreboard();
                Team Seeker = board.registerNewTeam("Seekers");
                Team Hider = board.registerNewTeam("Hiders");
                Seeker.setPrefix(ChatColor.RED + "Seeker ");
                Hider.setPrefix(ChatColor.AQUA + "Hider ");
                Hider.setAllowFriendlyFire(false);
                Seeker.setAllowFriendlyFire(false);
              
                Objective Teams = board.registerNewObjective("Test", "Players:");
                Teams.setDisplaySlot(DisplaySlot.SIDEBAR);
                Teams.setDisplayName(ChatColor.GREEN + "Players:");
    And this part thats sets the player score:

    Code:
    Score score = Teams.getScore(eplayer.getName());
                score.setScore(0);
    And this part thats divides the players between the teams, this has to be in the onPlayerJoinEvent:

    Code:
    if (oplayer % 2 == 0) {
                      // even
                    Hider.addPlayer(eplayer);
                    eplayer.sendMessage(ChatColor.YELLOW  + "Hello" + ChatColor.BLUE + eplayer.getName() + ChatColor.YELLOW + "!");
                    eplayer.sendMessage(ChatColor.AQUA + "You joined Hiders team!");
                    } else {
                      // odd
                    Seeker.addPlayer(eplayer);
                    eplayer.sendMessage(ChatColor.YELLOW  + "Hello" + ChatColor.BLUE + eplayer.getName() + ChatColor.YELLOW + "!");
                    eplayer.sendMessage(ChatColor.RED + "You joined Seekers team!");
            }
            }
    But notice that the last 2 codes use eplayer, wich is the player from the onPlayerJoinEvent.

    Wich part should i paste where to make it work, or what should i edit?
     
  25. Online

    timtower Administrator Administrator Moderator

    @Cool_Nick_Name The scoreboard, the teams, the objective maybe.
    Those can be played in fields instead of local variables.
    And try to start value names with lower cases. It makes it very annoying to read with uppercasing, makes me think that you have tons of statics.
     
  26. Offline

    Cool_Nick_Name

    @timtower and how would the code look when it are fields? because i have no idea.
     
  27. Online

    timtower Administrator Administrator Moderator

  28. Offline

    VortexGmer

    @Cool_Nick_Name Please don't tell me you forgot to implements Listener and register that! :oops:
     
  29. Offline

    Cool_Nick_Name

    @VortexGmer Don't wurry, I already figured it out, but now as I start my server the plugin crashes, but when i reload it works fine :O

    I'll make another post for that later.
     
Thread Status:
Not open for further replies.

Share This Page