2021-03-22 23:06:40 +01:00
|
|
|
/*
|
|
|
|
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
|
2024-01-01 12:39:45 +01:00
|
|
|
* Copyright (C) 2016-2024 ViaVersion and contributors
|
2021-03-22 23:06:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-04-26 20:52:34 +02:00
|
|
|
package com.viaversion.viaversion.bukkit.util;
|
2016-06-19 23:35:49 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
2024-03-09 13:57:41 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import com.viaversion.viaversion.api.Via;
|
2024-02-14 17:56:28 +01:00
|
|
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
|
|
|
import com.viaversion.viaversion.api.protocol.version.VersionType;
|
2023-02-12 11:44:25 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2016-06-19 23:35:49 +02:00
|
|
|
|
2022-11-10 13:32:25 +01:00
|
|
|
public final class ProtocolSupportUtil {
|
|
|
|
private static final Method PROTOCOL_VERSION_METHOD;
|
|
|
|
private static final Method GET_ID_METHOD;
|
2016-06-19 23:35:49 +02:00
|
|
|
|
|
|
|
static {
|
2022-11-10 13:32:25 +01:00
|
|
|
Method protocolVersionMethod = null;
|
|
|
|
Method getIdMethod = null;
|
2016-06-19 23:35:49 +02:00
|
|
|
try {
|
|
|
|
protocolVersionMethod = Class.forName("protocolsupport.api.ProtocolSupportAPI").getMethod("getProtocolVersion", Player.class);
|
|
|
|
getIdMethod = Class.forName("protocolsupport.api.ProtocolVersion").getMethod("getId");
|
2022-11-10 13:32:25 +01:00
|
|
|
} catch (ReflectiveOperationException e) {
|
2016-06-19 23:35:49 +02:00
|
|
|
// ProtocolSupport not installed.
|
|
|
|
}
|
2022-11-10 13:32:25 +01:00
|
|
|
PROTOCOL_VERSION_METHOD = protocolVersionMethod;
|
|
|
|
GET_ID_METHOD = getIdMethod;
|
2016-06-19 23:35:49 +02:00
|
|
|
}
|
|
|
|
|
2024-02-14 17:56:28 +01:00
|
|
|
public static ProtocolVersion getProtocolVersion(Player player) {
|
2022-11-10 13:32:25 +01:00
|
|
|
if (PROTOCOL_VERSION_METHOD == null) {
|
2024-02-14 17:56:28 +01:00
|
|
|
return ProtocolVersion.unknown;
|
2022-11-10 13:32:25 +01:00
|
|
|
}
|
2016-06-19 23:35:49 +02:00
|
|
|
try {
|
2024-02-14 17:56:28 +01:00
|
|
|
final Object version = PROTOCOL_VERSION_METHOD.invoke(null, player);
|
|
|
|
final int id = (int) GET_ID_METHOD.invoke(version);
|
2024-02-14 22:53:50 +01:00
|
|
|
// List of pre netty (<= 1.6.4) versions supported by ProtocolSupport, needs to be updated if ProtocolSupport adds support for new versions.
|
2024-02-14 17:56:28 +01:00
|
|
|
final boolean preNetty = id == 78 || id == 74 || id == 73 || id == 61 || id == 60 || id == 51;
|
|
|
|
|
|
|
|
return ProtocolVersion.getProtocol(preNetty ? VersionType.RELEASE_INITIAL : VersionType.RELEASE, id);
|
2022-11-10 13:32:25 +01:00
|
|
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
2024-03-09 13:57:41 +01:00
|
|
|
Via.getPlatform().getLogger().log(Level.SEVERE, "Failed to get ProtocolSupport version", e);
|
2016-06-19 23:35:49 +02:00
|
|
|
}
|
2024-02-14 17:56:28 +01:00
|
|
|
return ProtocolVersion.unknown;
|
2016-06-19 23:35:49 +02:00
|
|
|
}
|
|
|
|
}
|