Search in sources :

Example 1 with InventoryPlaceholderEvent

use of com.loohp.interactivechat.api.events.InventoryPlaceholderEvent in project InteractiveChat by LOOHP.

the class EnderchestDisplay method process.

public static Component process(Component component, Optional<ICPlayer> optplayer, Player reciever, long unix) throws Exception {
    String plain = InteractiveChatComponentSerializer.plainText().serialize(component);
    if (InteractiveChat.enderPlaceholder.matcher(plain).find()) {
        String regex = InteractiveChat.enderPlaceholder.pattern();
        if (optplayer.isPresent()) {
            ICPlayer player = optplayer.get();
            if (PlayerUtils.hasPermission(player.getUniqueId(), "interactivechat.module.enderchest", true, 5)) {
                String replaceText = InteractiveChat.enderReplaceText;
                String title = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, InteractiveChat.enderTitle));
                String sha1 = HashUtils.createSha1(title, player.getEnderChest());
                if (!InteractiveChat.enderDisplay.containsKey(sha1)) {
                    int size = player.getEnderChest().getSize();
                    Inventory inv = Bukkit.createInventory(null, InventoryUtils.toMultipleOf9(size), title);
                    for (int j = 0; j < size; j++) {
                        if (player.getEnderChest().getItem(j) != null) {
                            if (!player.getEnderChest().getItem(j).getType().equals(Material.AIR)) {
                                inv.setItem(j, player.getEnderChest().getItem(j).clone());
                            }
                        }
                    }
                    InventoryPlaceholderEvent event = new InventoryPlaceholderEvent(player, reciever, component, unix, inv, InventoryPlaceholderType.ENDERCHEST);
                    Bukkit.getPluginManager().callEvent(event);
                    inv = event.getInventory();
                    InteractiveChatAPI.addInventoryToItemShareList(SharedType.ENDERCHEST, sha1, inv);
                    if (InteractiveChat.bungeecordMode) {
                        if (player.isLocal()) {
                            try {
                                BungeeMessageSender.forwardEnderchest(unix, player.getUniqueId(), player.isRightHanded(), player.getSelectedSlot(), player.getExperienceLevel(), null, inv);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
                String componentText = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, replaceText));
                List<String> hoverList = ConfigManager.getConfig().getStringList("ItemDisplay.EnderChest.HoverMessage");
                String hoverText = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, String.join("\n", hoverList)));
                String command = "/interactivechat viewender " + sha1;
                Component enderComponent = LegacyComponentSerializer.legacySection().deserialize(componentText);
                enderComponent = enderComponent.hoverEvent(HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(hoverText)));
                enderComponent = enderComponent.clickEvent(ClickEvent.runCommand(command));
                component = ComponentReplacing.replace(component, regex, true, enderComponent);
            }
        } else {
            Component message;
            if (InteractiveChat.playerNotFoundReplaceEnable) {
                message = LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.playerNotFoundReplaceText.replace("{Placeholder}", InteractiveChat.enderName));
            } else {
                message = Component.text(InteractiveChat.enderName);
            }
            if (InteractiveChat.playerNotFoundHoverEnable) {
                message = message.hoverEvent(HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.playerNotFoundHoverText.replace("{Placeholder}", InteractiveChat.enderName))));
            }
            if (InteractiveChat.playerNotFoundClickEnable) {
                String clickValue = ChatColorUtils.translateAlternateColorCodes('&', InteractiveChat.playerNotFoundClickValue.replace("{Placeholder}", InteractiveChat.enderName));
                message = message.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.valueOf(InteractiveChat.playerNotFoundClickAction), clickValue));
            }
            component = ComponentReplacing.replace(component, regex, true, message);
        }
        return component;
    } else {
        return component;
    }
}
Also used : ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) InventoryPlaceholderEvent(com.loohp.interactivechat.api.events.InventoryPlaceholderEvent) IOException(java.io.IOException) Component(net.kyori.adventure.text.Component) Inventory(org.bukkit.inventory.Inventory)

Example 2 with InventoryPlaceholderEvent

use of com.loohp.interactivechat.api.events.InventoryPlaceholderEvent in project InteractiveChat by LOOHP.

the class InventoryDisplay method layout0.

