mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 06:28:54 +00:00
Implement item linking
This commit is contained in:
1 parent
5b72fce0f0
commit
6269f52349
13 files changed
+132
-35
No files matched your search
@@ -142,7 +142,7 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
|
||||
*/
|
||||
public record Result(boolean cancelled, Component reason) implements ResultedCarbonEvent.Result {
|
||||
|
||||
private static final Result ALLOWED = new Result(true, empty());
|
||||
private static final Result ALLOWED = new Result(false, empty());
|
||||
|
||||
/**
|
||||
* Returns a Result that denotes the event was allowed and not cancelled.
|
||||
@@ -162,7 +162,7 @@ public class CarbonChatEvent implements ResultedCarbonEvent<CarbonChatEvent.Resu
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public static Result denied(final Component reason) {
|
||||
return new Result(false, reason);
|
||||
return new Result(true, reason);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ public final class CarbonEventHandler {
|
||||
if (event instanceof ResultedCarbonEvent<@NonNull ?> rce) {
|
||||
return !rce.result().cancelled();
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.punishments.MuteEntry;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.identity.Identified;
|
||||
import net.kyori.adventure.text.Component;
|
||||
@@ -101,12 +102,13 @@ public interface CarbonPlayer extends Audience, Identified {
|
||||
UUID uuid();
|
||||
|
||||
/**
|
||||
* Creates a {@link Component} with a content and item hover given the player's actively held item.
|
||||
* Creates the chat component for the item in the {@link InventorySlot}, or null if the slot is empty.
|
||||
*
|
||||
* @return the player's held item as an item hover component
|
||||
* @param slot the inventory slot containing the item
|
||||
* @return the chat component for the item in the slot, or null if the slot is empty
|
||||
* @since 2.0.0
|
||||
*/
|
||||
Component createItemHoverComponent();
|
||||
@Nullable Component createItemHoverComponent(final InventorySlot slot);
|
||||
|
||||
/**
|
||||
* The player's locale.
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class InventorySlot {
|
||||
|
||||
public static InventorySlot of(final String... placeholders) {
|
||||
return new InventorySlot(placeholders);
|
||||
}
|
||||
|
||||
private List<String> placeholders;
|
||||
|
||||
private InventorySlot() {
|
||||
|
||||
}
|
||||
|
||||
private InventorySlot(final String... placeholders) {
|
||||
this.placeholders = List.of(placeholders);
|
||||
}
|
||||
|
||||
public List<String> placeholders() {
|
||||
return this.placeholders;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.draycia.carbon.api.util;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public final class InventorySlots {
|
||||
|
||||
public static final InventorySlot HELMET = InventorySlot.of("helm", "helmet", "hat", "head");
|
||||
public static final InventorySlot CHEST = InventorySlot.of("chest", "chestplate");
|
||||
public static final InventorySlot LEGS = InventorySlot.of("legs", "leggings");
|
||||
public static final InventorySlot BOOTS = InventorySlot.of("boots", "feet");
|
||||
public static final InventorySlot MAIN_HAND = InventorySlot.of("main_hand", "hand", "item");
|
||||
public static final InventorySlot OFF_HAND = InventorySlot.of("off_hand");
|
||||
|
||||
public static List<InventorySlot> VALUES = List.of(HELMET, CHEST, LEGS, BOOTS, MAIN_HAND, OFF_HAND);
|
||||
|
||||
}
|
||||
@@ -26,6 +26,7 @@ import net.draycia.carbon.common.command.commands.ReplyCommand;
|
||||
import net.draycia.carbon.common.command.commands.UnmuteCommand;
|
||||
import net.draycia.carbon.common.command.commands.WhisperCommand;
|
||||
import net.draycia.carbon.common.listeners.DeafenHandler;
|
||||
import net.draycia.carbon.common.listeners.ItemLinkHandler;
|
||||
import net.draycia.carbon.common.listeners.MuteHandler;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
@@ -97,6 +98,7 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
||||
|
||||
// Listeners
|
||||
this.injector.getInstance(DeafenHandler.class);
|
||||
this.injector.getInstance(ItemLinkHandler.class);
|
||||
this.injector.getInstance(MuteHandler.class);
|
||||
|
||||
// Commands
|
||||
|
||||
@@ -6,6 +6,8 @@ import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
import net.draycia.carbon.api.util.InventorySlots;
|
||||
import net.draycia.carbon.bukkit.util.BukkitCapabilities;
|
||||
import net.draycia.carbon.common.users.CarbonPlayerCommon;
|
||||
import net.draycia.carbon.common.users.WrappedCarbonPlayer;
|
||||
@@ -15,6 +17,8 @@ import net.kyori.adventure.text.Component;
|
||||
import net.milkbowl.vault.permission.Permission;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.EntityEquipment;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.metadata.MetadataValue;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -90,35 +94,41 @@ public final class CarbonPlayerBukkit extends WrappedCarbonPlayer implements For
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component createItemHoverComponent() {
|
||||
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
|
||||
final @Nullable Player player = this.player(); // This is temporary (it's not)
|
||||
|
||||
if (player == null) {
|
||||
return Component.empty();
|
||||
return null;
|
||||
}
|
||||
|
||||
final @Nullable ItemStack itemStack;
|
||||
final EquipmentSlot equipmentSlot;
|
||||
|
||||
final @Nullable ItemStack mainHand = player.getInventory().getItemInMainHand();
|
||||
|
||||
if (mainHand != null && !mainHand.getType().isAir()) {
|
||||
itemStack = mainHand;
|
||||
if (slot.equals(InventorySlots.MAIN_HAND)) {
|
||||
equipmentSlot = EquipmentSlot.HAND;
|
||||
} else if (slot.equals(InventorySlots.OFF_HAND)) {
|
||||
equipmentSlot = EquipmentSlot.OFF_HAND;
|
||||
} else if (slot.equals(InventorySlots.HELMET)) {
|
||||
equipmentSlot = EquipmentSlot.HEAD;
|
||||
} else if (slot.equals(InventorySlots.CHEST)) {
|
||||
equipmentSlot = EquipmentSlot.CHEST;
|
||||
} else if (slot.equals(InventorySlots.LEGS)) {
|
||||
equipmentSlot = EquipmentSlot.LEGS;
|
||||
} else if (slot.equals(InventorySlots.BOOTS)) {
|
||||
equipmentSlot = EquipmentSlot.FEET;
|
||||
} else {
|
||||
final @Nullable ItemStack offHand = player.getInventory().getItemInOffHand();
|
||||
|
||||
if (offHand != null && !offHand.getType().isAir()) {
|
||||
itemStack = offHand;
|
||||
} else {
|
||||
itemStack = null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (itemStack == null) {
|
||||
return Component.empty();
|
||||
final @Nullable EntityEquipment equipment = player.getEquipment();
|
||||
|
||||
if (equipment == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (itemStack.getType().isAir()) {
|
||||
return Component.empty();
|
||||
final @Nullable ItemStack itemStack = equipment.getItem(equipmentSlot);
|
||||
|
||||
if (itemStack == null || itemStack.getType().isAir()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return itemStack.displayName();
|
||||
|
||||
@@ -10,6 +10,7 @@ import net.draycia.carbon.api.util.SourcedAudience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.adventure.text.minimessage.template.TemplateResolver;
|
||||
import net.kyori.moonshine.message.IMessageRenderer;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -31,7 +32,7 @@ public class BukkitMessageRenderer implements IMessageRenderer<SourcedAudience,
|
||||
final List<Template> templates = new ArrayList<>();
|
||||
|
||||
for (final var entry : resolvedPlaceholders.entrySet()) {
|
||||
templates.add(Template.of(entry.getKey(), entry.getValue()));
|
||||
templates.add(Template.template(entry.getKey(), entry.getValue()));
|
||||
}
|
||||
|
||||
if (receiver.sender() instanceof CarbonPlayer sender && sender.online()) {
|
||||
@@ -43,7 +44,7 @@ public class BukkitMessageRenderer implements IMessageRenderer<SourcedAudience,
|
||||
}
|
||||
}
|
||||
|
||||
return MiniMessage.miniMessage().parse(intermediateMessage, templates);
|
||||
return MiniMessage.miniMessage().deserialize(intermediateMessage, TemplateResolver.templates(templates));
|
||||
}
|
||||
|
||||
}
|
||||
+3
-2
@@ -10,6 +10,7 @@ import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.Template;
|
||||
import net.kyori.adventure.text.minimessage.template.TemplateResolver;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -89,14 +90,14 @@ public final class PlaceholderAPIMiniMessageParser {
|
||||
} else {
|
||||
final String key = "papi_generated_template_" + id;
|
||||
id++;
|
||||
templates.add(Template.of(key, LegacyComponentSerializer.legacySection().deserialize(replaced)));
|
||||
templates.add(Template.template(key, LegacyComponentSerializer.legacySection().deserialize(replaced)));
|
||||
matcher.appendReplacement(builder, "<" + key + ">");
|
||||
}
|
||||
}
|
||||
|
||||
matcher.appendTail(builder);
|
||||
|
||||
return this.miniMessage.parse(builder.toString(), templates);
|
||||
return this.miniMessage.deserialize(builder.toString(), TemplateResolver.templates(templates));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import cloud.commandframework.CommandManager;
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@@ -14,8 +12,7 @@ public class DeafenHandler {
|
||||
|
||||
@Inject
|
||||
public DeafenHandler(
|
||||
final CarbonChat carbonChat,
|
||||
final CommandManager<Commander> commandManager
|
||||
final CarbonChat carbonChat
|
||||
) {
|
||||
carbonChat.eventHandler().subscribe(CarbonChatEvent.class, 0, false, event -> {
|
||||
if (!event.sender().deafened()) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.draycia.carbon.common.listeners;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonChatEvent;
|
||||
import net.draycia.carbon.api.util.InventorySlots;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
|
||||
public class ItemLinkHandler {
|
||||
|
||||
@Inject
|
||||
public ItemLinkHandler(
|
||||
final CarbonChat carbonChat
|
||||
) {
|
||||
carbonChat.eventHandler().subscribe(CarbonChatEvent.class, 1, true, event -> {
|
||||
carbonChat.logger().info("test!");
|
||||
for (final var slot : InventorySlots.VALUES) {
|
||||
for (final var placeholder : slot.placeholders()) {
|
||||
event.message(
|
||||
event.message()
|
||||
.replaceText(TextReplacementConfig.builder()
|
||||
.matchLiteral("<" + placeholder + ">")
|
||||
.once()
|
||||
.replacement(builder -> {
|
||||
final var itemComponent = event.sender().createItemHoverComponent(slot);
|
||||
|
||||
if (itemComponent == null) {
|
||||
return builder;
|
||||
}
|
||||
|
||||
return event.sender().createItemHoverComponent(slot);
|
||||
})
|
||||
.build())
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import java.util.*;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.punishments.MuteEntry;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.audience.ForwardingAudience;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
@@ -62,8 +63,8 @@ public class CarbonPlayerCommon implements CarbonPlayer, ForwardingAudience.Sing
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component createItemHoverComponent() {
|
||||
return Component.empty();
|
||||
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.draycia.carbon.common.users;
|
||||
import net.draycia.carbon.api.channels.ChatChannel;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.api.users.punishments.MuteEntry;
|
||||
import net.draycia.carbon.api.util.InventorySlot;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -60,8 +61,8 @@ public abstract class WrappedCarbonPlayer implements CarbonPlayer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component createItemHoverComponent() {
|
||||
return this.carbonPlayerCommon().createItemHoverComponent();
|
||||
public @Nullable Component createItemHoverComponent(final InventorySlot slot) {
|
||||
return this.carbonPlayerCommon().createItemHoverComponent(slot);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user