[LIB] Fanciful: pleasant chat message formatting

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

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

    Dragonphase


    Do you mind sharing what you did to resolve this? Because I have the same problem.
     
  2. Offline

    Aikar

    mkremins
    I resolved a few quirks in item tooltips:

    Code:
        public FancifulChatAPI itemTooltip(final ItemStack itemStack) {
            NBTTagCompound root = CraftItemStack.asNMSCopy(itemStack).save(new NBTTagCompound());
            if (root.hasKey("tag")) {
                NBTTagCompound tag = HiddenItemMeta.filterItemLore(root.getCompound("tag"), false);
                NBTTagCompound display = tag.getCompound("display");
                if (display.hasKey("Lore")) {
                    NBTTagList lore = display.getList("Lore", 8);
                    NBTTagList newlore = new NBTTagList();
    
                    for (int i = 0; i < lore.size(); i++) {
                        String line = lore.f(i);
                        String newline = (line.isEmpty() ? " " : line.replace("\"", "\\\""));
                        newlore.add(new NBTTagString(newline));
                    }
                    display.set("Lore", newlore);
                }
    
                root.set("tag", tag);
            }
            return itemTooltip(root.toString());
        }
    Ignore the HiddenItemMeta thats for my fork.

    But you also can not send empty lines to the client or it renders "" in the lore line instead of empty, so im changing them to space on the fly, and also encode the " " in lore (client shows invalid item)

    Then same fix below for makeMultilineTooltip:
    for (int i = 1; i < lines.length; i++) {
    json.value(lines.isEmpty() ? " " : lines.replace("\"", "\\\""));
    }
     
    mkremins and Garris0n like this.
  3. Offline

    mkremins

    Thanks a bunch, I'll try to get these merged into the main repo over the weekend :)
     
  4. Offline

    mkremins

    Merged @Aikar's escaping fixes for multiline tooltips into the main repository and deployed another release candidate build containing the fixes (0.1.3-RC2). If all goes well with the release candidate, I'll mark multiline tooltips as stable and release 0.1.3 proper.
     
  5. Offline

    tissin

    I don't understand -- Do you have to upload the json .jar and/or the fanciful .jar files into your /plugins/ folder?
     
  6. Offline

    Aikar

    mkremins
    Found more issues - this has the infamous 1.7 chat bugs when Spigot had the 1.7 compat protocol where text goes white on new line.

    To fix this, I ended up changing how this whole API works a bit, and ripped out the style/color api and went back to using old style codes, and went through CraftBukkits CraftChatMessage wrapper

    https://github.com/starlis/EMC-Craf...m/empireminecraft/emcapi/FancifulChatAPI.java

    This has resolved the issues for me so colors stay proper on new lines.
    Don't know if you want to keep the style/color API, just passing on how I resolved the issues. merge in how you please.
     
    mkremins likes this.
  7. Offline

    mkremins

    If you're using Maven to build your plugin (which I highly recommend, as it's the community standard and saves you a ton of time and energy once you do the necessary legwork to get it set up in the first place), then you don't need to worry about this stuff – you can just use the maven-shade-plugin to shade in Fanciful to your assembled plugin jar (thus getting the JSON jar packaged alongside as well "for free").

    If you're not using Maven, then you need to find another way to get the libraries onto the classpath. You can't presently "install" Fanciful by dragging the Fanciful jar into the plugins folder, because Fanciful isn't packaged as a standalone Bukkit plugin and thus won't be loaded as one by Bukkit. (I'm experimenting with providing a standalone plugin version of Fanciful for those who don't want to use Maven, but it's still largely untested – watch this space and it might become available in a week or so.)

    Thanks again, I'll take a look at this and see how I can make it compatible with the existing API.
     
  8. Offline

    tissin

    Wow, I didn't know that was possible with Maven!
     
  9. Offline

    Celtic Minstrel

    I don't think this is a good thing to add to this API. The attributive form that you've created should be the base API, while XML processing (and even colour code processing) should be built on top of that.

    My initial thoughts on the matter when I heard of the new chat formatting was that Bukkit should add an API using AttributedString (a built-in class in java.text), which is perfectly suited to this application.

    Misc comments on the API...

    public FancyMessage itemTooltip(final String itemJSON)
    - This doesn't really seem like something that should be public?

    public FancyMessage then(final Object obj)
    - Why "then" instead of "add"?

    public String toJSONString()
    - This doesn't seem particularly useful to me.

    I may have more to say once I have a more complete view of what's possible. I may even submit a pull request!
     
  10. Offline

    mkremins

    Yeah, the post you're quoting is from a while ago and I don't currently plan to build anything like that into Fanciful core – so far, most people seem to be happy with the stuff that's already here, and it's pretty easy to build other APIs on top of this one if you really want an XMLish frontend.

    Hadn't heard of AttributedString before now – thanks for mentioning that one :p I'm still toying with the idea of submitting a pull request for a simplified version of this API to Bukkit itself, on the off chance that they start accepting pull requests for features again, and building on top of AttributedString looks like it might be the way to go.

    This is public mostly as an artifact of earlier versions of the API. Prior to v0.1.2, item serialization to JSON wasn't baked into Fanciful core as it is now, so this method was provided (and made public) to allow people to serialize items externally and then pass serialized representations into the message builder directly.

    In my brain, then implies ordering (i.e. this message part is displayed after the part you just wrote) more strongly than add (which could ostensibly be order-agnostic). Unfortunately, this is pretty subjective and might just come across as confusing to people other than me.

    Another historical artifact. This was added to the API before I'd decided that I would go ahead and include the OBC/NMS stuff necessary to send the finished message directly to players. Now that you mention it, though, it does provide performance benefits that FancyMessage.send(Player) presently lacks – you can use it to cache the JSON string and send that off to a bunch of players without any performance hit, while send(Player) invokes toJSONString() every time it's called. Heading off to fix that now :|

    Thanks for the feedback – looking forward to any pull requests you might submit :)
     
  11. Offline

    Celtic Minstrel

    Wait, does open_file actually work?
     
  12. Offline

    mkremins

    It exists in Minecraft's underlying JSON chat protocol, but I have no idea if it's actually implemented by the vanilla Minecraft client yet, or what sort of value you'd have to give to get it to actually display anything when interacted with. Documentation for the chat protocol in general is pretty thin on the ground.
     
  13. Offline

    Garris0n

    I believe it's used so that you can open a screenshot by clicking the message when you take one. I wonder if it's restricted to that folder or if you can use it to open any file...
     
  14. Offline

    Celtic Minstrel

    I can't see it being any use for anything server-side, though, since the server has pretty much no control over what files exist to be opened.
     
  15. Offline

    leimekiller

    Where can I get JSONStringer?
     
  16. Offline

    Georgeto

    mkremins likes this.
  17. Offline

    Cryptite

    mkremins: This is great stuff. Works most excellently. Great work!

    Edit: Once issue I noticed. If you have an item with lore on it that effectively has "blank lines" for spacing reasons, the itemtooltip displays those lines as "", as opposed to remaining blank like they would if you hovered over the actual ItemStack in your inventory.
     
  18. Offline

    Kainzo

    Just wanted to say your avatar is awesome
     
    mkremins likes this.
  19. Offline

    mkremins

    Yeah, this is a known bug that should be fixed by this commit. The fix hasn't made it into a proper point release yet (although we're about due for one), but you can update your version of Fanciful to 0.1.3-RC2 to get it working in the meantime :)
     
    Cryptite likes this.
  20. Offline

    hawkfalcon

    The sample code seems a bit outdated.
     
  21. Offline

    comniemeer

    What about the "translate"-tag?
     
  22. Offline

    Garris0n

    That's a tag? What's it do?
     
  23. Offline

    comniemeer

    Garris0n and mkremins like this.
  24. Offline

    mkremins

    Fanciful doesn't support it yet, but I'd be more than happy to consider incorporating an API for translations if it's something people want. Any particular functionality you need, besides the ability to specify translate values for message parts?
     
  25. How can we use it without meaven?
     
  26. Offline

    Toni

    mkremins loving fanciful already. I have a question though, do you have progress on the standalone plugin for Fanciful to act as a plugin? I rather have one library update every few weeks then having to shade everything into my plugin. Even with a minimal sized jar, it's a heaping 10mb, which is half the size craftbukkit is. I rather not have to upload 10mb every time I want to make minor changes to text editing for messages.
     
  27. Offline

    mkremins

    I'm hoping to get Fanciful 0.1.3 out either today or tomorrow; the first fanciful-plugin release should follow that update pretty shortly.

    This part I don't quite understand – the Maven-bundled JAR of the latest Fanciful release candidate comes out to 6kb on my machine, 25kb unpacked. Where are you getting 10mb?

    Standalone JARs of every Fanciful release since 0.1.2 are available from the Maven repository; you should be able to download and use them just like you would any other JAR. As of the next release, I also intend to begin offering a standalone plugin version of Fanciful that can be used as a runtime dependency for those who don't want to deal with it at compile time at all.
     
  28. Offline

    Toni

    mkremins here is my POM. It shades an entire library of things utilized by Fanciful
    Code:
    <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>
        <groupId>us.rpvp.eloserver</groupId>
        <artifactId>EloServer</artifactId>
        <version>1.0</version>
        <name>EloServer</name>
        <url>http://realmsofpvp.net/</url>
        <repositories>
            <repository>
                <id>bukkit-repo</id>
                <url>http://repo.bukkit.org/content/groups/public</url>
            </repository>
            <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>
        </repositories>
        <build>
            <defaultGoal>clean install</defaultGoal>
            <finalName>${project.name}</finalName>
            <resources>
                <resource>
                    <filtering>true</filtering>
                    <directory>${project.basedir}/src/main/resources</directory>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <minimizeJar>true</minimizeJar>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.bukkit</groupId>
                <artifactId>bukkit</artifactId>
                <version>1.7.2-R0.4-SNAPSHOT</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>mkremins</groupId>
                <artifactId>fanciful</artifactId>
                <version>0.1.2</version>
            </dependency>
        </dependencies>
    </project>
     
  29. Offline

    mkremins

    Oh wow – I did a little testing and, if I'm not mistaken, that POM will not only always include Bukkit in the compiled plugin JAR (unnecessary, since Bukkit and CraftBukkit are provided for you at runtime) but will actually include multiple copies of Bukkit and at least one copy of CB since Fanciful has a dependency on each. A trivial test plugin I compiled with your POM's <build> configuration spliced in easily hit 10mb without even including any actual plugin functionality.

    I strongly recommend swapping out your maven-shade-plugin configuration for something more like this (taken almost verbatim from the fanciful-plugin POM):
    Code:
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>2.2</version>
      <configuration>
        <artifactSet>
          <includes>
            <include>mkremins:fanciful</include>
          </includes>
        </artifactSet>
        <relocations>
          <relocation>
            <pattern>mkremins.fanciful</pattern>
            <shadedPattern>YOUR_PLUGIN_MAIN_PACKAGE_HERE.libs.mkremins.fanciful</shadedPattern>
          </relocation>
        </relocations>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    
    This should save you a ton of aggravation over compiled JAR sizes – the big win here is that, by using an explicit <includes> section to selectively shade in only the dependencies that aren't provided for you at runtime (i.e. Fanciful itself), you cut not one but two instances of Bukkit and either one or two instances of CraftBukkit from your compiled JAR.

    Now that's what I call a diet.

    I'd also like to take this opportunity to mention that Fanciful v0.1.3 has been officially released, including all the little fixes and API-friendliness improvements that have been accumulating over the past few weeks. Thanks to AmoebaMan, we've also managed to reduce Fanciful's dependency footprint by making use of the JSON lib bundled with (Craft?)Bukkit instead of shading in our own.

    The first build of fanciful-plugin (a standalone plugin version of Fanciful for those who don't mind inflicting a runtime dependency on server administrators if it means they don't have to deal with Maven – can't really say I blame 'em) should be released and posted to BukkitDev for approval shortly.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
    Garris0n and Toni like this.
  30. Offline

    Cryptite

    mkremins Any chance you could update your imports from 1_7_R1 to R2 to support CB1.7.5's new release?
     
Thread Status:
Not open for further replies.

Share This Page