Skip to content

Commit af5cae5

Browse files
author
Eliasegg
committed
Initial commit
0 parents  commit af5cae5

22 files changed

Lines changed: 1486 additions & 0 deletions

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Eclipse
2+
.classpath
3+
.project
4+
.settings
5+
6+
# IntelliJ
7+
.idea
8+
*.iml
9+
*.iws
10+
out
11+
12+
# Mac
13+
.DS_Store
14+
15+
# Maven
16+
log
17+
target
18+
dependency-reduced-pom.xml
19+
jars

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# PacketEntityAPI
2+
3+
FadeCloud Trial Project! spawn entities and do all kinds of things with them.
4+
## Features
5+
- Easy to use.
6+
- Well documented.
7+
- Highly-packed lightweight API.
8+
- Entities can be equipped with items.
9+
- Only dependency is ProtocolLib.
10+
- Entity teleport.
11+
- Entity move to a location.
12+
- Entities can play animations.
13+
- Metadata 100% supported. Entities can have Poses like sneaking/swimming.
14+
- You can also apply glowing effects and set the entity on fire.
15+
- Viewers list. You can decide to hide/show entities for whoever you want.
16+
- Entity Manager with useful methods to help you in your development.
17+
- TAB auto complete.
18+
- Reload config with a command.
19+
- Highly-packed lightweight plugin.
20+
- Easily extendable.
21+
## API Usage
22+
You'll want to use the manager in order to store all the custom entities on a list. This is for later use in your development, that way you can retrieve these whenever you want to.
23+
Everytime the create method is called, it will store the CustomEntity object into its list.
24+
25+
The way we spawn an entity is the following:
26+
```java
27+
CustomEntity entity = CustomEntityManager.getManager().createEntity(PacketEntityType type, Location location, UUID uuid);
28+
entity.addToViewerList(Player player);
29+
```
30+
31+
- **PacketEntityType** is a custom enum class. This was decided because Mojang likes to change entity ID's every update. I've grabbed the 1.18.2 ones and put them in a compact enum class. You can use it in the constructor like this: `PacketEntityType.BLAZE`. The other arguments are self-explanatory, the only note I have is that the UUID is set by us for later retrieval if we want to.
32+
- **CustomEntity#addToViewerList**. This allows you to add a player to the list of players that can see this entity. You can call **CustomEntity#removeFromViewersList** in order to remove the player from the viewers list. This does invoke the destroy packet to the entity but the CustomEntity object is still stored in the manager.
33+
34+
We can go ahead and destroy the entity for good using: `entity.destroy()`.
35+
36+
## Additional features
37+
There are multiple useful methods to use with the entity. They are all documented and tested! As a disclaimer though, some methods won't work for certain specific types of entities. For instance, you cannot put a helmet in a bee as that is not possible due to Minecraft limitations.
38+
39+
Some of the entity features are:
40+
- **addDisplayName** Adds a display name to the entity.
41+
- **hide** Hides the entity. This only makes the entity invisible.
42+
- **unhide** Unhides the entity.
43+
- **teleport** Teleports the entity.
44+
- **moveHere** Moves the entity to a specific location.
45+
- **moveHead** Moves the head of the entity to a specific location.
46+
- **addEquipment** Adds equipment to the entity and to a specific equipment slot.
47+
- **playAnimation** Plays an animation such as Crouching, Swimming, Elytra Flying, Sleeping.
48+
- **setMetadata** Sets the metadata for an entity. Mostly internal use but can be used if you want to add a missing metadata value.
49+
## How it works?
50+
51+
All of the magic is taken care of in the **CustomEntity** and **PacketWrapper** classes. This is where the entities, methods and useful stuff is inside. We have the wrappers package that contains all the PacketWrapper children, these represent a different packet that is needed to be sent to the client in order to get what we need for the entity to work as we expect to.
52+
53+
## Extending Party Crackers and Rewards
54+
As mentioned before, all you need to do is create a class that extends PacketWrapper. This will give you a CustomEntity object that you can use to execute the packets to.
55+
56+
It is pretty easy and straightforward. More stuff can be added such as Entity hit and what not.
57+
## Extra info and dependencies.
58+
59+
- Tested against Java 17, Minecraft 1.18.2 though it should work from 1.15 - 1.18.2 as some fields in the move packet were changed from integers to shorts.
60+
- [ProtocolLib ](https://www.spigotmc.org/resources/protocollib.1997/) is the only dependency. It made my life easier.
61+
- Little to no external libraries were used besides a borrowed class from [IllusionTheDev](https://gist.github.com/IllusionTheDev/8b0761be3b699fcfc0c082b753e6f063) in order to add display names.
62+
63+
I hope this API is good for your needs, a good effort (and a can of coke) went into it. ¡Adiós!
64+
65+
Total coding time including battle-testing it: 12 hours.

api/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<packaging>jar</packaging>
6+
<parent>
7+
<artifactId>PacketEntityAPI</artifactId>
8+
<groupId>com.fadecloud.packetentityapi</groupId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<modelVersion>4.0.0</modelVersion>
13+
<artifactId>packetentityapi-api</artifactId>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
18+
</properties>
19+
20+
<build>
21+
<defaultGoal>clean package</defaultGoal>
22+
<plugins>
23+
<plugin>
24+
<groupId>org.apache.maven.plugins</groupId>
25+
<artifactId>maven-compiler-plugin</artifactId>
26+
<version>3.8.1</version>
27+
<configuration>
28+
<source>17</source>
29+
<target>17</target>
30+
</configuration>
31+
</plugin>
32+
</plugins>
33+
</build>
34+
35+
<repositories>
36+
<repository>
37+
<id>spigot-repo</id>
38+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
39+
</repository>
40+
<repository>
41+
<id>dmulloy2-repo</id>
42+
<url>https://repo.dmulloy2.net/nexus/repository/public/</url>
43+
</repository>
44+
</repositories>
45+
46+
<dependencies>
47+
<dependency>
48+
<groupId>org.spigotmc</groupId>
49+
<artifactId>spigot-api</artifactId>
50+
<version>1.18.2-R0.1-SNAPSHOT</version>
51+
<scope>provided</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.comphenix.protocol</groupId>
55+
<artifactId>ProtocolLib</artifactId>
56+
<version>4.8.0</version>
57+
<scope>provided</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
</project>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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

Comments
 (0)