Fix Entity Tracker on Bungee being incorrect after server switch #1215

This commit is contained in:
Myles 2019-04-23 21:29:51 +01:00
parent 52b46a17cd
commit b4418c02ca
9 changed files with 66 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package us.myles.ViaVersion.bungee.handlers;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ServerConnectEvent;
import net.md_5.bungee.api.event.ServerConnectedEvent;
import net.md_5.bungee.api.event.ServerSwitchEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.score.Team;
import net.md_5.bungee.event.EventHandler;
@ -11,6 +12,8 @@ import net.md_5.bungee.protocol.packet.PluginMessage;
import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.protocol.ProtocolPipeline;
@ -22,6 +25,7 @@ import us.myles.ViaVersion.bungee.storage.BungeeStorage;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.packets.InventoryPackets;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.EntityIdProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.storage.EntityTracker;
import java.lang.reflect.Field;
@ -90,6 +94,25 @@ public class BungeeServerHandler implements Listener {
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onServerSwitch(ServerSwitchEvent e) {
// Update entity id
UserConnection userConnection = Via.getManager().getConnection(e.getPlayer().getUniqueId());
if (userConnection == null) return;
int playerId;
try {
playerId = Via.getManager().getProviders().get(EntityIdProvider.class).getEntityId(userConnection);
} catch (Exception ex) {
return; // Ignored
}
for (StoredObject storedObject : userConnection.getStoredObjects().values()) {
if (storedObject instanceof ExternalJoinGameListener) {
((ExternalJoinGameListener) storedObject).onExternalJoinGame(playerId);
}
}
}
public void checkServerChange(ServerConnectedEvent e, UserConnection user) throws Exception {
if (user == null) return;
// Auto-team handling

View File

@ -0,0 +1,5 @@
package us.myles.ViaVersion.api.data;
public interface ExternalJoinGameListener {
void onExternalJoinGame(int playerEntityId);
}

View File

@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_11to1_10.storage;
import com.google.common.base.Optional;
import com.google.common.collect.Sets;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_11Types;
@ -10,7 +11,7 @@ import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject {
public class EntityTracker extends StoredObject implements ExternalJoinGameListener {
private final Map<Integer, Entity1_11Types.EntityType> clientEntityTypes = new ConcurrentHashMap<>();
private final Set<Integer> holograms = Sets.newConcurrentHashSet();
@ -47,4 +48,9 @@ public class EntityTracker extends StoredObject {
public void removeHologram(int entId) {
holograms.remove(entId);
}
@Override
public void onExternalJoinGame(int playerEntityId) {
clientEntityTypes.put(playerEntityId, Entity1_11Types.EntityType.PLAYER);
}
}

View File

@ -1,6 +1,7 @@
package us.myles.ViaVersion.protocols.protocol1_12to1_11_1.storage;
import com.google.common.base.Optional;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_12Types;
@ -8,7 +9,7 @@ import us.myles.ViaVersion.api.entities.Entity1_12Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject {
public class EntityTracker extends StoredObject implements ExternalJoinGameListener {
private final Map<Integer, Entity1_12Types.EntityType> clientEntityTypes = new ConcurrentHashMap<>();
public EntityTracker(UserConnection user) {
@ -31,4 +32,8 @@ public class EntityTracker extends StoredObject {
return Optional.fromNullable(clientEntityTypes.get(id));
}
@Override
public void onExternalJoinGame(int playerEntityId) {
clientEntityTypes.put(playerEntityId, Entity1_12Types.EntityType.PLAYER);
}
}

View File

@ -1,6 +1,7 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.storage;
import com.google.common.base.Optional;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_13Types;
@ -8,7 +9,7 @@ import us.myles.ViaVersion.api.entities.Entity1_13Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject {
public class EntityTracker extends StoredObject implements ExternalJoinGameListener {
private final Map<Integer, Entity1_13Types.EntityType> clientEntityTypes = new ConcurrentHashMap<>();
public EntityTracker(UserConnection user) {
@ -31,4 +32,8 @@ public class EntityTracker extends StoredObject {
return Optional.fromNullable(clientEntityTypes.get(id));
}
@Override
public void onExternalJoinGame(int playerEntityId) {
clientEntityTypes.put(playerEntityId, Entity1_13Types.EntityType.PLAYER);
}
}

View File

@ -99,7 +99,7 @@ public class EntityPackets {
wrapper.set(Type.VAR_INT, 1, typeId);
// Register Type ID
wrapper.user().get(EntityTracker.class).addEntity(entityId, uuid, type1_14);
wrapper.user().get(EntityTracker.class).addEntity(entityId, type1_14);
}
});
}
@ -137,7 +137,7 @@ public class EntityPackets {
wrapper.set(Type.VAR_INT, 1, type);
// Register Type ID
wrapper.user().get(EntityTracker.class).addEntity(entityId, uuid, entType);
wrapper.user().get(EntityTracker.class).addEntity(entityId, entType);
MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user());
}
@ -178,7 +178,7 @@ public class EntityPackets {
Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER;
// Register Type ID
wrapper.user().get(EntityTracker.class).addEntity(entityId, uuid, entType);
wrapper.user().get(EntityTracker.class).addEntity(entityId, entType);
MetadataRewriter.handleMetadata(entityId, entType, wrapper.get(Types1_14.METADATA_LIST, 0), wrapper.user());
}
});

View File

@ -286,7 +286,7 @@ public class WorldPackets {
Entity1_14Types.EntityType entType = Entity1_14Types.EntityType.PLAYER;
// Register Type ID
wrapper.user().get(EntityTracker.class).addEntity(entityId, wrapper.user().get(ProtocolInfo.class).getUuid(), entType);
wrapper.user().get(EntityTracker.class).addEntity(entityId, entType);
}
});

