Skip to content

Instantly share code, notes, and snippets.

@bluelhf
Created February 21, 2020 14:52
Show Gist options
  • Save bluelhf/76e3eea2b9755e7abbf1f49454ef65f6 to your computer and use it in GitHub Desktop.
Save bluelhf/76e3eea2b9755e7abbf1f49454ef65f6 to your computer and use it in GitHub Desktop.
Easy serialization utility for item types in skript-mirror (tested in 1.15.2)
option NMS:
get:
return 4th element out of ((class "org.bukkit.Bukkit").getServer().getClass().getPackage().getName() split at ".") # Yay NMS version!
import:
net.minecraft.server.{@NMS}.MojangsonParser # Used for String -> NBTTagCompound conversion
net.minecraft.server.{@NMS}.ItemStack as NMSItemStack # Used to generate an NMS ItemStack from an NBTTagCompound with the NMSItemStack#a(NBTTagCompound) method
plural expression serialized %itemtypes%:
return type: strings
get:
loop exprs-1:
set {_} to random item out of loop-value # Random item can be used to get a bukkit ItemStack from a Skript ItemType
set {_a} to "%{_}.asNMSCopy()%%{_}.asNMSCopy().getTag() ? """"%" # NMS ItemStack's toString() method gives the item amount and ID, getTag() gives the NBTTagCompound (if it's set)
# Flip the item amount and item data to follow the format of /minecraft:give
set {_b::*} to {_a} parsed as "%integer% %string%"
add "%{_b::2}% %{_b::1}%" to {_ret::*}
return {_ret::*}
# Deserialize serialized strings
plural expression deserialized %strings%:
return type: item types
get:
loop exprs-1:
# Parse serialized item
set {_s} to "%loop-value%" # We do this begause of a bug in Skript :shrug:
set {_s::*} to {_s} parsed as "%string%{%string%} %integer%" ? {_s} parsed as "%string% %integer%" # Here, default value is used because not all serializations contain NBT.
set {_hasNbt} to true if {_s::3} is set, else false # This variable is just for readability
set {_nbt} to {_s::2} if {_hasNbt} is true
set {_amount} to {_s::3} if {_hasNbt} is true, else {_s::2}
set {_id} to {_s::1}
# Generate item with ID and amount
set {_itemcompound} to MojangsonParser.parse("{id:""%{_id}%"",Count:%{_amount}%b}")
set {_stack} to NMSItemStack.a({_itemcompound}) # This method can be used to create ItemStacks - see import trigger
# Add NBT if it exists
if {_hasNbt} is true:
{_stack}.setTag(MojangsonParser.parse("{%{_nbt}%}"))
add {_stack}.asBukkitCopy() to {_r::*}
return {_r::*}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment