use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.
the class ProtocolManagerImpl method getProtocolPath.
/**
* Calculates a path to get from an input protocol to the server's protocol.
*
* @param current current items in the path
* @param clientVersion current input version
* @param serverVersion desired output version
* @return path that has been generated, null if failed
*/
@Nullable
private Int2ObjectSortedMap<Protocol> getProtocolPath(Int2ObjectSortedMap<Protocol> current, int clientVersion, int serverVersion) {
// Fail-safe, protocol too complicated.
if (current.size() > maxProtocolPathSize)
return null;
// First, check if there is any protocols for this
Int2ObjectMap<Protocol> toServerProtocolMap = registryMap.get(clientVersion);
if (toServerProtocolMap == null) {
// Not supported
return null;
}
// Next, check if there is a direct, single Protocol path
Protocol protocol = toServerProtocolMap.get(serverVersion);
if (protocol != null) {
current.put(serverVersion, protocol);
// Easy solution
return current;
}
// There might be a more advanced solution... So we'll see if any of the others can get us there
Int2ObjectSortedMap<Protocol> shortest = null;
for (Int2ObjectMap.Entry<Protocol> entry : toServerProtocolMap.int2ObjectEntrySet()) {
// Ensure we don't go back to already contained versions
int translatedToVersion = entry.getIntKey();
if (current.containsKey(translatedToVersion))
continue;
// Check if the new version is farther away than the current client version
if (onlyCheckLoweringPathEntries && Math.abs(serverVersion - translatedToVersion) > Math.abs(serverVersion - clientVersion)) {
continue;
}
// Create a copy
Int2ObjectSortedMap<Protocol> newCurrent = new Int2ObjectLinkedOpenHashMap<>(current);
newCurrent.put(translatedToVersion, entry.getValue());
// Calculate the rest of the protocol starting from translatedToVersion and take the shortest
newCurrent = getProtocolPath(newCurrent, translatedToVersion, serverVersion);
if (newCurrent != null && (shortest == null || newCurrent.size() < shortest.size())) {
shortest = newCurrent;
}
}
// null if none found
return shortest;
}
use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.
the class ProtocolManagerImpl method getProtocolPath.
@Override
@Nullable
public List<ProtocolPathEntry> getProtocolPath(int clientVersion, int serverVersion) {
// Nothing to do!
if (clientVersion == serverVersion)
return null;
// Check cache
ProtocolPathKey protocolKey = new ProtocolPathKeyImpl(clientVersion, serverVersion);
List<ProtocolPathEntry> protocolList = pathCache.get(protocolKey);
if (protocolList != null) {
return protocolList;
}
// Calculate path
Int2ObjectSortedMap<Protocol> outputPath = getProtocolPath(new Int2ObjectLinkedOpenHashMap<>(), clientVersion, serverVersion);
if (outputPath == null) {
return null;
}
List<ProtocolPathEntry> path = new ArrayList<>(outputPath.size());
for (Int2ObjectMap.Entry<Protocol> entry : outputPath.int2ObjectEntrySet()) {
path.add(new ProtocolPathEntryImpl(entry.getIntKey(), entry.getValue()));
}
pathCache.put(protocolKey, path);
return path;
}
use of com.viaversion.viaversion.api.protocol.Protocol in project ViaVersion by ViaVersion.
the class EntityPackets method register.
public static void register(Protocol protocol) {
final PacketHandler metaTypeHandler = wrapper -> {
for (Metadata metadata : wrapper.get(Types1_13_2.METADATA_LIST, 0)) {
metadata.setMetaType(Types1_13_2.META_TYPES.byId(metadata.metaType().typeId()));
}
};
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Entity UUID
map(Type.UUID);
// 2 - Entity Type
map(Type.VAR_INT);
// 3 - X
map(Type.DOUBLE);
// 4 - Y
map(Type.DOUBLE);
// 5 - Z
map(Type.DOUBLE);
// 6 - Yaw
map(Type.BYTE);
// 7 - Pitch
map(Type.BYTE);
// 8 - Head Pitch
map(Type.BYTE);
// 9 - Velocity X
map(Type.SHORT);
// 10 - Velocity Y
map(Type.SHORT);
// 11 - Velocity Z
map(Type.SHORT);
// 12 - Metadata
map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
handler(metaTypeHandler);
}
});
protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Player UUID
map(Type.UUID);
// 2 - X
map(Type.DOUBLE);
// 3 - Y
map(Type.DOUBLE);
// 4 - Z
map(Type.DOUBLE);
// 5 - Yaw
map(Type.BYTE);
// 6 - Pitch
map(Type.BYTE);
// 7 - Metadata
map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
handler(metaTypeHandler);
}
});
protocol.registerClientbound(ClientboundPackets1_13.ENTITY_METADATA, new PacketRemapper() {
@Override
public void registerMap() {
// 0 - Entity ID
map(Type.VAR_INT);
// 1 - Metadata list
map(Types1_13.METADATA_LIST, Types1_13_2.METADATA_LIST);
handler(metaTypeHandler);
}
});
}
Aggregations