View File

@ -3,6 +3,7 @@ package us.myles.ViaVersion.protocols.protocol1_14to1_13_2.storage;
import com.google.common.base.Optional;
import lombok.Getter;
import lombok.Setter;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_14Types;
@ -11,7 +12,7 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class EntityTracker extends StoredObject {
public class EntityTracker extends StoredObject implements ExternalJoinGameListener {
private final Map<Integer, Entity1_14Types.EntityType> clientEntityTypes = new ConcurrentHashMap<>();
private final Map<Integer, Byte> insentientData = new ConcurrentHashMap<>();
@Getter
@ -30,7 +31,7 @@ public class EntityTracker extends StoredObject {
insentientData.remove(entityId);
}
public void addEntity(int entityId, UUID uuid, Entity1_14Types.EntityType type) {
public void addEntity(int entityId, Entity1_14Types.EntityType type) {
clientEntityTypes.put(entityId, type);
}
@ -50,4 +51,9 @@ public class EntityTracker extends StoredObject {
public Optional<Entity1_14Types.EntityType> get(int id) {
return Optional.fromNullable(clientEntityTypes.get(id));
}
@Override
public void onExternalJoinGame(int playerEntityId) {
clientEntityTypes.put(playerEntityId, Entity1_14Types.EntityType.PLAYER);
}
}

View File

@ -10,6 +10,7 @@ import us.myles.ViaVersion.api.Via;
import us.myles.ViaVersion.api.boss.BossBar;
import us.myles.ViaVersion.api.boss.BossColor;
import us.myles.ViaVersion.api.boss.BossStyle;
import us.myles.ViaVersion.api.data.ExternalJoinGameListener;
import us.myles.ViaVersion.api.data.StoredObject;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.entities.Entity1_10Types;
@ -31,7 +32,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
@Getter
public class EntityTracker extends StoredObject {
public class EntityTracker extends StoredObject implements ExternalJoinGameListener {
private final Map<Integer, UUID> uuidMap = new ConcurrentHashMap<>();
private final Map<Integer, Entity1_10Types.EntityType> clientEntityTypes = new ConcurrentHashMap<>();
private final Map<Integer, List<Metadata>> metadataBuffer = new ConcurrentHashMap<>();
@ -312,4 +313,9 @@ public class EntityTracker extends StoredObject {
return entityID;
}
}
@Override
public void onExternalJoinGame(int playerEntityId) {
clientEntityTypes.put(playerEntityId, Entity1_10Types.EntityType.PLAYER);
}
}