From c7983b261c9167eaa0a8eac35d2f43cfc664816b Mon Sep 17 00:00:00 2001 From: Tofaa <82680183+Tofaa2@users.noreply.github.com> Date: Fri, 5 Jan 2024 23:09:18 +0300 Subject: [PATCH] add some explanatory javadocs --- .idea/encodings.xml | 7 ++ .idea/gradle.xml | 11 ++- .idea/misc.xml | 14 +++ settings.gradle | 1 - .../java/me/tofaa/entitylib/EntityLib.java | 96 ++++++++++++++++++- .../entitylib/entity/EntityIdProvider.java | 1 - 6 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 .idea/encodings.xml diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..4d93d1d --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 7d081d3..f6ba79e 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -9,8 +9,17 @@ + + + diff --git a/.idea/misc.xml b/.idea/misc.xml index a58e41d..7c27775 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,3 +1,4 @@ + @@ -8,6 +9,19 @@ + + + + diff --git a/settings.gradle b/settings.gradle index fa9e207..a756887 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,3 @@ rootProject.name = 'EntityLib' include 'test-plugin' -include 'wrapper-entity' diff --git a/src/main/java/me/tofaa/entitylib/EntityLib.java b/src/main/java/me/tofaa/entitylib/EntityLib.java index 0c9be28..fdd6d65 100644 --- a/src/main/java/me/tofaa/entitylib/EntityLib.java +++ b/src/main/java/me/tofaa/entitylib/EntityLib.java @@ -6,6 +6,8 @@ import com.github.retrooper.packetevents.event.PacketReceiveEvent; import com.github.retrooper.packetevents.manager.server.ServerVersion; import com.github.retrooper.packetevents.protocol.entity.type.EntityType; import com.github.retrooper.packetevents.protocol.packettype.PacketType; +import com.github.retrooper.packetevents.protocol.player.InteractionHand; +import com.github.retrooper.packetevents.protocol.player.User; import com.github.retrooper.packetevents.wrapper.PacketWrapper; import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientInteractEntity; import me.tofaa.entitylib.entity.EntityInteractionProcessor; @@ -15,6 +17,7 @@ import me.tofaa.entitylib.exception.InvalidVersionException; import me.tofaa.entitylib.meta.EntityMeta; import me.tofaa.entitylib.meta.Metadata; import me.tofaa.entitylib.meta.types.LivingEntityMeta; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -45,6 +48,14 @@ public final class EntityLib { private static PacketEventsAPI packetEvents; private static MetaConverterRegistry metaRegistry; + /** + * Initialize EntityLib. + *

+ * This method should be called after PacketEvents is initialized. + * Loads the internal metadata converter registry and sets the library usable + *

+ * @param packetEvents PacketEventsAPI instance + */ public static void init(@NotNull PacketEventsAPI packetEvents) { if (initialized) { throw new IllegalStateException("EntityLib is already initialized"); @@ -58,6 +69,16 @@ public final class EntityLib { metaRegistry = new MetaConverterRegistry(); } + /** + * Enable entity interactions. + *

+ * Enables entity interactions to be handled by EntityLib, rather than the developer. + *
+ * This will register a PacketEvents listener for {@link PacketType.Play.Client#INTERACT_ENTITY} and call {@link EntityInteractionProcessor#process(WrapperEntity, WrapperPlayClientInteractEntity.InteractAction, InteractionHand, User)}. + *
+ * To set the interaction processor, call {@link EntityLib#setInteractionProcessor(EntityInteractionProcessor)}. + *

+ */ public static void enableEntityInteractions() { checkInit(); packetEvents.getEventManager().registerListener(new PacketListenerAbstract() { @@ -75,16 +96,35 @@ public final class EntityLib { }); } + /** + * @param entityId the entity id + * @return the entity with the given id, or null if an entity with that id does not exist + */ public static @Nullable WrapperEntity getEntity(int entityId) { checkInit(); return entitiesById.get(entityId); } + /** + * @param uuid the entity uuid + * @return the entity with the given uuid, or null if an entity with that uuid does not exist + */ public static @Nullable WrapperEntity getEntity(UUID uuid) { checkInit(); return entities.get(uuid); } + + /** + * Creates a new WrapperEntity with the given UUID and EntityType. + * This method will automatically create a new EntityMeta for the entity and keeps track of it internally. + * To get the entity, use {@link EntityLib#getEntity(UUID)} or {@link EntityLib#getEntity(int)}. + *

+ * In theoretically impossible cases, this method may return null. + * @param uuid the entity uuid + * @param entityType the entity type + * @return the created entity, or null if the entity could not be created + */ public static @Nullable WrapperEntity createEntity(UUID uuid, EntityType entityType) { checkInit(); int id = WrapperEntity.ID_PROVIDER.provide(); @@ -102,11 +142,21 @@ public final class EntityLib { return entity; } + /** + * @param entityId the entity id + * @return the metadata of the entity with the given id. If the entity does not exist, this method will return null. + */ public static @Nullable EntityMeta getMeta(int entityId) { checkInit(); return metadata.get(entityId); } + /** + * @param entityId the entity id + * @param metaClass the metadata class to cast to + * @return the metadata of the entity with the given id, cast to the given class. If the entity does not exist, this method will return null. + * @param the metadata class + */ public static @Nullable T getMeta(int entityId, Class metaClass) { checkInit(); EntityMeta meta = metadata.get(entityId); @@ -119,6 +169,12 @@ public final class EntityLib { return null; } + /** + * Creates a new EntityMeta for the given entity id and type, these are stored internally and can be retrieved using {@link EntityLib#getMeta(int)}. + * @param entityId the entity id + * @param entityType the entity type + * @return the created EntityMeta + */ public static EntityMeta createMeta(int entityId, EntityType entityType) { checkInit(); Metadata m = new Metadata(entityId); @@ -128,38 +184,68 @@ public final class EntityLib { return meta; } + /** + * Creates a new EntityMeta for an entity, these are stored internally and can be retrieved using {@link EntityLib#getMeta(int)}. + * @param entity the entity + * @return the created EntityMeta + */ public static EntityMeta createMeta(WrapperEntity entity) { return createMeta(entity.getEntityId(), entity.getEntityType()); } + /** + * @param entityType the entity type + * @return the metadata class of the given entity type + * @param gets the appropriate metadata class for the given entity type + */ public static Class getMetaClassOf(EntityType entityType) { return metaRegistry.getMetaClass(entityType); } + /** + * @return the packet events api instance that was used to initialize EntityLib + */ public static PacketEventsAPI getPacketEvents() { checkInit(); return packetEvents; } - public static void sendPacket(UUID user, PacketWrapper wrapper) { - checkInit(); - packetEvents.getProtocolManager().sendPacket(packetEvents.getProtocolManager().getChannel(user), wrapper); - } - + /** + * @return the specified interaction processor, or null if none is specified + */ public static @Nullable EntityInteractionProcessor getInteractionProcessor() { return interactionProcessor; } + /** + * Sets the interaction processor to the given one. + * @param interactionProcessor the interaction processor + */ public static void setInteractionProcessor(EntityInteractionProcessor interactionProcessor) { EntityLib.interactionProcessor = interactionProcessor; } + /** + * Another internal method to verify the server version is supported. Safe to use externally as its purpose is simple and to avoid code duplication + */ + @ApiStatus.Internal public static void verifyVersion(ServerVersion supported, String msg) { if (packetEvents.getServerManager().getVersion().isOlderThan(supported)) { throw new InvalidVersionException(msg); } } + /** + * A primarily internal method to send a packet wrapper to a User from the users UUID. This is useful for methods in {@link WrapperEntity}. Safe to use externally as its purpose is simple and to avoid code duplication + * @param user the user uuid + * @param wrapper the packet wrapper + */ + @ApiStatus.Internal + public static void sendPacket(UUID user, PacketWrapper wrapper) { + checkInit(); + packetEvents.getProtocolManager().sendPacket(packetEvents.getProtocolManager().getChannel(user), wrapper); + } + private static void checkInit() { if (!initialized) { throw new IllegalStateException("EntityLib is not initialized"); diff --git a/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java b/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java index 4c342c2..b461786 100644 --- a/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java +++ b/src/main/java/me/tofaa/entitylib/entity/EntityIdProvider.java @@ -1,6 +1,5 @@ package me.tofaa.entitylib.entity; - import java.util.concurrent.atomic.AtomicInteger; @FunctionalInterface