fix WrapperPlayer

This commit is contained in:
Tofaa 2024-02-23 01:37:33 +04:00
parent c7b486f0a9
commit aea7c5f7a2
15 changed files with 380 additions and 140 deletions

View file

@ -5,7 +5,15 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment=""> <list default="true" id="9d5d9b6f-43c8-41a4-bb42-a66ffc96c9b0" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestTextDisplayCommand.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/EntityLibAPI.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/meta/EntityMeta.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java" beforeDir="false" afterPath="$PROJECT_DIR$/api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/ExtraConversionUtil.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/InternalRegistryListener.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" beforeDir="false" afterPath="$PROJECT_DIR$/platforms/spigot/src/main/java/me/tofaa/entitylib/spigot/SpigotEntityLibPlatform.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" beforeDir="false" afterPath="$PROJECT_DIR$/test-plugin/src/main/java/me/tofaa/testentitylib/TestEntityLibPlugin.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -308,6 +316,8 @@
<workItem from="1708512937708" duration="3296000" /> <workItem from="1708512937708" duration="3296000" />
<workItem from="1708549665711" duration="100000" /> <workItem from="1708549665711" duration="100000" />
<workItem from="1708550708196" duration="523000" /> <workItem from="1708550708196" duration="523000" />
<workItem from="1708591139716" duration="1553000" />
<workItem from="1708597333736" duration="3637000" />
</task> </task>
<servers /> <servers />
</component> </component>

View file

@ -34,7 +34,6 @@ public interface EntityLibAPI<T> {
@NotNull <T extends WrapperEntity> T createEntity(EntityType type); @NotNull <T extends WrapperEntity> T createEntity(EntityType type);
@NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location); @NotNull WrapperPlayer spawnPlayer(UserProfile profile, Location location);
@NotNull <T extends WrapperEntity> T spawnEntity(@NotNull Class<T> wrapperClass, @NotNull EntityType entityType, @NotNull Location location); @NotNull <T extends WrapperEntity> T spawnEntity(@NotNull Class<T> wrapperClass, @NotNull EntityType entityType, @NotNull Location location);
@ -68,4 +67,6 @@ public interface EntityLibAPI<T> {
* @param tickContainer the TickContainer to add. * @param tickContainer the TickContainer to add.
*/ */
void addTickContainer(@NotNull TickContainer<?, T> tickContainer); void addTickContainer(@NotNull TickContainer<?, T> tickContainer);
void runLater(@NotNull Runnable runnable, long delayInTicks);
} }

View file

@ -3,6 +3,7 @@ package me.tofaa.entitylib.meta;
import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.manager.server.VersionComparison; import com.github.retrooper.packetevents.manager.server.VersionComparison;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes; import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
import com.github.retrooper.packetevents.protocol.entity.data.EntityMetadataProvider; import com.github.retrooper.packetevents.protocol.entity.data.EntityMetadataProvider;
import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose; import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose;
@ -214,6 +215,14 @@ public class EntityMeta implements EntityMetadataProvider {
return (byte) (value + amount); return (byte) (value + amount);
} }
public <T> void setIndex(byte index, @NotNull EntityDataType<T> dataType, T value) {
this.metadata.setIndex(index, dataType, value);
}
public <T> T getIndex(byte index, @Nullable T defaultValue) {
return this.metadata.getIndex(index, defaultValue);
}
public byte getMask(byte index) { public byte getMask(byte index) {
return this.metadata.getIndex(index, (byte) 0); return this.metadata.getIndex(index, (byte) 0);
} }

View file

