Error Hologram

Discussion in 'Spigot Plugin Development' started by ArestPlay, May 25, 2018.

  1. Hello. I wrote a plugin for Holograms but unfortunately instead of holograms my horse jumps with item. Does anyone know where I made a mistake?

    Sorry for my English

    Code:

    Code (Text):
    package pl.mc4play.api.objects.hologram;

    import net.minecraft.server.v1_8_R3.*;
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    public class Hologram {
        private static double distance = 0.23;
        private String id;
        private Location location;
        private List<String> lines;
        private List<Integer> ids;

        public Hologram(final String id, final Location location) {
            this.id = id;
            this.location = location;
            this.lines = new ArrayList<>();
            this.ids = new ArrayList<>();
        }

        private List<Integer> showLine(final Location location, final String string) {
            final WorldServer worldServer = ((CraftWorld)location.getWorld()).getHandle();
            final EntityWitherSkull entityWitherSkull = new EntityWitherSkull(worldServer);
            entityWitherSkull.setLocation(location.getX(), location.getY() + 1 + 55, location.getZ(), 0, 0);
            final PacketPlayOutSpawnEntity packetPlayOutSpawnEntity = new PacketPlayOutSpawnEntity(entityWitherSkull, 64);
            final EntityHorse entityHorse = new EntityHorse(worldServer);
            entityHorse.setLocation(location.getX(), location.getY() + 55, location.getZ(), 0, 0);
            entityHorse.setAge(-1700000);
            entityHorse.setCustomName(string);
            entityHorse.setCustomNameVisible(true);
            final PacketPlayOutSpawnEntityLiving packetPlayOutSpawnEntityLiving = new PacketPlayOutSpawnEntityLiving(entityHorse);
            for (final Player player : Bukkit.getOnlinePlayers()) {
                final EntityPlayer entityPlayer = ((CraftPlayer)player).getHandle();
                entityPlayer.playerConnection.sendPacket(packetPlayOutSpawnEntityLiving);
                entityPlayer.playerConnection.sendPacket(packetPlayOutSpawnEntity);
                final PacketPlayOutAttachEntity packetPlayOutAttachEntity = new PacketPlayOutAttachEntity(0, entityHorse, entityWitherSkull);
                entityPlayer.playerConnection.sendPacket(packetPlayOutAttachEntity);
            }
            return Arrays.asList(entityWitherSkull.getId(), entityHorse.getId());
        }

        private void show(final Location location) {
            if (lines == null) {
                return;
            }
            final Location first = location.clone().add(0, (lines.size() / 2) * distance, 0);
            for (int i = 0; i < lines.size(); i++) {
                ids.addAll(showLine(first.clone(), lines.get(i)));
                first.subtract(0, distance, 0);
            }
            this.location = location;
        }

        public void change(final String[] lines) {
            this.lines.clear();
            try {
                destroy();
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.lines.addAll(Arrays.asList(lines));
            if (location != null) {
                show(location);
            }
        }

        private void destroy() {
            int[] ints = new int[ids.size()];
            for (int i = 0; i < ints.length; i++) {
                if (i != 0) {
                    ints[i] = ids.get(i);
                }
            }
            final PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(ints);
            for (final Player player : Bukkit.getOnlinePlayers()) {
                ((CraftPlayer)player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy);
            }
            ids.clear();
        }
    }
     
  2. ciz

    ciz

    You should use armour stands, these are much more easier to work with.
     
    • Agree Agree x 1
  3. Like @ciz said, much easier with armostands and easy to handle.
    Try spawning an invisible armorstand and set a display name(if I remember correctly)
     
    #3 Nosmakos, May 25, 2018
    Last edited by a moderator: May 26, 2018
    • Like Like x 1