Solved Teleporting PLayers/setting warps

Discussion in 'Plugin Development' started by shades161, Mar 26, 2015.

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

    shades161

    I'm a new developer and am trying to make a plugin that will allow you to set a hub, lobby, and a spawn. The /spawn and /setspawn commands are properly working. My problem is the /hub /sethub /lobby /setlobby
    When a player issues /sethub it works properly and sets the coords in the config. But when a player issues /hub it errors (no stacktrace), won't teleport the player to the hub and instead tells them that it's empty and to use /sethub even though the hub is already set.
    HubCommands class (open)

    Code:
    package com.cloudcraftgaming.spawnjoin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    public class HubCommands
      implements CommandExecutor
    {
      Main plugin;
     
      public HubCommands(Main instance)
      {
        this.plugin = instance;
      }
     
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if ((sender instanceof Player))
        {
          Player p = (Player)sender;
          if ((cmd.getName().equalsIgnoreCase("sethub")) &&
            (p.hasPermission("SpawnJoin.sethub")) &&
            (this.plugin.getConfig().getString("HubEnabled").equalsIgnoreCase("True")))
          {
            this.plugin.getConfig().set("HUB.world", p.getLocation().getWorld().getName());
            this.plugin.getConfig().set("HUB.x", Double.valueOf(p.getLocation().getX()));
            this.plugin.getConfig().set("HUB.y", Double.valueOf(p.getLocation().getY()));
            this.plugin.getConfig().set("HUB.z", Double.valueOf(p.getLocation().getZ()));
            this.plugin.getConfig().set("HUB.yaw", Double.valueOf(p.getLocation().getYaw()));
            this.plugin.getConfig().set("HUB.pitch", Double.valueOf(p.getLocation().getPitch()));
            this.plugin.saveConfig();
            p.sendMessage(ChatColor.GOLD + "Hub saved!");
          }
        }
        else if ((sender instanceof Player))
        {
          Player p = (Player)sender;
          if ((cmd.getName().equalsIgnoreCase("hub")) &&
            (p.hasPermission("SpawnJoin.hub")) &&
            (this.plugin.getConfig().getString("HubEnabled").equalsIgnoreCase("True")))
          {
            World w = Bukkit.getServer().getWorld(this.plugin.getConfig().getString("HUB.world"));
            double x = this.plugin.getConfig().getDouble("HUB.x");
            double y = this.plugin.getConfig().getDouble("HUB.y");
            double z = this.plugin.getConfig().getDouble("HUB.z");
            int ya = this.plugin.getConfig().getInt("HUB.yaw");
            int pi = this.plugin.getConfig().getInt("HUB.pitch");
            p.teleport(new Location(w, x, y, z, ya, pi));
            p.sendMessage(ChatColor.GOLD + "You have been warped to the Hub!");
          }
        }
        return false;
      }
    }

    config.yml (open)

    Code:
    DO NOT DELETE: SpawnJoin is developed and managed by Shades161
    Config Version: '2.0'
    HubEnabled: 'True'
    LobbyEnabled: 'False'
    Join:
      Spawn: 'True'
      Hub: 'False'
      Lobby: 'False'
    HUB:
      world: world
      x: 149.1400900411232
      y: 74.0
      z: 268.6680471198019
      yaw: 0.0
      pitch: 0.0
    LOBBY:
      world: world
      x: 149.81801466701583
      y: 73.0
      z: 263.09876810634
      yaw: -96.89997100830078
      pitch: 21.449996948242188
    

    I have the commands and permissions properly registered in the Main class and in the plugin.yml
    (the LobbyCommands class is nearly identical to the HubCommands class but everything is changed to lobby.)
     
  2. Offline

    EpicCraft21

    Can't you just get the location instead of x, y, z, etc.?
     
  3. Offline

    shades161

    Would that still allow it to be set in the config?
     
  4. Offline

    EpicCraft21

    To be honest, I don't know, I don't use config a lot.
     
  5. Offline

    shades161

    I want the hub and lobby to be able to be set ingame or in the config using coords which is why it did the x, y, z stuff. But the /hub and /lobby commands don't seem to recognize what's in the config or tp the player to that location.
     
  6. Offline

    EpicCraft21

    Something like this should work:
    Code:
    Location loc;
    If cmd.getname equals set hub then
    loc = player.getLocation();//get the player first
    
    If cmd.getname equals hub
    player.teleport(loc);
    
     
    shades161 likes this.
  7. Offline

    mythbusterma

    @shades161

    Your logic is completely broken.

    Do you understand what if()...else if ()....else implies? Take a look at your code and try again.
     
    shades161 likes this.
  8. Offline

    shades161

    @mythbusterma @EpicCraft21
    I have resolved the issue. Saw my mistakes with the if, else if, and else statements.
    HubCommands class (open)

    Code:
    package com.cloudcraftgaming.spawnjoin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Server;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    
    public class HubCommands
      implements CommandExecutor
    {
      Main plugin;
     
      public HubCommands(Main instance)
      {
        this.plugin = instance;
      }
     
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (cmd.getName().equalsIgnoreCase("sethub"))
        {
          if (!(sender instanceof Player))
          {
            sender.sendMessage("Only players can do this!");
          }
          else
          {
            Player p = (Player)sender;
            if ((p.hasPermission("SpawnJoin.sethub")) && (this.plugin.getConfig().getString("HubEnabled").equalsIgnoreCase("True")))
            {
              this.plugin.getConfig().set("HUB.world", p.getLocation().getWorld().getName());
              this.plugin.getConfig().set("HUB.x", Double.valueOf(p.getLocation().getX()));
              this.plugin.getConfig().set("HUB.y", Double.valueOf(p.getLocation().getY()));
              this.plugin.getConfig().set("HUB.z", Double.valueOf(p.getLocation().getZ()));
              this.plugin.getConfig().set("HUB.yaw", Double.valueOf(p.getLocation().getYaw()));
              this.plugin.getConfig().set("HUB.pitch", Double.valueOf(p.getLocation().getPitch()));
              this.plugin.saveConfig();
              p.sendMessage(ChatColor.GOLD + "Hub saved!");
            }
            else
            {
              sender.sendMessage(ChatColor.RED + "You do not have permission to do that!");
            }
          }
        }
        else if (cmd.getName().equalsIgnoreCase("hub")) {
          if (!(sender instanceof Player))
          {
            sender.sendMessage("Only players can do this!");
          }
          else
          {
            Player p = (Player)sender;
            if ((p.hasPermission("spawnjoin.hub")) && (this.plugin.getConfig().getString("HubEnabled").equalsIgnoreCase("True")))
            {
              World w = Bukkit.getServer().getWorld(this.plugin.getConfig().getString("HUB.world"));
              double x = this.plugin.getConfig().getDouble("HUB.x");
              double y = this.plugin.getConfig().getDouble("HUB.y");
              double z = this.plugin.getConfig().getDouble("HUB.z");
              int ya = this.plugin.getConfig().getInt("HUB.yaw");
              int pi = this.plugin.getConfig().getInt("HUB.pitch");
              p.teleport(new Location(w, x, y, z, ya, pi));
              p.sendMessage(ChatColor.GOLD + "You have been warped to the Hub!");
            }
          }
        }
        return false;
      }
    }
    
     
Thread Status:
Not open for further replies.

Share This Page