Solved Whats wrong with this code !?!?

Discussion in 'Plugin Development' started by MineDoubleSpace, Aug 4, 2013.

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

    MineDoubleSpace

    Someone please tell me whats wrong with the code below, this is a simple plugin that "sethome" and "home". i tested it "sethome" command doesnt give any errors. but "home" command doesn't work
    but when i sethome manually with a bed and then "home" command work perfectly.

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    3. Player player = (Player) sender;
    4. if(cmd.getName().equalsIgnoreCase("sethome")){
    5. if(player.isOp()){
    6. player.sendMessage(ChatColor.BLUE + "Welcome to your new home!");
    7. player.setBedSpawnLocation(player.getLocation());
    8.  
    9. }else {
    10. player.sendMessage(ChatColor.RED + "No Permissions");
    11. }
    12. }
    13. if(cmd.getName().equalsIgnoreCase("Home")){
    14. player.sendMessage(ChatColor.GREEN + "Teleporting to home");
    15. player.teleport(player.getBedSpawnLocation());
    16. }
    17. return false;
    18.  
    19. }
    20. }


    anyone? please!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  2. Offline

    ZeusAllMighty11

    Please only bump your posts once every 24 hours.


    You didn't even specify what "didn't work", so we can't really help you. Are you getting errors in the console? Any weird crashes? Did your computer explode?
     
  3. try
    Code:java
    1. player.setBedSpawnLocation(player.getLocation(), true);


    seems like your version only sets bedspawn, if there is a valid bed.
     
  4. Offline

    JoshArgent

    MineDoubleSpace The code looks fine except you forgot to add 'return true;' when the command has been executed.
    Have you added the home command to your plugin.yml file?
     
  5. Offline

    MineDoubleSpace

    Adversarius TheGreenGamerHD JoshArgent


    Still getting this console error

    And plugin.yml is set, it sentMessage to the player, it just doesnt do the main job

    Code:
    22:55:05 [INFO] Preparing start region for level 2 (Seed: 3036716045621622727)
    22:55:05 [INFO] [Home] Enabling Home v1.0
    22:55:05 [INFO] Home Version 1.0 Has Beed Enabled!
    22:55:05 [SEVERE] Error occurred while enabling Home v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at me.mds.home.homeset.onEnable(homeset.java:24)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:457)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:381)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.jav
    a:282)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.
    java:264)
            at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:3
    13)
            at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:2
    90)
            at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:2
    50)
            at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.jav
    a:151)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :391)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    22:55:05 [INFO] Server permissions file permissions.yml is empty, ignoring it
    22:55:05 [INFO] Done (1.479s)! For help, type "help" or "?"
    22:55:06 [INFO] ----- Bukkit Auto Updater -----
     
  6. this error occurs in your onEnable().
    Need to see your code of it.
     
  7. Offline

    MineDoubleSpace

    Code:java
    1.  
    2. public void onDisable() {
    3. PluginDescriptionFile pdfFile = this.getDescription();
    4. this.logger.info(pdfFile.getName() + " Has been Disabled!");
    5. }
    6. public void onEnable() {
    7. PluginDescriptionFile pdfFile = this.getDescription();
    8. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Beed Enabled!");
    9. }
    10.  


    This is my OnEnable() Adversarius
     
  8. Then i would guess your logger-variable isnt initialized or created correctly.
     
  9. Offline

    MineDoubleSpace

    Code:java
    1. package me.mds.home;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class homeset extends JavaPlugin{
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public static homeset plugin;
    15.  
    16. public void onDisable() {
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " Has been Disabled!");
    19. }
    20. public void onEnable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Beed Enabled!");
    23. }
    24.  
    25. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    26. Player player = (Player) sender;
    27. if(cmd.getName().equalsIgnoreCase("sethome")){
    28. if(player.isOp()){
    29. player.sendMessage(ChatColor.BLUE + "Welcome to your new home!");
    30. player.setBedSpawnLocation(player.getLocation());
    31. }else {
    32. player.sendMessage(ChatColor.RED + "No Permissions");
    33. }
    34. }
    35. if(cmd.getName().equalsIgnoreCase("Home")){
    36. player.sendMessage(ChatColor.GREEN + "Teleporting to home");
    37. player.teleport(player.getBedSpawnLocation());
    38. }
    39. return true;
    40.  
    41. }
    42. }


    Here is the whole code :p Adversarius
     
  10. The exception says line 24 in your code.
    which line is it in the posted code? Because line 24 is empty here.
     
  11. Offline

    historio

    For what your doing your don't really need an OnEnable or OnDisable ?
     
  12. Offline

    Hoolean

  13. Offline

    MineDoubleSpace

    Code:
    name: Home
    main: me.mds.home.homeset
    version: 1.0
    author: MDS
    commands:
      home:
          description: teleport player to home
      sethome:
          description: set player home

    This is the plugin.yml pretty basic. Hoolean

    and i removed line 24 which was just a blank line and it stopped giving me the console error, but the plugin is still not working, i'm getting an internal error when i use "/home"

    Here is the problem i think what cause the error :
    when I "/sethome" bedLocation doesnt update.
    but when I set bed location manually by sleeping on a bed and using the command "/home"
    then it teleports me to the bed, historio Adversarius
     
  14. Offline

    Hoolean

    MineDoubleSpace

    Could you please post your new code and the full error! :)
     
  15. Offline

    historio

  16. did you changed your code like i said in post #4 to
    player.setBedSpawnLocation(player.getLocation(), true);
     
  17. Offline

    Hoolean

  18. Offline

    MineDoubleSpace

    Console error :
    Code:
    23:31:26 [INFO] MineDoubleSpace issued server command: /sethome
    23:31:28 [INFO] MineDoubleSpace issued server command: /home
    23:31:28 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'home
    ' in plugin Home v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    9)
            at org.bukkit.craftbukkit.v1_6_R2.CraftServer.dispatchCommand(CraftServe
    r.java:523)
            at net.minecraft.server.v1_6_R2.PlayerConnection.handleCommand(PlayerCon
    nection.java:964)
            at net.minecraft.server.v1_6_R2.PlayerConnection.chat(PlayerConnection.j
    ava:882)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :839)
            at net.minecraft.server.v1_6_R2.Packet3Chat.handle(SourceFile:49)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
            at org.bukkit.craftbukkit.v1_6_R2.entity.CraftPlayer.teleport(CraftPlaye
    r.java:385)
            at org.bukkit.craftbukkit.v1_6_R2.entity.CraftEntity.teleport(CraftEntit
    y.java:198)
            at me.mds.home.homeset.onCommand(homeset.java:36)
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
            ... 15 more
    >
    java code :
    Code:java
    1. package me.mds.home;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class homeset extends JavaPlugin{
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14. public static homeset plugin;
    15.  
    16. public void onDisable() {
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + " Has been Disabled!");
    19. }
    20. public void onEnable() {
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion() + " Has Beed Enabled!");
    23. }
    24. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    25. Player player = (Player) sender;
    26. if(cmd.getName().equalsIgnoreCase("sethome")){
    27. if(player.isOp()){
    28. player.sendMessage(ChatColor.BLUE + "Welcome to your new home!");
    29. player.setBedSpawnLocation(player.getLocation());
    30. }else {
    31. player.sendMessage(ChatColor.RED + "No Permissions");
    32. }
    33. }
    34. if(cmd.getName().equalsIgnoreCase("Home")){
    35. player.sendMessage(ChatColor.GREEN + "Teleporting to home");
    36. player.teleport(player.getBedSpawnLocation());
    37. }
    38. return true;
    39.  
    40. }
    41. }



    historio Adversarius
     
  19.  
  20. Offline

    MineDoubleSpace

    yes i did tried it but still didn't work


    That worked! thanks everyone for helping me! <33

    Hoolean Adversarius

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  21. Offline

    Hoolean

    Additionally to what Adversarius said, change:
    Code:
    if(cmd.getName().equalsIgnoreCase("Home")){
        player.sendMessage(ChatColor.GREEN + "Teleporting to home");
        player.teleport(player.getBedSpawnLocation());
    }
    to:
    Code:
    else if(cmd.getName().equalsIgnoreCase("home")){
        player.sendMessage(ChatColor.GREEN + "Teleporting to home");
        Location bedLocation = player.getBedSpawnLocation());
        if(bedLocation == null) {
            player.sendMessage(ChatColor.RED + "You don't have your home location set! Set it with /sethome or by sleeping!');
        } else {
            player.sendMessage(ChatColor.GREEN + "Teleporting to your home...");
            player.teleport(bedLocation);
        }
    }
     
  22. Offline

    MineDoubleSpace



    Thanks! I'm just starting on java.. Glad that you could help me <3
     
    Hoolean likes this.
  23. Offline

    historio

    Sorry for not helping D:
     
  24. Offline

    MineDoubleSpace

    Come on xD, you did help
     
  25. Offline

    historio

    Thanks
     
Thread Status:
Not open for further replies.

Share This Page