Included a bundles system, fixed entity attributes of an entity not being resent when respawned, implemented Hologram#removeLiner

This commit is contained in:
Tofaa2 2025-02-22 21:51:45 +04:00
parent 47f47b5653
commit 1f4aeef600
5 changed files with 54 additions and 8 deletions

View file

@ -1,6 +1,7 @@
package me.tofaa.entitylib; package me.tofaa.entitylib;
import com.github.retrooper.packetevents.PacketEventsAPI; import com.github.retrooper.packetevents.PacketEventsAPI;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public final class APIConfig { public final class APIConfig {
@ -11,6 +12,7 @@ public final class APIConfig {
private boolean tickTickables = false; private boolean tickTickables = false;
private boolean platformLogger = false; private boolean platformLogger = false;
private boolean bstats = true; private boolean bstats = true;
private boolean forceBundle = false;
public APIConfig(PacketEventsAPI<?> packetEvents) { public APIConfig(PacketEventsAPI<?> packetEvents) {
this.packetEvents = packetEvents; this.packetEvents = packetEvents;
@ -26,6 +28,11 @@ public final class APIConfig {
return this; return this;
} }
public @NotNull APIConfig forceBundles() {
this.forceBundle = true;
return this;
}
public @NotNull APIConfig usePlatformLogger() { public @NotNull APIConfig usePlatformLogger() {
this.platformLogger = true; this.platformLogger = true;
return this; return this;
@ -70,4 +77,10 @@ public final class APIConfig {
return bstats; return bstats;
} }
public boolean shouldForceBundles() {
return this.forceBundle
&& EntityLib.getOptionalApi().isPresent()
&& EntityLib.getOptionalApi().get().getPacketEvents().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_19_4);
}
} }

View file

@ -73,7 +73,7 @@ public class WrapperEntity implements Tickable {
if (spawned) return false; if (spawned) return false;
this.location = location; this.location = location;
this.spawned = true; this.spawned = true;
sendPacketToViewers( sendPacketsToViewers(
new WrapperPlayServerSpawnEntity( new WrapperPlayServerSpawnEntity(
entityId, entityId,
Optional.of(this.uuid), Optional.of(this.uuid),
@ -84,9 +84,12 @@ public class WrapperEntity implements Tickable {
location.getYaw(), location.getYaw(),
getObjectData(), getObjectData(),
createVeloPacket() createVeloPacket()
) ),
entityMeta.createPacket()
); );
sendPacketToViewers(entityMeta.createPacket()); if (this instanceof WrapperLivingEntity) {
sendPacketsToViewers(((WrapperLivingEntity)this).getAttributes().createPacket());
}
this.parent = parent; this.parent = parent;
parent.addEntity(this); parent.addEntity(this);
return true; return true;
@ -196,6 +199,9 @@ public class WrapperEntity implements Tickable {
sendPacket(uuid, createSpawnPacket()); sendPacket(uuid, createSpawnPacket());
sendPacket(uuid, entityMeta.createPacket()); sendPacket(uuid, entityMeta.createPacket());
sendPacket(uuid, createPassengerPacket()); sendPacket(uuid, createPassengerPacket());
if (this instanceof WrapperLivingEntity) {
sendPacket(uuid, ((WrapperLivingEntity)this).getAttributes().createPacket());
}
} }
if (EntityLib.getApi().getSettings().isDebugMode()) { if (EntityLib.getApi().getSettings().isDebugMode()) {
EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId); EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId);
@ -341,7 +347,6 @@ public class WrapperEntity implements Tickable {
consumer.accept(entityMeta); consumer.accept(entityMeta);
} }
public @NotNull UUID getUuid() { public @NotNull UUID getUuid() {
return uuid; return uuid;
} }
@ -393,7 +398,7 @@ public class WrapperEntity implements Tickable {
return viewerRules.get(index); return viewerRules.get(index);
} }
private WrapperPlayServerEntityVelocity getVelocityPacket() { public WrapperPlayServerEntityVelocity getVelocityPacket() {
Vector3d velocity = this.velocity.multiply(8000.0f / 20.0f); Vector3d velocity = this.velocity.multiply(8000.0f / 20.0f);
return new WrapperPlayServerEntityVelocity(entityId, velocity); return new WrapperPlayServerEntityVelocity(entityId, velocity);
} }
@ -441,13 +446,21 @@ public class WrapperEntity implements Tickable {
public void refresh() { public void refresh() {
if (!spawned) return; if (!spawned) return;
sendPacketToViewers(entityMeta.createPacket()); sendPacketsToViewers(entityMeta.createPacket(), createPassengerPacket());
sendPacketToViewers(createPassengerPacket());
} }
public void sendPacketToViewers(PacketWrapper<?> packet) { public void sendPacketToViewers(PacketWrapper<?> packet) {
if (EntityLib.getApi().getSettings().shouldForceBundles()) {
viewers.forEach(uuid -> {
sendPacket(uuid, new WrapperPlayServerBundle());
sendPacket(uuid, packet);
sendPacket(uuid, new WrapperPlayServerBundle());
});
}
else {
viewers.forEach(uuid -> sendPacket(uuid, packet)); viewers.forEach(uuid -> sendPacket(uuid, packet));
} }
}
public void sendPacketsToViewers(PacketWrapper<?>... wrappers) { public void sendPacketsToViewers(PacketWrapper<?>... wrappers) {
for (PacketWrapper<?> wrapper : wrappers) { for (PacketWrapper<?> wrapper : wrappers) {

View file

@ -45,6 +45,8 @@ public interface Hologram {
void setLine(int index, @Nullable Component line); void setLine(int index, @Nullable Component line);
void removeLine(int index);
void addLine(@Nullable Component line); void addLine(@Nullable Component line);
void addViewer(@NotNull UUID viewer); void addViewer(@NotNull UUID viewer);

View file

@ -110,6 +110,15 @@ final class LegacyHologram implements Hologram.Legacy {
teleport(location); teleport(location);
} }
@Override
public void removeLine(int index) {
if (index < 0 || index >= lines.size()) {
return;
}
this.lines.get(index).remove();
this.lines.remove(index);
}
@Override @Override
public void addLine(@Nullable Component line) { public void addLine(@Nullable Component line) {
setLine(lines.size(), line); setLine(lines.size(), line);

View file

@ -89,6 +89,15 @@ final class ModernHologram implements Hologram.Modern {
} }
} }
@Override
public void removeLine(int index) {
if (index < 0 || index >= lines.size()) {
return;
}
this.lines.get(index).remove();
this.lines.remove(index);
}
@Override @Override
public void addLine(@Nullable Component line) { public void addLine(@Nullable Component line) {
setLine(lines.size(), line); setLine(lines.size(), line);