Better logic for item updating on item entities

Specifically created to address InteractionVisualizer updating all metadata properties when updating the nametag - this commit prevents the item from jumping on updating. It also prevents the entity from being respawned if just the stack size updated.
This commit is contained in:
Camotoy 2021-07-22 09:16:19 -04:00
parent c3f65b176d
commit 11f048a065
No known key found for this signature in database
GPG key ID: 7EEFB66FE798081F
2 changed files with 24 additions and 3 deletions

View file

@ -29,9 +29,11 @@ import com.github.steveice10.mc.protocol.data.game.entity.metadata.EntityMetadat
import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack; import com.github.steveice10.mc.protocol.data.game.entity.metadata.ItemStack;
import com.nukkitx.math.vector.Vector3f; import com.nukkitx.math.vector.Vector3f;
import com.nukkitx.math.vector.Vector3i; import com.nukkitx.math.vector.Vector3i;
import com.nukkitx.protocol.bedrock.data.entity.EntityEventType;
import com.nukkitx.protocol.bedrock.data.entity.EntityFlag; import com.nukkitx.protocol.bedrock.data.entity.EntityFlag;
import com.nukkitx.protocol.bedrock.data.inventory.ItemData; import com.nukkitx.protocol.bedrock.data.inventory.ItemData;
import com.nukkitx.protocol.bedrock.packet.AddItemEntityPacket; import com.nukkitx.protocol.bedrock.packet.AddItemEntityPacket;
import com.nukkitx.protocol.bedrock.packet.EntityEventPacket;
import org.geysermc.connector.entity.type.EntityType; import org.geysermc.connector.entity.type.EntityType;
import org.geysermc.connector.network.session.GeyserSession; import org.geysermc.connector.network.session.GeyserSession;
import org.geysermc.connector.network.translators.item.ItemTranslator; import org.geysermc.connector.network.translators.item.ItemTranslator;
@ -115,9 +117,26 @@ public class ItemEntity extends ThrowableEntity {
@Override @Override
public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) { public void updateBedrockMetadata(EntityMetadata entityMetadata, GeyserSession session) {
if (entityMetadata.getId() == 8) { if (entityMetadata.getId() == 8) {
item = ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue()); ItemData item = ItemTranslator.translateToBedrock(session, (ItemStack) entityMetadata.getValue());
despawnEntity(session); if (this.item == null) {
spawnEntity(session); this.item = item;
spawnEntity(session);
} else if (item.equals(this.item, false, true, true)) {
// Don't bother respawning the entity if items are equal
if (this.item.getCount() != item.getCount()) {
// Just item count updated; let's make this easy
this.item = item;
EntityEventPacket packet = new EntityEventPacket();
packet.setRuntimeEntityId(geyserId);
packet.setType(EntityEventType.UPDATE_ITEM_STACK_SIZE);
packet.setData(this.item.getCount());
session.sendUpstreamPacket(packet);
}
} else {
this.item = item;
despawnEntity(session);
spawnEntity(session);
}
} }
super.updateBedrockMetadata(entityMetadata, session); super.updateBedrockMetadata(entityMetadata, session);

View file

@ -44,6 +44,7 @@ import org.geysermc.connector.registry.type.ItemMappings;
import org.geysermc.connector.utils.FileUtils; import org.geysermc.connector.utils.FileUtils;
import org.geysermc.connector.utils.LocaleUtils; import org.geysermc.connector.utils.LocaleUtils;
import javax.annotation.Nonnull;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -125,6 +126,7 @@ public abstract class ItemTranslator {
return itemStack; return itemStack;
} }
@Nonnull
public static ItemData translateToBedrock(GeyserSession session, ItemStack stack) { public static ItemData translateToBedrock(GeyserSession session, ItemStack stack) {
if (stack == null) { if (stack == null) {
return ItemData.AIR; return ItemData.AIR;