Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. package us.zonix.core.holograms;
  2.  
  3. import lombok.Getter;
  4. import net.minecraft.server.v1_8_R3.*;
  5. import org.bukkit.Location;
  6. import org.bukkit.configuration.ConfigurationSection;
  7. import org.bukkit.configuration.file.FileConfiguration;
  8. import us.zonix.core.CorePlugin;
  9. import us.zonix.core.util.Color;
  10. import us.zonix.core.util.LocationUtil;
  11. import us.zonix.core.util.PlayerUtil;
  12.  
  13. import java.io.IOException;
  14. import java.util.*;
  15. import java.util.concurrent.ThreadLocalRandom;
  16.  
  17. /**
  18. * Created by Marko on 09.03.2019.
  19. */
  20.  
  21. @Getter
  22. public class Hologram extends NMSHelper {
  23.  
  24. @Getter
  25. private static Map<String, Hologram> holograms = new HashMap<>();
  26.  
  27. private String message, name;
  28. private Location location;
  29. private int entityId;
  30.  
  31. private List<Hologram> linesAbove = new ArrayList<>();
  32. private List<Hologram> linesBelow = new ArrayList<>();
  33.  
  34. public Hologram() {
  35. this.load();
  36. }
  37.  
  38. public Hologram(String message, Location location) {
  39. this(message, "hologram-" + ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE), location, true);
  40. }
  41.  
  42. public Hologram(String message, String name, Location location, boolean add) {
  43. this.message = message;
  44. this.name = name;
  45. this.location = location;
  46. this.entityId = ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
  47.  
  48. if(add) {
  49. holograms.put(name, this);
  50. }
  51. }
  52.  
  53. private void load() {
  54. FileConfiguration config = CorePlugin.getInstance().getHolograms().getConfiguration();
  55. ConfigurationSection section = config.getConfigurationSection("holograms");
  56.  
  57. if(section == null) {
  58. return;
  59. }
  60.  
  61. section.getKeys(false).forEach(key -> {
  62. key = "holograms." + key + '.';
  63.  
  64. Hologram hologram = new Hologram(config.getString(key + "message"), config.getString(key + "name"), LocationUtil.convertLocation(config.getString(key + "location")), true);
  65.  
  66. ConfigurationSection aboveSection = config.getConfigurationSection(key + "above");
  67.  
  68. if(aboveSection != null) {
  69. for(String root : aboveSection.getKeys(false)) {
  70. String aboveKey = key + "above." + root + '.';
  71. hologram.getLinesAbove().add(new Hologram(config.getString(aboveKey + "message"), config.getString(aboveKey + "name"), LocationUtil.convertLocation(config.getString(aboveKey + "location")), false));
  72. }
  73. }
  74.  
  75. ConfigurationSection belowSection = config.getConfigurationSection(key + "below");
  76.  
  77. if(belowSection != null) {
  78. for(String root : belowSection.getKeys(false)) {
  79. String belowKey = key + "below." + root + '.';
  80. hologram.getLinesBelow().add(new Hologram(config.getString(belowKey + "message"), config.getString(belowKey + "name"), LocationUtil.convertLocation(config.getString(belowKey + "location")), false));
  81. }
  82. }
  83.  
  84. hologram.spawn(true);
  85. });
  86. }
  87.  
  88. public void save() {
  89. FileConfiguration config = CorePlugin.getInstance().getHolograms().getConfiguration();
  90. config.set("holograms", null);
  91.  
  92. holograms.values().stream().filter(Objects::nonNull).forEach(hologram -> {
  93. String key = "holograms." + hologram.getName() + '.';
  94.  
  95. config.set(key + "message", hologram.getMessage());
  96. config.set(key + "name", hologram.getName());
  97. config.set(key + "location", LocationUtil.parseLocation(hologram.getLocation()));
  98.  
  99. if(hologram.getLinesAbove() != null && !hologram.getLinesAbove().isEmpty()) {
  100. for(Hologram line : hologram.getLinesAbove()) {
  101. String aboveKey = key + "above." + line.getName() + '.';
  102.  
  103. config.set(aboveKey + "message", line.getMessage());
  104. config.set(aboveKey + "name", line.getName());
  105. config.set(aboveKey + "location", LocationUtil.parseLocation(line.getLocation()));
  106. }
  107. }
  108.  
  109. if(hologram.getLinesBelow() != null && !hologram.getLinesBelow().isEmpty()) {
  110. for(Hologram line : hologram.getLinesBelow()) {
  111. String belowKey = key + "below." + line.getName() + '.';
  112.  
  113. config.set(belowKey + "message", line.getMessage());
  114. config.set(belowKey + "name", line.getName());
  115. config.set(belowKey + "location", LocationUtil.parseLocation(line.getLocation()));
  116. }
  117. }
  118. });
  119.  
  120. try {
  121. config.save(CorePlugin.getInstance().getHolograms().getFile());
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126.  
  127. public void sendTeleportPacket(EntityPlayer player) {
  128. PacketPlayOutEntityTeleport teleport = new PacketPlayOutEntityTeleport();
  129.  
  130. NMSHelper.setValueStatic(teleport, "a", this.entityId);
  131. NMSHelper.setValueStatic(teleport, "b", MathHelper.floor(this.location.getX() * 32.0));
  132. NMSHelper.setValueStatic(teleport, "c", MathHelper.floor((this.location.getY()) * 32.0));
  133. NMSHelper.setValueStatic(teleport, "d", MathHelper.floor(this.location.getZ() * 32.0));
  134. NMSHelper.setValueStatic(teleport, "g", true);
  135.  
  136. player.playerConnection.sendPacket(teleport);
  137. }
  138.  
  139. public void sendSpawnPackets(EntityPlayer player, boolean rebuild) {
  140. if(PlayerUtil.getVersion(player.getBukkitEntity()) > 5) {
  141. PacketPlayOutEntityTeleport teleport = new PacketPlayOutEntityTeleport();
  142.  
  143. if(!rebuild) NMSHelper.setValueStatic(teleport, "a", this.entityId);
  144. NMSHelper.setValueStatic(teleport, "b", MathHelper.floor(this.location.getX() * 32.0));
  145. NMSHelper.setValueStatic(teleport, "c", MathHelper.floor((this.location.getY()/* + 54.55*/) * 32));
  146. NMSHelper.setValueStatic(teleport, "d", MathHelper.floor(this.location.getZ() * 32.0));
  147. NMSHelper.setValueStatic(teleport, "g", true);
  148.  
  149. PacketPlayOutSpawnEntityLiving stand = new PacketPlayOutSpawnEntityLiving();
  150. if (!rebuild) NMSHelper.setValueStatic(stand, "a", this.entityId);
  151. NMSHelper.setValueStatic(stand, "b", 30);
  152. NMSHelper.setValueStatic(stand, "c", MathHelper.floor(this.location.getX() * 32.0));
  153. NMSHelper.setValueStatic(stand, "d", MathHelper.floor((this.location.getY()) * 32.0));
  154. NMSHelper.setValueStatic(stand, "e", MathHelper.floor(this.location.getZ() * 32.0));
  155.  
  156. DataWatcher watcher = new DataWatcher((Entity) null);
  157. watcher.a(0, (byte) 0x20); // Invisible
  158. watcher.a(2, getNiceMessage()); // And set the armor stand name to the hologram text
  159. watcher.a(3, (byte) 1); // Always show nametag
  160. watcher.a(10, (byte) 0x16); // Zero Bounding Box (Marker)
  161.  
  162. NMSHelper.setValueStatic(stand, "l", watcher);
  163.  
  164. player.playerConnection.sendPacket(stand);
  165. player.playerConnection.sendPacket(teleport);
  166. } else {
  167. PacketPlayOutSpawnEntity skull = new PacketPlayOutSpawnEntity();
  168.  
  169. if(!rebuild) NMSHelper.setValueStatic(skull, "a", this.entityId + 1);
  170. NMSHelper.setValueStatic(skull, "b", MathHelper.floor(this.location.getX() * 32.0));
  171. NMSHelper.setValueStatic(skull, "c", MathHelper.floor((this.location.getY() + 54.55) * 32));
  172. NMSHelper.setValueStatic(skull, "d", MathHelper.floor(this.location.getZ() * 32.0));
  173. NMSHelper.setValueStatic(skull, "j", 66);
  174.  
  175. PacketPlayOutAttachEntity attach = new PacketPlayOutAttachEntity();
  176.  
  177. if(!rebuild) {
  178. setValue(attach, "b", this.entityId);
  179. setValue(attach, "c", this.entityId + 1);
  180. }
  181.  
  182. PacketPlayOutSpawnEntityLiving horse = new PacketPlayOutSpawnEntityLiving();
  183.  
  184. if(!rebuild) NMSHelper.setValueStatic(horse, "a", this.entityId);
  185. NMSHelper.setValueStatic(horse, "b", 100);
  186. NMSHelper.setValueStatic(horse, "c", MathHelper.floor(this.location.getX() * 32.0));
  187. NMSHelper.setValueStatic(horse, "d", MathHelper.floor((this.location.getY() + 54.55) * 32));
  188. NMSHelper.setValueStatic(horse, "e", MathHelper.floor(this.location.getZ() * 32.0));
  189.  
  190. DataWatcher watcher = new DataWatcher(null);
  191.  
  192. watcher.a(0, (byte) 0);
  193. watcher.a(1, (short) 300);
  194. watcher.a(2, getNiceMessage());
  195. watcher.a(3, (byte) 1);
  196. watcher.a(10, getNiceMessage());
  197. watcher.a(11, (byte) 1);
  198. watcher.a(12, -1700000);
  199.  
  200. NMSHelper.setValueStatic(horse, "l", watcher);
  201.  
  202. player.playerConnection.sendPacket(skull);
  203. player.playerConnection.sendPacket(horse);
  204. player.playerConnection.sendPacket(attach);
  205. }
  206. }
  207.  
  208. private void sendMessagePacket(EntityPlayer player) {
  209. DataWatcher watcher = new DataWatcher((Entity) null);
  210. watcher.a(2, getNiceMessage());
  211.  
  212. if(PlayerUtil.getVersion(player.getBukkitEntity()) < 5) {
  213. watcher.a(10, getNiceMessage());
  214. }
  215.  
  216. player.playerConnection.sendPacket(new PacketPlayOutEntityMetadata(this.entityId, watcher, true));
  217. }
  218.  
  219. public void sendDestroyPacket(EntityPlayer player) {
  220. player.playerConnection.sendPacket(new PacketPlayOutEntityDestroy(this.entityId, this.entityId + 1));
  221. }
  222.  
  223. public void teleportToNewLocation(Location newLocation) {
  224. this.location = newLocation;
  225.  
  226. this.linesAbove.iterator().forEachRemaining(hologram -> {
  227. Location location = hologram.location;
  228. location.setX(newLocation.getX());
  229. location.setY(newLocation.getY() + ((this.linesAbove.indexOf(hologram) + 1) * 0.3));
  230. location.setZ(newLocation.getZ());
  231.  
  232. getEntityPlayers(location).forEach(hologram::sendTeleportPacket);
  233. });
  234.  
  235. getEntityPlayers(newLocation).forEach(this::sendTeleportPacket);
  236.  
  237. this.linesBelow.iterator().forEachRemaining(hologram -> {
  238. Location location = hologram.location;
  239. location.setX(newLocation.getX());
  240. location.setY(newLocation.getY() - ((this.linesBelow.indexOf(hologram) + 1) * 0.3));
  241. location.setZ(newLocation.getZ());
  242.  
  243. getEntityPlayers(location).forEach(hologram::sendTeleportPacket);
  244. });
  245. }
  246.  
  247. public void spawn(boolean all) {
  248. if(all) {
  249. this.linesAbove.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(viewer -> hologram.sendSpawnPackets(viewer, false)));
  250. getEntityPlayers(this.location).forEach(player -> this.sendSpawnPackets(player, false));
  251. this.linesBelow.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(viewer -> hologram.sendSpawnPackets(viewer, false)));
  252. } else {
  253. getEntityPlayers(this.location)
  254. .forEach(viewer -> sendSpawnPackets(viewer, false));
  255. }
  256. }
  257.  
  258. public void spawn(EntityPlayer player, boolean all) {
  259. if(all) {
  260. this.linesAbove.iterator().forEachRemaining(hologram -> hologram.sendSpawnPackets(player, false));
  261. this.sendSpawnPackets(player, false);
  262. this.linesBelow.iterator().forEachRemaining(hologram -> hologram.sendSpawnPackets(player, false));
  263. } else {
  264. getEntityPlayers(this.location)
  265. .forEach(viewer -> sendSpawnPackets(viewer, false));
  266. }
  267. }
  268.  
  269. public void delete(boolean all) {
  270. if(all) {
  271. this.linesAbove.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(hologram::sendDestroyPacket));
  272. getEntityPlayers(this.location).forEach(this::sendDestroyPacket);
  273. this.linesBelow.forEach(hologram -> getEntityPlayers(hologram.getLocation()).forEach(hologram::sendDestroyPacket));
  274. } else {
  275. getEntityPlayers(this.location)
  276. .forEach(this::sendDestroyPacket);
  277. }
  278.  
  279. holograms.values().remove(this);
  280. }
  281.  
  282. public List<Hologram> getAllHolograms() {
  283. List<Hologram> toReturn = new ArrayList<>(this.linesAbove);
  284. toReturn.add(this);
  285. toReturn.addAll(this.linesBelow);
  286. return toReturn;
  287. }
  288.  
  289. public void addLineAbove(String input) {
  290. 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);
  291. toSpawn.spawn(false);
  292.  
  293. this.linesAbove.add(toSpawn);
  294. }
  295.  
  296. public void addLineBelow(String input) {
  297. 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);
  298. toSpawn.spawn(false);
  299.  
  300. this.linesBelow.add(toSpawn);
  301. }
  302.  
  303. public void removeLineAbove(String text) {
  304. Hologram hologram = getLinesByMessage(text, this.linesAbove);
  305.  
  306. if(hologram != null) {
  307. hologram.delete(false);
  308. this.linesAbove.remove(hologram);
  309. }
  310. }
  311.  
  312. public void removeLineBelow(String text) {
  313. Hologram hologram = getLinesByMessage(text, this.linesBelow);
  314.  
  315. if(hologram != null) {
  316. hologram.delete(false);
  317. this.linesBelow.remove(hologram);
  318. }
  319. }
  320.  
  321. public void setMessage(String input) {
  322. this.message = Color.translate(input);
  323. getEntityPlayers(this.location).forEach(this::sendMessagePacket);
  324. }
  325.  
  326. public Hologram getLinesByMessage(String input, List<Hologram> list) {
  327. return list.stream().filter(hologram -> hologram.getNiceMessage().equalsIgnoreCase(Color.translate(input))).findFirst().orElse(null);
  328. }
  329.  
  330. public String getNiceMessage() {
  331. return Color.translate(this.message);
  332. }
  333.  
  334. public static Hologram getByName(String input) {
  335. return holograms.values().stream().filter(hologram -> hologram.getName().equalsIgnoreCase(input)).findFirst().orElse(null);
  336. }
  337.  
  338. public static Hologram getById(int id) {
  339. return holograms.values().stream().filter(hologram -> hologram.getEntityId() == id).findFirst().orElse(null);
  340. }
  341. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement