|
| 1 | +package com.fadecloud.packetentityapi.api; |
| 2 | + |
| 3 | +import com.fadecloud.packetentityapi.api.wrappers.EntityAnimation; |
| 4 | +import com.fadecloud.packetentityapi.api.wrappers.EntityDisplayName; |
| 5 | +import com.fadecloud.packetentityapi.api.wrappers.EntityEquipment; |
| 6 | +import com.fadecloud.packetentityapi.api.wrappers.EntityHeadRotation; |
| 7 | +import com.fadecloud.packetentityapi.api.wrappers.EntityMetadata; |
| 8 | +import com.fadecloud.packetentityapi.api.wrappers.EntityMovement; |
| 9 | +import com.fadecloud.packetentityapi.api.wrappers.EntityTeleport; |
| 10 | +import com.fadecloud.packetentityapi.api.wrappers.EntityVisibility; |
| 11 | +import com.fadecloud.packetentityapi.api.wrappers.PacketEntityType; |
| 12 | + |
| 13 | +import org.bukkit.Bukkit; |
| 14 | +import org.bukkit.Location; |
| 15 | +import org.bukkit.entity.Player; |
| 16 | +import org.bukkit.inventory.EquipmentSlot; |
| 17 | +import org.bukkit.inventory.ItemStack; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.UUID; |
| 22 | + |
| 23 | +import java.util.concurrent.ThreadLocalRandom; |
| 24 | + |
| 25 | +public class CustomEntity implements EntityBase { |
| 26 | + |
| 27 | + private PacketEntityType entityType; |
| 28 | + private int entityId; |
| 29 | + private UUID uuid; |
| 30 | + private Location location; |
| 31 | + private List<UUID> viewers = new ArrayList<>(); |
| 32 | + |
| 33 | + @Override |
| 34 | + public CustomEntity create(PacketEntityType entityType, Location location, UUID uuid) { |
| 35 | + this.entityType = entityType; |
| 36 | + this.location = location; |
| 37 | + this.entityId = ThreadLocalRandom.current().nextInt(800000, 9999999 + 1); |
| 38 | + this.uuid = uuid; |
| 39 | + |
| 40 | + return this; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void addToViewerList(Player player) { |
| 45 | + EntityVisibility visibility = new EntityVisibility(this); |
| 46 | + visibility.spawn(this.location).send(player); |
| 47 | + this.viewers.add(player.getUniqueId()); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void removeFromViewerList(Player player) { |
| 52 | + EntityVisibility visibility = new EntityVisibility(this); |
| 53 | + visibility.destroy().send(player); |
| 54 | + this.viewers.remove(player.getUniqueId()); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void addDisplayName(String display) { |
| 59 | + EntityDisplayName name = new EntityDisplayName(this); |
| 60 | + name.queue(display).send(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void hide(Player player) { |
| 65 | + EntityMetadata meta = new EntityMetadata(this); |
| 66 | + meta.queue(EntityMetadata.EntityMetadataModifier.INVISIBLE, true).send(player); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void hide() { |
| 71 | + this.viewers.forEach(uuid -> this.hide(Bukkit.getPlayer(uuid))); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void unhide(Player player) { |
| 76 | + EntityMetadata meta = new EntityMetadata(this); |
| 77 | + meta.queue(EntityMetadata.EntityMetadataModifier.INVISIBLE, false).send(player); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void unhide() { |
| 82 | + this.viewers.forEach(uuid -> this.unhide(Bukkit.getPlayer(uuid))); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void destroy() { |
| 87 | + EntityVisibility visibility = new EntityVisibility(this); |
| 88 | + visibility.destroy().send(); |
| 89 | + CustomEntityManager.getManager().remove(this.entityId); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void teleport(Location location, boolean onGround) { |
| 94 | + EntityTeleport teleport = new EntityTeleport(this); |
| 95 | + teleport.queue(location, onGround).send(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void moveHere(Location location) { |
| 100 | + EntityMovement movement = new EntityMovement(this); |
| 101 | + movement.queue(location).send(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void moveHead(Location location) { |
| 106 | + EntityHeadRotation rotation = new EntityHeadRotation(this); |
| 107 | + rotation.queueRotate(location).send(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void addEquipment(ItemStack item, EquipmentSlot slot) { |
| 112 | + EntityEquipment entityEquipment = new EntityEquipment(this); |
| 113 | + entityEquipment.queue(item, slot).send(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void playAnimation(EntityAnimation.EntityAnimationType animationType) { |
| 118 | + EntityAnimation entityAnimation = new EntityAnimation(this); |
| 119 | + entityAnimation.queue(animationType).send(); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void setMetadata(EntityMetadata.EntityMetadataModifier metadata) { |
| 124 | + EntityMetadata meta = new EntityMetadata(this); |
| 125 | + meta.queue(metadata, true).send(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public <T> void setMetadata(int index, T value, Class<T> clazz) { |
| 130 | + EntityMetadata meta = new EntityMetadata(this); |
| 131 | + meta.queue(index, value, clazz).send(); |
| 132 | + } |
| 133 | + |
| 134 | + public void setLocation(Location location) { |
| 135 | + this.location = location; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public PacketEntityType getType() { |
| 140 | + return this.entityType; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public UUID getUUID() { |
| 145 | + return this.uuid; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public int getId() { |
| 150 | + return this.entityId; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Location getLocation() { |
| 155 | + return this.location; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public List<UUID> getViewers() { |
| 160 | + return this.viewers; |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments