My first Plugin Questions

Discussion in 'Plugin Development' started by pc_h8r, Feb 6, 2013.

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

    pc_h8r

    I have a few questions about making my first plugin.

    1) Do I need to use multiple classes? (What are the advantages?)
    2) Does making a team death-match plugin sound hard?
     
  2. Offline

    triarry

    Hi pc_h8r, good luck on your first plugin! I hope you use the Bukkit wiki because it is an invaluable source of information.

    You'll find that once you start making your plugin, multiple classes will become necessary. I like using multiple classes to keep the pieces of my plugins organized. You will also come across some points during the coding process that are expedited by using multiple class files.

    Making a team death-match plugin wouldn't be all that hard if you already know a bit about Java or another language. Now, if you want to make a team death-match plugin that has good HCI and is helpful to both server owners and players, you will need to spend time and put a lot of effort into your plugin. I recommend starting small, and just making a plugin that works, and then expanding and making the plugin make sense.

    I.e, plan before coding (use pseudo code!) This will make the actual coding process a lot easier. Before you start giving players fancy weapons and armor, just try and nail down getting them onto two teams. Do it in a step by step process so that you don't get lost and you'll be all set :)

    Hope this helps!
     
  3. Offline

    pc_h8r

    Ok, thanks for the answers. But what is pseudo code?
     
  4. Offline

    ZeusAllMighty11

    Pseudo code is just a 'thought'

    For example, if I was explaining how to make a location , i might say:

    Location l is new location world x y z

    instead of

    Location l = new Location(world, x,y,z)
     
  5. Offline

    pc_h8r

    Oh, ok thanks for clearing that up for me.
     
  6. Offline

    pc_h8r

    One more question...

    how can I go about storing players to 2 team variable?

    I want this (psuedo coding)


    Code:
    on player join
        if red team > blue team
            send player to blue team
        else
            send player to red team
     
  7. Offline

    Johnzeh

    Store them in lists like so:

    Code:
    public List<String> redteam = new ArrayList<String>();
    public List<String> blueteam = new ArrayList<String>();
    Then just check their sizes like so:

    Code:
    if(redteam.size() <= blueteam.size()) {
        //Adds them to red.
    Then you could do

    Code:
    else if(redteam > blueteam.size()) {
        //Add them to blue.
     
  8. Offline

    chasechocolate

    Johnzeh You need an extra ".size()" after your second check. You could also choose a random team if the teams are equal:
    Code:java
    1. if(redTeam.size() == blueTeam.size()){
    2. Random random = new Random();
    3. boolean randomBoolean = random.nextBoolean();
    4.  
    5. if(randomBoolean){
    6. //Add them to red
    7. } else {
    8. //Add them to blue
    9. }
    10. }
     
  9. Offline

    pc_h8r

    Oh wow, thanks for the help!

    Now I can move forward!:)

    Well, what is the correct way of getting the event when a player logs in? Im trying to assign them a team when thy log in, and I have the

    Code:
    public String getName() {
    return player.getName();
    }
    and that is ready to get the name of the player who logs in and it will use that name and add it to the correct team.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  10. Offline

    gomeow

    Personally, I would have an enum for the teams, and and HashMap
     
  11. Offline

    hi_guy_5

    just make a player login listener the by making a new class and insted of putting
    extends JavaPlugin
    after the class put
    implements Listener

    @EventHandler
    public void playerlogin(PlayerLoginEvent event){
     
  12. Offline

    pc_h8r

    Cant get my plugin to launch:(

    Code:
    22:17:52 [SEVERE] Could not load 'plugins/MCTDM.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassCastException: class com.github.hackintoshman.mctdm.Main
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:184)
        at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305)
        at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230)
        at org.bukkit.craftbukkit.v1_4_R1.CraftServer.loadPlugins(CraftServer.java:239)
        at org.bukkit.craftbukkit.v1_4_R1.CraftServer.<init>(CraftServer.java:217)
        at net.minecraft.server.v1_4_R1.PlayerList.<init>(PlayerList.java:55)
        at net.minecraft.server.v1_4_R1.DedicatedPlayerList.<init>(SourceFile:11)
        at net.minecraft.server.v1_4_R1.DedicatedServer.init(DedicatedServer.java:104)
        at net.minecraft.server.v1_4_R1.MinecraftServer.run(MinecraftServer.java:399)
        at net.minecraft.server.v1_4_R1.ThreadServerApplication.run(SourceFile:849)
    Caused by: java.lang.ClassCastException: class com.github.hackintoshman.mctdm.Main
        at java.lang.Class.asSubclass(Class.java:3018)
        at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:174)
        ... 9 more
    
    Code:

    Code:
    package com.github.hackintoshman.mctdm;
     
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import org.bukkit.event.player.PlayerJoinEvent;
     
    public class Main {
       
       
       
        public int redScore;
        public int blueScore;
        Player player;
        public static List<String> redTeam = new ArrayList<String>();
        public static List<String> blueTeam = new ArrayList<String>();
        public static String playerJoin;
       
       
        public String getName() {
            return player.getName();
        }
       
        public void PlayerJoin (PlayerJoinEvent event){
            @SuppressWarnings("unused")
            String playerJoin = event.getPlayer().getName();
        }
       
        public static void main(String[] args) {
           
            if(redTeam.size() > blueTeam.size()){
               
                //Add to blueTeam
                blueTeam.add(playerJoin);
                broadcast(ChatColor.YELLOW + "[MCTDM]" + playerJoin + "has been added to the Blue Team!");
            }//End of blue size < red size if statement
           
            if(redTeam.size() < blueTeam.size()){
               
                //Add to redTeam
                redTeam.add(playerJoin);
                broadcast(ChatColor.YELLOW + "[MCTDM]" + playerJoin + "has been added to the Red Team!");
               
            }//End of red size < blue size if statement
           
            if(redTeam.size() == blueTeam.size()){
                  Random random = new Random();
                  boolean randomBoolean = random.nextBoolean();
               
                  if(randomBoolean){
                     
                    //Add them to red
                    redTeam.add(playerJoin);
                    broadcast(ChatColor.YELLOW + "[MCTDM]" + playerJoin + "has been added to the Red Team!");
                     
                  } else {
                     
                    //Add them to blue
                      blueTeam.add(playerJoin);
                    broadcast(ChatColor.YELLOW + "[MCTDM]" + playerJoin + "has been added to the Blue Team!");
                     
                  }
                }//End of equal team size if statement
     
           
           
           
           
        }//End of Method Main
     
        private static void broadcast(String string) {
            // TODO Auto-generated method stub
           
        }
     
    }//End of Class Main
    
    plugin.yml:

    Code:
    name: MCTDM
    main: com.github.hackintoshman.mctdm.Main
    version: 1.0
     
  13. Offline

    gomeow

    pc_h8r
    Make Main extend JavaPlugin
     
  14. Offline

    pc_h8r

    Ok thanks, I did that and now it loads!

    I have made the base of my plugin, but I have a problem. I want people to join and then have them automatically put into a team, but I cant get them into a team.


    Here is the main class:

    Code:java
    1. package com.github.hackintoshman.mctdm;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. import java.util.Random;
    8. import org.bukkit.event.player.PlayerJoinEvent;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. import org.bukkit.Bukkit;
    11.  
    12.  
    13. public class Main extends JavaPlugin{
    14.  
    15. public int redScore;
    16. public int blueScore;
    17. Player player;
    18. public static List<String> redTeam = new ArrayList<String>();
    19. public static List<String> blueTeam = new ArrayList<String>();
    20. public static String playerJoin;
    21.  
    22.  
    23.  
    24. public String PlayerJoin (PlayerJoinEvent event){
    25. String playerJoin = event.getPlayer().getName();
    26. return playerJoin;
    27. }
    28.  
    29. public static void main(String[] args) {
    30.  
    31. if(redTeam.size() > blueTeam.size()){
    32.  
    33. //Add to blueTeam
    34. blueTeam.add(playerJoin);
    35. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the Blue Team!");
    36. playerJoin = null;
    37.  
    38. }//End of blue size < red size if statement
    39.  
    40. if(redTeam.size() < blueTeam.size()){
    41.  
    42. //Add to redTeam
    43. redTeam.add(playerJoin);
    44. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the Red Team!");
    45. playerJoin = null;
    46.  
    47. }//End of red size < blue size if statement
    48.  
    49. if(redTeam.size() == blueTeam.size()){
    50. Random random = new Random();
    51. boolean randomBoolean = random.nextBoolean();
    52.  
    53. if(randomBoolean){
    54.  
    55. //Add them to red
    56. redTeam.add(playerJoin);
    57. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the" + ChatColor.RED + "Red Team!");
    58. playerJoin = null;
    59.  
    60. } else {
    61.  
    62. //Add them to blue
    63. blueTeam.add(playerJoin);
    64. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the" + ChatColor.BLUE + "Blue Team!");
    65. playerJoin = null;
    66.  
    67. }
    68. }//End of equal team size if statement
    69.  
    70.  
    71.  
    72.  
    73.  
    74. }//End of Method Main
    75.  
    76. }//End of Class Main
    77.  


    and my Commands class:


    Code:java
    1. package com.github.hackintoshman.mctdm;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10.  
    11.  
    12.  
    13. public class Commands {
    14.  
    15. private static List<String> redTeam = new ArrayList<String>();
    16. private static List<String> blueTeam = new ArrayList<String>();
    17.  
    18. public static List<String> getredTeam() {
    19. return redTeam;
    20. }
    21.  
    22. public static List<String> getblueTeam() {
    23. return blueTeam;
    24. }
    25.  
    26. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    27. if(cmd.getName().equalsIgnoreCase("team")){ // If the player typed /basic then do the following...
    28. if (redTeam.contains(sender)) {
    29. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "You are on the Red Team!");
    30. }
    31. if (blueTeam.contains(sender)) {
    32. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "You are on the Blue Team!");
    33. }
    34. if (redTeam.isEmpty() && blueTeam.isEmpty()){
    35. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "It doesnt work!");
    36. }
    37. return true;
    38. } //If this has happened the function will return true.
    39. // If this hasn't happened the a value of false will be returned.
    40. return false;
    41. }
    42.  
    43. }
    44.  


    and my plugin.yml:

    Code:java
    1.  
    2. name: MCTDM
    3. main: com.github.hackintoshman.mctdm.Main
    4. version: 1.0
    5. commands:
    6. team:
    7. description: Checks to see what team you are on!
    8. usage: /team
    9. score:
    10. description: Checks the Scores!
    11. usage: /score
    12. stats:
    13. description: Checks your statistics!
    14. usage: /stats
    15. list:
    16. description: Shows the list of players!
    17. usage: /list
    18.  


    havent added all of my commands in yet, I am just thinking ahead.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  15. Offline

    Tirelessly

    pc_h8r You shouldn't have a main method, you should have event listeners. You shouldn't be making your own methods that are implemented in bukkit (getName(), broadcastMessage()), you should be using bukkit's.
     
  16. Hey there!
    A few hints and tips that I've picked up.
    • The main class of a plugin should be the name of the plugin so that main in your plugin.yml looks like this "main: com.github.hackintoshman.mctdm.MCTDM" and the class will of course need renaming.
    • You have a main method in the plugin which appears to be trying to act as a constructor, but I can't tell. This is generally a bad idea and you should be looking to use events like onEnable to setup your plugin.
    • http://wiki.bukkit.org/Plugin_Tutorial - This is a great place to start to get your first plugin up and running.
     
  17. Offline

    teunie75

    Teleport player by:
    1. Gettings the location of something else, and teleport to there.
    2. Setting a x, y and z value for that player and teleport them to themselves, thats a simple thing to do.
     
  18. Offline

    pc_h8r

    Tirelessly

    Well, I think I took all of your suggestions:

    MCTDM class:
    Code:java
    1. package com.github.hackintoshman.mctdm;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.entity.Player;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7. import java.util.Random;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.Bukkit;
    10.  
    11.  
    12. public class MCTDM extends JavaPlugin{
    13.  
    14. public int redScore;
    15. public int blueScore;
    16. static Player playerJoin;
    17. public static List<Player> redTeam = new ArrayList<Player>();
    18. public static List<Player> blueTeam = new ArrayList<Player>();
    19.  
    20. public Player getplayerJoin() {
    21. return playerJoin;
    22. }
    23.  
    24.  
    25. public static void main(String[] args) {
    26.  
    27. if(redTeam.size() > blueTeam.size()){
    28.  
    29. //Add to blueTeam
    30. blueTeam.add(playerJoin);
    31. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the Blue Team!");
    32. playerJoin = null;
    33.  
    34. }//End of blue size < red size if statement
    35.  
    36. if(redTeam.size() < blueTeam.size()){
    37.  
    38. //Add to redTeam
    39. redTeam.add(playerJoin);
    40. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the Red Team!");
    41. playerJoin = null;
    42.  
    43. }//End of red size < blue size if statement
    44.  
    45. if(redTeam.size() == blueTeam.size()){
    46. Random random = new Random();
    47. boolean randomBoolean = random.nextBoolean();
    48.  
    49. if(randomBoolean){
    50.  
    51. //Add them to red
    52. redTeam.add(playerJoin);
    53. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the" + ChatColor.RED + "Red Team!");
    54. playerJoin = null;
    55.  
    56. } else {
    57.  
    58. //Add them to blue
    59. blueTeam.add(playerJoin);
    60. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM]" + playerJoin + " has been added to the" + ChatColor.BLUE + "Blue Team!");
    61. playerJoin = null;
    62.  
    63. }
    64. }//End of equal team size if statement
    65.  
    66.  
    67.  
    68.  
    69.  
    70. }//End of Method Main
    71.  
    72. }//End of Class Main
    73.  


    player join listener class

    Code:java
    1.  
    2. package com.github.hackintoshman.mctdm;
    3.  
    4. import org.bukkit.event.player.PlayerJoinEvent;
    5. import org.bukkit.entity.Player;
    6.  
    7. public class MCTDMPlayerJoinListener {
    8.  
    9. static Player playerJoin;
    10.  
    11. public String PlayerJoin (PlayerJoinEvent event){
    12. String playerJoin = event.getPlayer().getName();
    13. return playerJoin;
    14.  
    15. }
    16.  


    Commands class:


    Code:java
    1.  
    2.  
    3. package com.github.hackintoshman.mctdm;
    4.  
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.command.Command;
    11. import org.bukkit.command.CommandSender;
    12. import org.bukkit.entity.Player;
    13.  
    14.  
    15.  
    16. public class Commands {
    17.  
    18. private static List<Player> redTeam = new ArrayList<Player>();
    19. private static List<Player> blueTeam = new ArrayList<Player>();
    20.  
    21. public static List<Player> getredTeam() {
    22. return redTeam;
    23. }
    24.  
    25. public static List<Player> getblueTeam() {
    26. return blueTeam;
    27. }
    28.  
    29.  
    30.  
    31. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    32. if(cmd.getName().equalsIgnoreCase("team")){ // If the player typed /basic then do the following...
    33. if (redTeam.contains(sender)) {
    34. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "You are on the Red Team!");
    35. }
    36. if (blueTeam.contains(sender)) {
    37. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "You are on the Blue Team!");
    38. }
    39. if (redTeam.isEmpty() && blueTeam.isEmpty()){
    40. Bukkit.broadcastMessage(ChatColor.YELLOW + "[MCTDM] " + "It doesnt work!");
    41. System.out.println("Failed");
    42. }
    43. return true;
    44. } //If this has happened the function will return true.
    45. // If this hasn't happened the a value of false will be returned.
    46. return false;
    47. }
    48.  
    49. }
    50.  
    51.  
    52. }
    53.  

    When people join. they still dont get added to an array, and when i do /team, it just prints /team in the chat. Plaease help with your wonderful answers:)
     
  19. Offline

    Tirelessly

Thread Status:
Not open for further replies.

Share This Page