Search in sources :

Example 1 with UpdaterResponse

use of com.loohp.interactivechat.updater.Updater.UpdaterResponse in project InteractiveChat by LOOHP.

the class Commands method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (!label.equalsIgnoreCase("interactivechat") && !label.equalsIgnoreCase("ic")) {
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.AQUA + "InteractiveChat written by LOOHP!");
        sender.sendMessage(ChatColor.GOLD + "You are running InteractiveChat version: " + InteractiveChat.plugin.getDescription().getVersion());
        return true;
    }
    if (args[0].equalsIgnoreCase("reload")) {
        if (sender.hasPermission("interactivechat.reload")) {
            InteractiveChat.closeSharedInventoryViews();
            ConfigManager.reloadConfig();
            InteractiveChat.placeholderCooldownManager.reloadPlaceholders();
            PlayerUtils.resetAllPermissionCache();
            Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> InteractiveChat.playerDataManager.reload());
            if (InteractiveChat.bungeecordMode) {
                try {
                    BungeeMessageSender.reloadBungeeConfig(System.currentTimeMillis());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            sender.sendMessage(InteractiveChat.reloadPluginMessage);
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("update")) {
        if (sender.hasPermission("interactivechat.update")) {
            sender.sendMessage(ChatColor.AQUA + "[InteractiveChat] InteractiveChat written by LOOHP!");
            sender.sendMessage(ChatColor.GOLD + "[InteractiveChat] You are running InteractiveChat version: " + InteractiveChat.plugin.getDescription().getVersion());
            Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
                UpdaterResponse version = Updater.checkUpdate();
                if (version.getResult().equals("latest")) {
                    if (version.isDevBuildLatest()) {
                        sender.sendMessage(ChatColor.GREEN + "[InteractiveChat] You are running the latest version!");
                    } else {
                        Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId(), true);
                    }
                } else {
                    Updater.sendUpdateMessage(sender, version.getResult(), version.getSpigotPluginId());
                }
            });
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("mentiontoggle")) {
        if (sender.hasPermission("interactivechat.mention.toggle")) {
            if (args.length == 1) {
                if (sender instanceof Player) {
                    Player player = (Player) sender;
                    PlayerData pd = InteractiveChat.playerDataManager.getPlayerData(player);
                    if (pd.isMentionDisabled()) {
                        pd.setMentionDisabled(false);
                        Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                        sender.sendMessage(InteractiveChat.mentionEnable);
                    } else {
                        pd.setMentionDisabled(true);
                        Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                        sender.sendMessage(InteractiveChat.mentionDisable);
                    }
                    if (InteractiveChat.bungeecordMode) {
                        try {
                            BungeeMessageSender.signalPlayerDataReload(System.currentTimeMillis(), player.getUniqueId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    sender.sendMessage(InteractiveChat.noConsoleMessage);
                }
            } else {
                if (sender.hasPermission("interactivechat.mention.toggle.others")) {
                    Player player = Bukkit.getPlayer(args[1]);
                    if (player != null) {
                        PlayerData pd = InteractiveChat.playerDataManager.getPlayerData(player);
                        if (pd.isMentionDisabled()) {
                            pd.setMentionDisabled(false);
                            Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                            sender.sendMessage(InteractiveChat.mentionEnable);
                        } else {
                            pd.setMentionDisabled(true);
                            Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                            sender.sendMessage(InteractiveChat.mentionDisable);
                        }
                        if (InteractiveChat.bungeecordMode) {
                            try {
                                BungeeMessageSender.signalPlayerDataReload(System.currentTimeMillis(), player.getUniqueId());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        sender.sendMessage(InteractiveChat.invalidPlayerMessage);
                    }
                } else {
                    sender.sendMessage(InteractiveChat.noPermissionMessage);
                }
            }
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("setinvdisplaylayout")) {
        if (sender.hasPermission("interactivechat.module.inventory.setlayout")) {
            try {
                if (args.length == 1) {
                    sender.sendMessage(InteractiveChat.notEnoughArgs);
                } else if (args.length == 2) {
                    if (sender instanceof Player) {
                        int layout = Integer.parseInt(args[1]);
                        if (!InventoryDisplay.LAYOUTS.contains(layout)) {
                            throw new NumberFormatException();
                        }
                        Player player = (Player) sender;
                        PlayerData pd = InteractiveChat.playerDataManager.getPlayerData(player);
                        pd.setInventoryDisplayLayout(layout);
                        Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                        sender.sendMessage(InteractiveChat.setInvDisplayLayout.replace("{Layout}", layout + ""));
                        if (InteractiveChat.bungeecordMode) {
                            try {
                                BungeeMessageSender.signalPlayerDataReload(System.currentTimeMillis(), player.getUniqueId());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        sender.sendMessage(InteractiveChat.noConsoleMessage);
                    }
                } else {
                    if (sender.hasPermission("interactivechat.module.inventory.setlayout.others")) {
                        Player player = Bukkit.getPlayer(args[2]);
                        if (player != null) {
                            int layout = Integer.parseInt(args[1]);
                            if (!InventoryDisplay.LAYOUTS.contains(layout)) {
                                throw new NumberFormatException();
                            }
                            PlayerData pd = InteractiveChat.playerDataManager.getPlayerData(player);
                            pd.setInventoryDisplayLayout(layout);
                            Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> pd.save());
                            sender.sendMessage(InteractiveChat.setInvDisplayLayout.replace("{Layout}", layout + ""));
                            if (InteractiveChat.bungeecordMode) {
                                try {
                                    BungeeMessageSender.signalPlayerDataReload(System.currentTimeMillis(), player.getUniqueId());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            sender.sendMessage(InteractiveChat.invalidPlayerMessage);
                        }
                    } else {
                        sender.sendMessage(InteractiveChat.noPermissionMessage);
                    }
                }
            } catch (NumberFormatException e) {
                sender.sendMessage(InteractiveChat.invalidArgs);
            }
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("list")) {
        try {
            if (sender.hasPermission("interactivechat.list")) {
                int start = 0;
                int end = InteractiveChat.placeholderList.size();
                if (args.length > 1) {
                    start = Integer.parseInt(args[1]) - 1;
                    if (start < 0) {
                        start = 0;
                    }
                }
                if (args.length > 2) {
                    end = Integer.parseInt(args[2]);
                    if (end < 0) {
                        end = InteractiveChat.placeholderList.size();
                    }
                }
                InteractiveChatAPI.sendMessageUnprocessed(sender, LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.listPlaceholderHeader));
                String body = InteractiveChat.listPlaceholderBody;
                List<Component> items = new ArrayList<>();
                if (sender.hasPermission("interactivechat.list.all")) {
                    int i = 0;
                    for (ICPlaceholder placeholder : InteractiveChat.placeholderList.values()) {
                        i++;
                        String text = body.replace("{Order}", i + "").replace("{Keyword}", placeholder.getName()).replace("{Description}", placeholder.getDescription());
                        items.add(LegacyComponentSerializer.legacySection().deserialize(text));
                    }
                } else {
                    int i = 0;
                    for (ICPlaceholder placeholder : InteractiveChat.placeholderList.values()) {
                        if ((placeholder.isBuildIn() && sender.hasPermission(placeholder.getPermission())) || (!placeholder.isBuildIn() && (sender.hasPermission(placeholder.getPermission()) || !InteractiveChat.useCustomPlaceholderPermissions))) {
                            i++;
                            String text = body.replace("{Order}", i + "").replace("{Keyword}", placeholder.getName()).replace("{Description}", placeholder.getDescription());
                            items.add(LegacyComponentSerializer.legacySection().deserialize(text));
                        }
                    }
                }
                for (int i = start; i < end && i < items.size(); i++) {
                    InteractiveChatAPI.sendMessageUnprocessed(sender, items.get(i));
                }
            } else {
                sender.sendMessage(InteractiveChat.noPermissionMessage);
            }
        } catch (Exception e) {
            sender.sendMessage(InteractiveChat.invalidArgs);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("parse")) {
        if (sender.hasPermission("interactivechat.parse")) {
            if (sender instanceof Player) {
                String str = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
                Player player = (Player) sender;
                Optional<ICPlayer> icplayer = Optional.of(ICPlayerFactory.getICPlayer(player));
                Bukkit.getScheduler().runTaskAsynchronously(InteractiveChat.plugin, () -> {
                    String text = str;
                    try {
                        long unix = System.currentTimeMillis();
                        if (InteractiveChat.chatAltColorCode.isPresent() && player.hasPermission("interactivechat.chatcolor.translate")) {
                            text = ChatColorUtils.translateAlternateColorCodes(InteractiveChat.chatAltColorCode.get(), str);
                        }
                        Component component = Component.text(text);
                        if (InteractiveChat.usePlayerName) {
                            component = PlayernameDisplay.process(component, icplayer, player, unix);
                        }
                        if (InteractiveChat.useItem) {
                            component = ItemDisplay.process(component, icplayer, player, unix);
                        }
                        if (InteractiveChat.useInventory) {
                            component = InventoryDisplay.process(component, icplayer, player, unix);
                        }
                        if (InteractiveChat.useEnder) {
                            component = EnderchestDisplay.process(component, icplayer, player, unix);
                        }
                        component = CustomPlaceholderDisplay.process(component, icplayer, player, InteractiveChat.placeholderList.values(), unix);
                        if (InteractiveChat.clickableCommands) {
                            component = CommandsDisplay.process(component);
                        }
                        if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_16) && InteractiveChat.fontTags) {
                            if (PlayerUtils.hasPermission(player.getUniqueId(), "interactivechat.customfont.translate", true, 250)) {
                                component = ComponentFont.parseFont(component);
                            }
                        }
                        String json = InteractiveChatComponentSerializer.gson().serialize(component);
                        if (json.length() > InteractiveChat.packetStringMaxLength) {
                            InteractiveChatAPI.sendMessageUnprocessed(sender, Component.text(text));
                        } else {
                            InteractiveChatAPI.sendMessageUnprocessed(sender, component);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            } else {
                sender.sendMessage(String.join(" ", Arrays.copyOfRange(args, 1, args.length)));
            }
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (args[0].equalsIgnoreCase("chat")) {
        if (sender.hasPermission("interactivechat.chat")) {
            if (args.length > 1) {
                if (sender instanceof Player) {
                    String message = String.join(" ", Arrays.copyOfRange(args, 1, args.length));
                    PacketContainer packet = InteractiveChat.protocolManager.createPacket(PacketType.Play.Client.CHAT);
                    packet.getStrings().write(0, message);
                    try {
                        InteractiveChat.protocolManager.recieveClientPacket((Player) sender, packet);
                    } catch (IllegalAccessException | InvocationTargetException e) {
                        e.printStackTrace();
                    }
                } else {
                    sender.sendMessage(InteractiveChat.noConsoleMessage);
                }
            }
        } else {
            sender.sendMessage(InteractiveChat.noPermissionMessage);
        }
        return true;
    }
    if (sender instanceof Player && args.length > 1) {
        Player player = (Player) sender;
        switch(args[0].toLowerCase()) {
            case "viewinv":
                {
                    PlayerData data = InteractiveChat.playerDataManager.getPlayerData(player);
                    String hash = args[1];
                    if (data == null || data.getInventoryDisplayLayout() == 0) {
                        Inventory inv = InteractiveChat.inventoryDisplay.get(hash);
                        if (inv != null) {
                            Bukkit.getScheduler().runTask(InteractiveChat.plugin, () -> player.openInventory(inv));
                        } else {
                            player.sendMessage(PlaceholderAPI.setPlaceholders(player, InteractiveChat.invExpiredMessage));
                        }
                    } else {
                        Inventory inv = InteractiveChat.inventoryDisplay1Upper.get(hash);
                        Inventory inv2 = InteractiveChat.inventoryDisplay1Lower.get(hash);
                        if (inv != null && inv2 != null) {
                            Bukkit.getScheduler().runTask(InteractiveChat.plugin, () -> {
                                player.openInventory(inv);
                                InventoryUtils.sendFakePlayerInventory(player, inv2, true, false);
                                InteractiveChat.viewingInv1.put(player.getUniqueId(), hash);
                            });
                        } else {
                            player.sendMessage(PlaceholderAPI.setPlaceholders(player, InteractiveChat.invExpiredMessage));
                        }
                    }
                    break;
                }
            case "viewender":
                {
                    Inventory inv = InteractiveChat.enderDisplay.get(args[1]);
                    if (inv != null) {
                        Bukkit.getScheduler().runTask(InteractiveChat.plugin, () -> player.openInventory(inv));
                    } else {
                        player.sendMessage(PlaceholderAPI.setPlaceholders(player, InteractiveChat.invExpiredMessage));
                    }
                    break;
                }
            case "viewitem":
                {
                    Inventory inv = InteractiveChat.itemDisplay.get(args[1]);
                    if (inv != null) {
                        Bukkit.getScheduler().runTask(InteractiveChat.plugin, () -> player.openInventory(inv));
                    } else {
                        player.sendMessage(PlaceholderAPI.setPlaceholders(player, InteractiveChat.invExpiredMessage));
                    }
                    break;
                }
            case "viewmap":
                ItemStack map = InteractiveChat.mapDisplay.get(args[1]);
                if (map != null) {
                    Bukkit.getScheduler().runTask(InteractiveChat.plugin, () -> MapViewer.showMap(player, map));
                } else {
                    player.sendMessage(PlaceholderAPI.setPlaceholders(player, InteractiveChat.invExpiredMessage));
                }
                break;
        }
        return true;
    }
    sender.sendMessage(ChatColorUtils.translateAlternateColorCodes('&', Bukkit.spigot().getConfig().getString("messages.unknown-command")));
    return true;
}
Also used : ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) Player(org.bukkit.entity.Player) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) PacketContainer(com.comphenix.protocol.events.PacketContainer) ArrayList(java.util.ArrayList) UpdaterResponse(com.loohp.interactivechat.updater.Updater.UpdaterResponse) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ICPlaceholder(com.loohp.interactivechat.objectholders.ICPlaceholder) Component(net.kyori.adventure.text.Component) ItemStack(org.bukkit.inventory.ItemStack) PlayerData(com.loohp.interactivechat.data.PlayerDataManager.PlayerData) Inventory(org.bukkit.inventory.Inventory)

Aggregations

PacketContainer (com.comphenix.protocol.events.PacketContainer)1 PlayerData (com.loohp.interactivechat.data.PlayerDataManager.PlayerData)1 ICPlaceholder (com.loohp.interactivechat.objectholders.ICPlaceholder)1 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)1 UpdaterResponse (com.loohp.interactivechat.updater.Updater.UpdaterResponse)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Component (net.kyori.adventure.text.Component)1 Player (org.bukkit.entity.Player)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1