@ -24,7 +24,7 @@ public class WrapperEntity implements Tickable, TrackedEntity {
private EntityType entityType; private EntityType entityType;
private EntityMeta entityMeta; private EntityMeta entityMeta;
private boolean ticking; private boolean ticking;
private Location location; protected Location location;
private Location preRidingLocation; private Location preRidingLocation;
private final Set<UUID> viewers; private final Set<UUID> viewers;
private boolean onGround; private boolean onGround;
@ -48,12 +48,35 @@ public class WrapperEntity implements Tickable, TrackedEntity {
if (spawned) return false; if (spawned) return false;
this.location = location; this.location = location;
this.spawned = true; this.spawned = true;
int data = 0; sendPacketToViewers(
new WrapperPlayServerSpawnEntity(
entityId,
Optional.of(this.uuid),
entityType,
location.getPosition(),
location.getPitch(),
location.getYaw(),
location.getYaw(),
getObjectData(),
createVeloPacket()
)
);
sendPacketToViewers(entityMeta.createPacket());
return true;
}
protected int getObjectData() {
if (entityMeta instanceof ObjectData) {
return ((ObjectData) entityMeta).getObjectData();
}
return 0;
}
protected Optional<Vector3d> createVeloPacket() {
Optional<Vector3d> velocity; Optional<Vector3d> velocity;
double veloX = 0, veloY = 0, veloZ = 0; double veloX = 0, veloY = 0, veloZ = 0;
if (entityMeta instanceof ObjectData) { if (entityMeta instanceof ObjectData) {
ObjectData od = (ObjectData) entityMeta; ObjectData od = (ObjectData) entityMeta;
data = od.getObjectData();
if (od.requiresVelocityPacketAtSpawn()) { if (od.requiresVelocityPacketAtSpawn()) {
final WrapperPlayServerEntityVelocity veloPacket = getVelocityPacket(); final WrapperPlayServerEntityVelocity veloPacket = getVelocityPacket();
veloX = veloPacket.getVelocity().getX(); veloX = veloPacket.getVelocity().getX();
@ -66,21 +89,7 @@ public class WrapperEntity implements Tickable, TrackedEntity {
} else { } else {
velocity = Optional.of(new Vector3d(veloX, veloY, veloZ)); velocity = Optional.of(new Vector3d(veloX, veloY, veloZ));
} }
sendPacketToViewers( return velocity;
new WrapperPlayServerSpawnEntity(
entityId,
Optional.of(this.uuid),
entityType,
location.getPosition(),
location.getPitch(),
location.getYaw(),
location.getYaw(),
data,
velocity
)
);
sendPacketToViewers(entityMeta.createPacket());
return true;
} }
public void setLocation(Location location) { public void setLocation(Location location) {
@ -94,6 +103,10 @@ public class WrapperEntity implements Tickable, TrackedEntity {
public void despawn() { public void despawn() {
if (!spawned) return; if (!spawned) return;
spawned = false; spawned = false;
if (this instanceof WrapperPlayer) {
WrapperPlayer p = (WrapperPlayer) this;
sendPacketsToViewers(p.tabListRemovePacket());
}
sendPacketToViewers(new WrapperPlayServerDestroyEntities(entityId)); sendPacketToViewers(new WrapperPlayServerDestroyEntities(entityId));
} }
@ -133,7 +146,20 @@ public class WrapperEntity implements Tickable, TrackedEntity {
return; return;
} }
if (spawned) { if (spawned) {
WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity( if (this instanceof WrapperPlayer) {
WrapperPlayer p = (WrapperPlayer) this;
sendPacket(uuid, p.tabListPacket());
}
sendPacket(uuid, createSpawnPacket());
sendPacket(uuid, entityMeta.createPacket());
}
if (EntityLib.getApi().getSettings().isDebugMode()) {
EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId);
}
}
protected WrapperPlayServerSpawnEntity createSpawnPacket() {
return new WrapperPlayServerSpawnEntity(
entityId, entityId,
Optional.of(this.uuid), Optional.of(this.uuid),
entityType, entityType,
@ -141,12 +167,9 @@ public class WrapperEntity implements Tickable, TrackedEntity {
location.getPitch(), location.getPitch(),
location.getYaw(), location.getYaw(),
location.getYaw(), location.getYaw(),
0, getObjectData(),
Optional.empty() createVeloPacket()
); );
sendPacket(uuid, packet);
sendPacket(uuid, entityMeta.createPacket());
}
} }
public void addViewer(User user) { public void addViewer(User user) {
@ -177,6 +200,10 @@ public class WrapperEntity implements Tickable, TrackedEntity {
if (!viewers.remove(uuid)) { if (!viewers.remove(uuid)) {
return; return;
} }
if (this instanceof WrapperPlayer) {
WrapperPlayer p = (WrapperPlayer) this;
sendPacket(uuid, p.tabListRemovePacket());
}
sendPacket(uuid, new WrapperPlayServerDestroyEntities(entityId)); sendPacket(uuid, new WrapperPlayServerDestroyEntities(entityId));
} }

View file

@ -7,10 +7,15 @@ import java.util.UUID;
public class WrapperLivingEntity extends WrapperEntity{ public class WrapperLivingEntity extends WrapperEntity{
private final WrapperEntityEquipment equipment;
public WrapperLivingEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta entityMeta) { public WrapperLivingEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta entityMeta) {
super(entityId, uuid, entityType, entityMeta); super(entityId, uuid, entityType, entityMeta);
this.equipment = new WrapperEntityEquipment(this);
}
public WrapperEntityEquipment getEquipment() {
return equipment;
} }
} }

View file

@ -3,40 +3,55 @@ package me.tofaa.entitylib.wrapper;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.player.*; import com.github.retrooper.packetevents.protocol.player.*;
import com.github.retrooper.packetevents.protocol.world.Location; import com.github.retrooper.packetevents.protocol.world.Location;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo; import com.github.retrooper.packetevents.util.Vector3d;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoRemove; import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate; import com.github.retrooper.packetevents.wrapper.play.server.*;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnPlayer; import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.EntityMeta; import me.tofaa.entitylib.meta.EntityMeta;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import java.util.ArrayList; import java.util.*;
import java.util.List;
public class WrapperPlayer extends WrapperLivingEntity { public class WrapperPlayer extends WrapperLivingEntity {
private final UserProfile profile; private UserProfile profile;
private GameMode gameMode = GameMode.CREATIVE; private GameMode gameMode = GameMode.CREATIVE;
private Component displayName; private Component displayName;
private boolean tablist = true; private boolean tablist = true;
private int latency = -1;
public WrapperPlayer(UserProfile profile, int entityId) { public WrapperPlayer(UserProfile profile, int entityId) {
super(entityId, profile.getUUID(), EntityTypes.PLAYER, EntityMeta.createMeta(entityId, EntityTypes.PLAYER)); super(entityId, profile.getUUID(), EntityTypes.PLAYER, EntityMeta.createMeta(entityId, EntityTypes.PLAYER));
this.profile = profile; this.profile = profile;
} }
public WrapperPlayServerPlayerInfoUpdate tabListPacket() {
EnumSet<WrapperPlayServerPlayerInfoUpdate.Action> actions = EnumSet.of(
WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LISTED
);
return new WrapperPlayServerPlayerInfoUpdate(
actions,
createInfo()
);
}
public List<TextureProperty> getTextures() {
return profile.getTextureProperties();
}
public WrapperPlayServerPlayerInfoRemove tabListRemovePacket() {
return new WrapperPlayServerPlayerInfoRemove(getUuid());
}
public void setGameMode(GameMode gameMode) { public void setGameMode(GameMode gameMode) {
this.gameMode = gameMode; this.gameMode = gameMode;
sendPacketsToViewers(new WrapperPlayServerPlayerInfo( sendPacketsToViewers(new WrapperPlayServerPlayerInfoUpdate(WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_GAME_MODE, createInfo()));
WrapperPlayServerPlayerInfo.Action.UPDATE_GAME_MODE,
new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)));
} }
public void setDisplayName(Component displayName) { public void setDisplayName(Component displayName) {
this.displayName = displayName; this.displayName = displayName;
sendPacketsToViewers(new WrapperPlayServerPlayerInfo( sendPacketsToViewers(new WrapperPlayServerPlayerInfoUpdate(WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_DISPLAY_NAME, createInfo()));
WrapperPlayServerPlayerInfo.Action.UPDATE_DISPLAY_NAME,
new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)));
} }
public Component getDisplayName() { public Component getDisplayName() {
@ -51,56 +66,54 @@ public class WrapperPlayer extends WrapperLivingEntity {
return profile.getTextureProperties(); return profile.getTextureProperties();
} }
public void setTextureProperties(List<TextureProperty> textureProperties) {
profile.setTextureProperties(textureProperties);
despawn();
System.out.println("Despawning");
EntityLib.getApi().runLater(() -> spawn(getLocation()), 2L);
}
public GameMode getGameMode() { public GameMode getGameMode() {
return gameMode; return gameMode;
} }
@Override public boolean isInTablist() {
public void addViewer(User user) { return tablist;
//user.sendPacket(createAddPacket());
sendJoiningPackets();
super.addViewer(user);
} }
@Override public void setInTablist(boolean tablist) {
public void removeViewer(User user) { this.tablist = tablist;
user.sendPacket(createRemovePacket()); sendPacketToViewers(tabListPacket());
super.removeViewer(user); if (!tablist) {
sendPacketToViewers(tabListRemovePacket());
}
} }
@Override public int getLatency() {
public boolean spawn(Location location) { return latency;
this.setLocation(location);
WrapperPlayServerSpawnPlayer packet = new WrapperPlayServerSpawnPlayer(getEntityId(), getUuid(), location, getEntityMeta());
sendPacketsToViewers(packet);
return true;
//return super.spawn(location);
} }
private void sendJoiningPackets() { public void setLatency(int latency) {
List<WrapperPlayServerPlayerInfo.PlayerData> data = new ArrayList<>(); this.latency = latency;
data.add(new WrapperPlayServerPlayerInfo.PlayerData(displayName, profile, gameMode, null, -1)); sendPacketsToViewers(
WrapperPlayServerPlayerInfo p1 = new WrapperPlayServerPlayerInfo( new WrapperPlayServerPlayerInfoUpdate(
WrapperPlayServerPlayerInfo.Action.ADD_PLAYER, WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_LATENCY,
data createInfo()
);
sendPacketsToViewers(p1);
}
private WrapperPlayServerPlayerInfoUpdate createAddPacket() {
return new WrapperPlayServerPlayerInfoUpdate(
WrapperPlayServerPlayerInfoUpdate.Action.ADD_PLAYER,
new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
profile,
true, -1, gameMode, displayName, null
) )
); );
} }
private WrapperPlayServerPlayerInfoRemove createRemovePacket() { protected WrapperPlayServerPlayerInfoUpdate.PlayerInfo createInfo() {
return new WrapperPlayServerPlayerInfoRemove(getUuid()); return new WrapperPlayServerPlayerInfoUpdate.PlayerInfo(
profile,
tablist,
latency,
gameMode,
displayName,
null
);
} }
} }

