Search in sources :

Example 1 with TranslatableComponent

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class DiscordCommands method layout0.

private static void layout0(OfflineICPlayer player, String sha1, String title) throws Exception {
    Inventory inv = Bukkit.createInventory(null, 54, title);
    int f1 = 0;
    int f2 = 0;
    int u = 45;
    for (int j = 0; j < Math.min(player.getInventory().getSize(), 45); j++) {
        ItemStack item = player.getInventory().getItem(j);
        if (item != null && !item.getType().equals(Material.AIR)) {
            if ((j >= 9 && j < 18) || j >= 36) {
                if (item.getType().equals(InteractiveChat.invFrame1.getType())) {
                    f1++;
                } else if (item.getType().equals(InteractiveChat.invFrame2.getType())) {
                    f2++;
                }
            }
            if (j < 36) {
                inv.setItem(u, item.clone());
            }
        }
        if (u >= 53) {
            u = 18;
        } else {
            u++;
        }
    }
    ItemStack frame = f1 > f2 ? InteractiveChat.invFrame2.clone() : InteractiveChat.invFrame1.clone();
    ItemMeta frameMeta = frame.getItemMeta();
    frameMeta.setDisplayName(ChatColor.YELLOW + "");
    frame.setItemMeta(frameMeta);
    for (int j = 0; j < 18; j++) {
        inv.setItem(j, frame);
    }
    int level = player.getExperienceLevel();
    ItemStack exp = XMaterial.EXPERIENCE_BOTTLE.parseItem();
    if (InteractiveChat.version.isNewerThan(MCVersion.V1_15)) {
        TranslatableComponent expText = Component.translatable(InventoryDisplay.getLevelTranslation(level)).color(NamedTextColor.YELLOW).decorate(TextDecoration.ITALIC);
        if (level != 1) {
            expText = expText.args(Component.text(level + ""));
        }
        exp = NBTEditor.set(exp, InteractiveChatComponentSerializer.gson().serialize(expText), "display", "Name");
    } else {
        ItemMeta expMeta = exp.getItemMeta();
        expMeta.setDisplayName(ChatColor.YELLOW + LanguageUtils.getTranslation(InventoryDisplay.getLevelTranslation(level), InteractiveChat.language).replaceFirst("%s|%d", level + ""));
        exp.setItemMeta(expMeta);
    }
    inv.setItem(1, exp);
    inv.setItem(3, player.getInventory().getItem(39));
    inv.setItem(4, player.getInventory().getItem(38));
    inv.setItem(5, player.getInventory().getItem(37));
    inv.setItem(6, player.getInventory().getItem(36));
    ItemStack offhand = player.getInventory().getSize() > 40 ? player.getInventory().getItem(40) : null;
    if (!InteractiveChat.version.isOld() || (offhand != null && offhand.getType().equals(Material.AIR))) {
        inv.setItem(8, offhand);
    }
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
        ItemStack skull = SkinUtils.getSkull(player.getUniqueId());
        ItemMeta meta = skull.getItemMeta();
        String name = ChatColorUtils.translateAlternateColorCodes('&', InteractiveChatDiscordSrvAddon.plugin.shareInvCommandSkullName.replace("{Player}", player.getName()));
        meta.setDisplayName(name);
        skull.setItemMeta(meta);
        inv.setItem(0, skull);
    });
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY, sha1, inv);
    if (InteractiveChat.bungeecordMode) {
        try {
            long time = System.currentTimeMillis();
            BungeeMessageSender.addInventory(time, SharedType.INVENTORY, sha1, title, inv);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 2 with TranslatableComponent

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class DiscordCommands method layout1.

private static void layout1(OfflineICPlayer player, String sha1, String title) throws Exception {
    int selectedSlot = player.getSelectedSlot();
    int level = player.getExperienceLevel();
    Inventory inv = Bukkit.createInventory(null, 54, title);
    int f1 = 0;
    int f2 = 0;
    for (int j = 0; j < Math.min(player.getInventory().getSize(), 45); j++) {
        if (j == selectedSlot || j >= 36) {
            ItemStack item = player.getInventory().getItem(j);
            if (item != null && !item.getType().equals(Material.AIR)) {
                if (item.getType().equals(InteractiveChat.invFrame1.getType())) {
                    f1++;
                } else if (item.getType().equals(InteractiveChat.invFrame2.getType())) {
                    f2++;
                }
            }
        }
    }
    ItemStack frame = f1 > f2 ? InteractiveChat.invFrame2.clone() : InteractiveChat.invFrame1.clone();
    ItemMeta frameMeta = frame.getItemMeta();
    frameMeta.setDisplayName(ChatColor.YELLOW + "");
    frame.setItemMeta(frameMeta);
    for (int j = 0; j < 54; j++) {
        inv.setItem(j, frame);
    }
    inv.setItem(12, player.getInventory().getItem(39));
    inv.setItem(21, player.getInventory().getItem(38));
    inv.setItem(30, player.getInventory().getItem(37));
    inv.setItem(39, player.getInventory().getItem(36));
    ItemStack offhand = player.getInventory().getSize() > 40 ? player.getInventory().getItem(40) : null;
    if (InteractiveChat.version.isOld() && (offhand == null || offhand.getType().equals(Material.AIR))) {
        inv.setItem(24, player.getInventory().getItem(selectedSlot));
    } else {
        inv.setItem(23, offhand);
        inv.setItem(25, player.getInventory().getItem(selectedSlot));
    }
    ItemStack exp = XMaterial.EXPERIENCE_BOTTLE.parseItem();
    if (InteractiveChat.version.isNewerThan(MCVersion.V1_15)) {
        TranslatableComponent expText = Component.translatable(InventoryDisplay.getLevelTranslation(level)).color(NamedTextColor.YELLOW).decorate(TextDecoration.ITALIC);
        if (level != 1) {
            expText = expText.args(Component.text(level + ""));
        }
        exp = NBTEditor.set(exp, InteractiveChatComponentSerializer.gson().serialize(expText), "display", "Name");
    } else {
        ItemMeta expMeta = exp.getItemMeta();
        expMeta.setDisplayName(ChatColor.YELLOW + LanguageUtils.getTranslation(InventoryDisplay.getLevelTranslation(level), InteractiveChat.language).replaceFirst("%s|%d", level + ""));
        exp.setItemMeta(expMeta);
    }
    inv.setItem(37, exp);
    Inventory inv2 = Bukkit.createInventory(null, 45, title);
    for (int j = 0; j < Math.min(player.getInventory().getSize(), 45); j++) {
        ItemStack item = player.getInventory().getItem(j);
        if (item != null && !item.getType().equals(Material.AIR)) {
            inv2.setItem(j, item.clone());
        }
    }
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
        ItemStack skull = SkinUtils.getSkull(player.getUniqueId());
        ItemMeta meta = skull.getItemMeta();
        String name = ChatColorUtils.translateAlternateColorCodes('&', InteractiveChatDiscordSrvAddon.plugin.shareInvCommandSkullName.replace("{Player}", player.getName()));
        meta.setDisplayName(name);
        skull.setItemMeta(meta);
        inv.setItem(10, skull);
    });
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY1_UPPER, sha1, inv);
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY1_LOWER, sha1, inv2);
    if (InteractiveChat.bungeecordMode) {
        try {
            long time = System.currentTimeMillis();
            BungeeMessageSender.addInventory(time, SharedType.INVENTORY1_UPPER, sha1, title, inv);
            BungeeMessageSender.addInventory(time, SharedType.INVENTORY1_LOWER, sha1, title, inv2);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Also used : TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 3 with TranslatableComponent

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ComponentStringUtils method extractItemStack.

@SuppressWarnings("deprecation")
public static ItemStack extractItemStack(Component component) {
    component = ComponentFlattening.flatten(component);
    List<Component> children = new ArrayList<>(component.children());
    for (int i = 0; i < children.size(); i++) {
        Component child = children.get(i);
        HoverEvent<?> hoverEvent = child.hoverEvent();
        if (hoverEvent != null && hoverEvent.action().equals(HoverEvent.Action.SHOW_ITEM)) {
            ShowItem showItem = (ShowItem) hoverEvent.value();
            Key key = showItem.item();
            int count = showItem.count();
            String simpleNbt = "{id:\"" + key.asString() + "\", Count: " + count + "b}";
            String longNbt = showItem.nbt() == null ? null : showItem.nbt().string();
            ItemStack itemstack = null;
            try {
                itemstack = ItemNBTUtils.getItemFromNBTJson(simpleNbt);
            } catch (Throwable e) {
            }
            if (longNbt != null) {
                try {
                    itemstack = Bukkit.getUnsafe().modifyItemStack(itemstack, longNbt);
                } catch (Throwable e) {
                }
            }
            if (itemstack != null) {
                return itemstack;
            }
        }
        if (child instanceof TranslatableComponent) {
            TranslatableComponent trans = (TranslatableComponent) child;
            List<Component> withs = new ArrayList<>(trans.args());
            for (Component with : withs) {
                ItemStack itemstack = extractItemStack(with);
                if (itemstack != null) {
                    return itemstack;
                }
            }
            trans = trans.args(withs);
            children.set(i, trans);
        }
    }
    return null;
}
Also used : ShowItem(com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent.ShowItem) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) ArrayList(java.util.ArrayList) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) ItemStack(org.bukkit.inventory.ItemStack) Key(com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)

