Optimization of item-replacer script

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

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

gloatymadn

Member
Feb 9, 2024
13
0
1
23
Hi! at my server most items replaced to mmoitems analog , and this script replace plain items if they somehow get into the player's inventory.
The problem is in the structure of the script itself, at the moment it takes about 800 MS (0.8 sec) to load it
I am afraid that in the future this time will grow to several tens of seconds, and this is already a significant time.

Mi_items_replacer:

Code:
on inventory slot change:
  if event-item is a plain stick:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain stick with air in player's inventory
    execute console command "/mi give MATERIAL STICK %event-player% %{_amount}%"
  if event-item is a plain wooden pickaxe:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden pickaxe with air in player's inventory
    execute console command "/mi give TOOL WOODEN_PICKAXE  %event-player% %{_amount}%"
  if event-item is a plain wooden shovel:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden shovel with air in player's inventory
    execute console command "/mi give TOOL WOODEN_SHOVEL  %event-player% %{_amount}%"
  if event-item is a plain wooden hoe:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden hoe with air in player's inventory
    execute console command "/mi give TOOL WOODEN_HOE  %event-player% %{_amount}%"
  if event-item is a plain wooden sword:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden sword with air in player's inventory
    execute console command "/mi give SWORD WOODEN_SWORD  %event-player% %{_amount}%"

Above is only 12% of the script, this is for understanding the structure of the script.

How can this be optimized?

server version: 1.20.4
Skript addons: Skbee, skquery, skrayfall, skutiles, skript-reflect
 
Hi! at my server most items replaced to mmoitems analog , and this script replace plain items if they somehow get into the player's inventory.
The problem is in the structure of the script itself, at the moment it takes about 800 MS (0.8 sec) to load it
I am afraid that in the future this time will grow to several tens of seconds, and this is already a significant time.

Mi_items_replacer:

Code:
on inventory slot change:
  if event-item is a plain stick:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain stick with air in player's inventory
    execute console command "/mi give MATERIAL STICK %event-player% %{_amount}%"
  if event-item is a plain wooden pickaxe:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden pickaxe with air in player's inventory
    execute console command "/mi give TOOL WOODEN_PICKAXE  %event-player% %{_amount}%"
  if event-item is a plain wooden shovel:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden shovel with air in player's inventory
    execute console command "/mi give TOOL WOODEN_SHOVEL  %event-player% %{_amount}%"
  if event-item is a plain wooden hoe:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden hoe with air in player's inventory
    execute console command "/mi give TOOL WOODEN_HOE  %event-player% %{_amount}%"
  if event-item is a plain wooden sword:
    wait 1 tick
    set {_amount} to item amount of event-item
    replace all plain wooden sword with air in player's inventory
    execute console command "/mi give SWORD WOODEN_SWORD  %event-player% %{_amount}%"

Above is only 12% of the script, this is for understanding the structure of the script.

How can this be optimized?

server version: 1.20.4
Skript addons: Skbee, skquery, skrayfall, skutiles, skript-reflect
Functions. Functions can run multiple similar lines using only one line which makes them useful for Skripts that have many repetitive or similar lines. Example of optimization using a function and your code:
Code:
function replaceitem(p: player, i: text, i2: text, type: text):
   set {_am} to amount of {_i2}
   remove all {_i2} from {_p}'s inventory
   execute console command "/mi give %{_type}% %{_i}% %{_p}% %{_am}%"

if event-item is a plain stick:
   replaceitem(player, "STICK", "plain stick", "Material")
Keep in mind, the code is untested and you may need to change a bit to fit your needs.