dev.triumphteam.gui.components.util.VersionHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of triumph-gui Show documentation
Show all versions of triumph-gui Show documentation
Library for easy creation of GUIs for Bukkit plugins.
The newest version!
/**
* MIT License
*
* Copyright (c) 2021 TriumphTeam
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package dev.triumphteam.gui.components.util;
import com.google.common.primitives.Ints;
import dev.triumphteam.gui.components.exception.GuiException;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class for detecting server version for legacy support :(
*/
public final class VersionHelper {
private static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackage().getName();
// Unbreakable change
private static final int V1_11 = 1110;
// Material and components on items change
private static final int V1_13 = 1130;
// PDC and customModelData
private static final int V1_14 = 1140;
// Paper adventure changes
private static final int V1_16_5 = 1165;
// SkullMeta#setOwningPlayer was added
private static final int V1_12_1 = 1121;
// PlayerProfile API
private static final int V1_20_1 = 1201;
private static final int V1_20_5 = 1205;
private static final int CURRENT_VERSION = getCurrentVersion();
/**
* Checks if the version supports Components or not
* Spigot always false
*/
public static final boolean IS_COMPONENT_LEGACY = CURRENT_VERSION < V1_16_5;
/**
* Checks if the version is lower than 1.13 due to the item changes
*/
public static final boolean IS_ITEM_LEGACY = CURRENT_VERSION < V1_13;
/**
* Checks if the version supports the {@link org.bukkit.inventory.meta.ItemMeta#setUnbreakable(boolean)} method
*/
public static final boolean IS_UNBREAKABLE_LEGACY = CURRENT_VERSION < V1_11;
/**
* Checks if the version supports {@link org.bukkit.persistence.PersistentDataContainer}
*/
public static final boolean IS_PDC_VERSION = CURRENT_VERSION >= V1_14;
/**
* Checks if the version doesn't have {@link org.bukkit.inventory.meta.SkullMeta#setOwningPlayer(OfflinePlayer)} and
* {@link org.bukkit.inventory.meta.SkullMeta#setOwner(String)} should be used instead
*/
public static final boolean IS_SKULL_OWNER_LEGACY = CURRENT_VERSION < V1_12_1;
/**
* Checks if the version has {@link org.bukkit.inventory.meta.ItemMeta#setCustomModelData(Integer)}
*/
public static final boolean IS_CUSTOM_MODEL_DATA = CURRENT_VERSION >= V1_14;
/**
* Checks if the version has {@link org.bukkit.profile.PlayerProfile}
*/
public static final boolean IS_PLAYER_PROFILE_API = CURRENT_VERSION >= V1_20_1;
/**
* Starting with version 1.20.5 the internal field referenced by {@link ItemMeta#getDisplayName()} is no longer a string
*/
public static final boolean IS_ITEM_NAME_COMPONENT = CURRENT_VERSION >= V1_20_5;
private static final boolean IS_PAPER = checkPaper();
public static final boolean IS_FOLIA = checkFolia();
/**
* Check if the server has access to the Paper API
* Taken from PaperLib
*
* @return True if on Paper server (or forks), false anything else
*/
private static boolean checkPaper() {
try {
Class.forName("com.destroystokyo.paper.PaperConfig");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}
/**
* Check if the server has access to the Folia API
* Taken from Folia
*
* @return True if on Folia server (or forks), false anything else
*/
private static boolean checkFolia() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}
/**
* Gets the current server version
*
* @return A protocol like number representing the version, for example, 1.16.5 -> 1165
*/
private static int getCurrentVersion() {
// No need to cache since will only run once
final Matcher matcher = Pattern.compile("(?\\d+\\.\\d+)(?\\.\\d+)?").matcher(Bukkit.getBukkitVersion());
final StringBuilder stringBuilder = new StringBuilder();
if (matcher.find()) {
stringBuilder.append(matcher.group("version").replace(".", ""));
final String patch = matcher.group("patch");
if (patch == null) stringBuilder.append("0");
else stringBuilder.append(patch.replace(".", ""));
}
//noinspection UnstableApiUsage
final Integer version = Ints.tryParse(stringBuilder.toString());
// Should never fail
if (version == null) throw new GuiException("Could not retrieve server version!");
return version;
}
public static Class> craftClass(@NotNull final String name) throws ClassNotFoundException {
return Class.forName(CRAFTBUKKIT_PACKAGE + "." + name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy