Search in sources :

Example 51 with ChatColor

use of org.bukkit.ChatColor in project Glowstone by GlowstoneMC.

the class TextMessage method convert.

/**
 * Convert from an old-style to a new-style chat message.
 *
 * @param text The The text of the message.
 * @return The converted JSON structure.
 */
@SuppressWarnings("unchecked")
private static JSONObject convert(String text) {
    // state
    List<JSONObject> items = new LinkedList<>();
    Set<ChatColor> formatting = EnumSet.noneOf(ChatColor.class);
    StringBuilder current = new StringBuilder();
    ChatColor color = null;
    // work way through text, converting colors
    for (int i = 0; i < text.length(); ++i) {
        char ch = text.charAt(i);
        if (ch != ChatColor.COLOR_CHAR) {
            // no special handling
            current.append(ch);
            continue;
        }
        if (i == text.length() - 1) {
            // ignore color character at end
            continue;
        }
        // handle colors
        append(items, current, color, formatting);
        ChatColor code = ChatColor.getByChar(text.charAt(++i));
        if (code == ChatColor.RESET) {
            color = null;
            formatting.clear();
        } else if (code.isFormat()) {
            formatting.add(code);
        } else {
            color = code;
            formatting.clear();
        }
    }
    append(items, current, color, formatting);
    // convert list of items into structure
    if (items.isEmpty()) {
        // no items, return a blank message
        JSONObject object = new JSONObject();
        object.put("text", "");
        return object;
    } else if (items.size() == 1) {
        // only one item, return it as-is
        return items.get(0);
    } else {
        JSONObject object = items.get(0);
        if (object.size() == 1) {
            // only contains "text", no formatting, can reuse
            object.put("extra", items.subList(1, items.size()));
        } else {
            // must put everything in the "extra" list
            object = new JSONObject();
            object.put("text", "");
            object.put("extra", items);
        }
        return object;
    }
}
Also used : JSONObject(org.json.simple.JSONObject) ChatColor(org.bukkit.ChatColor) LinkedList(java.util.LinkedList)

Example 52 with ChatColor

use of org.bukkit.ChatColor in project BKCommonLib by bergerhealer.

the class ChatText method setMessage.

/**
 * Sets the chat text using a legacy format-encoded String
 *
 * @param messageText to set to
 */
public final void setMessage(String messageText) {
    // Optimization for empty strings
    if (messageText.isEmpty()) {
        handle = IChatBaseComponentHandle.ChatSerializerHandle.empty();
        return;
    }
    // Use CraftBukkit util's method of parsing
    handle = CraftChatMessageHandle.fromString(messageText, true)[0];
    // Find trailing color formatting characters in the message that would otherwise be dropped
    ArrayList<ChatColor> trailing_formatting_chars = new ArrayList<ChatColor>(0);
    for (int i = messageText.length() - 2; i >= 0; i -= 2) {
        if (messageText.charAt(i) == StringUtil.CHAT_STYLE_CHAR) {
            ChatColor color = ChatColor.getByChar(messageText.charAt(i + 1));
            if (color != null) {
                trailing_formatting_chars.add(0, color);
                continue;
            }
        }
        break;
    }
    if (!trailing_formatting_chars.isEmpty()) {
        handle.addSibling(IChatBaseComponentHandle.ChatSerializerHandle.modifiersToComponent(trailing_formatting_chars));
    }
}
Also used : ArrayList(java.util.ArrayList) ChatColor(org.bukkit.ChatColor)

Example 53 with ChatColor

use of org.bukkit.ChatColor in project MyPet by xXKeyleXx.

the class PetShop method open.

