mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-04 06:28:53 +00:00
Add functionality constants
This commit is contained in:
2 files changed
+34
-7
No files matched your search
@@ -32,11 +32,13 @@ import net.kyori.adventure.text.format.TextColor;
|
|||||||
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
|
import net.kyori.adventure.text.serializer.bungeecord.BungeeCordComponentSerializer;
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import net.md_5.bungee.api.ChatColor;
|
import net.md_5.bungee.api.ChatColor;
|
||||||
import net.md_5.bungee.api.chat.*;
|
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||||
|
import net.md_5.bungee.api.chat.HoverEvent;
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||||
import net.md_5.bungee.api.chat.hover.content.Content;
|
import net.md_5.bungee.api.chat.hover.content.Content;
|
||||||
import org.apache.commons.lang.NotImplementedException;
|
import org.apache.commons.lang.NotImplementedException;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
@@ -45,10 +47,13 @@ import java.util.ArrayList;
|
|||||||
public final class CarbonUtils {
|
public final class CarbonUtils {
|
||||||
|
|
||||||
public static Component createComponent(final Player player) {
|
public static Component createComponent(final Player player) {
|
||||||
try {
|
if (!FunctionalityConstants.HAS_HOVER_EVENT_METHOD) {
|
||||||
|
return net.kyori.adventure.text.TextComponent.empty();
|
||||||
|
}
|
||||||
|
|
||||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||||
|
|
||||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
if (itemStack.getType().isAir()) {
|
||||||
return net.kyori.adventure.text.TextComponent.empty();
|
return net.kyori.adventure.text.TextComponent.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,9 +76,6 @@ public final class CarbonUtils {
|
|||||||
component.color(ChatColor.WHITE).append("]");
|
component.color(ChatColor.WHITE).append("]");
|
||||||
|
|
||||||
return BungeeCordComponentSerializer.get().deserialize(component.create());
|
return BungeeCordComponentSerializer.get().deserialize(component.create());
|
||||||
} catch (NoSuchMethodError ignored) {}
|
|
||||||
|
|
||||||
return net.kyori.adventure.text.TextComponent.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TextColor parseColor(ChatUser user, String input) {
|
public static TextColor parseColor(ChatUser user, String input) {
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package net.draycia.carbon.util;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemFactory;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
@SuppressWarnings("ResultOfMethodCallIgnored") // This is a common pattern in this class.
|
||||||
|
public final class FunctionalityConstants {
|
||||||
|
|
||||||
|
public static final boolean HAS_HOVER_EVENT_METHOD = noThrow(() -> ItemFactory.class.getMethod("hoverContentOf", ItemStack.class));
|
||||||
|
|
||||||
|
private static boolean noThrow(@NonNull ThrowingRunnable runnable) {
|
||||||
|
try {
|
||||||
|
runnable.run();
|
||||||
|
return true;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@FunctionalInterface
|
||||||
|
private interface ThrowingRunnable {
|
||||||
|
void run() throws Exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user