feat: potion effect controller system
This commit is contained in:
parent
53e4d917cd
commit
0868a75617
3 changed files with 432 additions and 85 deletions
|
@ -25,11 +25,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
|||
import java.util.function.Consumer;
|
||||
|
||||
public class WrapperEntity implements Tickable {
|
||||
|
||||
private final UUID uuid;
|
||||
private final int entityId;
|
||||
private EntityType entityType;
|
||||
private EntityMeta entityMeta;
|
||||
private final EntityType entityType;
|
||||
private final EntityMeta entityMeta;
|
||||
private boolean ticking;
|
||||
protected Location location;
|
||||
private Location preRidingLocation;
|
||||
|
@ -62,6 +61,7 @@ public class WrapperEntity implements Tickable {
|
|||
public WrapperEntity(UUID uuid, EntityType entityType) {
|
||||
this(EntityLib.getPlatform().getEntityIdProvider().provide(uuid, entityType), uuid, entityType);
|
||||
}
|
||||
|
||||
public WrapperEntity(EntityType entityType) {
|
||||
this(EntityLib.getPlatform().getEntityUuidProvider().provide(entityType), entityType);
|
||||
}
|
||||
|
@ -72,8 +72,10 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
public boolean spawn(Location location, EntityContainer parent) {
|
||||
if (spawned) return false;
|
||||
|
||||
this.location = location;
|
||||
this.spawned = true;
|
||||
|
||||
sendPacketsToViewers(
|
||||
new WrapperPlayServerSpawnEntity(
|
||||
entityId,
|
||||
|
@ -88,9 +90,14 @@ public class WrapperEntity implements Tickable {
|
|||
),
|
||||
entityMeta.createPacket()
|
||||
);
|
||||
|
||||
if (this instanceof WrapperLivingEntity) {
|
||||
sendPacketsToViewers(((WrapperLivingEntity)this).getAttributes().createPacket());
|
||||
WrapperLivingEntity wrapperLivingEntity = (WrapperLivingEntity) this;
|
||||
wrapperLivingEntity.createSpawnPackets().forEach(packetWrapper -> {
|
||||
sendPacket(uuid, packetWrapper);
|
||||
});
|
||||
}
|
||||
|
||||
this.parent = parent;
|
||||
parent.addEntity(this);
|
||||
return true;
|
||||
|
@ -101,7 +108,7 @@ public class WrapperEntity implements Tickable {
|
|||
return SpawnPacketProvider.GENERAL.provide(this);
|
||||
}
|
||||
|
||||
public boolean spawn(Location location) {
|
||||
public boolean spawn(@NotNull Location location) {
|
||||
return spawn(location, EntityLib.getApi().getDefaultContainer());
|
||||
}
|
||||
|
||||
|
@ -110,6 +117,7 @@ public class WrapperEntity implements Tickable {
|
|||
if (entityMeta instanceof ObjectData) {
|
||||
return ((ObjectData) entityMeta).getObjectData();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -117,6 +125,7 @@ public class WrapperEntity implements Tickable {
|
|||
public Optional<Vector3d> createVeloPacket() {
|
||||
Optional<Vector3d> velocity;
|
||||
double veloX = 0, veloY = 0, veloZ = 0;
|
||||
|
||||
if (entityMeta instanceof ObjectData) {
|
||||
ObjectData od = (ObjectData) entityMeta;
|
||||
if (od.requiresVelocityPacketAtSpawn()) {
|
||||
|
@ -126,11 +135,13 @@ public class WrapperEntity implements Tickable {
|
|||
veloZ = veloPacket.getVelocity().getZ();
|
||||
}
|
||||
}
|
||||
|
||||
if (veloX == 0 && veloY == 0 && veloZ == 0) {
|
||||
velocity = Optional.of(Vector3d.zero());
|
||||
} else {
|
||||
velocity = Optional.of(new Vector3d(veloX, veloY, veloZ));
|
||||
}
|
||||
|
||||
return velocity;
|
||||
}
|
||||
|
||||
|
@ -141,37 +152,37 @@ public class WrapperEntity implements Tickable {
|
|||
public void remove() {
|
||||
if (parent != null) {
|
||||
parent.removeEntity(this, true);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
despawn();
|
||||
}
|
||||
}
|
||||
|
||||
public void despawn() {
|
||||
if (!spawned) return;
|
||||
|
||||
spawned = false;
|
||||
|
||||
if (this instanceof WrapperPlayer) {
|
||||
WrapperPlayer p = (WrapperPlayer) this;
|
||||
sendPacketsToViewers(p.tabListRemovePacket());
|
||||
}
|
||||
|
||||
sendPacketToViewers(new WrapperPlayServerDestroyEntities(entityId));
|
||||
}
|
||||
|
||||
public void teleport(@NotNull Location location, boolean onGround) {
|
||||
if (!spawned) {
|
||||
return;
|
||||
}
|
||||
if (!spawned) return;
|
||||
|
||||
this.location = location;
|
||||
this.onGround = onGround;
|
||||
sendPacketToViewers(
|
||||
new WrapperPlayServerEntityTeleport(
|
||||
|
||||
sendPacketToViewers(new WrapperPlayServerEntityTeleport(
|
||||
entityId,
|
||||
location.getPosition(),
|
||||
location.getYaw(),
|
||||
location.getPitch(),
|
||||
onGround
|
||||
)
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
public void teleport(@NotNull Location location) {
|
||||
|
@ -180,30 +191,40 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Adds a viewer to the viewers set. The viewer will receive all packets and be informed of this addition
|
||||
*
|
||||
* @param uuid the uuid of the user to add
|
||||
*/
|
||||
public void addViewer(UUID uuid) {
|
||||
public void addViewer(@NotNull UUID uuid) {
|
||||
if (!viewers.add(uuid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (location == null) {
|
||||
if (EntityLib.getApi().getSettings().isDebugMode()) {
|
||||
EntityLib.getPlatform().getLogger().warning("Location is null for entity " + entityId + ". Cannot spawn.");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (spawned) {
|
||||
if (this instanceof WrapperPlayer) {
|
||||
WrapperPlayer p = (WrapperPlayer) this;
|
||||
sendPacket(uuid, p.tabListPacket());
|
||||
}
|
||||
|
||||
sendPacket(uuid, createSpawnPacket());
|
||||
sendPacket(uuid, entityMeta.createPacket());
|
||||
sendPacket(uuid, createPassengerPacket());
|
||||
|
||||
if (this instanceof WrapperLivingEntity) {
|
||||
sendPacket(uuid, ((WrapperLivingEntity)this).getAttributes().createPacket());
|
||||
WrapperLivingEntity wrapperLivingEntity = (WrapperLivingEntity) this;
|
||||
wrapperLivingEntity.createSpawnPackets().forEach(packetWrapper -> {
|
||||
sendPacket(uuid, packetWrapper);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (EntityLib.getApi().getSettings().isDebugMode()) {
|
||||
EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId);
|
||||
}
|
||||
|
@ -235,62 +256,70 @@ public class WrapperEntity implements Tickable {
|
|||
);
|
||||
}
|
||||
|
||||
public void addViewer(User user) {
|
||||
public void addViewer(@NotNull User user) {
|
||||
addViewer(user.getUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a viewer silently into the viewers set. The viewer will not receive any packets or be informed of this addition
|
||||
*
|
||||
* @param uuid the uuid of the user to add
|
||||
*/
|
||||
public void addViewerSilently(UUID uuid) {
|
||||
public void addViewerSilently(@NotNull UUID uuid) {
|
||||
viewers.add(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a viewer silently into the viewers set. The viewer will not receive any packets or be informed of this addition
|
||||
*
|
||||
* @param user the user to add
|
||||
*/
|
||||
public void addViewerSilently(User user) {
|
||||
public void addViewerSilently(@NotNull User user) {
|
||||
addViewerSilently(user.getUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a viewer from the viewers set of this entity. The viewer will be informed of this removal and will no longer receive any packets
|
||||
*
|
||||
* @param uuid the uuid of the user to remove
|
||||
*/
|
||||
public void removeViewer(UUID uuid) {
|
||||
public void removeViewer(@NotNull UUID uuid) {
|
||||
if (!viewers.remove(uuid)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this instanceof WrapperPlayer) {
|
||||
WrapperPlayer p = (WrapperPlayer) this;
|
||||
sendPacket(uuid, p.tabListRemovePacket());
|
||||
}
|
||||
|
||||
sendPacket(uuid, new WrapperPlayServerDestroyEntities(entityId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a viewer from the viewers set of this entity. The viewer will be informed of this removal and will no longer receive any packets
|
||||
*
|
||||
* @param user the user to remove
|
||||
*/
|
||||
public void removeViewer(User user) {
|
||||
public void removeViewer(@NotNull User user) {
|
||||
removeViewer(user.getUUID());
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a viewer silently into the viewers set. The viewer will not receive any packets or be informed of this removal
|
||||
*
|
||||
* @param uuid of the user to remove
|
||||
*/
|
||||
public void removeViewerSilently(UUID uuid) {
|
||||
public void removeViewerSilently(@NotNull UUID uuid) {
|
||||
viewers.remove(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* removes a viewer silently into the viewers set. The viewer will not receive any packets or be informed of this removal
|
||||
*
|
||||
* @param user the user to remove
|
||||
*/
|
||||
public void removeViewerSilently(User user) {
|
||||
public void removeViewerSilently(@NotNull User user) {
|
||||
removeViewerSilently(user.getUUID());
|
||||
}
|
||||
|
||||
|
@ -298,11 +327,11 @@ public class WrapperEntity implements Tickable {
|
|||
return onGround;
|
||||
}
|
||||
|
||||
public Vector3d getVelocity() {
|
||||
public @NotNull Vector3d getVelocity() {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
public void setVelocity(Vector3d velocity) {
|
||||
public void setVelocity(@NotNull Vector3d velocity) {
|
||||
this.velocity = velocity;
|
||||
sendPacketToViewers(getVelocityPacket());
|
||||
}
|
||||
|
@ -331,7 +360,7 @@ public class WrapperEntity implements Tickable {
|
|||
return entityId;
|
||||
}
|
||||
|
||||
public EntityMeta getEntityMeta() {
|
||||
public @NotNull EntityMeta getEntityMeta() {
|
||||
return entityMeta;
|
||||
}
|
||||
|
||||
|
@ -339,12 +368,12 @@ public class WrapperEntity implements Tickable {
|
|||
return metaClass.cast(entityMeta);
|
||||
}
|
||||
|
||||
public <T extends EntityMeta> void consumeEntityMeta(@NotNull Class<T> metaClass, Consumer<T> consumer) {
|
||||
public <T extends EntityMeta> void consumeEntityMeta(@NotNull Class<T> metaClass, @NotNull Consumer<T> consumer) {
|
||||
T meta = getEntityMeta(metaClass);
|
||||
consumer.accept(meta);
|
||||
}
|
||||
|
||||
public void consumeMeta(Consumer<EntityMeta> consumer) {
|
||||
public void consumeMeta(@NotNull Consumer<EntityMeta> consumer) {
|
||||
consumer.accept(entityMeta);
|
||||
}
|
||||
|
||||
|
@ -358,13 +387,14 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Returns an unmodifiable set of the passengers of the entity.
|
||||
*
|
||||
* @return the passengers of the entity
|
||||
*/
|
||||
public Set<Integer> getPassengers() {
|
||||
public @NotNull Set<Integer> getPassengers() {
|
||||
return Collections.unmodifiableSet(passengers);
|
||||
}
|
||||
|
||||
public WrapperEntity getRiding() {
|
||||
public @Nullable WrapperEntity getRiding() {
|
||||
return EntityLib.getApi().getEntity(riding);
|
||||
}
|
||||
|
||||
|
@ -433,20 +463,22 @@ public class WrapperEntity implements Tickable {
|
|||
new WrapperPlayServerEntityRotation(entityId, yaw, pitch, onGround),
|
||||
new WrapperPlayServerEntityHeadLook(entityId, yaw)
|
||||
);
|
||||
|
||||
this.location.setYaw(yaw);
|
||||
this.location.setPitch(pitch);
|
||||
}
|
||||
|
||||
public void rotateHead(Location location) {
|
||||
public void rotateHead(@NotNull Location location) {
|
||||
rotateHead(location.getYaw(), location.getPitch());
|
||||
}
|
||||
|
||||
public void rotateHead(WrapperEntity entity) {
|
||||
public void rotateHead(@NotNull WrapperEntity entity) {
|
||||
rotateHead(entity.getLocation());
|
||||
}
|
||||
|
||||
public void refresh() {
|
||||
if (!spawned) return;
|
||||
|
||||
sendPacketsToViewers(entityMeta.createPacket(), createPassengerPacket());
|
||||
}
|
||||
|
||||
|
@ -457,13 +489,12 @@ public class WrapperEntity implements Tickable {
|
|||
sendPacket(uuid, packet);
|
||||
sendPacket(uuid, new WrapperPlayServerBundle());
|
||||
});
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
viewers.forEach(uuid -> sendPacket(uuid, packet));
|
||||
}
|
||||
}
|
||||
|
||||
public void sendPacketsToViewers(PacketWrapper<?>... wrappers) {
|
||||
public void sendPacketsToViewers(PacketWrapper<?> @NotNull ... wrappers) {
|
||||
for (PacketWrapper<?> wrapper : wrappers) {
|
||||
sendPacketToViewers(wrapper);
|
||||
}
|
||||
|
@ -483,13 +514,16 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
private static void sendPacket(UUID user, PacketWrapper<?> wrapper) {
|
||||
if (wrapper == null) return;
|
||||
|
||||
Object channel = EntityLib.getApi().getPacketEvents().getProtocolManager().getChannel(user);
|
||||
if (channel == null) {
|
||||
if (EntityLib.getApi().getSettings().isDebugMode()) {
|
||||
EntityLib.getPlatform().getLogger().warning("Failed to send packet to " + user + " because the channel was null. They may be disconnected/not online.");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
EntityLib.getApi().getPacketEvents().getProtocolManager().sendPacket(channel, wrapper);
|
||||
}
|
||||
|
||||
|
@ -504,12 +538,14 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
|
||||
*
|
||||
* @param passenger the entity id of the passenger
|
||||
*/
|
||||
public void addPassenger(int passenger) {
|
||||
if (passengers.contains(passenger)) {
|
||||
throw new IllegalArgumentException("Passenger already exists");
|
||||
}
|
||||
|
||||
passengers.add(passenger);
|
||||
sendPacketToViewers(createPassengerPacket());
|
||||
WrapperEntity e = EntityLib.getApi().getEntity(passenger);
|
||||
|
@ -533,9 +569,10 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
|
||||
*
|
||||
* @param passengers the entity ids of the passengers
|
||||
*/
|
||||
public void addPassengers(int... passengers) {
|
||||
public void addPassengers(int @NotNull ... passengers) {
|
||||
for (int passenger : passengers) {
|
||||
addPassenger(passenger);
|
||||
}
|
||||
|
@ -543,17 +580,19 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Adds a passenger to the entity. The passenger will be visible to all viewers of the entity.
|
||||
*
|
||||
* @param passenger the wrapper entity passenger
|
||||
*/
|
||||
public void addPassenger(WrapperEntity passenger) {
|
||||
public void addPassenger(@NotNull WrapperEntity passenger) {
|
||||
addPassenger(passenger.getEntityId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds multiple passengers to the entity. The passengers will be visible to all viewers of the entity.
|
||||
*
|
||||
* @param passengers the wrapper entity passengers
|
||||
*/
|
||||
public void addPassengers(WrapperEntity... passengers) {
|
||||
public void addPassengers(WrapperEntity @NotNull ... passengers) {
|
||||
for (WrapperEntity passenger : passengers) {
|
||||
addPassenger(passenger);
|
||||
}
|
||||
|
@ -561,12 +600,14 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
|
||||
*
|
||||
* @param passenger the entity id of the passenger
|
||||
*/
|
||||
public void removePassenger(int passenger) {
|
||||
if (!passengers.contains(passenger)) {
|
||||
throw new IllegalArgumentException("Passenger does not exist");
|
||||
}
|
||||
|
||||
passengers.remove(passenger);
|
||||
sendPacketToViewers(createPassengerPacket());
|
||||
WrapperEntity e = EntityLib.getApi().getEntity(passenger);
|
||||
|
@ -588,15 +629,16 @@ public class WrapperEntity implements Tickable {
|
|||
* @param passenger the passenger wrapper entity
|
||||
* @return true if the entity has the passenger, false otherwise
|
||||
*/
|
||||
public boolean hasPassenger(WrapperEntity passenger) {
|
||||
public boolean hasPassenger(@NotNull WrapperEntity passenger) {
|
||||
return hasPassenger(passenger.getEntityId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
|
||||
*
|
||||
* @param passengers the entity ids of the passengers
|
||||
*/
|
||||
public void removePassengers(int... passengers) {
|
||||
public void removePassengers(int @NotNull ... passengers) {
|
||||
for (int passenger : passengers) {
|
||||
removePassenger(passenger);
|
||||
}
|
||||
|
@ -604,17 +646,19 @@ public class WrapperEntity implements Tickable {
|
|||
|
||||
/**
|
||||
* Removes a passenger from the entity. The passenger will be removed from the view of all viewers of the entity.
|
||||
*
|
||||
* @param passenger the wrapper entity passenger
|
||||
*/
|
||||
public void removePassenger(WrapperEntity passenger) {
|
||||
public void removePassenger(@NotNull WrapperEntity passenger) {
|
||||
removePassenger(passenger.getEntityId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes multiple passengers from the entity. The passengers will be removed from the view of all viewers of the entity.
|
||||
*
|
||||
* @param passengers the wrapper entity passengers
|
||||
*/
|
||||
public void removePassengers(WrapperEntity... passengers) {
|
||||
public void removePassengers(WrapperEntity @NotNull ... passengers) {
|
||||
for (WrapperEntity passenger : passengers) {
|
||||
removePassenger(passenger);
|
||||
}
|
||||
|
@ -631,11 +675,11 @@ public class WrapperEntity implements Tickable {
|
|||
return Collections.unmodifiableSet(viewers);
|
||||
}
|
||||
|
||||
public boolean hasViewer(UUID uuid) {
|
||||
public boolean hasViewer(@NotNull UUID uuid) {
|
||||
return viewers.contains(uuid);
|
||||
}
|
||||
|
||||
public boolean hasViewer(User user) {
|
||||
public boolean hasViewer(@NotNull User user) {
|
||||
return hasViewer(user.getUUID());
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,275 @@
|
|||
package me.tofaa.entitylib.wrapper;
|
||||
|
||||
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
|
||||
import com.github.retrooper.packetevents.protocol.potion.PotionType;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityEffect;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerRemoveEntityEffect;
|
||||
import me.tofaa.entitylib.tick.Tickable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class WrapperEntityPotionEffect implements Tickable {
|
||||
private final WrapperLivingEntity entity;
|
||||
private final Map<PotionType, WrapperPotionEffect> effects = new ConcurrentHashMap<>();
|
||||
|
||||
private boolean notifyChanges = true;
|
||||
private boolean ticking = true;
|
||||
|
||||
public WrapperEntityPotionEffect(WrapperLivingEntity entity) {
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param ambient A
|
||||
* @param visible A
|
||||
* @param showIcons A
|
||||
* @param factorData The factor data of the potion effect, if hasFactorData is false this will be ignored
|
||||
*/
|
||||
public void addPotionEffect(
|
||||
PotionType type,
|
||||
int amplifier,
|
||||
int duration,
|
||||
boolean ambient,
|
||||
boolean visible,
|
||||
boolean showIcons,
|
||||
@Nullable NBTCompound factorData
|
||||
) {
|
||||
this.effects.put(type, new WrapperPotionEffect(
|
||||
type,
|
||||
amplifier,
|
||||
duration,
|
||||
ambient,
|
||||
visible,
|
||||
showIcons,
|
||||
factorData
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param flags The bit flags of the potion effect see: <a href="https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect">https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect</a>
|
||||
* @param factorData The factor data of the potion effect, if hasFactorData is false this will be ignored
|
||||
*/
|
||||
public void addPotionEffect(
|
||||
PotionType type,
|
||||
int amplifier,
|
||||
int duration,
|
||||
byte flags,
|
||||
@Nullable NBTCompound factorData
|
||||
) {
|
||||
BitSet flagsBitSet = BitSet.valueOf(new byte[]{flags});
|
||||
|
||||
boolean ambient = flagsBitSet.get(0);
|
||||
boolean visible = flagsBitSet.get(1);
|
||||
boolean icons = flagsBitSet.get(2);
|
||||
|
||||
addPotionEffect(type, amplifier, duration, ambient, visible, icons, factorData);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param flags The bit flags of the potion effect see: <a href="https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect">https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect</a>
|
||||
* @param hasFactorData Whether the potion effect has factor data
|
||||
* @param factorData The factor data of the potion effect, if hasFactorData is false this will be ignored
|
||||
*/
|
||||
public void addPotionEffect(
|
||||
PotionType type,
|
||||
int amplifier,
|
||||
int duration,
|
||||
byte flags,
|
||||
boolean hasFactorData,
|
||||
@Nullable NBTCompound factorData
|
||||
) {
|
||||
addPotionEffect(type, amplifier, duration, flags, hasFactorData ? factorData : null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a potion effect to the entity.
|
||||
* EntityLib will not keep track of the potions you give or what you do with them,
|
||||
* this simply sends the packet to the viewers of the entity.
|
||||
*
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param flags The bit flags of the potion effect see: <a href="https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect">https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect</a>
|
||||
*/
|
||||
public void addPotionEffect(
|
||||
PotionType type,
|
||||
int amplifier,
|
||||
int duration,
|
||||
byte flags
|
||||
) {
|
||||
addPotionEffect(type, amplifier, duration, flags, false, null);
|
||||
}
|
||||
|
||||
public void removePotionEffect(@NotNull PotionType potionType) {
|
||||
if (this.effects.remove(potionType) != null) {
|
||||
this.entity.sendPacketsToViewers(createRemoveEffectPacket(potionType));
|
||||
}
|
||||
}
|
||||
|
||||
public void clearPotionEffects() {
|
||||
new ArrayList<>(this.effects.keySet()).forEach(this::removePotionEffect);
|
||||
}
|
||||
|
||||
public @NotNull List<WrapperPlayServerEntityEffect> createEffectPackets() {
|
||||
List<WrapperPlayServerEntityEffect> packets = new ArrayList<>();
|
||||
|
||||
this.effects.forEach((potionType, effect) -> packets.add(createEffectPacket(effect)));
|
||||
|
||||
return packets;
|
||||
}
|
||||
|
||||
public @NotNull WrapperPlayServerEntityEffect createEffectPacket(@NotNull WrapperPotionEffect effect) {
|
||||
PotionType potionType = effect.getPotionType();
|
||||
int amplifier = effect.getAmplifier();
|
||||
int duration = effect.getDuration();
|
||||
boolean ambient = effect.isAmbient();
|
||||
boolean visible = effect.isVisible();
|
||||
boolean icons = effect.hasIcons();
|
||||
NBTCompound factorData = effect.getFactorData();
|
||||
long createdAt = effect.getCreatedAt();
|
||||
|
||||
int remainingDuration = duration;
|
||||
if (duration != -1) {
|
||||
long elapsedMillis = System.currentTimeMillis() - createdAt;
|
||||
|
||||
int elapsedTicks = (int) (elapsedMillis / 50);
|
||||
|
||||
remainingDuration = Math.max(duration - elapsedTicks, 0);
|
||||
}
|
||||
|
||||
WrapperPlayServerEntityEffect wrapperPlayServerEntityEffect = new WrapperPlayServerEntityEffect(0, null, 0, 0, (byte) 0);
|
||||
|
||||
wrapperPlayServerEntityEffect.setEntityId(this.entity.getEntityId());
|
||||
wrapperPlayServerEntityEffect.setPotionType(potionType);
|
||||
wrapperPlayServerEntityEffect.setEffectAmplifier(amplifier);
|
||||
wrapperPlayServerEntityEffect.setEffectDurationTicks(remainingDuration);
|
||||
wrapperPlayServerEntityEffect.setFactorData(factorData);
|
||||
wrapperPlayServerEntityEffect.setAmbient(ambient);
|
||||
wrapperPlayServerEntityEffect.setVisible(visible);
|
||||
wrapperPlayServerEntityEffect.setShowIcon(icons);
|
||||
|
||||
return wrapperPlayServerEntityEffect;
|
||||
}
|
||||
|
||||
public @NotNull WrapperPlayServerRemoveEntityEffect createRemoveEffectPacket(@NotNull PotionType potionType) {
|
||||
return new WrapperPlayServerRemoveEntityEffect(this.entity.getEntityId(), potionType);
|
||||
}
|
||||
|
||||
public void refresh() {
|
||||
if (notifyChanges) {
|
||||
new ArrayList<>(this.effects.values()).forEach(effect -> {
|
||||
WrapperPlayServerEntityEffect wrapperPlayServerEntityEffect = createEffectPacket(effect);
|
||||
|
||||
this.entity.sendPacketsToViewers(wrapperPlayServerEntityEffect);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNotifyingChanges() {
|
||||
return notifyChanges;
|
||||
}
|
||||
|
||||
public void setNotifyChanges(boolean notifyChanges) {
|
||||
this.notifyChanges = notifyChanges;
|
||||
refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTicking() {
|
||||
return this.ticking;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTicking(boolean ticking) {
|
||||
this.ticking = ticking;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(long time) {
|
||||
Set<PotionType> toRemove = new HashSet<>();
|
||||
|
||||
this.effects.values().forEach(effect -> {
|
||||
PotionType potionType = effect.getPotionType();
|
||||
|
||||
int duration = effect.getDuration();
|
||||
if (duration <= -1) return; // Infinity effect
|
||||
|
||||
long createdAt = effect.getCreatedAt();
|
||||
|
||||
long elapsedMillis = time - createdAt;
|
||||
|
||||
int elapsedTicks = (int) (elapsedMillis / 50);
|
||||
|
||||
int remainingDuration = duration - elapsedTicks;
|
||||
if (remainingDuration <= 0) toRemove.add(potionType);
|
||||
});
|
||||
|
||||
toRemove.forEach(this::removePotionEffect);
|
||||
}
|
||||
|
||||
public static class WrapperPotionEffect {
|
||||
private final PotionType potionType;
|
||||
private final int amplifier;
|
||||
private final int duration;
|
||||
private final boolean ambient;
|
||||
private final boolean visible;
|
||||
private final boolean icons;
|
||||
private final @Nullable NBTCompound factorData;
|
||||
private final long createdAt;
|
||||
|
||||
private WrapperPotionEffect(PotionType potionType, int amplifier, int duration, boolean ambient, boolean visible, boolean icons, @Nullable NBTCompound factorData) {
|
||||
this.potionType = potionType;
|
||||
this.amplifier = amplifier;
|
||||
this.duration = duration;
|
||||
this.ambient = ambient;
|
||||
this.visible = visible;
|
||||
this.icons = icons;
|
||||
this.factorData = factorData;
|
||||
this.createdAt = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public PotionType getPotionType() {
|
||||
return potionType;
|
||||
}
|
||||
|
||||
public int getAmplifier() {
|
||||
return amplifier;
|
||||
}
|
||||
|
||||
public int getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
public boolean isAmbient() {
|
||||
return ambient;
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return visible;
|
||||
}
|
||||
|
||||
public boolean hasIcons() {
|
||||
return icons;
|
||||
}
|
||||
|
||||
public @Nullable NBTCompound getFactorData() {
|
||||
return factorData;
|
||||
}
|
||||
|
||||
public long getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,25 +4,34 @@ import com.github.retrooper.packetevents.manager.server.ServerVersion;
|
|||
import com.github.retrooper.packetevents.protocol.entity.type.EntityType;
|
||||
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
|
||||
import com.github.retrooper.packetevents.protocol.potion.PotionType;
|
||||
import com.github.retrooper.packetevents.protocol.world.Location;
|
||||
import com.github.retrooper.packetevents.wrapper.PacketWrapper;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityAnimation;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityEffect;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerEntityEquipment;
|
||||
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerHurtAnimation;
|
||||
import me.tofaa.entitylib.EntityLib;
|
||||
import me.tofaa.entitylib.container.EntityContainer;
|
||||
import me.tofaa.entitylib.meta.EntityMeta;
|
||||
import me.tofaa.entitylib.utils.VersionUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class WrapperLivingEntity extends WrapperEntity{
|
||||
|
||||
public class WrapperLivingEntity extends WrapperEntity {
|
||||
private final WrapperEntityEquipment equipment;
|
||||
private final WrapperEntityAttributes attributes;
|
||||
private final WrapperEntityPotionEffect potionEffect;
|
||||
|
||||
public WrapperLivingEntity(int entityId, UUID uuid, EntityType entityType, EntityMeta entityMeta) {
|
||||
super(entityId, uuid, entityType, entityMeta);
|
||||
|
||||
this.equipment = new WrapperEntityEquipment(this);
|
||||
this.attributes = new WrapperEntityAttributes(this);
|
||||
this.potionEffect = new WrapperEntityPotionEffect(this);
|
||||
}
|
||||
|
||||
public WrapperLivingEntity(int entityId, UUID uuid, EntityType entityType) {
|
||||
|
@ -36,6 +45,7 @@ public class WrapperLivingEntity extends WrapperEntity{
|
|||
public WrapperLivingEntity(UUID uuid, EntityType entityType) {
|
||||
this(EntityLib.getPlatform().getEntityIdProvider().provide(uuid, entityType), uuid, entityType);
|
||||
}
|
||||
|
||||
public WrapperLivingEntity(EntityType entityType) {
|
||||
this(EntityLib.getPlatform().getEntityUuidProvider().provide(entityType), entityType);
|
||||
}
|
||||
|
@ -43,24 +53,51 @@ public class WrapperLivingEntity extends WrapperEntity{
|
|||
@Override
|
||||
public void refresh() {
|
||||
super.refresh();
|
||||
equipment.refresh();
|
||||
attributes.refresh();
|
||||
|
||||
this.equipment.refresh();
|
||||
this.potionEffect.refresh();
|
||||
this.attributes.refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick(long time) {
|
||||
this.potionEffect.tick(time);
|
||||
|
||||
super.tick(time);
|
||||
}
|
||||
|
||||
public WrapperEntityAttributes getAttributes() {
|
||||
return attributes;
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
public WrapperEntityEquipment getEquipment() {
|
||||
return this.equipment;
|
||||
}
|
||||
|
||||
public WrapperEntityPotionEffect getPotionEffect() {
|
||||
return this.potionEffect;
|
||||
}
|
||||
|
||||
public @NotNull List<PacketWrapper<?>> createSpawnPackets() {
|
||||
List<PacketWrapper<?>> packets = new ArrayList<>();
|
||||
|
||||
packets.add(getAttributes().createPacket());
|
||||
packets.add(getEquipment().createPacket());
|
||||
packets.addAll(this.potionEffect.createEffectPackets());
|
||||
|
||||
return packets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a potion effect to the entity.
|
||||
* EntityLib will not keep track of the potions you give or what you do with them,
|
||||
* this simply sends the packet to the viewers of the entity.
|
||||
*
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param flags The bit flags of the potion effect see: https://wiki.vg/Protocol#Entity_Effect
|
||||
* @param flags The bit flags of the potion effect see:
|
||||
* <a href="https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect">https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect</a>
|
||||
* @param hasFactorData Whether the potion effect has factor data
|
||||
* @param factorData The factor data of the potion effect, if hasFactorData is false this will be ignored
|
||||
*/
|
||||
|
@ -72,25 +109,19 @@ public class WrapperLivingEntity extends WrapperEntity{
|
|||
boolean hasFactorData,
|
||||
@Nullable NBTCompound factorData
|
||||
) {
|
||||
sendPacketToViewers(
|
||||
new WrapperPlayServerEntityEffect(
|
||||
getEntityId(),
|
||||
type,
|
||||
amplifier,
|
||||
duration,
|
||||
flags
|
||||
)
|
||||
);
|
||||
this.potionEffect.addPotionEffect(type, amplifier, duration, flags, hasFactorData, factorData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a potion effect to the entity.
|
||||
* EntityLib will not keep track of the potions you give or what you do with them,
|
||||
* this simply sends the packet to the viewers of the entity.
|
||||
*
|
||||
* @param type The type of the potion effect {@link com.github.retrooper.packetevents.protocol.potion.PotionTypes}
|
||||
* @param amplifier The amplifier of the potion effect. The notchian client displays the number as (amplifier + 1)
|
||||
* @param duration The duration of the potion effect in ticks, if set to -1 the client will display the effect as infinite
|
||||
* @param flags The bit flags of the potion effect see: https://wiki.vg/Protocol#Entity_Effect
|
||||
* @param flags The bit flags of the potion effect see:
|
||||
* <a href="https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect">https://minecraft.wiki/w/Java_Edition_protocol#Entity_Effect</a>
|
||||
*/
|
||||
public void addPotionEffect(
|
||||
PotionType type,
|
||||
|
@ -124,6 +155,7 @@ public class WrapperLivingEntity extends WrapperEntity{
|
|||
|
||||
/**
|
||||
* Plays the hurt animation of the entity.
|
||||
*
|
||||
* @param yaw The yaw of the entity when the hurt animation is played.
|
||||
* For any entity other than a player it's safe to simply put 0, as it's not used.
|
||||
* The yaw is only used on 1.19.4+.
|
||||
|
@ -152,8 +184,4 @@ public class WrapperLivingEntity extends WrapperEntity{
|
|||
new WrapperPlayServerEntityAnimation(getEntityId(), type)
|
||||
);
|
||||
}
|
||||
|
||||
public WrapperEntityEquipment getEquipment() {
|
||||
return equipment;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue