My Own /setspawn & /spawn Wont work! HELP

Discussion in 'Plugin Development' started by BechGameplay, Apr 7, 2014.

Thread Status:
Not open for further replies.
  1. Hey Guys! I am trying to make a plugin, that makes players tp to spawn.
    i got some commands:
    /towny = Tp you to the set spawn
    /settowny = Sets the spawn

    i really need some help beacuse it wont read my /towny command!
    it reads my /settowny command and it works but when i am trying to /towny it will do nothing
    PLEASE HELP!

    My Code:
    Code:java
    1. package me.BechGameplay.townytp;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.World;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12.  
    13. public class main extends JavaPlugin{
    14. public final Logger logger = Logger.getLogger("Minecraft");
    15. public static main plugin;
    16.  
    17. @Override
    18. public void onEnable(){
    19. this.getConfig().options().copyDefaults(true);
    20. this.saveConfig();
    21. }
    22.  
    23. @Override
    24. public void onDisable(){
    25.  
    26.  
    27. this.saveConfig();
    28. }
    29.  
    30. public boolean onCommand(CommandSender sender, Command cmd,
    31. String CommandLabel, String[] args){
    32. Player player = (Player) sender;
    33.  
    34. if(cmd.getName().equalsIgnoreCase("settowny")){
    35.  
    36. if(sender instanceof Player){
    37.  
    38. if(player.hasPermission("townytp.settowny")){
    39.  
    40. this.getConfig().set("Spawn" + ".X", player.getLocation().getBlockX());
    41. this.getConfig().set("Spawn" + ".Y", player.getLocation().getBlockY());
    42. this.getConfig().set("Spawn" + ".Z", player.getLocation().getBlockZ());
    43. this.getConfig().set("Spawn" + ".Yaw", player.getLocation().getYaw());
    44. this.getConfig().set("Spawn" + ".Pitch", player.getLocation().getPitch());
    45. this.getConfig().set("Spawn" + ".World", player.getLocation().getWorld());
    46.  
    47. player.sendMessage(ChatColor.GREEN + "Spawn set!");
    48.  
    49. }else if((cmd.getName().equalsIgnoreCase("towny"))){
    50.  
    51. if(sender instanceof Player){
    52.  
    53. if(player.hasPermission("townytp.towny")){
    54.  
    55. int spawnX = this.getConfig().getInt("Spawn" + ".X");
    56. int spawnY = this.getConfig().getInt("Spawn" + ".Y");
    57. int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
    58. int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
    59. int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
    60. Object world = this.getConfig().get("Spawn" + ".World");
    61.  
    62. Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
    63.  
    64. player.teleport(spawn);
    65.  
    66. player.sendMessage(ChatColor.GREEN + "Velkommen Til Towny Spawn!");
    67.  
    68. }
    69. }
    70. }
    71. }
    72. }
    73. return false;
    74. }
    75. }


    MY PLUGIN.YML
    Code:
    name: TownyTp
    version: 1.0
    main: me.BechGameplay.townytp.main
    author: BechGameplay
    website: www.google.com
    commands:
        towny:
            description: tp dig til townyspawn
            permission: townytp.towny
        settowny:
            description: set Spawn
            permission: townytp.settowny
           
    permissions:
      townytp.settowny:
        description: Set Towny Spawn
        default: op
      townytp.towny:
        description: Tp Dig Til Towny
        default: op
    PLEASE HELP ME!
     
  2. Offline

    Arcticmike

    Remove the else and put the towny command on the next line
    Code:java
    1. }else if((cmd.getName().equalsIgnoreCase("towny"))){
    2.  
     
  3. Offline

    Arcticmike

    Okay there i see it here's a fix
    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd,
    2. String CommandLabel, String[] args){
    3. Player player = (Player) sender;
    4.  
    5. if(cmd.getName().equalsIgnoreCase("settowny")){
    6.  
    7. if(sender instanceof Player){
    8.  
    9. if(player.hasPermission("townytp.settowny")){
    10.  
    11. this.getConfig().set("Spawn" + ".X", player.getLocation().getBlockX());
    12. this.getConfig().set("Spawn" + ".Y", player.getLocation().getBlockY());
    13. this.getConfig().set("Spawn" + ".Z", player.getLocation().getBlockZ());
    14. this.getConfig().set("Spawn" + ".Yaw", player.getLocation().getYaw());
    15. this.getConfig().set("Spawn" + ".Pitch", player.getLocation().getPitch());
    16. this.getConfig().set("Spawn" + ".World", player.getLocation().getWorld());
    17.  
    18. player.sendMessage(ChatColor.GREEN + "Spawn set!");
    19.  
    20. }
    21. }
    22. } else if((cmd.getName().equalsIgnoreCase("towny"))){
    23.  
    24. if(sender instanceof Player){
    25.  
    26. if(player.hasPermission("townytp.towny")){
    27.  
    28. int spawnX = this.getConfig().getInt("Spawn" + ".X");
    29. int spawnY = this.getConfig().getInt("Spawn" + ".Y");
    30. int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
    31. int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
    32. int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
    33. Object world = this.getConfig().get("Spawn" + ".World");
    34.  
    35. Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
    36.  
    37. player.teleport(spawn);
    38.  
    39. player.sendMessage(ChatColor.GREEN + "Velkommen Til Towny Spawn!");
    40.  
    41. }
    42. }
    43. }
    44. return false;
    45. }
     
  4. Arcticmike

    I will try it out. Thx! I will Tell you if it Works xD
     
  5. Arcticmike
    Can you help me how to make Cooldown and Delay on the Teleport!??
     
  6. Offline

    Brixishuge

    BechGameplay Please, just please, use google or any other search engine before asking. It's not that we won'te help, problem is that you will not learn how to do it. Learn it by yourself and if you really get stuck somewhere I'm sure there will be someone to help you with errors you can't solve.
    Here's what you are asking for:
    http://wiki.bukkit.org/Scheduler_Programming
     
  7. Thx! I know but i couldnt find anything on the net!
    Thx For Helping! :)
     
  8. Offline

    Arcticmike

    Well did it work?
     
  9. Yes it worked so Fine! Thx You!
     
  10. Offline

    MooshViolet

    BechGameplay To make a delay, just make a bukkit runnable. This reads ticks. I believe 20 ticks is a second so if you want a 5 second delay, you would have to make a new runnable and set it to 100 ticks
     
  11. Offline

    coasterman10

    You may want to be a little more wary of YouTube tutorials. Right away I see this:
    Code:java
    1. public final Logger logger = Logger.getLogger("Minecraft");
    2. public static main plugin;


    You are also casting sender to a Player before your instanceof check. You shouldn't declare a Player object until after the instanceof check.

    You're saving the world object to the config and trying to read it back. When saving locations, you don't actually want to save the whole world; you just want to save its name, and Bukkit will get the world for you. To properly save the world's name:
    Code:java
    1. getConfig().set("Spawn" + ".World", player.getLocation().getWorld().getName());

    To retrieve the world from the config:
    Code:java
    1. String worldName = getConfig().getString("Spawn" + ".World");
    2. World world = getServer().getWorld(worldName);


    Now that we've cleared up these issues, the actual problem with your command setup is that you are checking for your second command within the conditional block for the first one. After the "player.sendMessage(ChatColor.GREEN + "Spawn set!");" line, you are 3 conditionals deeper into the code. To get back to the main onCommand method body, you need to close all three brackets. If you're using Eclipse or another IDE, you should notice how it likes to tab back to the original indentation.

    After making these changes, the code should follow this form:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("settowny")) {
    2. if (sender instanceof Player) {
    3. Player player = (Player) sender;
    4. if (player.hasPermission("townytp.settowny")) {
    5. // ...
    6. }
    7. }
    8. } else if ((cmd.getName().equalsIgnoreCase("towny"))) {
    9. if (sender instanceof Player) {
    10. Player player = (Player) sender;
    11. if (player.hasPermission("townytp.towny")) {
    12. // ...
    13. }
    14. }
    15. }
    16. // ...
     
  12. Offline

    Plo124

    Because you are likely using 'Towny' as a plugin on your server, the command /towny is most likely already in use.
     
Thread Status:
Not open for further replies.

Share This Page