Solved Easy question about mob spawning

Discussion in 'Plugin Development' started by WHQ, Oct 28, 2014.

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

    WHQ

    Hi guys!

    Im new to coding (watched around 3 tutorials so far) and i have a simple question about spawning a mob.

    I have made this small plugin and i tested it out on my server but for some reason the creeper does not spawn? Everything else works fine.

    Code:java
    1.  
    2. public class GetRektMain extends JavaPlugin {
    3. public final Logger logger = Logger.getLogger("minecraft");
    4.  
    5. public void onDisable(){
    6. this.logger.info("GetRekt by WHQ has been disabled m8!");
    7. }
    8.  
    9.  
    10. public void onEnable(){
    11. this.logger.info("GetRekt by WHQ has been enabled m8!");
    12.  
    13. }
    14.  
    15. public boolean onCommand(CommandSender sender, Command cmd, String label, String [] args){
    16. Player p = (Player) sender;
    17. Player rekt = p.getServer().getPlayer(args[0]);
    18. Location rektloc = rekt.getLocation();
    19. EntityType creeper = EntityType.CREEPER;
    20.  
    21.  
    22.  
    23. if(cmd.getName().equalsIgnoreCase("rek")){
    24. if(args.length >= 2){
    25. p.sendMessage(ChatColor.DARK_RED+"Too many arguments m8! Try /rek <playername>");
    26. }
    27.  
    28. if(args.length == 1){
    29. if(p.getServer().getPlayer(args[0]) != null){
    30. rekt.sendMessage(ChatColor.GREEN+"Get Rekt m8!");
    31. rekt.getWorld().spawnEntity(rektloc, creeper);
    32. rekt.getWorld().playSound(rektloc, Sound.COW_HURT, 1, 1);
    33. }else{
    34. p.sendMessage(ChatColor.DARK_RED+"Can't rek player m8, is he online?");
    35. }
    36. }
    37. }
    38. return false;
    39. }
    40. }
    41.  


    Thanks in advance :)
     
  2. Offline

    Watto

    You need to set the command executor in your onEnable

    Code:java
    1. this.getCommand("rek").setExecutor(this);


    I'd suggest reading this if you have already.
     
    WHQ likes this.
  3. Offline

    FerusGrim

    First of all, stop watching YouTube tutorials. Please, for the love of God, and all that is holy. Read a book, learn Java. When you know Java, read the Bukkit JavaDocs.

    Why? Because most YouTube videos teach HORRIBLE practices. For example:
    Code:java
    1. public final Logger logger = Logger.getLogger("minecraft");
    Never use this method of logging. Not only does it not grant you the correct logger, but it's simpler to just use Server#getLogger. The correct way to get the logger for your Plugin is by using JavaPlugin#getLogger.

    Code:java
    1. this.logger.info("GetRekt by WHQ has been disabled m8!");
    2. this.logger.info("GetRekt by WHQ has been enabled m8!");

    These messages are unnecessary. Bukkit, at this point, already declares when a plugin is being enabled/disabled.

    Code:java
    1. Player p = (Player) sender;

    You're declaring a Sender as a Player, without making sure that the Sender is a player. Normally, you would surround this with an if() check, to see if the sender field is actually an instance of a Player.

    Code:java
    1. Player rekt = p.getServer().getPlayer(args[0]);

    Not only are you not making sure that args[] even has any arguments to use, but you're not checking if that method returns null. This will lead to both a ArrayIndexOutOfBoundsException and a NullPointerException, if used incorrectly.

    Code:java
    1. if(args.length >= 2){

    I suppose this isn't wrong as much as it is unconventional, but generallu you'd rather check if args.length() was greater-than 1, rather than greater-than-or-equal-to 2.

    Code:java
    1. if(args.length == 1){

    At this point, if you've already made sure that the arguments aren't greater than 1, and you've already attempted to make a call to args[0], you may as well skip this if() check. Otherwise, you'll get a null player and miss this call.

    I'm not trying to be mean. But, really... YouTube tutorials are not a good replacement for actual knowledge. It's easy to pick up a book, or download a .pdf file from the web, and learn. Java is a fun language - one that I have a passion for, and one that I think is easy for others to share that passion. Don't learn from outdated, overrated videos on YouTube.

    Also, don't learn Java by relying on the Bukkit API, the same way you don't want to learn Spanish by watching Mexican soap operas. I guess it may work for some people, but you'll never really understand some of the underlying methods until you try to work it out for yourself.
     
    Dragonphase, Aqua and Goblom like this.
  4. Offline

    WHQ

    FerusGrim

    Yeah i'm sorry about my messed up code (this is my first atempt of making a bukkit plugin) :(

    I will stop watching youtube tutorials and i will focus on actually learning java properly before trying to make bukkit plugins.
     
  5. Offline

    FerusGrim

    Good. I don't want you to be discouraged. Feel free to message me personally if you need any help, related to Bukkit or not. I'm sure others, if you're putting forth the effort to learn Java properly would offer the same support.
     
    WHQ likes this.
Thread Status:
Not open for further replies.

Share This Page