Solved making a Location 'shared'

Discussion in 'Plugin Development' started by pingpong1999, Apr 15, 2016.

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

    pingpong1999

    Hey guys! Yea I know.. I post too many threads but y'know I'm only a starter.
    Code:
    package KoTH;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
    @Override
    public void onEnable(){
     
    }
     
    @Override
    public void onDisable(){
     
    }
     
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
        Player player = (Player) sender;
     
        if(cmd.getName().equalsIgnoreCase("KoTH") && sender instanceof Player && args.length == 0){
            player.sendMessage(ChatColor.RED + "KoTH Help");
            player.sendMessage(ChatColor.GRAY + "/KoTH Sethub" + ChatColor.GOLD + " Set Hub Point");
            player.sendMessage(ChatColor.GRAY + "/KoTH SetRedSpawn" + ChatColor.GOLD + " Set Red Team's SpawnPoint");
            player.sendMessage(ChatColor.GRAY + "/KoTH SetBlueSpawn" + ChatColor.GOLD + " Set Blue Team's SpawnPoint");
            player.sendMessage(ChatColor.GRAY + "/KoTH SetCapturePoint" + ChatColor.GOLD + " Set Capture Point");
            return true;
        }else if(cmd.getName().equalsIgnoreCase("Koth") && sender instanceof Player && args[0].equalsIgnoreCase("sethub")){
            double pX = player.getLocation().getX();
            double pY = player.getLocation().getY();
            double pZ = player.getLocation().getZ();
            Location Hub = new Location(player.getWorld(), pX, pY, pZ);
            player.sendMessage("Set Hub Position at " + Hub.getX(), Hub.getY(), Hub.getZ();
            return true;
    {
        }else if(!(sender instanceof Player)){
            return false;
        }
    }
    
    I dont really have the command in here but earlier I tried to implement /Koth tpto (location Name) and I found that it could not find the location "Hub" it seems it is not shared.. how can I implement this effectively ? Also, How can I save the locations, do I need to use hashmaps?
    -Edit I did not add all the commands because they are all the same command doing the same thing, just different names
     
  2. Offline

    Zombie_Striker

    Don't name your main class "Main", instead, name it the name of your plugin

    Follow Java NamingConventions: The packagenames should either be a domain you own (E.g. com.google if you own google.com), or use me.<Name>.<project>
    If these methods do nothing, you can delete them.
    Although you are checking if the sender is a player, you're doing it after you created the player instance. Instead, please check if the sender is a player before you create the player vartiable

    There is no reason for the sender to be a player in the code block. CommandSender contains the sendMessage method, so you do not need to check if the player is a sender in this case.

    That open bracket here may be messing you up here. Remove it.

    This both does nothing and is established before when you check if it was a player. Remove this block.
     
    Mitch1337 likes this.
  3. Offline

    pingpong1999

    thanks, this makes my code more efficient but still dosent solve my problem
     
  4. Offline

    Zombie_Striker

    @pingpong1999
    Here is what you should do to add what you want:
    1. Create a Hashmaps, where the keys will be Strings and the Values will be the Location. This will represent the Names of each location (Strings) and the location associated with each name.
    2. In the onEnable, load the Hashmap.
    3. In the onDisable, save the hashmap to the config.
    4. When a player wants to teleport to a location, get the location from the hashmap by it's name and teleport the player there.
    5. If you need to get all the names of each location from the hashmap, use Hashmap.keylist()
     
  5. Offline

    pingpong1999

    Thankyou!
     
Thread Status:
Not open for further replies.

Share This Page