Advertisement
JackOUT

Untitled

May 19th, 2022
534
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package games.coob.hologram;
  2.  
  3. import games.coob.smp.nms.HologramRegistryI;
  4. import games.coob.smp.nms.NMSHologramI;
  5. import net.citizensnpcs.api.npc.NPCRegistry;
  6. import net.minecraft.network.protocol.game.PacketPlayOutEntityDestroy;
  7. import net.minecraft.network.protocol.game.PacketPlayOutEntityMetadata;
  8. import net.minecraft.network.protocol.game.PacketPlayOutSpawnEntityLiving;
  9. import net.minecraft.server.level.WorldServer;
  10. import net.minecraft.world.entity.decoration.EntityArmorStand;
  11. import org.bukkit.Location;
  12. import org.bukkit.entity.Player;
  13. import org.mineacademy.fo.Valid;
  14. import org.mineacademy.fo.collection.SerializedMap;
  15. import org.mineacademy.fo.plugin.SimplePlugin;
  16. import org.mineacademy.fo.remain.Remain;
  17.  
  18. import java.util.UUID;
  19.  
  20. public class NMSHologram_v1_18 implements NMSHologramI {
  21.  
  22.     /**
  23.      * The spawned NMS entity
  24.      */
  25.     private EntityArmorStand entityArmorStand;
  26.  
  27.     private String[] lines;
  28.  
  29.     @Override
  30.     public Object createEntity(final Object nmsWorld, final Location location) {
  31.         entityArmorStand = new EntityArmorStand((WorldServer) nmsWorld, location.getX(), location.getY(), location.getZ());
  32.  
  33.         if (!HologramRegistryI.isRegistered(entityArmorStand.getBukkitEntity().getUniqueId()))
  34.             HologramRegistryI.register(this);
  35.  
  36.         return entityArmorStand;
  37.     }
  38.  
  39.     @Override
  40.     public void sendPackets(final Player player, final Object nmsArmorStand) {
  41.         final EntityArmorStand nmsStand = (EntityArmorStand) nmsArmorStand;
  42.  
  43.         Remain.sendPacket(player, new PacketPlayOutSpawnEntityLiving(nmsStand));
  44.         Remain.sendPacket(player, new PacketPlayOutEntityMetadata(nmsStand.ae(), nmsStand.ai(), true));
  45.     }
  46.  
  47.     @Override
  48.     public UUID getUniqueId() {
  49.         return this.entityArmorStand.getBukkitEntity().getUniqueId();
  50.     }
  51.  
  52.     /**
  53.      * Convenience method to return the location of this NPC.
  54.      *
  55.      * @return
  56.      */
  57.     @Override
  58.     public Location getLocation() {
  59.         Valid.checkBoolean(this.isCreated(), "Cannot call getLocation when " + this + " is not created");
  60.  
  61.         return this.entityArmorStand.getBukkitEntity().getLocation();
  62.     }
  63.  
  64.     @Override
  65.     public void setLines(final String[] lines) {
  66.         this.lines = lines;
  67.     }
  68.  
  69.     @Override
  70.     public String[] getLines() {
  71.         return this.lines;
  72.     }
  73.  
  74.     @Override
  75.     public void remove(final Player player) {
  76.         Remain.sendPacket(player, new PacketPlayOutEntityDestroy(this.entityArmorStand.ae()));
  77.         HologramRegistryI.unregister(this);
  78.         player.removeMetadata(getUniqueId().toString(), SimplePlugin.getInstance());
  79.     }
  80.  
  81.     @Override
  82.     public void hide(final Player player) {
  83.         Remain.sendPacket(player, new PacketPlayOutEntityDestroy(this.entityArmorStand.ae()));
  84.         player.removeMetadata(getUniqueId().toString(), SimplePlugin.getInstance());
  85.     }
  86.  
  87.     /**
  88.      * Return if this hologram is spawned
  89.      *
  90.      * @return
  91.      */
  92.     private boolean isCreated() {
  93.         this.entityArmorStand.getBukkitEntity();
  94.         return true;
  95.     }
  96.  
  97.     @Override
  98.     public SerializedMap serialize() {
  99.         Valid.checkBoolean(this.isCreated(), "Cannot save non-created holograms");
  100.  
  101.         return SerializedMap.ofArray(
  102.                 "UUID", this.entityArmorStand.getBukkitEntity(),
  103.                 "Lines", this.lines,
  104.                 "Last_Location", this.getLocation());
  105.     }
  106.  
  107.     /**
  108.      * Converts information saved in data.db file as a map into an NPC,
  109.      * also spawning it. After spawn this NPC will auto register in {@link NPCRegistry}
  110.      *
  111.      * @param map
  112.      * @return
  113.      */
  114.     public static NMSHologram_v1_18 deserialize(final SerializedMap map) {
  115.         final String[] lines = map.getStringList("Lines").toArray(new String[0]);
  116.         final Location lastLocation = map.getLocation("Last_Location");
  117.         final Object nmsWorld = Remain.getHandleWorld(lastLocation.getWorld());
  118.         final NMSHologram_v1_18 hologram = new NMSHologram_v1_18();
  119.  
  120.         hologram.createEntity(nmsWorld, lastLocation);
  121.         hologram.setLines(lines);
  122.  
  123.         return hologram;
  124.     }
  125.  
  126.     @Override
  127.     public void show(final Location location, final Player player, final String... linesOfText) {
  128.         // TODO Auto-generated method stub
  129.  
  130.     }
  131. }
Advertisement
RAW Paste Data Copied
Advertisement