Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package us.zonix.core.holograms;
- import lombok.Getter;
- import net.minecraft.server.v1_8_R3.*;
- import org.bukkit.Location;
- import org.bukkit.configuration.ConfigurationSection;
- import org.bukkit.configuration.file.FileConfiguration;
- import us.zonix.core.CorePlugin;
- import us.zonix.core.util.Color;
- import us.zonix.core.util.LocationUtil;
- import us.zonix.core.util.PlayerUtil;
- import java.io.IOException;
- import java.util.*;
- import java.util.concurrent.ThreadLocalRandom;
- /**
- * Created by Marko on 09.03.2019.
- */
- @Getter
- public class Hologram extends NMSHelper {
- @Getter
- private static Map<String, Hologram> holograms = new HashMap<>();
- private String message, name;
- private Location location;
- private int entityId;
- private List<Hologram> linesAbove = new ArrayList<>();
- private List<Hologram> linesBelow = new ArrayList<>();
- public Hologram() {
- this.load();
- }
- public Hologram(String message, Location location) {
- this(message, "hologram-" + ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE), location, true);
- }
- public Hologram(String message, String name, Location location, boolean add) {
- this.message = message;
- this.name = name;
- this.location = location;
- this.entityId = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
- if(add) {
- holograms.put(name, this);
- }
- }
- private void load() {
- FileConfiguration config = CorePlugin.getInstance().getHolograms().getConfiguration();
- ConfigurationSection section = config.getConfigurationSection("holograms");
- if(section == null) {
- return;
- }
- section.getKeys(false).forEach(key -> {
- key = "holograms." + key + '.';
- Hologram hologram = new Hologram(config.getString(key + "message"), config.getString(key + "name"), LocationUtil.convertLocation(config.getString(key + "location")), true);
- ConfigurationSection aboveSection = config.getConfigurationSection(key + "above");
- if(aboveSection != null) {
- for(String root : aboveSection.getKeys(false)) {
- String aboveKey = key + "above." + root + '.';
- hologram.getLinesAbove().add(new Hologram(config.getString(aboveKey + "message"), config.getString(aboveKey + "name"), LocationUtil.convertLocation(config.getString(aboveKey + "location")), false));
- }
- }
- ConfigurationSection belowSection = config.getConfigurationSection(key + "below");
- if(belowSection != null) {
- for(String root : belowSection.getKeys(false)) {
- String belowKey = key + "below." + root + '.';
- hologram.getLinesBelow().add(new Hologram(config.getString(belowKey + "message"), config.getString(belowKey + "name"), LocationUtil.convertLocation(config.getString(belowKey + "location")), false));
- }
- }
- hologram.spawn(true);
- });
- }
- public void save() {
- FileConfiguration config = CorePlugin.getInstance().getHolograms().getConfiguration();
- config.set("holograms", null);
- holograms.values().stream().filter(Objects::nonNull).forEach(hologram -> {
- String key = "holograms." + hologram.getName() + '.';
- config.set(key + "message", hologram.getMessage());
- config.set(key + "name", hologram.getName());
- config.set(key + "location", LocationUtil.parseLocation(hologram.getLocation()));
- if(hologram.getLinesAbove() != null && !hologram.getLinesAbove().isEmpty()) {
- for(Hologram line : hologram.getLinesAbove()) {
- String aboveKey = key + "above." + line.getName() + '.';
- config.set(aboveKey + "message", line.getMessage());
- config.set(aboveKey + "name", line.getName());
- config.set(aboveKey + "location", LocationUtil.parseLocation(line.getLocation()));
- }
- }
- if(hologram.getLinesBelow() != null && !hologram.getLinesBelow().isEmpty()) {
- for(Hologram line : hologram.getLinesBelow()) {
- String belowKey = key + "below." + line.getName() + '.';
- config.set(belowKey + "message", line.getMessage());
- config.set(belowKey + "name", line.getName());
- config.set(belowKey + "location", LocationUtil.parseLocation(line.getLocation()));
- }
- }
- });
- try {
- config.save(CorePlugin.getInstance().getHolograms().getFile());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void sendTeleportPacket(EntityPlayer player) {
- PacketPlayOutEntityTeleport teleport = new PacketPlayOutEntityTeleport();
- NMSHelper.setValueStatic(teleport, "a", this.entityId);
- NMSHelper.setValueStatic(teleport, "b", MathHelper.floor(this.location.getX() * 32.0));
- NMSHelper.setValueStatic(teleport, "c", MathHelper.floor((this.location.getY()) * 32.0));
- NMSHelper.setValueStatic(teleport, "d", MathHelper.floor(this.location.getZ() * 32.0));
- NMSHelper.setValueStatic(teleport, "g", true);
- player.playerConnection.sendPacket(teleport);
- }
- public void sendSpawnPackets(EntityPlayer player, boolean rebuild) {
- if(PlayerUtil.getVersion(player.getBukkitEntity()) > 5) {
- PacketPlayOutEntityTeleport teleport = new PacketPlayOutEntityTeleport();
- if(!rebuild) NMSHelper.setValueStatic(teleport, "a", this.entityId);
- NMSHelper.setValueStatic(teleport, "b", MathHelper.floor(this.location.getX() * 32.0));
- NMSHelper.setValueStatic(teleport, "c", MathHelper.floor((this.location.getY()/* + 54.55*/) * 32));
- NMSHelper.setValueStatic(teleport, "d", MathHelper.floor(this.location.getZ() * 32.0));
- NMSHelper.setValueStatic(teleport, "g", true);
- PacketPlayOutSpawnEntityLiving stand = new PacketPlayOutSpawnEntityLiving();
- if (!rebuild) NMSHelper.setValueStatic(stand, "a", this.entityId);
- NMSHelper.setValueStatic(stand, "b", 30);
- NMSHelper.setValueStatic(stand, "c", MathHelper.floor(this.location.getX() * 32.0));
- NMSHelper.setValueStatic(stand, "d", MathHelper.floor((this.location.getY()) * 32.0));
- NMSHelper.setValueStatic(stand, "e", MathHelper.floor(this.location.getZ() * 32.0));
- DataWatcher watcher = new DataWatcher((Entity) null);
- watcher.a(0, (byte) 0x20); // Invisible
- watcher.a(2, getNiceMessage()); // And set the armor stand name to the hologram text
- watcher.a(3, (byte) 1); // Always show nametag
- watcher.a(10, (byte) 0x16); // Zero Bounding Box (Marker)
- NMSHelper.setValueStatic(stand, "l", watcher);
- player.playerConnection.sendPacket(stand);
- player.playerConnection.sendPacket(teleport);
- } else {
- PacketPlayOutSpawnEntity skull = new PacketPlayOutSpawnEntity();
- if(!rebuild) NMSHelper.setValueStatic(skull, "a", this.entityId + 1);
- NMSHelper.setValueStatic(skull, "b", MathHelper.floor(this.location.getX() * 32.0));
- NMSHelper.setValueStatic(skull, "c", MathHelper.floor((this.location.getY() + 54.55) * 32));
- NMSHelper.setValueStatic(skull, "d", MathHelper.floor(this.location.getZ() * 32.0));
- NMSHelper.setValueStatic(skull, "j", 66);
- PacketPlayOutAttachEntity attach = new PacketPlayOutAttachEntity();
- if(!rebuild) {
- setValue(attach, "b", this.entityId);
- setValue(attach, "c", this.entityId + 1);
- }
- PacketPlayOutSpawnEntityLiving horse = new PacketPlayOutSpawnEntityLiving();
- if(!rebuild) NMSHelper.setValueStatic(horse, "a", this.entityId);
- NMSHelper.setValueStatic(horse, "b", 100);
- NMSHelper.setValueStatic(horse, "c", MathHelper.floor(this.location.getX() * 32.0));
- NMSHelper.setValueStatic(horse, "d", MathHelper.floor((this.location.getY() + 54.55) * 32));
- NMSHelper.setValueStatic(horse, "e", MathHelper.floor(this.location.getZ() * 32.0));
- DataWatcher watcher = new DataWatcher(null);
- watcher.a(0, (byte) 0);
- watcher.a(1, (short) 300);
- watcher.a(2, getNiceMessage());
- watcher.a(3, (byte) 1);
- watcher.a(10, getNiceMessage());
- watcher.a(11, (byte) 1);
- watcher.a(12, -1700000);
- NMSHelper.setValueStatic(horse, "l", watcher);
- player.playerConnection.sendPacket(skull);
- player.playerConnection.sendPacket(horse);
- player.playerConnection.sendPacket(attach);
- }
- }
- private void sendMessagePacket(EntityPlayer player) {
- DataWatcher watcher = new DataWatcher((Entity) null);
- watcher.a(2, getNiceMessage());
- if(PlayerUtil.getVersion(player.getBukkitEntity()) < 5) {
- watcher.a(10, getNiceMessage());
- }
- player.playerConnection.sendPacket(new PacketPlayOutEntityMetadata(this.entityId, watcher, true));
- }
- public void sendDestroyPacket(EntityPlayer player) {
- player.playerConnection.sendPacket(new PacketPlayOutEntityDestroy(this.entityId, this.entityId + 1));
- }
- public void teleportToNewLocation(Location newLocation) {
- this.location = newLocation;
- this.linesAbove.iterator().forEachRemaining(hologram -> {
- Location location = hologram.location;
- location.setX(newLocation.getX());
- location.setY(newLocation.getY() + ((this.linesAbove.indexOf(hologram) + 1) * 0.3));
- location.setZ(newLocation.getZ());
- getEntityPlayers(location).forEach(hologram::sendTeleportPacket);
- });
- getEntityPlayers(newLocation).forEach(this::sendTeleportPacket);
- this.linesBelow.iterator().forEachRemaining(hologram -> {
- Location location = hologram.location;
- location.setX(newLocation.getX());
- location.setY(newLocation.getY() - ((this.linesBelow.indexOf(hologram) + 1) * 0.3));
- location.setZ(newLocation.getZ());
- getEntityPlayers(location).forEach(hologram::sendTeleportPacket);
- });
- }
- public void spawn(boolean all) {
- if(all) {
- this.linesAbove.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(viewer -> hologram.sendSpawnPackets(viewer, false)));
- getEntityPlayers(this.location).forEach(player -> this.sendSpawnPackets(player, false));
- this.linesBelow.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(viewer -> hologram.sendSpawnPackets(viewer, false)));
- } else {
- getEntityPlayers(this.location)
- .forEach(viewer -> sendSpawnPackets(viewer, false));
- }
- }
- public void spawn(EntityPlayer player, boolean all) {
- if(all) {
- this.linesAbove.iterator().forEachRemaining(hologram -> hologram.sendSpawnPackets(player, false));
- this.sendSpawnPackets(player, false);
- this.linesBelow.iterator().forEachRemaining(hologram -> hologram.sendSpawnPackets(player, false));
- } else {
- getEntityPlayers(this.location)
- .forEach(viewer -> sendSpawnPackets(viewer, false));
- }
- }
- public void delete(boolean all) {
- if(all) {
- this.linesAbove.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(hologram::sendDestroyPacket));
- getEntityPlayers(this.location).forEach(this::sendDestroyPacket);
- this.linesBelow.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(hologram::sendDestroyPacket));
- } else {
- getEntityPlayers(this.location)
- .forEach(this::sendDestroyPacket);
- }
- holograms.values().remove(this);
- }
- public List<Hologram> getAllHolograms() {
- List<Hologram> toReturn = new ArrayList<>(this.linesAbove);
- toReturn.add(this);
- toReturn.addAll(this.linesBelow);
- return toReturn;
- }
- public void addLineAbove(String input) {
- Hologram toSpawn = new Hologram(input, "hologram-" + ThreadLocalRandom.current().nextInt(1000) + 100, (this.linesAbove.size() > 0 ? this.linesAbove.get(this.linesAbove.size() - 1) : this).getLocation().clone().add(0, 0.3, 0), false);
- toSpawn.spawn(false);
- this.linesAbove.add(toSpawn);
- }
- public void addLineBelow(String input) {
- Hologram toSpawn = new Hologram(input, "hologram-" + ThreadLocalRandom.current().nextInt(1000) + 100, (this.linesBelow.size() > 0 ? this.linesBelow.get(this.linesBelow.size() - 1) : this).getLocation().clone().subtract(0, 0.3, 0), false);
- toSpawn.spawn(false);
- this.linesBelow.add(toSpawn);
- }
- public void removeLineAbove(String text) {
- Hologram hologram = getLinesByMessage(text, this.linesAbove);
- if(hologram != null) {
- hologram.delete(false);
- this.linesAbove.remove(hologram);
- }
- }
- public void removeLineBelow(String text) {
- Hologram hologram = getLinesByMessage(text, this.linesBelow);
- if(hologram != null) {
- hologram.delete(false);
- this.linesBelow.remove(hologram);
- }
- }
- public void setMessage(String input) {
- this.message = Color.translate(input);
- getEntityPlayers(this.location).forEach(this::sendMessagePacket);
- }
- public Hologram getLinesByMessage(String input, List<Hologram> list) {
- return list.stream().filter(hologram -> hologram.getNiceMessage().equalsIgnoreCase(Color.translate(input))).findFirst().orElse(null);
- }
- public String getNiceMessage() {
- return Color.translate(this.message);
- }
- public static Hologram getByName(String input) {
- return holograms.values().stream().filter(hologram -> hologram.getName().equalsIgnoreCase(input)).findFirst().orElse(null);
- }
- public static Hologram getById(int id) {
- return holograms.values().stream().filter(hologram -> hologram.getEntityId() == id).findFirst().orElse(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement