Manage Holograms.

Discussion in 'Spigot Plugin Development' started by JoseGamer_PT, May 28, 2016.

  1. HELLO SPIGOT COMMUNITY!

    So... I am working with holograms.
    At the moment this is my hologram class:

    HDs.java
    Code (Text):

    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;

    import pt.josegamerpt.realholograms.config.HologramsYML;

    public class HDs {

        public static void spawnHologram(Location loc, int i, String text) {
            ArmorStand as = (ArmorStand) loc.getWorld().spawnEntity(loc, EntityType.ARMOR_STAND);
            as.setGravity(false);
            as.setCanPickupItems(false);
            if (i == 0) {
            } else {
                as.setCustomName(text);
            }
            as.setCustomNameVisible(true);
            as.setVisible(false);
        }

        public static void deleteHologram(String name) {
            String hdname = HologramsYML.ficheiro().getString("Holograms." + name + ".Name");
            for (World w : Bukkit.getWorlds()) {
                List<Entity> entidades = w.getEntities();
                for (Entity hd : entidades) {
                    if (hd.getType().equals(EntityType.ARMOR_STAND)) {
                        if (hd.getName().equals(hdname)) {
                            hd.remove();
                        }
                    }
                }
            }
        }
     
    config.yml
    Code (Text):

    DefaultHologramCreateMessage: '&fThis is an Example Message! &bDo /hd addline <hologram> <line>!'
     
    holograms.yml (example storage system)
    Code (Text):

    Holograms:
      Magod:
        Name: §fThis is an Example Message! §bDo /hd addline <hologram> <line>!
        loc:
          x: -6.524996594140161
          y: 81.0
          z: 361.60554144679764
      SampleText:
        Name: 'Testing this hologram!'
        loc:
          x: -4.512213134335532
          y: 82.0
          z: 367.60554144679764
     
    How i can, with this code:

    - Remove An Hologram
    - Despawn all Holograms in holograms.yml in reload/restart.
    - Spawn all Holograms in holograms.yml in reload/restart

    THANKS FOR YOUR TIME!
    - JOSE
     
  2. When you create the hologram you could store it's uuid and then when you want to delete it get all entities in the world(I think world.getEntities()) and run through it comparing the uuids.
     
  3. Remove hologram: Unset in config, and delete entity.
    Despawn all: As you did, hd.remove on all of them.
    Spawn all: Loop through and spawn them.
     
  4. Look at my HDs.java

    I tried build an method, but didn't work D;
     
  5. Yeah, but you'll need to change it if you're going to use configuration files.

    To create a hologram, make a new instance of the class with the Location/Display in the constructor.
     
  6. Like this ?

    Code (Text):

    import org.bukkit.Location;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;

    public class Holograms {

        public static Entity entity;
        public static Location location;
        public static String display;

        public Holograms(Location location, String display) {
            Holograms.location = location;
            Holograms.display = display;
            spawn();
        }

        public static void spawn() {
            if (Holograms.entity != null) {
                delete();
            }
            ArmorStand stand = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
            stand.setGravity(false);
            stand.setCanPickupItems(false);
            stand.setCustomNameVisible(true);
            stand.setCustomName(display);
            stand.setVisible(false);
            Holograms.entity = stand;
        }

        public static void delete() {
            entity.remove();
            Holograms.entity = null;
        }
    }
     
     
  7. I'm confused, you haven't changed anything lol.
     
  8. Sorry. Wrong Code ;p

    Code (Text):

    import org.bukkit.Location;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;

    public class Holograms {

        public static Entity entity;
        public static Location location;
        public static String display;

        public static void spawn(Location location, String display) {
            Holograms.location = location;
            Holograms.display = display;
            if (Holograms.entity != null) {
                delete();
            }
            ArmorStand stand = (ArmorStand) location.getWorld().spawnEntity(location, EntityType.ARMOR_STAND);
            stand.setGravity(false);
            stand.setCanPickupItems(false);
            stand.setCustomNameVisible(true);
            stand.setCustomName(display);
            stand.setVisible(false);
            Holograms.entity = stand;
        }

        public static void delete() {
            entity.remove();
            Holograms.entity = null;
        }
     
     
  9. Oh god you've made it worse. It isn't going to work if you make the variables static. I think you need to learn more java before using my code.

    Keep it the way I had it, you need to use your own configuration to load them.
     
  10. Ok. D;
     
  11. So.. i have a problem when i am triyng to delete an hologram:

    Code (Text):

    public static void deleteHologram(String name) {
            String hdname = HologramsYML.ficheiro().getString("Holograms." + name + ".Name");
            World w = Bukkit.getServer().getWorld(HologramsYML.ficheiro().getString("Holograms." + name + ".world"));
            List<Entity> entidades = w.getEntities();
            Log.info("1");
            for (Entity e : entidades) {
                if (e.getType().equals(EntityType.ARMOR_STAND)) {
                    Log.info("2");
                    if (e.getCustomName().equals(hdname)) {
                        Log.info("3");
                        e.remove();
                    }
                }
            }
        }
     
    I am trying to get the "scoreboard" name.
    But the scoreboard don't be removed!