Resource Quick n' Dirty Holograms

Discussion in 'Plugin Help/Development/Requests' started by Funnygatt, Feb 12, 2015.

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

    Funnygatt

    Hi Everyone,

    This is my first post to the Bukkit Resources, so hopefully I format this well :)

    Due to the fact that I'm not the best at Java, I haven't been able to learn how Packets work thoroughly, and I needed some Holograms. I previously used LibraryAddict's holograms which he posted on GitHub, but I wanted to use something I could understand. So I made some Quick n' Dirty Holograms that use actual Armor Stand entities. It's got minimal abilities and effects, but it does work quite well; except for the fact that sometimes the Armor Stand is visible for a tick.

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.EntityTeleportEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class QuickHolograms implements Listener {
       
       
       
        String[] lines = null;
        Location loc = null;
        ArmorStand stand = null;
        ArrayList<ArmorStand> stands = new ArrayList<>();
        double distanceBetweenStands = 0.24;
        Boolean active = false;
        Entity entityToFollow = null;
        Plugin plugin = null;
       
        public QuickHolograms(Location location, String[] text, Plugin p){
            lines = text;
            loc = location;
            active = true;
            startHologram();
            QuickHologramTracker.holoList.add(this);
            plugin = p;
    
            Bukkit.getPluginManager().registerEvents(this, p);
    
    
        }
    
        @EventHandler
        public void onPassengerDeath(EntityDeathEvent e){
            if (active)
            if (e.getEntity() == entityToFollow){
                this.deleteHologram();
            }
        }
    
        @EventHandler
        public void onPassengerTeleport(EntityTeleportEvent e){
            if (active)
            if (e.getEntity() == entityToFollow){
                this.stopFollowingEntity();
                e.setCancelled(true);
                final Entity ent = e.getEntity();
                final Location loc = e.getTo();
                Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
                    @Override
                    public void run() {
                        ent.teleport(loc);
                    }
                }, 10L);
                this.followEntity(ent);
    
            }
        }
    
        public List<ArmorStand> getEntities(){
            return this.stands;
        }
    
        public void moveHologram(Location location){
            if (active){
                loc = location;
                updateHologram(this);
            }
        }
    
        public void followEntity(Entity entity){
            entityToFollow = entity;
            updateHologram(this);
        }
    
        public void stopFollowingEntity(){
            entityToFollow = null;
            updateHologram(this);
        }
       
        public void deleteHologram(){
            if (active){
                lines = null;
                for (ArmorStand stand : stands){
                    stand.remove();
                }
    
                active = false;
                QuickHologramTracker.holoList.remove(this);
                stands.clear();
            }
        }
       
        private void startHologram(){
            if (active){
                Integer lineID = -1;
                Location spawnLoc = loc;
                spawnLoc.setY(spawnLoc.getY() + distanceBetweenStands);
                ArmorStand stand = null;
                ArmorStand prevStand = null;
                for (String line : lines){
                    spawnLoc.setY(spawnLoc.getY() - distanceBetweenStands);
                    stand = loc.getWorld().spawn(spawnLoc, ArmorStand.class);
                    stand.setRemoveWhenFarAway(false);
                    stand.setVisible(false);
                    stand.setSmall(true);
                    stand.setBasePlate(false);
                    stand.setGravity(false);
                    stand.setHelmet(new ItemStack(Material.BONE));
                    stand.setChestplate(new ItemStack(Material.BONE));
                    stand.setLeggings(new ItemStack(Material.BONE));
                    stand.setBoots(new ItemStack(Material.BONE));
                    stand.setArms(false);
                    stand.setCustomNameVisible(true);
                    stand.setCustomName(colorString(line));
                    stands.add(stand);
                    LivingEntity ent = stand;
                    ent.setRemoveWhenFarAway(false);
                    lineID++;
                }
            }
        }
    
        public void updateHologram(QuickHolograms holo){
            if (active){
                for (ArmorStand stand : stands){
                    stand.remove();
                }
                stands.clear();
                Location spawnLoc = loc;
                spawnLoc.setY(spawnLoc.getY() + distanceBetweenStands);
                ArmorStand stand = null;
                ArmorStand prevStand = null;
                for (String line : lines){
                    spawnLoc.setY(spawnLoc.getY() - distanceBetweenStands);
                    stand = loc.getWorld().spawn(spawnLoc, ArmorStand.class);
                    stand.setRemoveWhenFarAway(false);
                    stand.setVisible(false);
                    stand.setSmall(true);
                    stand.setBasePlate(false);
                    stand.setGravity(false);
                    stand.setHelmet(new ItemStack(Material.BONE));
                    stand.setChestplate(new ItemStack(Material.BONE));
                    stand.setLeggings(new ItemStack(Material.BONE));
                    stand.setBoots(new ItemStack(Material.BONE));
                    stand.setArms(false);
                    stand.setCustomNameVisible(true);
                    stand.setCustomName(colorString(line));
                    LivingEntity ent = stand;
                    if (prevStand != null){
                        if (entityToFollow != null) {
                            stand.setPassenger(prevStand);
                        }
                    }
                    ent.setRemoveWhenFarAway(false);
                    stands.add(stand);
                    prevStand = stand;
                }
                if (entityToFollow != null) {
                    entityToFollow.setPassenger(prevStand);
                }
    
            }
        }
       
        private String colorString(String s){
            return ChatColor.translateAlternateColorCodes('&', s);
        }
       
    }
    
    Any tips and criticism is welcomed, but please don't tell me to use Packets - I know I should, but this is a learning experience for me.

    Thanks - Funnygatt!
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives
     
  3. Offline

    Funnygatt

    Oh, thank you. I couldn't figure out how to do it. <3
     
Thread Status:
Not open for further replies.

Share This Page