Skip to content

Commit

Permalink
Beautify client version
Browse files Browse the repository at this point in the history
  • Loading branch information
xaw3ep committed Apr 13, 2023
1 parent bef2c4e commit dc3a487
Show file tree
Hide file tree
Showing 4 changed files with 248 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static String getInspectMessage(final Player player, final String c1, fin

builder.append("\n "+ c1 + "" + c2 + "•" + c1 + cI + (pData.isBedrockPlayer() ? " Is a Bedrock player" : " Is a Java player") + c1 + ".");

builder.append("\n "+ c1 + "" + c2 + "•" + c1 +" Is playing with version (protocol ID) " + pData.getClientVersionID());
builder.append("\n "+ c1 + "" + c2 + "•" + c1 +" Is playing with version " + pData.getClientVersion().getReleaseName() + "(" + pData.getClientVersionID() + ")");

if (player.isOp()){
builder.append("\n "+ c1 + "" + c2 + "•" + c1 + cI + " Is OP" + c1 + ".");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* 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/>.
*/
package fr.neatmonster.nocheatplus.compat.versions;

public enum ClientVersion {
/**
* 1.7.2 or 1.7.4 or 1.7.5 have the same protocol version.
*/
V_1_7_2(4),
/**
* 1.7.6 or 1.7.7 or 1.7.8 or 1.7.9 or 1.7.10 have the same protocol version.
*/
V_1_7_10(5),

V_1_8(47),

V_1_9(107), V_1_9_1(108), V_1_9_2(109),
/**
* 1.9.3 or 1.9.4 have the same protocol version.
*/
V_1_9_3(110),
V_1_10(210),
V_1_11(315),
/**
* 1.11.1 or 1.11.2 have the same protocol version.
*/
V_1_11_1(316), V_1_12(335), V_1_12_1(338), V_1_12_2(340),

V_1_13(393), V_1_13_1(401), V_1_13_2(404),

V_1_14(477), V_1_14_1(480), V_1_14_2(485), V_1_14_3(490), V_1_14_4(498),

V_1_15(573), V_1_15_1(575), V_1_15_2(578),

V_1_16(735), V_1_16_1(736), V_1_16_2(751), V_1_16_3(753),
/**
* 1.16.4 or 1.16.5 have the same protocol version.
*/
V_1_16_4(754),

V_1_17(755), V_1_17_1(756),

/**
* 1.18 or 1.18.1 have the same protocol version.
*/
V_1_18(757),
V_1_18_2(758),

V_1_19(759), V_1_19_1(760), V_1_19_3(761), V_1_19_4(762),


LOWER_THAN_KNOWN_VERSIONS(V_1_7_2.protocolID - 1, false),
HIGHER_THAN_KNOWN_VERSIONS(V_1_19_4.protocolID + 1, false),
UNKNOWN(-1, false);

private final int protocolID;
private final String name;

ClientVersion(int protocolVersion) {
this.protocolID = protocolVersion;
this.name = name().substring(2).replace("_", ".");
}

ClientVersion(int protocolVersion, boolean knownVersion) {
this.protocolID = protocolVersion;
if (!knownVersion) {
this.name = name();
} else {
this.name = name().substring(2).replace("_", ".");
}
}

private static final ClientVersion[] VALUES = values();
private static final int LOWEST_KNOWN_PROTOCOL_VERSION = LOWER_THAN_KNOWN_VERSIONS.protocolID + 1;
private static final int HIGHEST_KNOWN_PROTOCOL_VERSION = HIGHER_THAN_KNOWN_VERSIONS.protocolID - 1;

/**
* Get a ClientVersion enum by protocol version.
*
* @param protocolVersion the protocol version.
* @return ClientVersion
*/
public static ClientVersion getById(int protocolVersion) {
if (protocolVersion < LOWEST_KNOWN_PROTOCOL_VERSION) {
return LOWER_THAN_KNOWN_VERSIONS;
} else if (protocolVersion > HIGHEST_KNOWN_PROTOCOL_VERSION) {
return HIGHER_THAN_KNOWN_VERSIONS;
} else {
for (ClientVersion version : VALUES) {
if (version.protocolID > protocolVersion) {
break;
} else if (version.protocolID == protocolVersion) {
return version;
}
}
return UNKNOWN;
}
}

/**
* Get the release name of this client version.
* For example, for the V_1_19_4 enum constant, it would return "1.19.4".
*
* @return Release name
*/
public String getReleaseName() {
return name;
}

/**
* Protocol version of this client version.
*
* @return Protocol version.
*/
public int getProtocolVersion() {
return protocolID;
}

/**
* Get the lastest version
*
* @return Known lastest version
*/
public static ClientVersion getLatest() {
return VALUES[VALUES.length-4]; // -3 to ignore unknown, lower_than, higher than; -1 for offset index=>-4
}

/**
* Get the oldest version
*
* @return Known oldest version
*/
public static ClientVersion getOldest() {
return VALUES[0];
}

/**
* Test if the given version is between the two given ones.
*
* @param versionLow
* Lower bound
* @param includeLow
* If to allow equality for the low edge.
* @param versionHigh
* Upper bound
* @param includeHigh
* If to allow equality for the high edge.
* @return true if the client version is in given range
*/
public boolean isVersionBetween(ClientVersion versionLow, boolean includeLow, ClientVersion versionHigh, boolean includeHigh) {
if (includeLow) {
if (isOlderThan(versionLow)) {
return false;
}
} else {
if (isOlderThanOrEquals(versionLow)) {
return false;
}
}
if (includeHigh) {
if (isNewerThan(versionHigh)) {
return false;
}
} else {
if (isNewerThanOrEquals(versionHigh)) {
return false;
}
}
return true;
}

/**
* Check if client version is newer compare to the compared client version?
*
* @param target Client version to test
* @return true if yes and viceversa
*/
public boolean isNewerThan(ClientVersion target) {
return protocolID > target.protocolID;
}

/**
* Check if client version is newer or equals compare to the compared client version?
*
* @param target Client version to test
* @return true if yes and viceversa
*/
public boolean isNewerThanOrEquals(ClientVersion target) {
return protocolID >= target.protocolID;
}

/**
* Check if client version is older compare to the compared client version?
*
* @param target Client version to test
* @return true if yes and viceversa
*/
public boolean isOlderThan(ClientVersion target) {
return protocolID < target.protocolID;
}

/**
* Check if client version older than or equal compare to the compared client version?
*
* @param target Client version to test
* @return true if yes and viceversa
*/
public boolean isOlderThanOrEquals(ClientVersion target) {
return protocolID <= target.protocolID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.compat.AlmostBoolean;
import fr.neatmonster.nocheatplus.compat.versions.ClientVersion;
import fr.neatmonster.nocheatplus.components.config.value.OverrideType;
import fr.neatmonster.nocheatplus.components.data.IData;
import fr.neatmonster.nocheatplus.components.data.IDataOnRemoveSubCheckData;
Expand Down Expand Up @@ -347,6 +348,15 @@ public void removeSubCheckData(
*/
public int getClientVersionID();

/**
* Get the client's version protocol through ViaVersion or ProtocolSupport. <br>
* Requires CompatNoCheatPlus (subject to change)
* @see https://wiki.vg/Protocol_version_numbers
*
* @return ClientVersion.UNKNOWN, if it cannot be determined.
*/
public ClientVersion getClientVersion();

/**
* Set the client's version protocol ID as given by ProtocolSupport or ViaVersion.
* Currently done externally, through CompatNoCheatPlus (subject to change)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import fr.neatmonster.nocheatplus.checks.CheckType;
import fr.neatmonster.nocheatplus.checks.moving.util.MovingUtil;
import fr.neatmonster.nocheatplus.compat.AlmostBoolean;
import fr.neatmonster.nocheatplus.compat.versions.ClientVersion;
import fr.neatmonster.nocheatplus.components.config.value.OverrideType;
import fr.neatmonster.nocheatplus.components.data.ICanHandleTimeRunningBackwards;
import fr.neatmonster.nocheatplus.components.data.IData;
Expand Down Expand Up @@ -173,6 +174,7 @@ public class PlayerData implements IPlayerData {
private boolean requestUpdateInventory = false;
private boolean requestPlayerSetBack = false;
private int versionID = -1;
private ClientVersion clientVersion = ClientVersion.UNKNOWN;

private boolean frequentPlayerTaskShouldBeScheduled = false;
/** Actually queried ones. */
Expand Down Expand Up @@ -460,6 +462,7 @@ void onPlayerLeave(final Player player, final long timeNow, Collection<Class<? e
invalidateOffline();
bedrockPlayer = false;
versionID = -1;
clientVersion = ClientVersion.UNKNOWN;
}

/**
Expand Down Expand Up @@ -890,6 +893,16 @@ public int getClientVersionID() {
return versionID;
}

/**
* Get the client's version through ViaVersion or ProtocolSupport. <br>
* Requires CompatNoCheatPlus (subject to change)
* @return
*/
@Override
public ClientVersion getClientVersion() {
return clientVersion;
}

/**
* Set the client's version ID as given by ProtocolSupport or ViaVersion
*
Expand All @@ -898,6 +911,7 @@ public int getClientVersionID() {
@Override
public void setClientVersionID(final int versionID) {
this.versionID = versionID;
this.clientVersion = ClientVersion.getById(versionID);
}

/**
Expand Down

0 comments on commit dc3a487

Please sign in to comment.