[Tutorial] Holograms - 1.8

Discussion in 'Spigot Plugin Development' started by Assossa, May 9, 2015.

  1. -={ Intro }=-
    I was bored today, so I decided to make a tutorial. I always wondered how servers like Mineplex and Hypixel made that floating text. I saw the Hologram plugins out there, but I wanted to write my own from scratch so I understand it better.

    Today I will share with you the secret of how it all works, and rejoice there is no nms or packets involved!
    By the way, I will not be covering touchscreen holograms in this tutorial, that involves lots of trigonometry and I am lazy.​


    -={ Explanation }=-
    So how exactly do these holograms work?
    Well, they actually are very simple. All that happens is an Armor Stand is spawned, made invisible, and given a name. That's literally it!


    -={ Code }=-
    The code to do all this is very easy. Feel free to modify it as much as you want!
    PHP:
    ArmorStand as = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND); //Spawn the ArmorStand

    as.setGravity(false); //Make sure it doesn't fall
    as.setCanPickupItems(false); //I'm not sure what happens if you leave this as it is, but you might as well disable it
    as.setCustomName(text); //Set this to the text you want
    as.setCustomNameVisible(true); //This makes the text appear no matter if your looking at the entity or not
    as.setVisible(false); //Makes the ArmorStand invisible
    This snippet (Thanks to @MCAeolus ) prevents players from right clicking items onto the armorstand:
    PHP:
    @EventHandler
    public void manipulate(PlayerArmorStandManipulateEvent e)
    {
            if(!e.getRightClicked().isVisible())
            {
                e.setCancelled(true);
            }
    }

    P.S. If this helped you at all, remember to leave a rating! It really helps me out!​
     
    #1 Assossa, May 9, 2015
    Last edited: May 9, 2015
    • Useful x 21
    • Like x 15
    • Agree x 3
    • Informative x 2
    • Creative x 1
  2. Awesome tutorial. Now do a tutorial on touchscreen holograms ;)
     
    • Agree Agree x 2
  3. I will later on, I don't have the time right now.
     
  4. Very nice tutorial!
    I made a little command for utilizing it if anyone would want to see an example:

    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {

            if(cmd.getName().equals("hologram")){

                if(sender instanceof Player){

                    if(args.length != 0) {

                        Location location = ((Player) sender).getLocation();
                        ArmorStand as = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND); //Spawn the ArmorStand


                        String myString = ""; //we're going to store the arguments here

                        String arg;
                        for(int i = 0; i < args.length; i++){ //loop threw all the arguments
                            if(i != args.length - 1) {
                                arg = args[i] + " "; //get the argument, and add a space so that the words get spaced out
                            }else{
                                arg = args[i]; //So there will be no space after last argument
                            }

                            myString = myString + arg; //add the argument to myString

                        }
                        String textFinal = ChatColor.translateAlternateColorCodes('&',myString);

                        as.setGravity(false); //Make sure it doesn't fall
                        as.setCanPickupItems(false); //I'm not sure what happens if you leave this as it is, but you might as well disable it
                        as.setCustomName(textFinal); //Set this to the text you want
                        as.setCustomNameVisible(true); //This makes the text appear no matter if your looking at the entity or not
                        as.setVisible(false); //Makes the ArmorStand invisible

                    }else{

                        sender.sendMessage("No extra parameter set!");

                    }

                }else{

                    System.out.println("Only players can do this.");


                }

            }

            return false;
        }
     
    • Useful Useful x 3
    • Like Like x 1
  5. Hi, can you make sure that nobody can place anything on that invisible armor stand as well? :D
     
  6. Lol, wasn't sure what you meant about placing items until I did this...
    [​IMG]

    ANYWAYS, until @Assossa puts together something better, I made a little getover fix to stop that from happening.
    Simply add this event:

    Code (Text):
    @EventHandler
        public void manipulate(PlayerArmorStandManipulateEvent e){

            if(!e.getRightClicked().isVisible()){

                e.setCancelled(true);

            }

        }
    Hope that helps ;)
     
    • Like Like x 4
  7. Thanks! I was trying to figure out how to prevent it :p
     
    • Friendly Friendly x 1
  8. If you don't have the knowledge to create touchscreen holograms out of this... You should REALLY look at the docs/learn Java...
     
    • Funny Funny x 1
  9. I guess you would do it using a PlayerInteractEntityEvent or EntityDamageByEntityEvent, but the problem with that is that you need to hit below the text to get it working, I think @HarrysMc ment that when you actually touch the text something happens
     
  10. Hahah awesome tutorial :D
     
  11. I do not think That hypixel and mineplex are using this method ..
    They could use armorstand but with packets .
     
  12. Not really. The text appears above the entity. So a entity interact event would not work. You would have to check for all right clicks, then check if the player is in range and if they are looking in the same direction as the entity.
    They do use armorstands, and they probably do use packets. Unfortunately, I am not well versed in packets.
     
  13. Thank you about it :D
     
  14. Thank you so much for posting this.
    It is pretty easy but really clever!
     
  15. Thanks! Please remember to leave a rating on my post :D
     
  16. First: I already know how to do it it was just an idea for a follow up tutorial.
    Second: You are saying that by learning Java only you would be able to do that and that is not true you would need to know quite a bit of Bukkit first.
     
    #16 HarrysMc, May 11, 2015
    Last edited: May 11, 2015
    • Agree Agree x 5
  17. Will this interfere with other plugins that modify existing entities and/or armor stands? I use similar code but it does it all through packets, so the server doesn't see them but the client does, so there's no way for the server to accidentally delete the entities.

    This is a great tutorial either way. It is very straightforward and easy to follow. I'm glad you posted it!
     
    • Like Like x 1
  18. Enlighten us, master coder ;)
     
    • Funny Funny x 5
    • Optimistic Optimistic x 2
  19. Thanks!
     
  20. amazing , i will use that on my next plugin!