public void open(final Player player) {
    if (!MyPetApi.getHookHelper().isEconomyEnabled()) {
        player.sendMessage(Translation.getString("Message.No.Economy", player));
        return;
    }
    VaultHook economyHook = (VaultHook) MyPetApi.getHookHelper().getEconomy();
    IconMenu shop = new IconMenu(Colorizer.setColors(displayName), event -> {
        if (pets.containsKey(event.getPosition())) {
            final ShopMyPet pet = pets.get(event.getPosition());
            if (pet != null) {
                final Player p = event.getPlayer();
                final MyPetPlayer owner;
                if (MyPetApi.getPlayerManager().isMyPetPlayer(p)) {
                    owner = MyPetApi.getPlayerManager().getMyPetPlayer(player);
                    if (owner.hasMyPet() && !Permissions.has(owner, "MyPet.shop.storage")) {
                        p.sendMessage(Translation.getString("Message.Command.Trade.Receiver.HasPet", player));
                        return;
                    }
                } else {
                    owner = null;
                }
                final BukkitRunnable confirmRunner = new BukkitRunnable() {

                    @Override
                    public void run() {
                        IconMenu menu = new IconMenu(Util.formatText(Translation.getString("Message.Shop.Confirm.Title", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice())), event1 -> {
                            if (event1.getPosition() == 3) {
                                if (pet.getPrice() > 0) {
                                    if (economyHook.canPay(p.getUniqueId(), pet.getPrice())) {
                                        if (economyHook.getEconomy().withdrawPlayer(p, pet.getPrice()).transactionSuccess()) {
                                            switch(wallet) {
                                                case Bank:
                                                    economyHook.getEconomy().bankDeposit(walletOwner, pet.getPrice());
                                                    break;
                                                case Player:
                                                    economyHook.getEconomy().depositPlayer(Bukkit.getOfflinePlayer(UUID.fromString(walletOwner)), pet.getPrice());
                                                case Private:
                                                    depositPrivate(pet.getPrice());
                                                    break;
                                            }
                                        } else {
                                            p.sendMessage(Translation.getString("Message.No.Money", player));
                                            return;
                                        }
                                    } else {
                                        p.sendMessage(Translation.getString("Message.Shop.NoMoney", player));
                                        return;
                                    }
                                }
                                final MyPetPlayer petOwner;
                                if (owner == null) {
                                    petOwner = MyPetApi.getPlayerManager().registerMyPetPlayer(player);
                                } else {
                                    petOwner = owner;
                                }
                                pet.setOwner(petOwner);
                                final StoredMyPet clonedPet = MyPetApi.getMyPetManager().getInactiveMyPetFromMyPet(pet);
                                clonedPet.setOwner(petOwner);
                                clonedPet.setWorldGroup(WorldGroup.getGroupByWorld(player.getWorld().getName()).getName());
                                clonedPet.setUUID(null);
                                MyPetApi.getRepository().addMyPet(clonedPet, new RepositoryCallback<Boolean>() {

                                    @Override
                                    public void callback(Boolean value) {
                                        p.sendMessage(Util.formatText(Translation.getString("Message.Shop.Success", player), clonedPet.getPetName(), economyHook.getEconomy().format(pet.getPrice())));
                                        MyPetCreateEvent createEvent = new MyPetCreateEvent(clonedPet, MyPetCreateEvent.Source.PetShop);
                                        Bukkit.getServer().getPluginManager().callEvent(createEvent);
                                        if (petOwner.hasMyPet()) {
                                            p.sendMessage(Util.formatText(Translation.getString("Message.Shop.SuccessStorage", player), clonedPet.getPetName()));
                                        } else {
                                            petOwner.setMyPetForWorldGroup(WorldGroup.getGroupByWorld(player.getWorld().getName()), clonedPet.getUUID());
                                            MyPetApi.getRepository().updateMyPetPlayer(petOwner, null);
                                            MyPet activePet = MyPetApi.getMyPetManager().activateMyPet(clonedPet).get();
                                            activePet.createEntity();
                                        }
                                    }
                                });
                            }
                            event1.setWillClose(true);
                            event1.setWillDestroy(true);
                        }, MyPetApi.getPlugin());
                        IconMenuItem icon = new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "LIME_WOOL")).setData(5).setTitle(ChatColor.GREEN + Translation.getString("Name.Yes", player)).setLore(ChatColor.RESET + Util.formatText(Translation.getString("Message.Shop.Confirm.Yes", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice())));
                        if (owner != null && owner.hasMyPet()) {
                            icon.addLoreLine("").addLoreLine(Translation.getString("Message.Shop.Confirm.SendStorage", player));
                        }
                        menu.setOption(3, icon);
                        menu.setOption(5, new IconMenuItem().setMaterial(EnumSelector.find(Material.class, "WOOL", "RED_WOOL")).setData(14).setTitle(ChatColor.RED + Translation.getString("Name.No", player)).setLore(ChatColor.RESET + Util.formatText(Translation.getString("Message.Shop.Confirm.No", player), pet.getPetName(), economyHook.getEconomy().format(pet.getPrice()))));
                        menu.open(player);
                    }
                };
                if (owner != null && owner.hasMyPet()) {
                    MyPetApi.getRepository().getMyPets(owner, new RepositoryCallback<List<StoredMyPet>>() {

                        @Override
                        public void callback(List<StoredMyPet> value) {
                            int petCount = getInactivePetCount(value, WorldGroup.getGroupByWorld(player.getWorld().getName()).getName()) - 1;
                            int limit = getMaxPetCount(p);
                            if (petCount >= limit) {
                                p.sendMessage(Util.formatText(Translation.getString("Message.Command.Switch.Limit", player), limit));
                                return;
                            }
                            confirmRunner.runTaskLater(MyPetApi.getPlugin(), 5L);
                        }
                    });
                } else {
                    confirmRunner.runTaskLater(MyPetApi.getPlugin(), 5L);
                }
            }
        }
    }, MyPetApi.getPlugin());
    double balance = economyHook.getBalance(player);
    for (int pos : pets.keySet()) {
        ShopMyPet pet = pets.get(pos);
        IconMenuItem icon = pet.getIcon();
        ChatColor canPay = balance >= pet.getPrice() ? ChatColor.GREEN : ChatColor.RED;
        icon.addLoreLine(ChatColor.RESET + "" + ChatColor.BLUE + Translation.getString("Name.Price", player) + ": " + canPay + economyHook.getEconomy().format(pet.getPrice()), 0);
        shop.setOption(pos, icon);
    }
    shop.open(player);
}
Also used : Player(org.bukkit.entity.Player) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) IconMenu(de.Keyle.MyPet.api.gui.IconMenu) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPet(de.Keyle.MyPet.api.entity.MyPet) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Material(org.bukkit.Material) VaultHook(de.Keyle.MyPet.util.hooks.VaultHook) MyPetCreateEvent(de.Keyle.MyPet.api.event.MyPetCreateEvent) IconMenuItem(de.Keyle.MyPet.api.gui.IconMenuItem) ChatColor(org.bukkit.ChatColor) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet)

