Creating a Warp Plugin

Discussion in 'Plugin Development' started by iCancer, Oct 20, 2016.

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

    iCancer

    I am trying to make a warp plugin, but I'm getting several errors, and I don't know why.
    Code:
    
    
    if (command.getName().equalsIgnoreCase("setwarp")) {
    
    if (!(sender instanceof Player)) {
    
    sender.sendMessage(corePlugin + "You must be a Player to execute this command!");
    
    }
    
    if (player.hasPermission(setWarp)) {
    
    if (args.length == 0) {
    
    player.sendMessage(corePlugin + "Usage: " + command.getUsage());
    
    }
    
    getConfig().set("warps." + args[0] + ".world",  player.getLocation().getWorld());
    
    getConfig().set("warps." + args[0] + ".x",  player.getLocation().getWorld());
    
    getConfig().set("warps." + args[0] + ".y",  player.getLocation().getWorld());
    
    getConfig().set("warps." + args[0] + ".z",  player.getLocation().getWorld());
    
    player.sendMessage(corePlugin + "Warp " +  args[0] + " has been set!");
    
    
    }
    
    }
    
    
    if (command.getName().equalsIgnoreCase("warp")) {
    
    if (!(sender instanceof Player)) {
    
    sender.sendMessage(corePlugin + "You must be a Player to execute this command!");
    
    }
    
    if(player.hasPermission(warp)) {
    
    if (args.length == 0) {
    
    player.sendMessage(corePlugin + "Usage: " + command.getUsage());
    
    }
    
    World world = Bukkit.getServer().getWorld(getConfig().set("warps." + args[0] + ".world",  player.getLocation().getWorld()));
    
    double x = ("warps." + args[0] + ".x",  player.getLocation().getWorld());
    
    double y = ("warps." + args[0] + ".y",  player.getLocation().getWorld());
    
    double z = ("warps." + args[0] + ".z",  player.getLocation().getWorld());
    
    player.teleport(new Location(world, x, y, z));
    
    }
    
    if ((getConfig().getConfigurationSection("warps." + args [0])) == null) {
    
    player.sendMessage(corePlugin + "Warp " + args[0] + " does not exist!");
    
    }
    
    }
    
    Please let me know what I did wrong.

    EDIT by Moderator: Changed spoiler into code tag
     
    Last edited by a moderator: Oct 20, 2016
  2. Offline

    Zombie_Striker

    @iCancer
    First, please use the [code][/code] tags instead of spoilers. I can't quote any of your lines and the formatting is broken.

    We can't help you unless you tell us what those errors are and what lines are causing them.

    [Edit] since a mod is able to modify the tags for you, here are your problems:

    You are forgetting to return after this line, so it will continue to read, and because of that, throw errors and essentially do nothing.

    Because you have this line, this means you created the player before you even check if it is a Player. This is bad, because the CCE that is thrown for blidnly casting object thrown when you actually cast the object, not when you first use it. Only create the player AFTER you know it actually is a player.

    1. Still forgetting to return. Return after sending the message.
    2. The onCommand will still issue the useage if you return false. Remove the sendmessage line and simply return false.

    The first line was (somewhat) good, the rest is just wrong. You are setting the "X" equal to the world. How is the X coord equal to a world? This is why copying and pasting is bad.

    BTW: When I said "Somewhat", what I meant was that you were on the right path. That line would not actually work because you are saving the Instance to the config, not the world name (which you need). Add .getName() after getting the world to save the world name.


    Now for this, None of this should work.
    1. You are setting the variable instead of Getting the first world from the config.
    2. The next three lines are just nonsense that should not do anything.
    You may need to just delete this section lines and start over.

    This is only check after you already access the sections. That means this is useless where it is. Move this so this runs first before the section above, and remember to return if it is null.

    Also, I will never actually be null. The path may not exist, but there will always be an object that will be returned. Check if the path exists instead of if it is null.
     
    Last edited: Oct 21, 2016
    Lordloss, danichef and timtower like this.
  3. Offline

    iCancer

    This is the new code I am using. I get an error at:

    Code:
    Location location = new Location(configSection.getString("world"), configSection.getDouble("X"), configSection.getDouble("Y"), configSection.getDouble("Z"));
    The error states that it has not been defined. The full code is here:

    Code:
             * Warp Commands
             *
             */
            if (command.getName().equalsIgnoreCase("setwarp")) {
                if (!(sender instanceof Player)) {
                sender.sendMessage(corePlugin + "You may not set Warps!");
                return true;
                }
                if (!player.hasPermission(setWarp)) {
                player.sendMessage(corePlugin + "You may not set Warps!");
                return true;
                }
                if (args.length == 0) {
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + command.getUsage());
                return true;
                }
                Location location = player.getLocation();
                getConfig().createSection(args[0].toLowerCase());
                // This is getting the location of a player. It will then create a new section in the config called {WARPNAME}.
                ConfigurationSection configSection = getConfig().getConfigurationSection(args[0].toLowerCase());
                configSection.set("X", location.getX());
                configSection.set("Y", location.getY());
                configSection.set("Z", location.getZ());
                configSection.set("world", location.getWorld().getName());
                saveConfig();
                // Save all the necessary components of the location in this section, and save the config
                player.sendMessage(corePlugin + "Created warp: " + args[0] + " at your location.");
                }
           
            if (command.getName().equalsIgnoreCase("warp")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage(corePlugin + "You may not Warp!");
                return true;
                }
                if (!player.hasPermission(warp)) {
                    player.sendMessage(corePlugin + "You may not Warp!");
                    return true;
                }
                if (args.length == 0) {
                    player.sendMessage(corePlugin + command.getUsage() + "\nValid Warps");
               
                    for (String key : getConfig().getKeys(false)) {
                        player.sendMessage(key);
                    }
                    return true;
                }
               
                ConfigurationSection configSection = getConfig().getConfigurationSection(args[0].toLowerCase());
                Location location = new Location(configSection.getString("world"), configSection.getDouble("X"), configSection.getDouble("Y"), configSection.getDouble("Z"));
                player.teleport(location);
                player.sendMessage("Warped to: " + args[0]);
               
            }
     
  4. Offline

    Zombie_Striker

    @iCancer
    Can you post the full error log?

    [Edit]
    This returns a string, not a world. Use Bukkit.getWorld(String) to turn a String into an existing world.
     
  5. Offline

    iCancer

    Thanks. I'll try it out.
     
  6. Offline

    Zombie_Striker

    @iCancer
    If that solves your problem (which it should), mark this thread as solved.
     
  7. Offline

    iCancer

    I'm trying out a different way, but it seems to be working ineffectively. I'm trying to get the default section in my config.yml (which I made creating a file manually) and asking the plugin, to my knowledge, to get my "WARPS." Config Section. I think proceed to create a section according to the first command argument. I test my plugin out, yet I get an Internal Error.






    Code:
            /*
             *
             * Warp Commands
             *
             */
            if (command.getName().equalsIgnoreCase("setwarp")) {
                if (!(sender instanceof Player) || !(player.hasPermission(setWarp))) {
                    sender.sendMessage(plugin + "Invalid Permissions!!");
                    return true;
                }
                if (args.length != 1) {
                    player.sendMessage(plugin + "Usage: " + command.getUsage());
                    return true;
                }
                if (getConfig().getConfigurationSection("WARPS.").contains(args[0].toUpperCase())) {
                    player.sendMessage(plugin + "Error! Warp \"" + args[0] + "\" is already a Warp!!");
                    return true;
                }
                Location location = player.getLocation();
                getConfig().getConfigurationSection("WARPS.").createSection(args[0].toUpperCase()).createSection(".WORLD").set(".WORLD", location.getWorld().getName());
                getConfig().getConfigurationSection("WARPS.").createSection(args[0].toUpperCase()).createSection(".X").set(".X", location.getBlockX());
                getConfig().getConfigurationSection("WARPS.").createSection(args[0].toUpperCase()).createSection(".Y").set(".Y", location.getBlockY());
                getConfig().getConfigurationSection("WARPS.").createSection(args[0].toUpperCase()).createSection(".Z").set(".Z", location.getBlockZ());
                player.sendMessage(plugin + "Warp " + args[0] + " has been set to your location!!");
                return true;
            }
     
  8. Offline

    Zombie_Striker

  9. Offline

    iCancer

    Error Code (open)

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo} span.s1 {font-variant-ligatures: no-common-ligatures} span.Apple-tab-span {white-space:pre}

    [20:11:23 INFO]: TitanLS issued server command: /setwarp Spawn

    [20:11:23 ERROR]: null

    org.bukkit.command.CommandException: Unhandled exception executing command 'setwarp' in plugin UniverseMC_Core_Plugin v1.41 [BETA]

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[PluginCommand.class:KCauldron-1.7.10-2.1403.1.54]

    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:181) ~[SimpleCommandMap.class:KCauldron-1.7.10-2.1403.1.54]

    at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServer.java:717) ~[CraftServer.class:KCauldron-1.7.10-2.1403.1.54]

    at net.minecraft.network.NetHandlerPlayServer.func_147361_d(NetHandlerPlayServer.java:1317) [nh.class:?]

    at net.minecraft.network.NetHandlerPlayServer.func_147354_a(NetHandlerPlayServer.java:1099) [nh.class:?]

    at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(C01PacketChatMessage.java:38) [ir.class:?]

    at net.minecraft.network.play.client.C01PacketChatMessage.func_148833_a(C01PacketChatMessage.java:53) [ir.class:?]

    at net.minecraft.network.NetworkManager.func_74428_b(NetworkManager.java:244) [ej.class:?]

    at net.minecraft.network.NetworkSystem.func_151269_c(NetworkSystem.java:173) [nc.class:?]

    at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:980) [MinecraftServer.class:?]

    at net.minecraft.server.dedicated.DedicatedServer.func_71190_q(DedicatedServer.java:430) [lt.class:?]

    at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:798) [MinecraftServer.class:?]

    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:658) [MinecraftServer.class:?]

    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_102]

    Caused by: java.lang.NullPointerException

    at universemc.Main.onCommand(Main.java:231) ~[?:?]

    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[PluginCommand.class:KCauldron-1.7.10-2.1403.1.54]

    ... 13 more


    I've also read the post, but it did not help me with this.
     
  10. Offline

    Zombie_Striker

    @iCancer
    It should have:
    There is something null on line 231. Go through each variable and null check.
     
  11. Offline

    iCancer

    How do I do that exactly? Sorry if I'm a little arrogant, but I haven't had any errors yet, so this is my first one :) The code is this:

    Code:
    if (getConfig().getConfigurationSection("WARPS").contains(args[0].toUpperCase())) {
    I've played around with it, but nothing seems to work.
     
  12. Offline

    Zombie_Striker

    You are not arrogant in this case. Not running into errors just means you are new. Trust me, they will happen a lot in the future.

    Null checking means to go through each variable and see if they are null. You can do this by using:
    Code:
    if(VARIABLE == null)
    Bukkit.broadcastMessage("Variable is null.");
    If you see that message, it means the variable is null. For this, the only variable that can be null is the configuration section. Test it.

    [EDIT] After reading the JavaDocs, I found this:
    This does not check if there is a Key with the path (what you want), but if the configuration you have contains the path. In this case, you want to see if the word "args[0]" is inside the word "WARP" (Which it most likely wont. The only way this is true would be if args[0] is "WARP").

    To achieve what you want, use "ConfigSection#getKeys(false)" to get all the keys for the configuration section. For loop through all those keys and see if one of the keys is equal to args[0]. If it is, then the path exists.
     
Thread Status:
Not open for further replies.

Share This Page