Advertisement
JackOUT

Untitled

Jan 13th, 2023
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. package games.coob.v1_19;
  2.  
  3. import games.coob.nmsinterface.NMSHologramI;
  4. import lombok.Getter;
  5. import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
  6. import net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket;
  7. import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
  8. import net.minecraft.server.level.ServerLevel;
  9. import net.minecraft.world.entity.decoration.ArmorStand;
  10. import org.bukkit.Location;
  11. import org.bukkit.World;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.metadata.FixedMetadataValue;
  14. import org.mineacademy.fo.ReflectionUtil;
  15. import org.mineacademy.fo.Valid;
  16. import org.mineacademy.fo.collection.SerializedMap;
  17. import org.mineacademy.fo.plugin.SimplePlugin;
  18. import org.mineacademy.fo.remain.Remain;
  19.  
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import java.util.UUID;
  23.  
  24. public class NMSHologram_v1_19 implements NMSHologramI {
  25.  
  26.     /**
  27.      * The spawned NMS entity
  28.      */
  29.     @Getter
  30.     private ArmorStand entityArmorStand;
  31.  
  32.     private List<String> lines;
  33.  
  34.     @Override
  35.     public Object createEntity(final Object nmsWorld, final Location location) {
  36.         entityArmorStand = new ArmorStand((ServerLevel) nmsWorld, location.getX(), location.getY(), location.getZ());
  37.  
  38.         if (!HologramRegistry_v1_19.getInstance().isRegistered(entityArmorStand.getBukkitEntity().getUniqueId()))
  39.             HologramRegistry_v1_19.getInstance().register(this);
  40.  
  41.         return entityArmorStand;
  42.     }
  43.  
  44.     @Override
  45.     public void sendPackets(final Player player, final Object nmsArmorStand) {
  46.         final ArmorStand nmsStand = (ArmorStand) nmsArmorStand;
  47.  
  48.         Remain.sendPacket(player, new ClientboundAddEntityPacket(nmsStand));
  49.         Remain.sendPacket(player, new ClientboundSetEntityDataPacket(nmsStand.getId(), nmsStand.getEntityData().packDirty()));
  50.     }
  51.  
  52.     @Override
  53.     public UUID getUniqueId() {
  54.         return this.entityArmorStand.getBukkitEntity().getUniqueId();
  55.     }
  56.  
  57.     /**
  58.      * Convenience method to return the location of this NPC.
  59.      *
  60.      * @return
  61.      */
  62.     @Override
  63.     public Location getLocation() {
  64.         Valid.checkBoolean(this.isCreated(), "Cannot call getLocation when " + this + " is not created");
  65.  
  66.         return this.entityArmorStand.getBukkitEntity().getLocation();
  67.     }
  68.  
  69.     @Override
  70.     public void setLines(final List<String> lines) {
  71.         this.lines = lines;
  72.     }
  73.  
  74.     @Override
  75.     public List<String> getLines() {
  76.         return this.lines;
  77.     }
  78.  
  79.     @Override
  80.     public void remove(final Player player) {
  81.         Remain.sendPacket(player, new ClientboundRemoveEntitiesPacket(this.entityArmorStand.getId()));
  82.         HologramRegistry_v1_19.getInstance().unregister(this);
  83.         player.removeMetadata(getUniqueId().toString(), SimplePlugin.getInstance());
  84.         System.out.println("removed");
  85.     }
  86.  
  87.     @Override
  88.     public void hide(final Player player) {
  89.         Remain.sendPacket(player, new ClientboundRemoveEntitiesPacket(this.entityArmorStand.getId()));
  90.         player.removeMetadata(getUniqueId().toString(), SimplePlugin.getInstance());
  91.         System.out.println("hiding");
  92.     }
  93.  
  94.     @Override
  95.     public void show(Location location, final Player player, final String... linesOfText) {
  96.         final World world = location.getWorld();
  97.  
  98.         if (world == null)
  99.             return;
  100.  
  101.         System.out.println("showing");
  102.  
  103.         final Object nmsWorld = Remain.getHandleWorld(location.getWorld());
  104.  
  105.         setLines(Arrays.asList(linesOfText));
  106.  
  107.         for (final String line : linesOfText) {
  108.             final Object nmsArmorStand = this.createEntity(nmsWorld, location);
  109.             final org.bukkit.entity.ArmorStand armorStand = ReflectionUtil.invoke("getBukkitEntity", nmsArmorStand);
  110.  
  111.             armorStand.setVisible(false);
  112.             Remain.setCustomName(armorStand, line);
  113.             this.sendPackets(player, nmsArmorStand);
  114.             location = location.subtract(0, 0.26, 0);
  115.         }
  116.  
  117.         player.setMetadata(getUniqueId().toString(), new FixedMetadataValue(SimplePlugin.getInstance(), ""));
  118.     }
  119.  
  120.     /**
  121.      * Return if this hologram is spawned
  122.      *
  123.      * @return
  124.      */
  125.     private boolean isCreated() {
  126.         entityArmorStand.getBukkitEntity();
  127.         return true;
  128.     }
  129.  
  130.     @Override
  131.     public SerializedMap serialize() {
  132.         Valid.checkBoolean(this.isCreated(), "Cannot save non-created holograms");
  133.  
  134.         return SerializedMap.ofArray(
  135.                 "UUID", this.entityArmorStand.getBukkitEntity().getUniqueId(),
  136.                 "Lines", this.lines,
  137.                 "Last_Location", this.getLocation());
  138.     }
  139.  
  140.     public static NMSHologram_v1_19 deserialize(final SerializedMap map) {
  141.         final List<String> lines = map.getStringList("Lines");
  142.         final Location lastLocation = map.getLocation("Last_Location");
  143.         final Object nmsWorld = Remain.getHandleWorld(lastLocation.getWorld());
  144.         final NMSHologram_v1_19 hologram = new NMSHologram_v1_19();
  145.  
  146.         hologram.createEntity(nmsWorld, lastLocation);
  147.         hologram.setLines(lines);
  148.  
  149.         return hologram;
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement