Easy skript to Auto Compress materials! (With Full Explaination)

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

Jake

Supporter
Jan 30, 2022
217
14
18
18
Italy
github.com
This easy to edit code will make compressing a lot easier, especially for new servers!

Want to check the code and understand how it works to get better at coding?
Information about the code can be found as an example of the Floor function in the SkriptHub Docs!

Further info can be found below:

Periodical Event:

The code Checks every 1 second, this can be changed but to avoid too much lag i just set it to 1 second.
View attachment 6791


Loop:

So every 1 second we will loop all players:
View attachment 6783



But looping all the players is a waste of performance, why not just loop all the players that actually have the material we want to compress? Good idea!


Contains:

View attachment 6784
So we are looping all the players whose inventory contain atleast 64 of oak wood which is the average minimum amount for a compressed oak wood!


Amount of Items:

After this, we need to check how much wood there is in the loop-player's inventory, so we set a local variable to the total amount of oak wood in the loop-player's inventory, {_n} stands for number but you can put whatever you want!
View attachment 6785


Arithmetic:

now we divide the total amount of oak wood the player has / 64
View attachment 6786 this way we have the number of stacks
(64 / 64) = 1 stack
(128 / 64) = 2 stacks
and so on

BUT WAIT! what if its not a multiple of 64?? (ex. 70 oak wood, 165 oak wood) good question! to solve this problem we need to round down the obtained number of stacks!


Floor: (you can find the same code in the examples of this function, there's a small explaination there aswell!)

View attachment 6787 what the floor function does is very simple, it... takes the value to the floor, no really, picture every full number as a room and each number is on top of each other, by using this function with a number that has decimals (ex. 5.1, 9.35), it will be rounded down (ex. 5.1 = 5, 9.35 = 9)

lets take 163 as an example, dividing it by 64 gets up a total of 2.54 stacks which is not what we want, by rounding this number down we get the integer "2" and we leave the 0.54 , furthermore if we were to multiply these numbers by 64, we will notice that 2 * 64 = 128 (2 stacks) and that 0.54 * 64 = 35 which is the remaining wood that does not get affected by this code!

Hold on tight, i know this is a very long explaination but bare with me, everyone gains from this!

Loop: (same as before)

now we loop {_n} times
View attachment 6788
what this means it that whatever is intended inside this loop will happen {_n} times ({_n} being the amount of stacks the player has)
ex. if i had 1 stack this will loop only once, if i had 7 stacks this will loop 7 times etc.

Change Effect [Remove] + Arithmetic + Inventory [Expression]

now we remove the wood that is going to be compressed
View attachment 6789
by removing the amount of stacks * 64 we effectively remove the exact amount of compressable oak wood in the player's inventory

Change Effect [Give]

View attachment 6790
now we simply give the amount of stacks ( 1 stack = 1 compressed item ) as 1 compressed item, to the loop player!

Message:

(If you're unfamiliar with skript, when using variables in text you need to put them between %%, this way skript knows that you're not just putting some brackets into some text but you want to insert the value of that variable there!)

last but not necessary, lets send a message to the player that this compression has been done :emoji_grinning:
View attachment 6792

For any help leave comments!
I will always be active in the Skunity Forums!