Teleporting to a hashmap location.

Discussion in 'Plugin Development' started by xXRobbie21Xx, Sep 6, 2014.

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

    xXRobbie21Xx

    Okay, im making a parkour plugin but when trying to get a player to teleport back to there latest checkpoint, it doesn't work.
    When a player clicks something, it adds them to a hashmap with there location.
    When they click something else it teleports them to that hashmap location if there in it.

    It seems pretty strait forward to me but, its telling me that the player isn't in the hashmap upon trying to teleport back, and the only situation that it does tell me the player teleported back, is when i dont move at all. (Set the check point, then go back to it without moving) <that tells me that it works.

    Hashmap:
    Code:
        public Map<UUID, Location> checkPoint = new HashMap<UUID, Location>();
    Edit: I want to point out that there are no errors in the console.

    Click event:
    Code:
    @EventHandler
        public void onInvClickEvent(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            UUID uuid = p.getUniqueId();
     
            if (e.getInventory().equals(parkourInv)) {
                if (e.getCurrentItem().getType().equals(Material.REDSTONE)) {
                    if (parkour.containsKey(uuid)) {
     
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have set your checkpoint!");
                        checkPoint.put(uuid, p.getLocation());
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
     
                    } else {
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You must enter a parkour course in order to use this feature!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType().equals(Material.SUGAR)) {
                    Location loc = checkPoint.get(uuid);
                    if (!checkPoint.containsKey(uuid)) {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You havent set a checkpoint yet!");
                    } else {
                   
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.teleport(loc);
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You have teleported to your most recent checkpoint!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType()
                        .equals(Material.GLOWSTONE_DUST)) {
                    if (parkour.containsKey(uuid)) {
     
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have left the " + parkour.get(uuid)
                                + ChatColor.BLUE + " parkour!");
                        parkour.remove(uuid);
     
                    } else {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You arent participating in any parkour courses!");
                    }
     
                }
     
                e.setCancelled(true);
     
            }
     
        }
     
    }
    
    If someone could let me know where im messing up, it would be greatly appreciated.

    If you need the whole class just let me know.

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

    OracleTarget

    xXRobbie21Xx
    1) Did you register the events?

    Can you please show the whole class?
     
  3. Offline

    xXRobbie21Xx


    Everything is registered.
    Code:
    package parkourMain;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.util.UUID;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.FireworkEffect.Type;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.block.Sign;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class parkourMain extends JavaPlugin implements Listener {
     
        public Map<UUID, String> parkour = new HashMap<UUID, String>();
        public Map<UUID, Location> checkPoint = new HashMap<UUID, Location>();
     
        String prefix = ChatColor.translateAlternateColorCodes('&', getConfig()
                .getString("ChatPrefix"));
     
        public static Inventory parkourInv;
     
        public void onEnable() {
            parkourInv = Bukkit.createInventory(null, 9, "§6§lParkour");
     
            getServer().getPluginManager().registerEvents(this, this);
     
            saveDefaultConfig();
     
        }
     
        public void onDisable() {
     
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
     
            if (sender instanceof Player) {
                Player p = (Player) sender;
     
                if (commandLabel.equalsIgnoreCase("ParkourOpen")) {
     
                    ItemStack setPoint = new ItemStack(Material.REDSTONE);
                    ItemMeta smd = setPoint.getItemMeta();
                    smd.setDisplayName(ChatColor.GREEN + "§nSet Checkpoint!");
                    setPoint.setItemMeta(smd);
     
                    ItemStack telePoint = new ItemStack(Material.SUGAR);
                    ItemMeta tmd = telePoint.getItemMeta();
                    tmd.setDisplayName(ChatColor.GREEN + "§nTeleport back!");
                    telePoint.setItemMeta(tmd);
     
                    ItemStack leave = new ItemStack(Material.GLOWSTONE_DUST);
                    ItemMeta lmd = leave.getItemMeta();
                    lmd.setDisplayName(ChatColor.GREEN + "§nLeave the Parkour!");
                    leave.setItemMeta(lmd);
                    checkPoint.remove(p.getUniqueId());
     
                    ItemStack filler = new ItemStack(Material.THIN_GLASS);
                    ItemMeta fmd = filler.getItemMeta();
                    fmd.setDisplayName(ChatColor.GREEN + "§n-+-+-+-+-+-+-");
                    filler.setItemMeta(fmd);
     
                    parkourInv.clear();
                    parkourInv.setItem(0, setPoint);
                    parkourInv.setItem(1, filler);
                    parkourInv.setItem(2, filler);
                    parkourInv.setItem(3, filler);
                    parkourInv.setItem(4, telePoint);
                    parkourInv.setItem(5, filler);
                    parkourInv.setItem(6, filler);
                    parkourInv.setItem(7, filler);
                    parkourInv.setItem(8, leave);
     
                    p.openInventory(parkourInv);
                }
            }
     
            return false;
     
        }
     
        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Action action = e.getAction();
            Block b = e.getClickedBlock();
            UUID uuid = p.getUniqueId();
     
            if (action == Action.RIGHT_CLICK_BLOCK
                    && e.getClickedBlock().getType() == Material.SIGN_POST) {
                Sign s = (Sign) b.getState();
                if (s.getLine(1).equalsIgnoreCase(ChatColor.BLUE + "[Start]")) {
                    if (!s.getLine(2).isEmpty()) {
     
                        if (!parkour.containsKey(uuid)) {
                            parkour.put(p.getUniqueId(), s.getLine(2));
                            p.sendMessage(prefix + ChatColor.BLUE
                                    + "You have entered the " + s.getLine(2)
                                    + ChatColor.BLUE + " parkour!");
     
                        }
     
                        else if (parkour.containsKey(uuid)) {
                            p.sendMessage(prefix + ChatColor.BLUE
                                    + "You are allready entered in the "
                                    + parkour.get(uuid) + ChatColor.BLUE
                                    + " parkour!");
                        }
     
                    }
     
                }
     
                else if (s.getLine(1).equalsIgnoreCase(ChatColor.BLUE + "[Finish]")) {
                    if (s.getLine(2).equalsIgnoreCase(parkour.get(uuid))) {
     
                        if (parkour.containsKey(uuid)) {
     
                            Firework fw = (Firework) p.getWorld().spawn(
                                    p.getLocation(), Firework.class);
                            FireworkMeta fwm = fw.getFireworkMeta();
                            fwm.addEffect(FireworkEffect.builder().flicker(true)
                                    .trail(true).with(Type.BALL)
                                    .with(Type.BALL_LARGE).withColor(Color.AQUA)
                                    .withColor(Color.BLUE).withColor(Color.PURPLE)
                                    .withFade(Color.TEAL).build());
                            fwm.setPower(1);
                            fw.setFireworkMeta(fwm);
     
                            p.playSound(p.getLocation(), Sound.EXPLODE, 1, 1);
     
                            Bukkit.broadcastMessage(prefix + p.getDisplayName()
                                    + ChatColor.GOLD + ChatColor.BOLD
                                    + " has completed the " + s.getLine(2)
                                    + ChatColor.GOLD + ChatColor.BOLD + " parkour!");
                            parkour.remove(uuid);
                        } else {
                            p.sendMessage(prefix
                                    + ChatColor.BLUE
                                    + "You arent participating in any parkour courses!");
                        }
     
                    }
     
                }
     
            }
     
        }
     
        @EventHandler
        public void onSignChange(SignChangeEvent e) {
            Player p = e.getPlayer();
            String name = e.getLine(1);
            if (e.getLine(0).equalsIgnoreCase("[Start]")) {
                if (!e.getLine(1).isEmpty()) {
                    p.sendMessage(prefix + ChatColor.BLUE + "The " + ChatColor.RED
                            + name + ChatColor.BLUE + " start sign was set!");
                    e.setLine(0, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    e.setLine(1, ChatColor.BLUE + "[Start]");
                    e.setLine(2, ChatColor.DARK_PURPLE + name);
                    e.setLine(3, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
     
                }
     
            }
     
            else if (e.getLine(0).equalsIgnoreCase("[Finish]")) {
                if (!e.getLine(1).isEmpty()) {
                    p.sendMessage(prefix + ChatColor.BLUE + "The " + ChatColor.RED
                            + name + ChatColor.BLUE + " start sign was set! ");
                    e.setLine(0, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    e.setLine(1, ChatColor.BLUE + "[Finish]");
                    e.setLine(2, ChatColor.DARK_PURPLE + name);
                    e.setLine(3, ChatColor.BOLD + "+*+*+*+*+*+*+*+");
                    p.playSound(p.getLocation(), Sound.ANVIL_LAND, 1, 1);
                }
            }
     
        }
     
        @EventHandler
        public void onInvClickEvent(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            UUID uuid = p.getUniqueId();
     
            if (e.getInventory().equals(parkourInv)) {
                if (e.getCurrentItem().getType().equals(Material.REDSTONE)) {
                    if (parkour.containsKey(uuid)) {
     
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have set your checkpoint!");
                        checkPoint.put(uuid, p.getLocation());
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
     
                    } else {
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You must enter a parkour course in order to use this feature!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType().equals(Material.SUGAR)) {
                    Location loc = checkPoint.get(uuid);
                    if (!checkPoint.containsKey(uuid)) {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You havent set a checkpoint yet!");
                    } else {
                       
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.teleport(loc);
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You have teleported to your most recent checkpoint!");
                    }
     
                }
     
                else if (e.getCurrentItem().getType()
                        .equals(Material.GLOWSTONE_DUST)) {
                    if (parkour.containsKey(uuid)) {
     
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You have left the " + parkour.get(uuid)
                                + ChatColor.BLUE + " parkour!");
                        parkour.remove(uuid);
     
                    } else {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You arent participating in any parkour courses!");
                    }
     
                }
     
                e.setCancelled(true);
     
            }
     
        }
     
    }
    
     
  4. Offline

    CraftBang

    So this part is wrong
    Code:
      else if (e.getCurrentItem().getType().equals(Material.SUGAR)) {
                    Location loc = checkPoint.get(uuid);
                    if (!checkPoint.containsKey(uuid)) {
                        p.sendMessage(prefix + ChatColor.BLUE
                                + "You havent set a checkpoint yet!");
                    } else {
                     
                        p.playSound(p.getLocation(), Sound.NOTE_PLING, 1, 1);
                        p.teleport(loc);
                        p.sendMessage(prefix
                                + ChatColor.BLUE
                                + "You have teleported to your most recent checkpoint!");
                    }
     
                }
    
    First of all, the location called loc CAN be null.
    You're getting the location BEFORE checking if checkPoint contains the player's uuid.

    And can you explain me again what's going wrong, I don't really understand it, because I don't see any event that listens to the player moving, what maybe could do something with your checkPoint hashmap.
     
  5. Offline

    xXRobbie21Xx

    CraftBang Okay, i posted the whole class in my last reply, and whats wrong is the fact that when i try to go back to a checkpoint (after i move around) , it tells me im not in the hashmap, however when set a checkpoint and dont move, it says that im teleported.
    As for me getting the location before checking, it doesnt affect outcome either way, within this current bug. (i just switched it)
     
  6. Offline

    CraftBang

    Is there any PlayerMoveEvent in other classes?

    Well he already passed that check, so it's not possible that this is wrong. But ye, it's not the best idea to use this code :p
    A better idea should be the name.

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

    xXRobbie21Xx

    CraftBang No thats the only class.
    And can you explain this please: " But ye, it's not the best idea to use this code :p
    A better idea should be the name."
     
  8. Offline

    DusRonald

    Example code:
    Code:java
    1.  
    2. private HashMap<String, Location> locs = new HashMap<String, Location>();
    3.  
    4. public void teleport(Player p) {
    5. if(locs.containsKey(p.getName())){
    6. p.teleport(locs.get(p.getName()));
    7. }
    8. }
    9.  
    10. public void addToLocs(String playerName, World world, double x, double y, double z){
    11. Location loc = new Location(world, x, y ,z);
    12.  
    13. if(!locs.containsKey(playerName)){
    14. locs.put(playerName, loc);
    15. }
    16.  
    17. }
    18.  
    19.  
     
  9. Offline

    xXRobbie21Xx

    DusRonald wow, thanks. I know spoon feeding is looked down upon, but i can assure you that showing me that, tought me more than any description could :p
     
  10. Offline

    DusRonald

    Euhmm no thanks ?
     
  11. Offline

    xXRobbie21Xx

    DusRonald bro i was thanking you for showing me what you did lol
     
  12. Offline

    CraftBang

    Well you're checking if the INVENTORY is the same, but I think you can better check if the INVENTORY name is the same. Because if you change something about the inventory (lets say an extra item or whatever) than your code wno't work anymore.
     
  13. Offline

    DusRonald

    Now i think "what"..

    Oh lol?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
Thread Status:
Not open for further replies.

Share This Page