Example 54 with ChatColor

use of org.bukkit.ChatColor in project MyMaid2 by jaoafa.

the class Cmd_Chat method onCommand.

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    if (args.length >= 1 && args[0].equalsIgnoreCase("help")) {
        SendUsageMessage(sender, cmd);
        return true;
    }
    if (args.length >= 2) {
        if (Bukkit.getPlayerExact(args[0]) != null) {
            SendMessage(sender, cmd, "オンラインユーザーを話者に指定できません。");
            return true;
        }
        ChatColor color = ChatColor.GRAY;
        Boolean chatcolor = true;
        if (args[args.length - 1].equalsIgnoreCase("color:" + "AQUA")) {
            color = ChatColor.AQUA;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "BLACK")) {
            color = ChatColor.BLACK;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "BLUE")) {
            color = ChatColor.BLUE;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_AQUA")) {
            color = ChatColor.DARK_AQUA;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_BLUE")) {
            color = ChatColor.DARK_BLUE;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_GRAY")) {
            color = ChatColor.DARK_GRAY;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_GREEN")) {
            color = ChatColor.DARK_GREEN;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_PURPLE")) {
            color = ChatColor.DARK_PURPLE;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "DARK_RED")) {
            color = ChatColor.DARK_RED;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "GOLD")) {
            color = ChatColor.GOLD;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "GREEN")) {
            color = ChatColor.GREEN;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "LIGHT_PURPLE")) {
            color = ChatColor.LIGHT_PURPLE;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "RED")) {
            color = ChatColor.RED;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "WHITE")) {
            color = ChatColor.WHITE;
        } else if (args[args.length - 1].equalsIgnoreCase("color:" + "YELLOW")) {
            color = ChatColor.YELLOW;
        } else {
            chatcolor = false;
        }
        String text = "";
        int c = 1;
        while (args.length > c) {
            if ((args.length - 1) == c && chatcolor) {
                break;
            }
            text += args[c] + " ";
            c++;
        }
        text = ChatColor.translateAlternateColorCodes('&', text);
        if (args[0].equalsIgnoreCase("jaotan")) {
            color = ChatColor.GOLD;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Bukkit.broadcastMessage(ChatColor.GRAY + "[" + sdf.format(new Date()) + "]" + color + "■" + ChatColor.WHITE + args[0] + ": " + text);
        DiscordSend("**" + args[0] + "**: " + text);
        return true;
    } else {
        SendUsageMessage(sender, cmd);
        return true;
    }
}
Also used : ChatColor(org.bukkit.ChatColor) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 55 with ChatColor

use of org.bukkit.ChatColor in project MyMaid2 by jaoafa.

the class SKKColors method Save.

/**
 * ログインメッセージをセーブします。
 * @return 完了したかどうか
 * @author mine_book000
 */
public static boolean Save() {
    FileConfiguration data = YamlConfiguration.loadConfiguration(file);
    data.set("JoinMessageList", MessageList);
    data.set("LastText", LastText);
    Map<String, String> color = new HashMap<>();
    for (Entry<String, ChatColor> c : Cmd_Color.color.entrySet()) {
        color.put(c.getKey(), c.getValue().name());
    }
    data.set("Color", color);
    try {
        data.save(file);
        return true;
    } catch (IOException e) {
        BugReporter(e);
        return false;
    }
}
Also used : FileConfiguration(org.bukkit.configuration.file.FileConfiguration) HashMap(java.util.HashMap) IOException(java.io.IOException) ChatColor(org.bukkit.ChatColor)

Aggregations

ChatColor (org.bukkit.ChatColor)56 ArrayList (java.util.ArrayList)8 Player (org.bukkit.entity.Player)8 JSONObject (org.json.simple.JSONObject)6 IOException (java.io.IOException)4 SimpleDateFormat (java.text.SimpleDateFormat)3 Mage (com.elmakers.mine.bukkit.api.magic.Mage)2 PlayerVoteData (com.jaoafa.MyMaid2.Lib.PlayerVoteData)2 MassiveList (com.massivecraft.massivecore.collections.MassiveList)2 Method (java.lang.reflect.Method)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 MaxMoneyException (net.ess3.api.MaxMoneyException)2 DyeColor (org.bukkit.DyeColor)2 Color (pl.themolka.arcade.util.Color)2 FancyMessage (br.net.fabiozumbi12.RedProtect.Bukkit.Fanciful.FancyMessage)1 RPGui (br.net.fabiozumbi12.RedProtect.Bukkit.RPGui)1 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)1