[LIB] Fanciful: pleasant chat message formatting

Discussion in 'Resources' started by mkremins, Nov 15, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    mkremins

    Could you post your pom.xml? It sounds like you may be missing the section that instructs Maven to copy ("shade") the Fanciful classes into your compiled plugin jar, which is the most likely cause for the exception you're seeing.
     
    Garris0n likes this.
  2. Offline

    JasonDL13

    I didn't feel like spamming the forum, so I put it on pastebin: http://pastebin.com/PgYN5v7A
     
  3. Offline

    Jozeth

    Quick question:
    Will I have to set the join message to nothing and then add my own 'FanyMessage' to replace it, or is there a way to add it into .setJoinMessage?
     
  4. How do you do newLines? \n comes up with some weird character
     
  5. Offline

    ImDeJay

    mkremins
    I am trying to convert this to a json string then send it to the player using sendrawmessage which i thought would work but it doesnt. Is there a way for this to work, or do i have to use .send(Player player)?

    my code:
    Code:java
    1. String blah = new FancyMessage("This is a test")
    2. .color(ChatColor.AQUA)
    3. .style(ChatColor.BOLD)
    4. .tooltip("This is a hover message...")
    5. .toJSONString();
    6.  
    7. player.sendRawMessage(blah);


    my result:
    [​IMG]

    If i use .send(Player player) everything works fine but i would like to do some stuff using strings instead of sending directly to the player.

    Code:java
    1. new FancyMessage("This is a test")
    2. .color(ChatColor.AQUA)
    3. .style(ChatColor.BOLD)
    4. .tooltip("This is a hover message...")
    5. .send(player);[/code]
     
  6. Offline

    JasonDL13

    Try using setJoinMessage(null) and Bukkit.broadcastMessage the new join message for use a for-while loop of all the players and use p.sendMessage(joinMessage);
     
  7. mkremins fyi: your maven repo on github isnt working :*(
     
  8. Offline

    minoneer

    Great Library - works like a charm and is so simple to use!

    I'm having a problem though - when playing around with it, I created a rather long message (about 5k letters plus formatting). When sending it to a player, it disconnects the client with an internal exception (but no errors msg on client or server terminal). When sending it as "oldMessageFormat" to the ConsoleSender, it times out and kicks all connected clients. Any Ideas why this might be? Is there some kind of limit for the message size? If so, any ideas how high it might be? The same Message works fine with a less chars.

    Regards, minoneer
     
  9. Offline

    Garris0n

    minoneer Well, it's apparently limited to 32767 bytes, but that's the only limit I'm aware of.

    Either way, what possible justification do you have for sending the client five thousand characters?
     
  10. Offline

    xTrollxDudex

    minoneer
    Umm, you'll need to send multiple chat messages for that, a chat line holds 3 total cells in the chat, and it has a limited amount of chars able to be in it.
     
  11. Anyone know why this doesn't work at all? I can't seem to figure it out :(

    Code:java
    1. public void formatMessage(Player player, String message){
    2. Settlement s = SettlementManager.getManager().getPlayerSettlement(player.getUniqueId());
    3. if (s != null){
    4. new FancyMessage("[")
    5. .color(ChatColor.DARK_GRAY)
    6. .then(s.getName())
    7. .tooltip(s.getDescription())
    8. .color(ChatColor.AQUA)
    9. .then("]")
    10. .color(ChatColor.DARK_GRAY)
    11. .then(player.getDisplayName() + ": ")
    12. .then(message)
    13. .send(player);
    14. }
    15. }
     
  12. Offline

    Garris0n

    >Are you calling the method?
    >Is 's' null?
     
  13. Garris0n
    I know for a fact it isn't null and yes I'm calling the method :p It's not formatting the message for some reason :(

    Edit: Nvm, it decided to work now. It wasn't working before even though none of the code changed... odd.
     
  14. Offline

    Garris0n

    Export problems perhaps?
     
  15. Garris0n
    Possibly. Not sure though. Oh the mysteries of programming...
     
  16. Offline

    Garris0n

    Yes, you have to use the send() method or implement the packet-sending on your own.

    What exactly do you want to do to the string that you can't do to the FancyMessage?
     
  17. Offline

    maved145

    Garris0n mkremins

    Ok so hears my error:

    http://pastebin.com/783esBWG

    and here my code:

    Code:java
    1. package me.Maved145.PixelmonInfomation;
    2.  
    3. import static org.bukkit.ChatColor.*;
    4.  
    5. import java.util.ArrayList;
    6.  
    7. import java.util.List;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.EventHandler;
    12. import org.bukkit.event.Listener;
    13. import org.bukkit.event.player.AsyncPlayerChatEvent;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. import mkremins.fanciful.FancyMessage;
    18.  
    19. public class main extends JavaPlugin implements Listener
    20. {
    21.  
    22. List<String> names = new ArrayList<String>();
    23.  
    24. public void onEnable()
    25. {
    26. names.add("Jamie");
    27. names.add("Harry");
    28. PluginManager pm = getServer().getPluginManager();
    29. pm.registerEvents(this, this);
    30. }
    31.  
    32. public void onDisable()
    33. {
    34.  
    35. }
    36.  
    37. static String advertisement()
    38. {
    39. return new FancyMessage("Visit ").color(GREEN).then("our website").color(YELLOW).style(UNDERLINE).link("[url]http://awesome-server.net[/url]").tooltip("AwesomeServer Forums").then(" to win ").color(GREEN).then("big prizes!").color(AQUA).style(BOLD).tooltip("Terms and conditions may apply. Offer not valid in Sweden.").toJSONString();
    40. }
    41.  
    42. @EventHandler
    43. public void onChat(AsyncPlayerChatEvent e)
    44. {
    45. // Player player = e.getPlayer();
    46. String message = e.getMessage();
    47.  
    48. for (String str : names)
    49. {
    50. if (message.contains(str))
    51. {
    52. e.setCancelled(true);
    53. // String name = str;
    54. for (Player players : Bukkit.getOnlinePlayers())
    55. {
    56.  
    57. players.sendMessage(advertisement());
    58.  
    59. }
    60. }
    61.  
    62. }
    63.  
    64. }
    65. }
    66.  


    I have no clue whats wrong if somebody can help me.
     
  18. Offline

    ImDeJay


    I was wanting to take the .getformat in playerchatevent and have it so when you hover over a players name it displayed information about the player, but i then realised that this wasnt possible.
     
  19. Offline

    Garris0n

    First of all, read this and fix everything that's named incorrectly.

    Second, have you shaded the library into your jar file?
     
  20. Offline

    maved145

    Garris0n
    Forget it, i fixed it. Its a shame you have to use .send. I’m trying to replace one word that someone is saying in chat so it becomes colour and has a tooltip. But i can’t find a way to do it.
     
  21. Offline

    quentin27230

    How to put a JSON message in a Stringuilder ? Because when i do this code :

    Code:java
    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String name, String[] args) {
    3. if(name.equals("kits")){
    4. if(sender instanceof Player){
    5. Player player = (Player)sender;
    6. if(player.getWorld().getName().equals("feastvoid")){
    7. StringBuilder Haskit = new StringBuilder();
    8. StringBuilder Nokit = new StringBuilder();
    9. FancyMessage fm = new FancyMessage();
    10.  
    11. if(player.hasPermission(Permissions.cannibal)){
    12. Haskit.append(fm.then("Cannibal, ").tooltip("Test JSON msg").toJSONString());
    13. } else {
    14. Nokit.append("Cannibal, ");
    15. }
    16.  
    17. if(Haskit.toString().endsWith(", ")){
    18. player.sendRawMessage("§bChoisis ton kit ! §7 /kit [kitname]");
    19. player.sendRawMessage("§aVos kits :§f " + Haskit.toString().substring(0, Haskit.length()));
    20. } else {
    21. player.sendRawMessage("§bChoisi ton kit ! §7 /kit [kitname]");
    22. player.sendRawMessage("§aVos kits :§f " + Haskit.toString());
    23. }
    24. if(Nokit.toString().endsWith(", ")){
    25. player.sendRawMessage("§cAutres kits :§f " + Nokit.toString().substring(0, Nokit.length()));
    26. } else {
    27. player.sendRawMessage("§cAutres kits :§f " + Nokit.toString());
    28. }
    29. } else {
    30. player.sendMessage("§cCette commande est désactivée dans ce monde !");
    31. return false;
    32. }
    33. } else {
    34. sender.sendMessage("§cSeulement les joueurs peuvent faire cette commande !");
    35. return false;
    36. }
    37. }
    38. return false;
    39. }
    40.  


    I receive a message in a TellRaw format in the StringBuilder :/
     
  22. I came across something strange; craftbukkit.jar kept downloading as it was apparently a Maven dependency but I never set it. I decided to look into my console on building and it came from Fanciful. I had to exclude bukkit and craftbukkit in my pom.xml to avoid building jars with 160,000 KB.

    Perhaps add the "provided" scope to the Fanciful's pom.xml? Not a huge issue but it did cause some inconveniences.
     
  23. Offline

    mkremins

    Thanks for the heads up, just qualified both Bukkit and CraftBukkit dependencies with scope=provided in the POM. Is the default behavior now correct with the latest snapshot build?
     
  24. mkremins
    Yup, it's all good now. Thanks!
     
  25. Offline

    $c0p3rZ'z

    mkremins
    Hi,
    How did you replace a string in a message to a JSON message ?
    Thanks !
     
  26. Offline

    mkremins

    Unfortunately, I don't think the (Async)PlayerChatEvent interface currently offers a way to replace an intercepted chat message (in whole or in part) with a /tellraw-format JSON message. If you really want to use Fanciful to format player chat messages, you might be able to simulate the desired behavior by cancelling an intercepted ChatEvent and broadcasting an equivalent FancyMessage to every player in event.recipients. However, this might break interoperability with other plugins that listen to chat.

    There's no easy way to go about this. Generally speaking, a message is either plain text alone or /tellraw-format JSON – you can't mix the two within a single broadcast. Instead of attempting to insert a FancyMessage into the middle of a StringBuilder, I'd instead use the FancyMessage.then() method to insert the desired text into the FancyMessage itself before and after the "body text".

    Alternatively, you could use FancyMessage.toOldMessageFormat()to convert a FancyMessage to a plain string containing only ChatColor-style formatting and color codes, but this will strip out things like tooltips and click event handlers attached to parts of the FancyMessage. This probably isn't the behavior you want.
     
  27. Offline

    $c0p3rZ'z

    I don't understand
    If I send the message + the json message, it do not replace the special string and the Json message it's at the end.
    So it's really not possible :( ?

    mkremins
     
  28. Offline

    valon750

    Now, in the main post it's stated that for best results to use Maven, however Maven as a whole tends to confuse me, therefore is there a workaround for those using just straight up Eclipse?
     
  29. Offline

    viper_monster

    valon750 You can download the source code from GitHub. :)
     
  30. When i first started doing java, and then making plugins for minecraft it confused me to.
    But it gives you allot of freedom when you try it.

    starting of with maven is the hardest part. Once setup you can copy it to other project and edit it fairly easy.
    so i will show you here my maven document and i hope this helps you!
    if you have any problems what so ever PM me.

    the only thing you need to add to make a maven file is to add a pom.xml.
    if you already started a project inside an IDE look online how to convert it!

    pom.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <artifactId>(name)</artifactId>                                         <!-- replace (name) with your project name -->
        
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  <!-- these are some basic settings you don't need to edit them-->
            <maven.compiler.source>1.7</maven.compiler.source>
            <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
        
        <build>                                                                                 <!-- we are saying here that we are going to do thing in the build phase -->
            <plugins>                                                                           <!-- we are going to use plugin's -->
                <plugin>                                                                        <!-- and that plugin is ... -->
                    <groupId>org.apache.maven.plugins</groupId>                                 <!-- a plugin from here -->
                    <artifactId>maven-shade-plugin</artifactId>                                 <!-- that is called maven-shade-plugin, This plugin allows us to shade fancifull inside our jar -->
                    <version>2.2</version>
                    <executions>                                                                <!-- these are the settings for the plugin -->
                        <execution>
                            <phase>package</phase>                              
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <finalName>(name)-${project.version}</finalName>                <!-- replace (name) with your project name again -->
                                <createDependencyReducedPom>false</createDependencyReducedPom>
                                <minimizeJar>true</minimizeJar>
                                <artifactSet>                                                   <!-- the thing that we are going to shade -->
                                    <includes>
                                        <include>mkremins:fanciful</include>
                                        <include>org.json:json</include>
                                    </includes>
                                </artifactSet>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
            <resources>                                                         <!-- here we are saying that there are other resources (the plugin.yml and (optional) the config.yml -->
                <resource>                                                      <!-- these resources are inside the jar! so you don't have to drag them in! -->
                    <targetPath>.</targetPath>
                    <filtering>true</filtering>
                    <directory>${basedir}/src/main/resources/</directory>       <!-- the place we have put theses resources -->
                    <includes>
                        <include>plugin.yml</include>
                        <include>config.yml</include>                           <!-- you can remove this if you don't have a config.yml -->
                    </includes>
                </resource>
            </resources>
        </build>
        
        <dependencies>                                                          <!-- our project needs the bukkit and fanciful library so we say that we depend on them -->
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>                                 <!-- when a new version comes out you just have to edit this number (note that development versions automaticly will update ... -->
                <version>1.7.10-R0.1-SNAPSHOT</version>                         <!-- ... to the lateset in the version branch due to the SNAPSHOT call -->
                <scope>provided</scope>                                         
            </dependency>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>craftbukkit</artifactId>
                <version>1.7.10-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>mkremins</groupId>                                     <!-- here we add fancifull -->
                <artifactId>fanciful</artifactId>
                <version>0.2.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
        
        <repositories>                                                          <!-- we have to say where we can get bukkit and fancifull -->
            <repository>
                <id>fanciful-mvn-repo</id>                                      
                <url>https://raw.github.com/mkremins/fanciful/mvn-repo/</url>
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>always</updatePolicy>
                </snapshots>
            </repository>
            <repository>
                <id>bukkit-repo</id>
                <url>http://repo.bukkit.org/content/groups/public/</url>
            </repository>
        </repositories>
    </project>
     
Thread Status:
Not open for further replies.

Share This Page