mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 06:28:54 +00:00
Add item->component
This commit is contained in:
1 parent
5d6c61976e
commit
407de5479e
4 files changed
+96
-63
No files matched your search
@@ -2,15 +2,26 @@ package net.draycia.carbon.bukkit;
|
||||
|
||||
import io.papermc.lib.PaperLib;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bstats.bukkit.Metrics;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import static java.util.Objects.requireNonNullElseGet;
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.Component.translatable;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
||||
|
||||
public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
private final Logger logger = LogManager.getLogger("CarbonChat");
|
||||
|
||||
@@ -39,4 +50,34 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createComponent(final @NonNull UUID uuid) {
|
||||
final Player player = Bukkit.getPlayer(uuid); // This is temporary (it's not)
|
||||
|
||||
if (player == null) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (itemStack.getType().isAir()) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
final TextComponent.Builder builder = text();
|
||||
|
||||
builder.hoverEvent(itemStack); // Let this be inherited by all coming components.
|
||||
builder.append(text("[", WHITE));
|
||||
|
||||
final Component displayName = itemStack.getItemMeta().displayName();
|
||||
|
||||
builder.append(requireNonNullElseGet(displayName, () ->
|
||||
translatable(itemStack.getType().getTranslationKey())));
|
||||
|
||||
builder.append(text("]", WHITE));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
Simple channel based chat plugin for Spigot
|
||||
Copyright (C) 2020 Alexander Söderberg
|
||||
Copyright (C) 2020 Josua Parks (Draycia)
|
||||
|
||||
Modifications made to work with Kyori/Adventure
|
||||
|
||||
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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.bukkit.util;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import static java.util.Objects.requireNonNullElseGet;
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.Component.translatable;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
||||
|
||||
public final class CarbonUtils {
|
||||
|
||||
private CarbonUtils() {
|
||||
|
||||
}
|
||||
|
||||
public static @NonNull Component createComponent(final @NonNull Player player) {
|
||||
final ItemStack itemStack = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (itemStack.getType().isAir()) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
final TextComponent.Builder builder = text();
|
||||
|
||||
builder.hoverEvent(itemStack); // Let this be inherited by all coming components.
|
||||
builder.append(text("[", WHITE));
|
||||
|
||||
final Component displayName = itemStack.getItemMeta().displayName();
|
||||
|
||||
builder.append(requireNonNullElseGet(displayName, () ->
|
||||
translatable(itemStack.getType().getTranslationKey())));
|
||||
|
||||
builder.append(text("]", WHITE));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user