View file

@ -19,6 +19,14 @@ public interface Hologram<W> {
return new LegacyHologram<>(location, lines); return new LegacyHologram<>(location, lines);
} }
static <C> Hologram.@NotNull Modern<C> modern(@NotNull Location location) {
return new ModernHologram<>(location);
}
static <C> Hologram.@NotNull Modern<C> modern(@NotNull Location location, List<Component> lines) {
return new ModernHologram<>(location, lines);
}
@NotNull Location getLocation(); @NotNull Location getLocation();

View file

@ -19,6 +19,17 @@ final class ModernHologram<W> implements Hologram.Modern<W> {
private List<WrapperEntity> lines = new ArrayList<>(3); private List<WrapperEntity> lines = new ArrayList<>(3);
private Consumer<TextDisplayMeta> modifier; private Consumer<TextDisplayMeta> modifier;
ModernHologram(@NotNull Location location) {
this.location = location;
}
ModernHologram(@NotNull Location location, List<Component> lines) {
this(location);
for (Component line : lines) {
addLine(line);
}
}
@Override @Override
public void show() { public void show() {
for (WrapperEntity line : lines) { for (WrapperEntity line : lines) {

View file

@ -4,27 +4,60 @@ import com.github.retrooper.packetevents.protocol.entity.pose.EntityPose;
import com.github.retrooper.packetevents.protocol.player.HumanoidArm; import com.github.retrooper.packetevents.protocol.player.HumanoidArm;
import com.github.retrooper.packetevents.protocol.player.TextureProperty; import com.github.retrooper.packetevents.protocol.player.TextureProperty;
import com.github.retrooper.packetevents.protocol.player.UserProfile; import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.google.common.collect.Multimap;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.entity.Pose; import org.bukkit.entity.Pose;
import org.bukkit.inventory.MainHand; import org.bukkit.inventory.MainHand;
import org.bukkit.profile.PlayerProfile; import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures; import org.jetbrains.annotations.Nullable;
import java.util.Collections; import java.lang.reflect.InvocationTargetException;
import java.util.List; import java.lang.reflect.Method;
import java.util.UUID; import java.util.*;
public final class ExtraConversionUtil { public final class ExtraConversionUtil {
private static Method PLAYER_PROFILE_METHOD;
private static Class<?> PROFILE_CLASS;
private static Method GET_PROPERTIES_METHOD;
static {
try {
PLAYER_PROFILE_METHOD = SpigotReflectionUtil.CRAFT_PLAYER_CLASS.getMethod("getProfile");
PLAYER_PROFILE_METHOD.setAccessible(true); // Dont care :D
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private ExtraConversionUtil() { private ExtraConversionUtil() {
} }
public static UserProfile fromBukkitPlayerProfile(PlayerProfile player) { public static @Nullable UserProfile getProfileFromBukkitPlayer(Player player) {
UUID uuid = player.getUniqueId();
String name = player.getName(); try {
// TODO: Textures Object profile = PLAYER_PROFILE_METHOD.invoke(player); // GameProfile
return new UserProfile(uuid, name, Collections.emptyList()); if (PROFILE_CLASS == null) {
PROFILE_CLASS = profile.getClass();
GET_PROPERTIES_METHOD = PROFILE_CLASS.getMethod("getProperties");
GET_PROPERTIES_METHOD.setAccessible(true); // Again dont care;
}
Multimap properties = (Multimap) GET_PROPERTIES_METHOD.invoke(profile);
Collection<?> textures = properties.get("textures");
if (textures == null || textures.isEmpty()) return new UserProfile(player.getUniqueId(), player.getName());
Object texture = textures.iterator().next();
String value = (String) texture.getClass().getDeclaredMethod("value").invoke(texture);
String signature = (String) texture.getClass().getDeclaredMethod("signature").invoke(texture);
ArrayList<TextureProperty> t = new ArrayList<>();
t.add(new TextureProperty("textures", value, signature));
return new UserProfile(player.getUniqueId(), player.getName(), t);
} catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
} }
public static EntityPose fromBukkitPose(Pose pose) { public static EntityPose fromBukkitPose(Pose pose) {

View file

@ -11,6 +11,7 @@ import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSp
import me.tofaa.entitylib.TrackedEntity; import me.tofaa.entitylib.TrackedEntity;
import me.tofaa.entitylib.event.types.UserStopTrackingEntityEvent; import me.tofaa.entitylib.event.types.UserStopTrackingEntityEvent;
import me.tofaa.entitylib.event.types.UserTrackingEntityEvent; import me.tofaa.entitylib.event.types.UserTrackingEntityEvent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
@ -32,6 +33,7 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
if (type == PacketType.Play.Server.DESTROY_ENTITIES) { if (type == PacketType.Play.Server.DESTROY_ENTITIES) {
WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(event); WrapperPlayServerDestroyEntities packet = new WrapperPlayServerDestroyEntities(event);
int[] ids = packet.getEntityIds(); int[] ids = packet.getEntityIds();
Bukkit.getScheduler().runTaskLater(platform.getHandle(), () -> {
for (int id : ids) { for (int id : ids) {
TrackedEntity tracked = findTracker(id); TrackedEntity tracked = findTracker(id);
if (tracked == null) { if (tracked == null) {
@ -39,6 +41,7 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
} }
platform.getEventHandler().callEvent(UserStopTrackingEntityEvent.class, new UserStopTrackingEntityEvent(user, tracked)); platform.getEventHandler().callEvent(UserStopTrackingEntityEvent.class, new UserStopTrackingEntityEvent(user, tracked));
} }
}, 2L);
} }
else if (type == PacketType.Play.Server.SPAWN_ENTITY) { else if (type == PacketType.Play.Server.SPAWN_ENTITY) {
WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity(event); WrapperPlayServerSpawnEntity packet = new WrapperPlayServerSpawnEntity(event);
@ -73,11 +76,13 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
} }
private void trackEntity(User user, int id) { private void trackEntity(User user, int id) {
Bukkit.getScheduler().runTaskLater(platform.getHandle(), () -> {
TrackedEntity entity = findTracker(id); TrackedEntity entity = findTracker(id);
if (entity == null) { if (entity == null) {
return; return;
} }
platform.getEventHandler().callEvent(UserTrackingEntityEvent.class, new UserTrackingEntityEvent(user, entity)); platform.getEventHandler().callEvent(UserTrackingEntityEvent.class, new UserTrackingEntityEvent(user, entity));
}, 2L);
} }
private TrackedEntity findTracker(int id) { private TrackedEntity findTracker(int id) {
@ -97,7 +102,6 @@ final class InternalRegistryListener extends PacketListenerAbstract implements L
public void onEntitySpawn(EntitySpawnEvent event) { public void onEntitySpawn(EntitySpawnEvent event) {
Entity e = event.getEntity(); Entity e = event.getEntity();
platform.getPlatformEntities().put(e.getEntityId(), e); platform.getPlatformEntities().put(e.getEntityId(), e);
System.out.println("Entity spawned: " + e.getEntityId() + " " + e.getType() + " " + e.getLocation());
} }
} }

View file

@ -75,7 +75,7 @@ public class SpigotEntityLibAPI extends AbstractEntityLibAPI<JavaPlugin, BukkitT
UUID uuid = e.getUniqueId(); UUID uuid = e.getUniqueId();
if (meta instanceof PlayerMeta) { if (meta instanceof PlayerMeta) {
Player p = (Player) e; Player p = (Player) e;
entity = new WrapperPlayer(ExtraConversionUtil.fromBukkitPlayerProfile(p.getPlayerProfile()), id); entity = new WrapperPlayer(ExtraConversionUtil.getProfileFromBukkitPlayer(p), id);
} }
else if (meta instanceof LivingEntityMeta) { else if (meta instanceof LivingEntityMeta) {
entity = new WrapperLivingEntity(id, uuid, type, meta); entity = new WrapperLivingEntity(id, uuid, type, meta);
@ -106,6 +106,8 @@ public class SpigotEntityLibAPI extends AbstractEntityLibAPI<JavaPlugin, BukkitT
tickContainer.setHandle(task); tickContainer.setHandle(task);
} }
@Override
public void runLater(@NotNull Runnable runnable, long delayInTicks) {
Bukkit.getScheduler().runTaskLater(platform.getHandle(), runnable, delayInTicks);
}
} }

View file

@ -4,6 +4,8 @@ import me.tofaa.entitylib.APIConfig;
import me.tofaa.entitylib.TrackedEntity; import me.tofaa.entitylib.TrackedEntity;
import me.tofaa.entitylib.common.AbstractPlatform; import me.tofaa.entitylib.common.AbstractPlatform;
import me.tofaa.entitylib.utils.ConcurrentWeakIdentityHashMap; import me.tofaa.entitylib.utils.ConcurrentWeakIdentityHashMap;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -51,10 +53,18 @@ public class SpigotEntityLibPlatform extends AbstractPlatform<JavaPlugin> {
@Override @Override
public @Nullable TrackedEntity findPlatformEntity(final int entityId) { public @Nullable TrackedEntity findPlatformEntity(final int entityId) {
if (!api.getSettings().shouldTrackPlatformEntities()) return null; if (!api.getSettings().shouldTrackPlatformEntities()) return null;
Entity e = platformEntities.get(entityId);
if (e == null) return null; for (World world : Bukkit.getWorlds()) {
Entity e = world.getEntities().stream().filter(entity -> entity.getEntityId() == entityId).findFirst().orElse(null);
if (e != null) {
return new SpigotEntity(e); return new SpigotEntity(e);
} }
}
return null;
// Entity e = platformEntities.get(entityId);
// if (e == null) return null;
// return new SpigotEntity(e);
}
@Override @Override
public SpigotEntityLibAPI getAPI() { public SpigotEntityLibAPI getAPI() {

View file

@ -6,26 +6,25 @@ import com.github.retrooper.packetevents.protocol.player.UserProfile;
import io.github.retrooper.packetevents.util.SpigotConversionUtil; import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.APIConfig; import me.tofaa.entitylib.APIConfig;
import me.tofaa.entitylib.EntityLib; import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.mobs.passive.ChickenMeta; import me.tofaa.entitylib.event.types.UserTrackingEntityEvent;
import me.tofaa.entitylib.spigot.SpigotEntityLibAPI; import me.tofaa.entitylib.spigot.SpigotEntityLibAPI;
import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform; import me.tofaa.entitylib.spigot.SpigotEntityLibPlatform;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import me.tofaa.entitylib.event.types.*;
import me.tofaa.entitylib.wrapper.WrapperPlayer; import me.tofaa.entitylib.wrapper.WrapperPlayer;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID; import java.util.UUID;
public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor { public class TestEntityLibPlugin extends JavaPlugin {
private SpigotEntityLibAPI api; private SpigotEntityLibAPI api;
private WrapperPlayer e;
@Override @Override
public void onEnable() { public void onEnable() {
@ -39,34 +38,16 @@ public class TestEntityLibPlugin extends JavaPlugin implements CommandExecutor {
EntityLib.init(platform, settings); EntityLib.init(platform, settings);
api = platform.getAPI(); api = platform.getAPI();
getCommand("testapi").setExecutor(this);
platform.getEventHandler().addEventCallback(UserTrackingEntityEvent.class, event -> {
event.getUser().sendMessage("Tracking: " + event.getEntity().getEntityId());
event.getUser().sendMessage("Size: " + platform.queryPlatformEntities().toArray().length);
});
platform.getEventHandler().addEventCallback(UserStopTrackingEntityEvent.class, event -> {
event.getUser().sendMessage("Stop Tracking: " + event.getEntity().getEntityId());
event.getUser().sendMessage("Size: " + platform.queryPlatformEntities().toArray().length);
});
}
@Override CommandMap commandMap;
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { try {
if (!(sender instanceof Player)) return false; commandMap = (CommandMap) Bukkit.getServer().getClass().getMethod("getCommandMap").invoke(Bukkit.getServer());
Player player = (Player) sender; commandMap.register("testapi", new TestTextDisplayCommand());
if (e != null) { commandMap.register("testplayer", new TestPlayerCommand());
e.remove(); }
player.sendMessage("Removed"); catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e = null; e.printStackTrace();
return true;
} }
UUID uuid = UUID.randomUUID();
UserProfile profile = new UserProfile(uuid, "RandomGoon");
e = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(uuid, EntityTypes.PLAYER));
e.addViewer(player.getUniqueId());
api.spawnEntity(e, SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
player.sendMessage("Spawned");
return true;
} }
} }

View file

@ -0,0 +1,84 @@
package me.tofaa.testentitylib;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import com.github.retrooper.packetevents.protocol.item.ItemStack;
import com.github.retrooper.packetevents.protocol.item.type.ItemTypes;
import com.github.retrooper.packetevents.protocol.player.GameMode;
import com.github.retrooper.packetevents.protocol.player.TextureProperty;
import com.github.retrooper.packetevents.protocol.player.UserProfile;
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfo;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPlayerInfoUpdate;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.spigot.ExtraConversionUtil;
import me.tofaa.entitylib.wrapper.WrapperPlayer;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import java.util.*;
public class TestPlayerCommand extends BukkitCommand {
private WrapperPlayer p;
public TestPlayerCommand() {
super("testplayer");
}
@Override
public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) {
Player player = (Player) commandSender;
if (strings.length != 1) {
player.sendMessage("Usage: /testplayer <spawn|hello|ping|gamemode|displayname|tablist|remove>");
return false;
}
String arg = strings[0].toLowerCase();
if (arg.equals("spawn")) {
UserProfile profile = new UserProfile(UUID.randomUUID(), "randomname", new ArrayList<>());
p = new WrapperPlayer(profile, EntityLib.getPlatform().getEntityIdProvider().provide(profile.getUUID(), EntityTypes.PLAYER));
p.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
p.addViewer(player.getUniqueId());
ItemStack stack = ItemStack.builder().type(ItemTypes.DIAMOND_BOOTS).build();
p.getEquipment().setBoots(stack);
}
else if (arg.equals( "texture")) {
p.setTextureProperties(ExtraConversionUtil.getProfileFromBukkitPlayer(player).getTextureProperties());
player.sendMessage("texture");
}
else if (arg.equals( "ping")) {
p.setLatency(100);
player.sendMessage("Pong");
}
else if (arg.equals( "gamemode")) {
p.setGameMode(GameMode.CREATIVE);
player.sendMessage("Gamemode set to creative");
}
else if (arg.equals( "displayname")) {
p.setDisplayName(Component.text("Hello"));
player.sendMessage("Display name set to Hello");
}
else if (arg.equals( "tablist")) {
p.setInTablist(!p.isInTablist());
player.sendMessage("Tablist " + (p.isInTablist() ? "enabled" : "disabled"));
}
else if (arg.equals("remove")) {
p.remove();
player.sendMessage("Entity removed");
}
return true;
}
@NotNull
@Override
public List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException {
if (args.length == 1) {
return Arrays.asList(new String[]{"spawn", "texture", "ping", "gamemode", "displayname", "tablist", "remove"});
}
return Collections.emptyList();
}
}

View file

@ -0,0 +1,42 @@
package me.tofaa.testentitylib;
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
import me.tofaa.entitylib.EntityLib;
import me.tofaa.entitylib.meta.display.TextDisplayMeta;
import me.tofaa.entitylib.wrapper.WrapperEntity;
import net.kyori.adventure.text.Component;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
public class TestTextDisplayCommand extends BukkitCommand {
private WrapperEntity e;
public TestTextDisplayCommand() {
super("textdisplay");
}
@Override
public boolean execute(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String[] strings) {
if (!(commandSender instanceof Player)) return true;
Player player = (Player) commandSender;
if (e == null) {
e = EntityLib.getApi().createEntity(EntityTypes.TEXT_DISPLAY);
if (e == null) {
player.sendMessage("Failed to spawn entity");
return true;
}
e.spawn(SpigotConversionUtil.fromBukkitLocation(player.getLocation()));
e.addViewer(player.getUniqueId());
player.sendMessage("Spawned");
}
String msg = String.join(" ", strings);
TextDisplayMeta meta = (TextDisplayMeta) e.getEntityMeta();
meta.setText(Component.text(msg));
player.sendMessage("Set text to: " + msg);
return true;
}
}