Can I upgrade this in anyway?

Discussion in 'Plugin Development' started by bioxkillerant172, Jan 18, 2015.

Thread Status:
Not open for further replies.
  1. I want to make an RPG and my first step was teleporting. I am relatively new to java so I might have a few mistakes. The final version of this script will be unlocking the teleports at certain levels or if you do a certain quest but that is too advanced for me at the moment. If you want to try and do that by all mean please do as It would help me out a lot but the main reason I posted this was for any basic upgrades. Thanks.
    Code:
    package me.project_rpg.cityteleports;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class cityteleports extends JavaPlugin {
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
                return true;
            }
    
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("city")) {
    
                if (args.length == 0) {
                    p.sendMessage(ChatColor.YELLOW + "The current citys are :");
                    p.sendMessage(ChatColor.YELLOW + "Runebroad");
                    p.sendMessage(ChatColor.YELLOW + "Yonkal");
                    p.sendMessage(ChatColor.YELLOW + "Carrick");
                    return false;
                }
    
            }
            if (args[0].equalsIgnoreCase("setrunebroad")) {
    
                if (!p.hasPermission("runebroad.set")) {
                    p.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return false;
                }
    
                getConfig().set("runebroad.world", p.getLocation().getWorld().getName());
                getConfig().set("runebroad.x", p.getLocation().getX());
                getConfig().set("runebroad.y", p.getLocation().getY());
                getConfig().set("runebroad.z", p.getLocation().getZ());
                getConfig().set("runebroad.pitch", p.getLocation().getPitch());
                getConfig().set("runebroad.yaw", p.getLocation().getYaw());
                saveConfig();
                p.sendMessage(ChatColor.GREEN + "Runebroad Spawn Set!");
                return true;
            }
    
            if (args[0].equalsIgnoreCase("runebroad")) {
    
                if (getConfig().getConfigurationSection("runebroad") == null) {
                    p.sendMessage(ChatColor.RED + "The Runebroad spawn has not yet been set!");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(getConfig().getString("runebroad.world"));
                double x = getConfig().getDouble("runebroad.x");
                double y = getConfig().getDouble("runebroad.y");
                double z = getConfig().getDouble("runebroad.z");
                double pitch = getConfig().getDouble("runebroad.pitch");
                double yaw = getConfig().getDouble("runebroad.yaw");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "Welcome to Runebroad!");
            }
    
    
            if (args[0].equalsIgnoreCase("setyonkal")) {
    
                if (!p.hasPermission("yonkal.set")) {
                    p.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return false;
                }
    
                getConfig().set("yonkal.world", p.getLocation().getWorld().getName());
                getConfig().set("yonkal.x", p.getLocation().getX());
                getConfig().set("yonkal.y", p.getLocation().getY());
                getConfig().set("yonkal.z", p.getLocation().getZ());
                getConfig().set("yonkal.pitch", p.getLocation().getPitch());
                getConfig().set("yonkal.yaw", p.getLocation().getYaw());
                saveConfig();
                p.sendMessage(ChatColor.GREEN + "Yonkal Spawn Set!");
                return true;
            }
    
            if (args[0].equalsIgnoreCase("yonkal")) {
    
                if (getConfig().getConfigurationSection("yonkal") == null) {
                    p.sendMessage(ChatColor.RED + "The Yonkal spawn has not yet been set!");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(getConfig().getString("yonkal.world"));
                double x = getConfig().getDouble("yonkal.x");
                double y = getConfig().getDouble("yonkal.y");
                double z = getConfig().getDouble("yonkal.z");
                double pitch = getConfig().getDouble("yonkal.pitch");
                double yaw = getConfig().getDouble("yonkal.yaw");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "Welcome to Yonkal!");
                return false;
            }
    
    
            if (args[0].equalsIgnoreCase("setcarrick")) {
    
                if (!p.hasPermission("carrick.set")) {
                    p.sendMessage(ChatColor.RED + "You are not permitted to do this!");
                    return false;
                }
    
                getConfig().set("carrick.world", p.getLocation().getWorld().getName());
                getConfig().set("carrick.x", p.getLocation().getX());
                getConfig().set("carrick.y", p.getLocation().getY());
                getConfig().set("carrick.z", p.getLocation().getZ());
                getConfig().set("carrick.pitch", p.getLocation().getPitch());
                getConfig().set("carrick.yaw", p.getLocation().getYaw());
                saveConfig();
                p.sendMessage(ChatColor.GREEN + "Carrick Spawn Set!");
                return true;
            }
    
            if (args[0].equalsIgnoreCase("carrick")) {
    
                if (getConfig().getConfigurationSection("carrick") == null) {
                    p.sendMessage(ChatColor.RED + "The Carrick spawn has not yet been set!");
                    return true;
                }
                World w = Bukkit.getServer().getWorld(getConfig().getString("carrick.world"));
                double x = getConfig().getDouble("carrick.x");
                double y = getConfig().getDouble("carrick.y");
                double z = getConfig().getDouble("carrick.z");
                double pitch = getConfig().getDouble("carrick.pitch");
                double yaw = getConfig().getDouble("carrick.yaw");
                p.teleport(new Location(w, x, y, z));
                p.sendMessage(ChatColor.GREEN + "Welcome to Carrick!");
                return false;
            }
            return false;
        }
    }
    EDIT by Timtower: fixed the code block
     
    Last edited by a moderator: Jan 19, 2015
  2. Offline

    Luke_Lax

    I don't have time but just to let you know this will error every time that it is not a player. Imagine it this way, could you send that red message to a command block?
     
  3. Offline

    1Rogue

    Yes. Yes you can.
     
    Last edited: Jan 19, 2015
    Konato_K and xTigerRebornx like this.
  4. Use a whole class for this , using maps , etc...
    And save the config on quit/disable , and load on join/enable .
     
  5. Offline

    Luke_Lax

    No. No you can't.

    java.lang.ClassCastException: org.bukkit.<version>.command.CraftBlockCommandSender cannot be cast to org.bukkit.entity.Player

    Unless in some magic update this changed... certainly not in last one.
     
  6. Offline

    1Rogue

    You don't need to cast to player, you can simply use CommandSender#sendMessage (which the OP did). The instanceof check returns before the OP's cast, that error will not happen.
     
    Konato_K and xTigerRebornx like this.
  7. Offline

    Luke_Lax

    I put the exact same code as the OP, please try it for yourself
     
  8. Offline

    1Rogue

    It's silly that you're arguing this, because a ClassCastException has to have a cast to even occur unless it's manually thrown. But I'll humor you:

    Code:java
    1. public class BukkitTest extends JavaPlugin {
    2.  
    3. @Override
    4. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    5. if (!(sender instanceof Player)) {
    6. sender.sendMessage(ChatColor.RED + "This plugin is for players only!");
    7. return true;
    8. }
    9.  
    10. Player p = (Player) sender;
    11. return true;
    12. }
    13.  
    14. }


    [​IMG]

    To further prove it, here it is without right-clicking command blocks enabled (using redstone power):

    [​IMG]
     
    xTigerRebornx likes this.
  9. Offline

    Luke_Lax

    Great, someone who gets upset....

    anyway, I don't know about 1.8 but 1.7.* and below you will get an error
     
  10. Offline

    1Rogue

    No you will not. The code above will work all the way back to Beta 1.8.1, and will work back until 1.4.2 for command blocks (since that handling is internalized in bukkit). I'm not upset, I'm pointing out the fact that casting has nothing to do with this, and .sendMessage will work with any CommandSender.
     
    Konato_K and xTigerRebornx like this.
  11. Right :p so in anyway can I upgrade this?
     
  12. Offline

    PreFiXAUT

    Well, you can make it way more simple like:
    Code:
    // They work all the same. Save em in here
    String[] setters = new String[]{"setrunebroad","otherGetter","anotherGetter"};
    for (String setter : setters) {
      // Loop through the List and check them
      if (!args[0].equalsIgnoreCase(getter)) continue;
      getConfig().set(setter + ".world", p.getLocation().getWorld().getName());
      getConfig().set(setter + ".x", p.getLocation().getX();
      // And so on. Type it ONCE, and your setters are done
    }
     
  13. Offline

    1Rogue

    Use an SLocation so that you can serialize the location directly, rather than hackily trying to parse config values:

    Code:java
    1. /*
    2. * Copyright (C) 2015 Codelanx, All Rights Reserved
    3. *
    4. * This work is licensed under a Creative Commons
    5. * Attribution-NonCommercial-NoDerivs 3.0 Unported License.
    6. *
    7. * This program is protected software: You are free to distrubute your
    8. * own use of this software under the terms of the Creative Commons BY-NC-ND
    9. * license as published by Creative Commons in the year 2015 or as published
    10. * by a later date. You may not provide the source files or provide a means
    11. * of running the software outside of those licensed to use it.
    12. *
    13. * This program is distributed in the hope that it will be useful,
    14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16. *
    17. * You should have received a copy of the Creative Commons BY-NC-ND license
    18. * along with this program. If not, see <[url]https://creativecommons.org/licenses/>[/url].
    19. */
    20. package com.codelanx.codelanxlib.serialize;
    21.  
    22. import java.util.HashMap;
    23. import java.util.Map;
    24. import java.util.UUID;
    25. import org.bukkit.Bukkit;
    26. import org.bukkit.Location;
    27. import org.bukkit.World;
    28. import org.bukkit.configuration.serialization.ConfigurationSerializable;
    29. import org.bukkit.configuration.serialization.SerializableAs;
    30. import org.bukkit.util.Vector;
    31.  
    32. /**
    33. * Class description for {@link SLocation}
    34. *
    35. * @since 0.0.1
    36. * @author 1Rogue
    37. * @version 0.0.1
    38. */
    39. @SerializableAs("Location")
    40. public class SLocation implements ConfigurationSerializable {
    41.  
    42. private final Vector loc;
    43. private final UUID uuid;
    44. private World world;
    45.  
    46. public SLocation(Location loc) {
    47. this.loc = loc.toVector();
    48. this.uuid = loc.getWorld().getUID();
    49. }
    50.  
    51. public SLocation(Map<String, Object> config) {
    52. this.loc = (Vector) config.get("location");
    53. this.uuid = UUID.fromString((String) config.get("world"));
    54. }
    55.  
    56. @Override
    57. public Map<String, Object> serialize() {
    58. Map<String, Object> back = new HashMap<>();
    59. back.put("location", this.loc);
    60. back.put("world", this.uuid.toString());
    61. return back;
    62. }
    63.  
    64. public SLocation valueOf(Map<String, Object> config) {
    65. return new SLocation(config);
    66. }
    67.  
    68. public SLocation deserialize(Map<String, Object> config) {
    69. return new SLocation(config);
    70. }
    71.  
    72. public World getWorld() {
    73. if (this.world == null) {
    74. this.world = Bukkit.getWorld(this.uuid);
    75. }
    76. return this.world;
    77. }
    78.  
    79. public Vector getVector() {
    80. return this.loc;
    81. }
    82.  
    83. public Location toLocation() {
    84. return this.getVector().toLocation(this.getWorld());
    85. }
    86.  
    87. }


    To use this class, register it first:

    Code:java
    1. ConfigurationSerialization.registerClass(SLocation.class);


    Then you can get the values from the config like so:

    Code:java
    1. //Saving a location to config
    2. Location loc = /* your location object */;
    3. getConfig().set("example", new SLocation(loc));
    4. //Retrieving that location back
    5. loc = ((SLocation) getConfig().get("example")).toLocation();


    Note that .toLocation() will lazily initialize the world object, so if your plugin loads before world loading (this is specified in the plugin yaml), then you would just store the SLocation objects until they actually needed to be a location. If you still need the coordinates or something but can't convert it to a location yet, you can still use the vector:

    Code:java
    1. //Your SLocation object when worlds are not loaded
    2. SLocation loc = /* your object */;
    3. int x = loc.getVector().getBlockX();
     
Thread Status:
Not open for further replies.

Share This Page