JackOUT

Untitled

Jan 8th, 2022
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package games.coob.core.specific.hologram;
  2.  
  3. import games.coob.core.hologram.HologramRegistry;
  4. import games.coob.core.npc.NPCRegistry;
  5. import net.minecraft.server.v1_16_R3.EntityArmorStand;
  6. import net.minecraft.server.v1_16_R3.PacketPlayOutEntityMetadata;
  7. import net.minecraft.server.v1_16_R3.PacketPlayOutSpawnEntityLiving;
  8. import net.minecraft.server.v1_16_R3.WorldServer;
  9. import org.bukkit.Location;
  10. import org.bukkit.entity.Player;
  11. import org.mineacademy.fo.Valid;
  12. import org.mineacademy.fo.collection.SerializedMap;
  13. import org.mineacademy.fo.remain.Remain;
  14.  
  15. import java.util.UUID;
  16.  
  17. class NMSHologram_v1_16 extends NMSHologram {
  18.  
  19.     /**
  20.      * The spawned NMS entity
  21.      */
  22.     private EntityArmorStand entityArmorStand;
  23.  
  24.     private String[] lines;
  25.  
  26.     @Override
  27.     protected Object createEntity(final Object nmsWorld, final Location location) {
  28.         entityArmorStand = new EntityArmorStand((WorldServer) nmsWorld, location.getX(), location.getY(), location.getZ());
  29.  
  30.         if (HologramRegistry.getInstance().isRegistered(entityArmorStand.getUniqueID()))
  31.             HologramRegistry.getInstance().register(this);
  32.  
  33.         return entityArmorStand;
  34.     }
  35.  
  36.     @Override
  37.     protected void sendPackets(final Player player, final Object nmsArmorStand) {
  38.         final EntityArmorStand nmsStand = (EntityArmorStand) nmsArmorStand;
  39.  
  40.         Remain.sendPacket(player, new PacketPlayOutSpawnEntityLiving(nmsStand));
  41.         Remain.sendPacket(player, new PacketPlayOutEntityMetadata(nmsStand.getId(), nmsStand.getDataWatcher(), true));
  42.     }
  43.  
  44.     @Override
  45.     public UUID getUniqueId() {
  46.         return this.entityArmorStand.getBukkitEntity().getUniqueId();
  47.     }
  48.  
  49.     /**
  50.      * Convenience method to return the location of this NPC.
  51.      *
  52.      * @return
  53.      */
  54.     @Override
  55.     public Location getLocation() {
  56.         Valid.checkBoolean(this.isCreated(), "Cannot call getLocation when " + this + " is not created");
  57.  
  58.         return this.entityArmorStand.getBukkitEntity().getLocation();
  59.     }
  60.  
  61.     @Override
  62.     public void setLines(final String[] lines) {
  63.         this.lines = lines;
  64.     }
  65.  
  66.     @Override
  67.     public String[] getLines() {
  68.         return this.lines;
  69.     }
  70.  
  71.     /**
  72.      * Return if this hologram is spawned
  73.      *
  74.      * @return
  75.      */
  76.     boolean isCreated() {
  77.         return entityArmorStand.getBukkitEntity() != null;
  78.     }
  79.  
  80.     @Override
  81.     public SerializedMap serialize() {
  82.         Valid.checkBoolean(this.isCreated(), "Cannot save non-created holograms");
  83.  
  84.         return SerializedMap.ofArray(
  85.                 "UUID", this.entityArmorStand.getUniqueID(),
  86.                 "Lines", this.lines,
  87.                 "Last_Location", this.getLocation());
  88.     }
  89.  
  90.     /**
  91.      * Converts information saved in data.db file as a map into an NPC,
  92.      * also spawning it. After spawn this NPC will auto register in {@link NPCRegistry}
  93.      *
  94.      * @param map
  95.      * @return
  96.      */
  97.     public static NMSHologram_v1_16 deserialize(final SerializedMap map) {
  98.         final String[] lines = map.getStringList("Lines").toArray(new String[0]);
  99.         final Location lastLocation = map.getLocation("Last_Location");
  100.         final Object nmsWorld = Remain.getHandleWorld(lastLocation.getWorld());
  101.         final NMSHologram_v1_16 hologram = new NMSHologram_v1_16();
  102.  
  103.         hologram.createEntity(nmsWorld, lastLocation);
  104.         hologram.setLines(lines);
  105.  
  106.         return hologram;
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment