Skip to content

Commit

Permalink
🔖 1.1.4 - Fixed issue with entity view distance
Browse files Browse the repository at this point in the history
  • Loading branch information
SkytAsul committed Mar 5, 2023
1 parent f074fa6 commit 629b4ab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Add this requirement to your maven `pom.xml` file:
<dependency>
<groupId>io.github.skytasul</groupId>
<artifactId>glowingentities</artifactId>
<version>1.1.3</version>
<version>{VERSION}</version>
<scope>compile</scope>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.skytasul</groupId>
<artifactId>glowingentities</artifactId>
<version>1.1.3</version>
<version>1.1.4</version>

<name>GlowingEntities</name>
<description>A Spigot util to easily make entities glow.</description>
Expand Down
48 changes: 37 additions & 11 deletions src/main/java/fr/skytasul/glowingentities/GlowingEntities.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,11 @@ private static byte computeFlags(GlowingData glowingData) {
return newFlags;
}

public static Object createFlagWatcherItem(byte newFlags) throws ReflectiveOperationException {
return watcherItemConstructor != null ? watcherItemConstructor.newInstance(watcherObjectFlags, newFlags)
: watcherBCreator.invoke(null, watcherObjectFlags, newFlags);
}

public static void removeGlowing(GlowingData glowingData) throws ReflectiveOperationException {
setMetadata(glowingData, glowingData.otherFlags);
}
Expand Down Expand Up @@ -530,7 +535,8 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
List<Object> items = (List<Object>) packetMetadataItems.get(msg);
if (items != null) {

List<Object> copy = null;
boolean containsFlags = false;
boolean edited = false;
for (int i = 0; i < items.size(); i++) {
Object item = items.get(i);
Object watcherObject;
Expand All @@ -542,32 +548,52 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
}

if (watcherObject.equals(watcherObjectFlags)) {
containsFlags = true;
byte flags = (byte) watcherItemDataGet.invoke(item);
glowingData.otherFlags = flags;
byte newFlags = computeFlags(glowingData);
if (newFlags != flags) {
if (copy == null) copy = new ArrayList<>(items);
copy.set(i,
watcherItemConstructor != null
? watcherItemConstructor.newInstance(watcherObjectFlags,
newFlags)
: watcherBCreator.invoke(null, watcherObjectFlags, newFlags));
// we cannot simply edit the item as it may be backed in the datawatcher
edited = true;
items = new ArrayList<>(items);
// we cannot simply edit the item as it may be backed in the datawatcher, so we
// make a copy of the list
items.set(i, createFlagWatcherItem(newFlags));
break;
// we can break right now as the "flags" datawatcher object may not be present
// twice in the same packet
}
}
}

if (copy != null) {
if (!edited && !containsFlags) {
// if the packet does not contain any flag information, we are unsure if it is a packet
// simply containing informations about another object's data update OR if it is a packet
// containing all non-default informations of the entity. Such as packet can be sent when
// the player has got far away from the entity and come in sight distance again.
// In this case, we must add manually the "flags" object, otherwise it would stay 0 and
// the entity would not be glowing.
// Ideally, we should listen for an "entity add" packet to be sure we are in the case
// above, but honestly it's annoying because there are multiple types of "entity add"
// packets, so we do like this instead. Less performant, but not by far.
byte flags = computeFlags(glowingData);
if (flags != 0) {
edited = true;
items = new ArrayList<>(items);
items.add(createFlagWatcherItem(flags));
}
}

if (edited) {
// some of the metadata packets are broadcasted to all players near the target entity.
// hence, if we directly edit the packet, some users that were not intended to see the
// glowing color will be able to see it. We should send a new packet to the viewer only.

Object newMsg;
if (version < 19 || (version == 19 && versionMinor < 3)) {
newMsg = packetMetadataConstructor.newInstance(entityID, watcherDummy, false);
packetMetadataItems.set(newMsg, copy);
packetMetadataItems.set(newMsg, items);
} else {
newMsg = packetMetadataConstructor.newInstance(entityID, copy);
newMsg = packetMetadataConstructor.newInstance(entityID, items);
}
packets.put(newMsg, dummy);
sendPackets(playerData.player, newMsg);
Expand Down

0 comments on commit 629b4ab

Please sign in to comment.