private static void layout0(ICPlayer player, String sha1, String title, Player reciever, Component component, long unix) 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(getLevelTranslation(level)).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, State.FALSE);
        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(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);
    }
    InventoryPlaceholderEvent event = new InventoryPlaceholderEvent(player, reciever, component, unix, inv, InventoryPlaceholderType.INVENTORY);
    Bukkit.getPluginManager().callEvent(event);
    inv = event.getInventory();
    Inventory finalRef = inv;
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
        ItemStack skull = SkinUtils.getSkull(player.getUniqueId());
        ItemMeta meta = skull.getItemMeta();
        String name = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, InteractiveChat.invSkullName));
        meta.setDisplayName(name);
        skull.setItemMeta(meta);
        finalRef.setItem(0, skull);
    });
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY, sha1, inv);
    if (InteractiveChat.bungeecordMode) {
        if (player.isLocal()) {
            try {
                Inventory toForward = 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)) {
                        toForward.setItem(j, item.clone());
                    }
                }
                BungeeMessageSender.forwardInventory(unix, player.getUniqueId(), player.isRightHanded(), player.getSelectedSlot(), player.getExperienceLevel(), null, toForward);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : TranslatableComponent(net.kyori.adventure.text.TranslatableComponent) InventoryPlaceholderEvent(com.loohp.interactivechat.api.events.InventoryPlaceholderEvent) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 3 with InventoryPlaceholderEvent

use of com.loohp.interactivechat.api.events.InventoryPlaceholderEvent in project InteractiveChat by LOOHP.

the class InventoryDisplay method layout1.

private static void layout1(ICPlayer player, String sha1, String title, Player reciever, Component component, long unix) throws Exception {
    boolean rightHanded = player.isRightHanded();
    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, rightHanded ? offhand : player.getInventory().getItem(selectedSlot));
        inv.setItem(25, rightHanded ? player.getInventory().getItem(selectedSlot) : offhand);
    }
    ItemStack exp = XMaterial.EXPERIENCE_BOTTLE.parseItem();
    if (InteractiveChat.version.isNewerThan(MCVersion.V1_15)) {
        TranslatableComponent expText = Component.translatable(getLevelTranslation(level)).color(NamedTextColor.YELLOW).decoration(TextDecoration.ITALIC, State.FALSE);
        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(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());
        }
    }
    InventoryPlaceholderEvent event = new InventoryPlaceholderEvent(player, reciever, component, unix, inv, InventoryPlaceholderType.INVENTORY1_UPPER);
    Bukkit.getPluginManager().callEvent(event);
    inv = event.getInventory();
    InventoryPlaceholderEvent event2 = new InventoryPlaceholderEvent(player, reciever, component, unix, inv2, InventoryPlaceholderType.INVENTORY1_LOWER);
    Bukkit.getPluginManager().callEvent(event2);
    inv2 = event2.getInventory();
    Inventory finalRef = inv;
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
        ItemStack skull = SkinUtils.getSkull(player.getUniqueId());
        ItemMeta meta = skull.getItemMeta();
        String name = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, InteractiveChat.invSkullName));
        meta.setDisplayName(name);
        skull.setItemMeta(meta);
        finalRef.setItem(10, skull);
    });
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY1_UPPER, sha1, inv);
    InteractiveChatAPI.addInventoryToItemShareList(SharedType.INVENTORY1_LOWER, sha1, inv2);
    if (InteractiveChat.bungeecordMode) {
        if (player.isLocal()) {
            try {
                BungeeMessageSender.forwardInventory(unix, player.getUniqueId(), player.isRightHanded(), player.getSelectedSlot(), player.getExperienceLevel(), null, inv2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : TranslatableComponent(net.kyori.adventure.text.TranslatableComponent) InventoryPlaceholderEvent(com.loohp.interactivechat.api.events.InventoryPlaceholderEvent) IOException(java.io.IOException) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Aggregations

InventoryPlaceholderEvent (com.loohp.interactivechat.api.events.InventoryPlaceholderEvent)3 IOException (java.io.IOException)3 Inventory (org.bukkit.inventory.Inventory)3 TranslatableComponent (net.kyori.adventure.text.TranslatableComponent)2 ItemStack (org.bukkit.inventory.ItemStack)2 ItemMeta (org.bukkit.inventory.meta.ItemMeta)2 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)1 Component (net.kyori.adventure.text.Component)1