Other Advanced date formatting with skript-mirror

  • 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!

btk5h

Addon Developer
Jan 25, 2017
154
159
0
Pyongyang, North Korea
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_language.skript:
import "java.lang.String"

Next, let's prepare the arguments we'll pass to String#format in a list variable.
code_language.skript:
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_language.skript:
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_language.skript:
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_language.skript:
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_language.skript:
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.
 
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.
 
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.
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_language.skript:
on unhang: #Needs MundoSK
    if hanged entity is an item frame:
        if event-entity is an arrow:
            cancel event
 
Last edited by a moderator: