diff --git a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java index 8b33dcf..6d677ba 100644 --- a/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java +++ b/src/main/java/me/tofaa/entitylib/entity/WrapperEntity.java @@ -12,10 +12,7 @@ import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.meta.EntityMeta; import org.jetbrains.annotations.NotNull; -import java.util.HashSet; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; public class WrapperEntity { @@ -64,12 +61,8 @@ public class WrapperEntity { ); } - - private static double distance(Location to, Location from) { - double x = to.getX() - from.getX(); - double y = to.getY() - from.getY(); - double z = to.getZ() - from.getZ(); - return Math.sqrt(x * x + y * y + z * z); + public @NotNull Collection getViewers() { + return Collections.unmodifiableCollection(viewers); } @@ -93,8 +86,7 @@ public class WrapperEntity { public void sendPacketToViewers(PacketWrapper packet) { viewers.forEach(uuid -> { - Object user = EntityLib.getPacketEvents().getProtocolManager().getChannel(uuid); - EntityLib.getPacketEvents().getProtocolManager().sendPacket(user, packet); + EntityLib.sendPacket(uuid, packet); }); } diff --git a/src/main/java/me/tofaa/entitylib/meta/Metadata.java b/src/main/java/me/tofaa/entitylib/meta/Metadata.java index 38a132c..8dd0d57 100644 --- a/src/main/java/me/tofaa/entitylib/meta/Metadata.java +++ b/src/main/java/me/tofaa/entitylib/meta/Metadata.java @@ -3,10 +3,13 @@ package me.tofaa.entitylib.meta; import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType; import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityMetadata; +import me.tofaa.entitylib.EntityLib; +import me.tofaa.entitylib.entity.WrapperEntity; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -31,6 +34,15 @@ public class Metadata { public void setIndex(byte index, @NotNull EntityDataType dataType, T value) { EntityData data = new EntityData(index, dataType, value); this.metadataMap.put(index, data); + WrapperEntity e = EntityLib.getEntity(entityId); + if (e != null && e.hasSpawned()) { + e.sendPacketToViewers( + new WrapperPlayServerEntityMetadata( + entityId, + getEntries() + ) + ); + } } @NotNull List getEntries() { diff --git a/test-plugin/src/main/java/me/tofaa/entitylib/TestEntityCommand.java b/test-plugin/src/main/java/me/tofaa/entitylib/TestEntityCommand.java index 6c69bee..d6fee53 100644 --- a/test-plugin/src/main/java/me/tofaa/entitylib/TestEntityCommand.java +++ b/test-plugin/src/main/java/me/tofaa/entitylib/TestEntityCommand.java @@ -3,12 +3,14 @@ package me.tofaa.entitylib; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.world.Location; import me.tofaa.entitylib.entity.WrapperEntity; +import me.tofaa.entitylib.meta.EntityMeta; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.Collections; import java.util.UUID; public class TestEntityCommand implements CommandExecutor { @@ -26,18 +28,17 @@ public class TestEntityCommand implements CommandExecutor { player.sendMessage("idk"); return false; } - } - - if (entity.hasSpawned()) { - entity.remove(); - player.sendMessage("removed"); - } - else { entity.addViewer(player.getUniqueId()); entity.spawn(fromBukkit(player.getLocation())); - player.sendMessage("spawned"); } + EntityMeta meta = entity.getMeta(); + meta.setOnFire(!meta.isOnFire()); + meta.setHasGlowingEffect(!meta.hasGlowingEffect()); + player.sendMessage("on fire: " + meta.isOnFire()); + player.sendMessage("glowing: " + meta.hasGlowingEffect()); + player.sendMessage("viewers: " + entity.getViewers()); + return false; }