Example 4 with TranslatableComponent

use of com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ComponentStringUtils method convertTranslatables.

public static Component convertTranslatables(Component component, Function<String, String> translateFunction) {
    component = ComponentFlattening.flatten(component);
    List<Component> children = new ArrayList<>(component.children());
    for (int i = 0; i < children.size(); i++) {
        Component current = children.get(i);
        if (current instanceof TranslatableComponent) {
            TranslatableComponent trans = (TranslatableComponent) current;
            Component translated = Component.text(translateFunction.apply(trans.key())).style(trans.style());
            for (Component arg : trans.args()) {
                translated = translated.replaceText(TextReplacementConfig.builder().matchLiteral("%s").replacement(convertTranslatables(arg, translateFunction)).once().build());
            }
            children.set(i, translated);
        }
    }
    return ComponentCompacting.optimize(component.children(children));
}
Also used : TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent) ArrayList(java.util.ArrayList) Component(com.loohp.interactivechat.libs.net.kyori.adventure.text.Component) TranslatableComponent(com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent)

Aggregations

TranslatableComponent (com.loohp.interactivechat.libs.net.kyori.adventure.text.TranslatableComponent)4 ItemStack (org.bukkit.inventory.ItemStack)3 Component (com.loohp.interactivechat.libs.net.kyori.adventure.text.Component)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Inventory (org.bukkit.inventory.Inventory)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 Key (com.loohp.interactivechat.libs.net.kyori.adventure.key.Key)1 ShowItem (com.loohp.interactivechat.libs.net.kyori.adventure.text.event.HoverEvent.ShowItem)1