mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 14:38:51 +00:00
Add functionality constants
This commit is contained in:
2 files changed
+51
-24
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,35 +47,35 @@ 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) {
|
||||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
return net.kyori.adventure.text.TextComponent.empty();
|
||||||
|
}
|
||||||
|
|
||||||
if (itemStack == null || itemStack.getType() == Material.AIR) {
|
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||||
return net.kyori.adventure.text.TextComponent.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
|
if (itemStack.getType().isAir()) {
|
||||||
HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
|
return net.kyori.adventure.text.TextComponent.empty();
|
||||||
|
}
|
||||||
|
|
||||||
ComponentBuilder component = new ComponentBuilder();
|
Content content = Bukkit.getItemFactory().hoverContentOf(itemStack);
|
||||||
component.event(event); // Let this be inherited by all coming components.
|
HoverEvent event = new HoverEvent(HoverEvent.Action.SHOW_ITEM, content);
|
||||||
component.color(ChatColor.WHITE).append("[");
|
|
||||||
|
|
||||||
if (itemStack.getItemMeta().hasDisplayName()) {
|
ComponentBuilder component = new ComponentBuilder();
|
||||||
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
|
component.event(event); // Let this be inherited by all coming components.
|
||||||
} else {
|
component.color(ChatColor.WHITE).append("[");
|
||||||
String name = itemStack.getItemMeta().hasDisplayName()
|
|
||||||
? itemStack.getItemMeta().getDisplayName()
|
|
||||||
: "item.minecraft." + itemStack.getType().name().toLowerCase(); // As of 1.13, Material is 1:1 with MC's names
|
|
||||||
component.append(new TranslatableComponent(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
component.color(ChatColor.WHITE).append("]");
|
if (itemStack.getItemMeta().hasDisplayName()) {
|
||||||
|
component.append(TextComponent.fromLegacyText(itemStack.getItemMeta().getDisplayName()));
|
||||||
|
} else {
|
||||||
|
String name = itemStack.getItemMeta().hasDisplayName()
|
||||||
|
? itemStack.getItemMeta().getDisplayName()
|
||||||
|
: "item.minecraft." + itemStack.getType().name().toLowerCase(); // As of 1.13, Material is 1:1 with MC's names
|
||||||
|
component.append(new TranslatableComponent(name));
|
||||||
|
}
|
||||||
|
|
||||||
return BungeeCordComponentSerializer.get().deserialize(component.create());
|
component.color(ChatColor.WHITE).append("]");
|
||||||
} catch (NoSuchMethodError ignored) {}
|
|
||||||
|
|
||||||
return net.kyori.adventure.text.TextComponent.empty();
|
return BungeeCordComponentSerializer.get().deserialize(component.create());
|
||||||
}
|
}
|
||||||
|
|
||||||
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