[1 VIEW IN 20 MINUTES?] Getting the location of a block stored in a HashMap

Discussion in 'Plugin Development' started by frozenpoptartmc, Aug 12, 2013.

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

    frozenpoptartmc

    I'm trying to spawn some mobs at a block and have them fly out in 8 directions.
    I can accomplish everything except actually getting the block I want them to spawn at.
    When a player types "!um" in the chat (um standing for Undead Mortar which is irrelevant, but whatever), I want to get the location of a block that is already stored in a HashMap and spawn the mobs there.
    The HashMap stores Location as the key, and Player as the value.
    And you can't just do hashmap.get(key); which would make life easier.

    Any help would be much appreciated and if something in my explanation doesn't make sense, let me know!
    - frozenpoptart
     
  2. Offline

    bobacadodl

    First of all, you should never store a player object in a hashmap. Instead, store the player's name, then do Bukkit.getPlayer(name) to get the player.

    Also, instead of storing the location, try converting locations to string, then converting the string back to a location

    Code:
        public static String locToString(Location l){
            String x = Integer.toString(l.getBlockX());
            String y = Integer.toString(l.getBlockY());
            String z = Integer.toString(l.getBlockZ());
            String pitch = Integer.toString((int) l.getPitch());
            String yaw = Integer.toString((int) l.getYaw());
            return l.getWorld().getName()+","+x+","+y+","+z+","+pitch+","+yaw;
        }
     
        public static Location stringToLoc(String s){
            String[] data = s.split(",");
            World w = Bukkit.getWorld(data[0]);
            Integer x = Integer.parseInt(data[1]);
            Integer y = Integer.parseInt(data[2]);
            Integer z = Integer.parseInt(data[3]);
            Integer pitch = Integer.parseInt(data[4]);
            Integer yaw = Integer.parseInt(data[5]);
            return new Location(w, x, y, z, pitch, yaw);
        }
     
Thread Status:
Not open for further replies.

Share This Page