1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Other Advanced date formatting with skript-mirror

Discussion in 'Tutorials' started by btk5h, Jun 23, 2017.

  1. btk5h

    Addon Developer

    Joined:
    Jan 25, 2017
    Messages:
    154
    Likes Received:
    158
    Skript doesn't offer much support for date formatting. Fortunately, Java provides advanced support for date formatting through String#format. Let's access it using skript-mirror!

    This tutorial is friendly to users playing around in the server console. Because none of the following examples require a player, you can follow along using Skript's effect commands!

    First, import Java's String class. This will allow you to alias the class to a variable instead of typing out the full name. This only has to be done once (until you clear or overwrite the variable), so it can be done once through an effect command or in an "on script load" event.
    Code (Skript):
    1. import "java.lang.String"
    Next, let's prepare the arguments we'll pass to String#format in a list variable.
    Code (Skript):
    1. set {format::*} to "The world will end on %%tc" and now.getTimestamp()
    Notice, we have to use now.getTimeStamp() to convert Skript's dates into timestamps that String#format can use.

    Let's print it out!
    Code (Skript):
    1. message "%{String}.format({format::*})%"
    You should get something similar to "The world will end on Fri Jun 23 14:12:27 PDT 2017".

    What if we want to use another format? Let's use another preset format.
    Code (Skript):
    1. set {format::*} to "The world will end on %%tD" and now.getTimestamp()
    Print it out using the same message code, and you should get something like "The world will end on 06/23/17".

    That's great, but what if we want to use our own date format? What if we want to pick what information we want to display? Let's try the same thing, but only display the names of the month and day.
    Code (Skript):
    1. set {format::*} to "The world will end in %%tB on a %%tA" and now.getTimestamp()
    Use the same code to print a message... and you should get an error. That's because whenever we use a format specifier (that's the %%tX part), it uses the next argument in the list. String#format is expecting to use a second argument, but we only provided one. We can change that by indexing our format specifiers.
    Code (Skript):
    1. set {format::*} to "The world will end in %%1$tB on a %%1$tA" and now.getTimestamp()
    Now you should get something like "The world will end in June on a Friday"!

    There's so much more you can do with String#format that I don't have time to cover here, but you can check out the huge list of supported specifiers here.
     
    • Like Like x 5
    • Winner Winner x 1
    • Informative Informative x 1
  2. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,181
    Likes Received:
    220
    I never ever get into java nor near to things like import, etc.
    Can you make a tutorial explaining not what is, but how and where to look to get this kind of stuff?

    For example, I want to find a way to stop arrows from breaking paitings and item frames. Where to look at for the java string?
    Thanks.
     
    • Agree Agree x 1
  3. ShaneBee

    Moderator Resource Staff Supporter + Addon Developer

    Joined:
    Sep 7, 2017
    Messages:
    2,181
    Likes Received:
    220
    You wouldn't need this addon for that; doesn't mean you can't do it with this addon. You can look into Spigot for a plugin that does it and then check the source of it to see how the plugin does it. That's the way you get it, basically.

    This addon is too advanced and needs at least a pretty basic java knowledge to know how it works, because, as you can see, the syntax for effects and expressions is similar to what an actual Java method is.

    By the way, here is what you asked.

    Code (Skript):
    1. on unhang: #Needs MundoSK
    2.     if hanged entity is an item frame:
    3.         if event-entity is an arrow:
    4.             cancel event
     
    #3 ShaneBee, Jun 25, 2017
    Last edited by a moderator: Jun 25, 2017
    • Informative Informative x 3

Share This Page

Loading...