mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +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
@@ -1,9 +1,15 @@
|
||||
package net.draycia.carbon.api;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface CarbonChat {
|
||||
|
||||
@NonNull Logger logger();
|
||||
|
||||
@NonNull Component createComponent(final @NonNull UUID uuid);
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,12 +2,26 @@ package net.draycia.carbon.sponge;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.bstats.sponge.Metrics;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.spongepowered.api.Sponge;
|
||||
import org.spongepowered.api.data.Keys;
|
||||
import org.spongepowered.api.data.type.HandTypes;
|
||||
import org.spongepowered.api.entity.living.player.server.ServerPlayer;
|
||||
import org.spongepowered.api.item.inventory.ItemStack;
|
||||
import org.spongepowered.plugin.PluginContainer;
|
||||
import org.spongepowered.plugin.jvm.Plugin;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.WHITE;
|
||||
|
||||
@Plugin("carbonchat")
|
||||
public class CarbonChatSponge implements CarbonChat {
|
||||
private final PluginContainer pluginContainer;
|
||||
@@ -31,4 +45,39 @@ public class CarbonChatSponge implements CarbonChat {
|
||||
public @NonNull Logger logger() {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull Component createComponent(final @NonNull UUID uuid) {
|
||||
final Optional<ServerPlayer> player = Sponge.server().player(uuid);
|
||||
|
||||
if (player.isPresent()) {
|
||||
ItemStack itemStack = null;
|
||||
|
||||
final ItemStack mainHand = player.get().itemInHand(HandTypes.MAIN_HAND);
|
||||
|
||||
if (!mainHand.isEmpty()) {
|
||||
itemStack = mainHand;
|
||||
} else {
|
||||
final ItemStack offHand = player.get().itemInHand(HandTypes.OFF_HAND);
|
||||
|
||||
if (!offHand.isEmpty()) {
|
||||
itemStack = offHand;
|
||||
}
|
||||
}
|
||||
|
||||
if (itemStack == null || itemStack.isEmpty()) {
|
||||
return empty();
|
||||
}
|
||||
|
||||
final TextComponent.Builder builder = text();
|
||||
|
||||
builder.append(text("[", WHITE));
|
||||
builder.append(itemStack.get(Keys.DISPLAY_NAME).orElse(itemStack.type().asComponent()));
|
||||
builder.append(text("]", WHITE));
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
return empty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user