Creating and Using Lists of Spawned Entities

Discussion in 'Spigot Plugin Development' started by Nathanacus, May 26, 2017.

  1. Hello everyone,

    I am trying to create my own hologram plugin, using invisible armor stands (I know there are hologram APIs out there, but I'm trying to do this with as few dependencies as possible). I can create the armor stands just fine, but I have not been able to figure out how to create and use a list of the stands that have been created, so that players can modify and delete them later.

    My code to create the stands is:
    Code (Text):
    //Create a custom hologram with: /nholo create [name] [text text text]
    if (args[0].equalsIgnoreCase("create")) {
        String holoName = args[1];
        //Create the holoText.
        //Start with the first text arg.
        String holoText = args[2];
        int arrayLength = args.length;
        //Loop through command args and add spaces between words.
        for (int i = 3; i < arrayLength; i++) {
            holoText = holoText + " " + args[i];
        //Create the hologram.
        ArmorStand stand = (ArmorStand) player.getWorld().spawnEntity(player.getLocation(), EntityType.ARMOR_STAND);
        stand.setGravity(false); //So it doesn't fall.
        stand.setVisible(false); //So you don't see the stand under the hologram.
        stand.setMarker(true); //So players can't interact with it.
        stand.setCustomName(ChatColor.translateAlternateColorCodes('&', holoText));
        //Tell the player that they've created a hologram.
        player.sendMessage(ChatColor.GREEN + "You have created a hologram called " + holoName + ".");

    As you may have gathered from the code, I would like to be able to identify the stand by the "holoName" string, which comes out of the /nholo create [name] [text] command, so that I could ultimately have commands to modify and delete the hologram. For example, I know that I would use stand.remove() to delete a hologram, but if I've created multiple stands at various points in time, how would I be able to specify which stand(s) I want removed?

    From what I can tell from searching around, it looks like my best bet is to generate a holograms.yml file to keep track of all of the stands that players have created, but I am not sure how I would then use that file to identify the specific entity to be removed, especially if I have multi-line holograms that use multiple armor stands at once. So, I guess my question is two-part: what are best practices for creating the list file, and how would I then use that file to look up and modify specific entities?

    My apologies if I could have found this already answered; I've tried YouTube videos, Google, searching these forums, and looking through the Spigot Javadoc, but have so far come up pretty empty. If I just stupidly missed something, by all means call me out on it. I used to have a better grasp of Java, but it's been a few years.

  2. They will have unique ids just like players, so you can store them based on that. You won't be able to retrieve them unless the chunk they're in is loaded, though. If you're planning on force-loading chunks (like if you want to remove a hologram in an unloaded chunk) then you may also want to save the chunk coordinates with each one.
    • Useful Useful x 1
  3. Thank you for the fast response! I will play around with entity unique IDs, I think I'm on the right track now. Thank you!
    • Friendly Friendly x 1