Skip to content

Commit 1f4aeef

Browse files
committed
Included a bundles system, fixed entity attributes of an entity not being resent when respawned, implemented Hologram#removeLiner
1 parent 47f47b5 commit 1f4aeef

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

api/src/main/java/me/tofaa/entitylib/APIConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.tofaa.entitylib;
22

33
import com.github.retrooper.packetevents.PacketEventsAPI;
4+
import com.github.retrooper.packetevents.manager.server.ServerVersion;
45
import org.jetbrains.annotations.NotNull;
56

67
public final class APIConfig {
@@ -11,6 +12,7 @@ public final class APIConfig {
1112
private boolean tickTickables = false;
1213
private boolean platformLogger = false;
1314
private boolean bstats = true;
15+
private boolean forceBundle = false;
1416

1517
public APIConfig(PacketEventsAPI<?> packetEvents) {
1618
this.packetEvents = packetEvents;
@@ -26,6 +28,11 @@ public APIConfig(PacketEventsAPI<?> packetEvents) {
2628
return this;
2729
}
2830

31+
public @NotNull APIConfig forceBundles() {
32+
this.forceBundle = true;
33+
return this;
34+
}
35+
2936
public @NotNull APIConfig usePlatformLogger() {
3037
this.platformLogger = true;
3138
return this;
@@ -70,4 +77,10 @@ public boolean shouldUseBstats() {
7077
return bstats;
7178
}
7279

80+
public boolean shouldForceBundles() {
81+
return this.forceBundle
82+
&& EntityLib.getOptionalApi().isPresent()
83+
&& EntityLib.getOptionalApi().get().getPacketEvents().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_19_4);
84+
}
85+
7386
}

api/src/main/java/me/tofaa/entitylib/wrapper/WrapperEntity.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public boolean spawn(Location location, EntityContainer parent) {
7373
if (spawned) return false;
7474
this.location = location;
7575
this.spawned = true;
76-
sendPacketToViewers(
76+
sendPacketsToViewers(
7777
new WrapperPlayServerSpawnEntity(
7878
entityId,
7979
Optional.of(this.uuid),
@@ -84,9 +84,12 @@ public boolean spawn(Location location, EntityContainer parent) {
8484
location.getYaw(),
8585
getObjectData(),
8686
createVeloPacket()
87-
)
87+
),
88+
entityMeta.createPacket()
8889
);
89-
sendPacketToViewers(entityMeta.createPacket());
90+
if (this instanceof WrapperLivingEntity) {
91+
sendPacketsToViewers(((WrapperLivingEntity)this).getAttributes().createPacket());
92+
}
9093
this.parent = parent;
9194
parent.addEntity(this);
9295
return true;
@@ -196,6 +199,9 @@ public void addViewer(UUID uuid) {
196199
sendPacket(uuid, createSpawnPacket());
197200
sendPacket(uuid, entityMeta.createPacket());
198201
sendPacket(uuid, createPassengerPacket());
202+
if (this instanceof WrapperLivingEntity) {
203+
sendPacket(uuid, ((WrapperLivingEntity)this).getAttributes().createPacket());
204+
}
199205
}
200206
if (EntityLib.getApi().getSettings().isDebugMode()) {
201207
EntityLib.getPlatform().getLogger().info("Added viewer " + uuid + " to entity " + entityId);
@@ -341,7 +347,6 @@ public void consumeMeta(Consumer<EntityMeta> consumer) {
341347
consumer.accept(entityMeta);
342348
}
343349

344-
345350
public @NotNull UUID getUuid() {
346351
return uuid;
347352
}
@@ -393,7 +398,7 @@ public void clearViewerRules() {
393398
return viewerRules.get(index);
394399
}
395400

396-
private WrapperPlayServerEntityVelocity getVelocityPacket() {
401+
public WrapperPlayServerEntityVelocity getVelocityPacket() {
397402
Vector3d velocity = this.velocity.multiply(8000.0f / 20.0f);
398403
return new WrapperPlayServerEntityVelocity(entityId, velocity);
399404
}
@@ -441,12 +446,20 @@ public void rotateHead(WrapperEntity entity) {
441446

442447
public void refresh() {
443448
if (!spawned) return;
444-
sendPacketToViewers(entityMeta.createPacket());
445-
sendPacketToViewers(createPassengerPacket());
449+
sendPacketsToViewers(entityMeta.createPacket(), createPassengerPacket());
446450
}
447451

448452
public void sendPacketToViewers(PacketWrapper<?> packet) {
449-
viewers.forEach(uuid -> sendPacket(uuid, packet));
453+
if (EntityLib.getApi().getSettings().shouldForceBundles()) {
454+
viewers.forEach(uuid -> {
455+
sendPacket(uuid, new WrapperPlayServerBundle());
456+
sendPacket(uuid, packet);
457+
sendPacket(uuid, new WrapperPlayServerBundle());
458+
});
459+
}
460+
else {
461+
viewers.forEach(uuid -> sendPacket(uuid, packet));
462+
}
450463
}
451464

452465
public void sendPacketsToViewers(PacketWrapper<?>... wrappers) {

api/src/main/java/me/tofaa/entitylib/wrapper/hologram/Hologram.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public interface Hologram {
4545

4646
void setLine(int index, @Nullable Component line);
4747

48+
void removeLine(int index);
49+
4850
void addLine(@Nullable Component line);
4951

5052
void addViewer(@NotNull UUID viewer);

api/src/main/java/me/tofaa/entitylib/wrapper/hologram/LegacyHologram.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ public void setLine(int index, @Nullable Component line) {
110110
teleport(location);
111111
}
112112

113+
@Override
114+
public void removeLine(int index) {
115+
if (index < 0 || index >= lines.size()) {
116+
return;
117+
}
118+
this.lines.get(index).remove();
119+
this.lines.remove(index);
120+
}
121+
113122
@Override
114123
public void addLine(@Nullable Component line) {
115124
setLine(lines.size(), line);

api/src/main/java/me/tofaa/entitylib/wrapper/hologram/ModernHologram.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ public void setLine(int index, @Nullable Component line) {
8989
}
9090
}
9191

92+
@Override
93+
public void removeLine(int index) {
94+
if (index < 0 || index >= lines.size()) {
95+
return;
96+
}
97+
this.lines.get(index).remove();
98+
this.lines.remove(index);
99+
}
100+
92101
@Override
93102
public void addLine(@Nullable Component line) {
94103
setLine(lines.size(), line);

0 commit comments

Comments
 (0)