Solved Setting hologram above ahead as player walks

Discussion in 'Spigot Plugin Development' started by Joshuak52, Mar 14, 2020.

  1. So I have a plugin and I want to set a hologram over the players had and when they walk I want it to follow them. This is what I have right now.

    Code (Text):
    @EventHandler
        public void onMove(PlayerMoveEvent event) {
            Player player = (Player) event.getPlayer();
            if(showingCrystals.contains(player.getName())) {
                if(event.getFrom().getX() != event.getTo().getX() || event.getFrom().getZ() != event.getTo().getZ()) {
                    for (Hologram hologram : HologramsAPI.getHolograms(plugin)) {
                        hologram.delete();
                    }
                    int y = (int) (player.getLocation().getBlockY() + 4);
                    World w = player.getWorld();
                    Location loc = new Location(w, player.getLocation().getX(), y, player.getLocation().getZ());
                    Hologram hologram = HologramsAPI.createHologram(plugin, loc);
                   
                    ItemStack head = new ItemStack(Material.PLAYER_HEAD);
                    SkullMeta skull = (SkullMeta) head.getItemMeta();
                    skull.setOwner(player.getName());
                    head.setItemMeta(skull);
                   
                    hologram.appendItemLine(head);
                    hologram.appendItemLine(new ItemStack(Material.END_CRYSTAL));
                    hologram.appendTextLine(ChatColor.DARK_RED + "[!]" + ChatColor.GRAY + player.getName() + ChatColor.DARK_RED + " has " + getConfig().getInt("Crystals." + player.getUniqueId()) + " pvp crystals");
                }
            } else {
                return;
            }
        }
    the biggest issue with this code is it's removing and adding the hologram every time the player takes a step even though he is not moving a full block.
     
  2. If you only want it to happen on a full block change do this: check where the hologram is currently, and get where the player is. If the distance between the two locations is greater than or equal to one, move the hologram to the player's new location.

    Also, I've never worked with that hologram api before, but why don't you just teleport the old hologram rather than recreating it?
     
  3. Honestly, I didn't even think about teleporting it hahaha. That seems to fix all of the issues, thank you my friend xD
     
  4. Haha, no problem.