Solved Making a Set Home and Home Command

Discussion in 'Plugin Development' started by gamemster2468, Sep 23, 2014.

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

    gamemster2468

    Hello,

    Recently I've been working on a core plugin for my survival server when I ran across the needing of /sethome and /home plugin.

    I've worked with configs before, it's just I don't know how I can save the location and the player.

    Any help would be great,
    Thanks.
     
  2. Offline

    Skionz

    gamemster2468 setup the config like this
    Code:
    12346087123689746:
      x: 50
      y: 75
      z: 50
      world: 'world'
    Then create the location by getting all the values.
    EDIT: The random numbers represents a players UUID
     
  3. Offline

    gamemster2468

    Skionz I understand that part, it's just I don't know how that in code, then call it when the player wants to go back to their home =/
     
  4. Your set command will be something like this:
    Code:
    //Define player as p
    String uuid = p.getUniqueId().toString();
    getConfig().set("uuid.world", p.getLocation().getWorld().getName());
    getConfig().set("uuid.x", p.getLocation().getX());
    getConfig().set("uuid.y", p.getLocation().getY());
    getConfig().set("uuid.z", p.getLocation().getZ());
    getConfig().set("uuid.pitch", p.getEyeLocation().getPitch());
    getConfig().set("uuid.yaw", p.getEyeLocation().getYaw());
    saveConfig();
    And your retrieve command will be something along the lines of this:
    Code:
    //Define player as p
    String uuid = p.getUniqueId().toString();
    World w = Bukkit.getServer().getWorld(getConfig().getString("uuid.world"));
    double x = getConfig().getDouble("uuid.x");
    double y = getConfig().getDouble("uuid.y");
    double z = getConfig().getDouble("uuid.z");
    double yaw = getConfig().getDouble("uuid.yaw");
    double pitch = getConfig().getDouble("uuid.pitch");
     
     
    p.teleport(new Location(w, x, y, z, (float)yaw, (float)pitch));
    return true;
    Have fun.
     
    Hawktasard likes this.
  5. Offline

    WeeSkilz

    Code:java
    1. public class Main extends JavaPlugin{
    2. public void onEnable() {
    3. saveDefaultConfig();
    4. }
    5.  
    6. public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args) {
    7. if (cmd.getName().equalsIgnoreCase("sethome")) {
    8. if(sender instanceof Player) {
    9. Player p = (Player) sender;
    10. Integer xlocation = p.getLocation().getBlockX();
    11. this.getConfig().set(p.getUniqueId().toString() + ".x", xlocation);
    12. }
    13. return true;
    14. }
    15. return false;
    16. }
    17. }

    That should set the value X in the config, it should be fairly obvious how to retrieve it from there.
     
  6. Offline

    gamemster2468

    How would I find out if a player already has a home set, and how could I delete that? WeeSkilz xtechgamer735
     
  7. Offline

    Hawktasard

    Doesn't getBlockX() return a non-double x?
    If so, you should rather use getX()

    Check if "uuid.world" != null or something?
    (I don't do yaml, but that's what I'd try first)

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

    WeeSkilz

    The value would automatically be overwritten by the set() function.
    As for checking if the player has a home set for other reasons, the method that Hawktasard suggested is probably your best option.
     
  9. Offline

    gamemster2468

    Wait, WeeSkilz - How would I call the location when the player types /home from your code?
     
  10. Offline

    Hawktasard

    Code:java
    1. //Something like this.
    2. int x = getConfig().getDouble("<uuid>.x");
    3. int y = getConfig().getDouble("<uuid>.y");
    4. int z = getConfig().getDouble("<uuid>.z");
    5. World w = plugin.getServer().getWorld(getConfig().getString("<uuid>.world"));
    6. Location loc = new Location(w,x,y,z);

    Edit: Remember to change the <uuid> to someuuidobject.toString();
     
  11. Offline

    gamemster2468

  12. Offline

    Hawktasard

    Kindof, but I'd recommend using techgamer's method, Since it stores an actual double + pitch & yaw (wich will control what direction the player is looking, etc).
    If you choose to use his method, change the teleport thing to something more like this;
    Code:java
    1. //Something like this.
    2. int x = getConfig().getDouble("<uuid>.x");
    3. int y = getConfig().getDouble("<uuid>.y");
    4. int z = getConfig().getDouble("<uuid>.z");
    5. float pitch = getConfig().getDouble("<uuid>.pitch");
    6. float yaw = getConfig().getDouble("<uuid>.yaw");
    7. World w = plugin.getServer().getWorld(getConfig().getString("<uuid>.world"));
    8. Location loc = new Location(w,x,y,z, yaw, pitch);
     
  13. Offline

    CraftCreeper6

  14. Offline

    Hawktasard

    Thanks, It's getting a little late :/
     
  15. Offline

    CraftCreeper6

  16. Offline

    gamemster2468

    Um, these are the errors I am getting:

    23.09 17:11:38[Server]INFO §7[§dDEV§7] §e§eStargate_ §8» §fwtf 23.09 17:11:36[Server]INFO ... 15 more 23.09 17:11:36[Server]INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) 23.09 17:11:36[Server]INFO at com.gamemaster.CoreCommands.SetHome.onCommand(SetHome.java:25) 23.09 17:11:36 [Server] INFO Caused by: java.lang.NullPointerException23.09 17:11:36[Server]INFO at fy.run(SourceFile:849) 23.09 17:11:36[Server]INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:497) 23.09 17:11:36[Server]INFO at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:599) 23.09 17:11:36[Server]INFO at ho.r(DedicatedServer.java:269) 23.09 17:11:36[Server]INFO at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:703) 23.09 17:11:36[Server]INFO at ht.b(SourceFile:30) 23.09 17:11:36[Server]INFO at iw.b(NetworkListenThread.java:57) 23.09 17:11:36[Server]INFO at iv.d(NetServerHandler.java:136) 23.09 17:11:36[Server]INFO at cg.b(TcpConnection.java:458) 23.09 17:11:36[Server]INFO at cu.a(SourceFile:44) 23.09 17:11:36[Server]INFO at iv.a(NetServerHandler.java:735) 23.09 17:11:36[Server]INFO at iv.d(NetServerHandler.java:769) 23.09 17:11:36[Server]INFO at x.a(CommandHandler.java:14) 23.09 17:11:36[Server]INFO at keepcalm.mods.bukkit.nmsforge.CommandHandlerImpl.a(CommandHandlerImpl.java:94) 23.09 17:11:36[Server]INFO at keepcalm.mods.bukkit.forgeHandler.commands.CommandExecutor2CommandBase.b(CommandExecutor2CommandBase.java:105) 23.09 17:11:36[Server]INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) 23.09 17:11:36[Server]INFO org.bukkit.command.CommandException: Unhandled exception executing command 'sethome' in plugin ResurrectionCore v0.1-DEV 23.09 17:11:33[Server]INFO §7[§dDEV§7] §e§eStargate_ §8» §f. 23.09 17:11:32[Server]INFO ... 15 more 23.09 17:11:32[Server]INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) 23.09 17:11:32[Server]INFO at com.gamemaster.CoreCommands.Home.onCommand(Home.java:28) 23.09 17:11:32 [Server] INFO Caused by: java.lang.NullPointerException23.09 17:11:32[Server]INFO at fy.run(SourceFile:849) 23.09 17:11:32[Server]INFO at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:497) 23.09 17:11:32[Server]INFO at net.minecraft.server.MinecraftServer.q(MinecraftServer.java:599) 23.09 17:11:32[Server]INFO at ho.r(DedicatedServer.java:269) 23.09 17:11:32[Server]INFO at net.minecraft.server.MinecraftServer.r(MinecraftServer.java:703) 23.09 17:11:32[Server]INFO at ht.b(SourceFile:30) 23.09 17:11:32[Server]INFO at iw.b(NetworkListenThread.java:57) 23.09 17:11:32[Server]INFO at iv.d(NetServerHandler.java:136) 23.09 17:11:32[Server]INFO at cg.b(TcpConnection.java:458) 23.09 17:11:32[Server]INFO at cu.a(SourceFile:44) 23.09 17:11:32[Server]INFO at iv.a(NetServerHandler.java:735) 23.09 17:11:32[Server]INFO at iv.d(NetServerHandler.java:769) 23.09 17:11:32[Server]INFO at x.a(CommandHandler.java:14) 23.09 17:11:32[Server]INFO at keepcalm.mods.bukkit.nmsforge.CommandHandlerImpl.a(CommandHandlerImpl.java:94) 23.09 17:11:32[Server]INFO at keepcalm.mods.bukkit.forgeHandler.commands.CommandExecutor2CommandBase.b(CommandExecutor2CommandBase.java:105) 23.09 17:11:32[Server]INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) 23.09 17:11:32[Server]INFO org.bukkit.command.CommandException: Unhandled exception executing command 'home' in plugin ResurrectionCore v0.1-DEV

    Here's my code:

    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                String uuid = p.getUniqueId().toString();
                if (Util.getPlugin().getConfig().contains("uuid.world")) {
                    World w = Bukkit.getServer().getWorld(Util.getPlugin().getConfig().getString("uuid.world"));
                    double x = Util.getPlugin().getConfig().getDouble("uuid.x");
                    double y = Util.getPlugin().getConfig().getDouble("uuid.y");
                    double z = Util.getPlugin().getConfig().getDouble("uuid.z");
                    double yaw = Util.getPlugin().getConfig().getDouble("uuid.yaw");
                    double pitch = Util.getPlugin().getConfig().getDouble("uuid.pitch");
                    p.teleport(new Location(w, x, y, z, (float) yaw, (float) pitch));
                    p.sendMessage(Msg.cPrefix + "You have been teleported to your home.");
                } else {
                    p.sendMessage(Msg.cPrefix + "You have not set your home yet.");
                    p.sendMessage(Msg.cPrefix + "You can set your home by using the /sethome command!");
                }
            }
            return false;
        }
    and

    Code:
     @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String args[]) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                String uuid = p.getUniqueId().toString();
                Util.getPlugin().getConfig().set("uuid.world", p.getLocation().getWorld().getName());
                Util.getPlugin().getConfig().set("uuid.x", p.getLocation().getX());
                Util.getPlugin().getConfig().set("uuid.y", p.getLocation().getY());
                Util.getPlugin().getConfig().set("uuid.z", p.getLocation().getZ());
                Util.getPlugin().getConfig().set("uuid.pitch", p.getEyeLocation().getPitch());
                Util.getPlugin().getConfig().set("uuid.yaw", p.getEyeLocation().getYaw());
                Util.getPlugin().saveConfig();
                p.sendMessage(Msg.cPrefix + "Your home has been set.");
            }
            return false;
        }
    @CraftCreeper6
    Hawktasard

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

    WeeSkilz

    It would actually be
    Code:java
    1. Double x = getConfig().getDouble("<uuid>.x");
    2. Double y = getConfig().getDouble("<uuid>.y");
    3. Double z = getConfig().getDouble("<uuid>.z");

    As you are getting a double from the config, which cannot be declared as an Integer.

    gamemster2468 you need to do getConfig().set(uuid + ".x", //location stuff); otherwise you are just saving it under the literal path uuid.x.

    I know that was poorly explained, but you would be using the word uuid and not the player's uuid.
     
    Hawktasard likes this.
  18. Offline

    gamemster2468

    WeeSkilz Do you know what I did wrong in my code?
     
  19. Offline

    Hawktasard

    Again, It's getting really really late.
    Thanks for correcting.

    Change all the "int" from my code to either "double" or "Double"

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

    gamemster2468

  21. Offline

    Hawktasard

    Okay, can you pastebin your error*?
    (Not sure if this is allowed, but http://pastebin.com)
     
  22. Offline

    gamemster2468

  23. Offline

    Hawktasard

  24. Offline

    gamemster2468

  25. Offline

    Hawktasard

    com.gamemaster.CoreCommands.SetHome.onCommand(SetHome.java:25)
    What's on line 25 in the SetHome class?

    Edit:
    Just try to fix whatever's on line 25, I have to go sleep.
    Maybe someone else can help you if you still can't fix it.
     
  26. Offline

    gamemster2468

    It's String uuid = p.getUniqueId().toString(); Hawktasard
     
  27. Offline

    WeeSkilz

    gamemster2468 You appear to have 2 onCommand methods, this is not allowed in the same class. You can use the command.getName().equalsIgnoreCase() function to have 2 commands in the same class.
     
  28. Offline

    gamemster2468

    Someone wanna help? WeeSkilz

    I don't have two in the same class? WeeSkilz

    Now /sethome works, but /home does not WeeSkilz

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

    WeeSkilz

    Can I see your code for /home please
     
Thread Status:
Not open for further replies.

Share This Page