Skip to content

Instantly share code, notes, and snippets.

@Snow-Pyon
Last active January 11, 2019 23:09
Show Gist options
  • Save Snow-Pyon/f73b95560eb3e1f2bc445ccd12a0d21e to your computer and use it in GitHub Desktop.
Save Snow-Pyon/f73b95560eb3e1f2bc445ccd12a0d21e to your computer and use it in GitHub Desktop.
NBT utilities for Skript using skript-mirror
# normal imports
import:
java.lang.String
java.util.regex.Pattern
org.bukkit.Bukkit
org.bukkit.entity.Entity
org.bukkit.inventory.ItemStack
ch.njol.skript.Skript
ch.njol.skript.util.Version
ch.njol.skript.aliases.ItemType
option nms-version:
get:
return Bukkit.getServer().getClass().getPackage().getName().split("\.")[3]
# NMS/CBO imports
import:
org.bukkit.craftbukkit.{@nms-version}.inventory.CraftItemStack
org.bukkit.craftbukkit.{@nms-version}.block.CraftBlock
net.minecraft.server.{@nms-version}.ItemStack as NMSItemStack
net.minecraft.server.{@nms-version}.NBTTagCompound
net.minecraft.server.{@nms-version}.NBTTagList
net.minecraft.server.{@nms-version}.MojangsonParser
net.minecraft.server.{@nms-version}.BlockPosition
# Costants
on script load:
set {NBT::pattern::version} to Pattern.compile("\d\.\d{1,2}(?:\.\d{1,2})?")
condition [(minecraft|mc)] version is (greater|1¦less) than <.+>:
parse:
if {NBT::pattern::version}.matcher(regex-1).matches() is false:
Skript.error("Invalid Minecraft version.")
else:
continue
check:
set {_compared-version} to new Version(expr)
set {_current-version} to Skript.getMinecraftVersion()
if parser mark is 1:
continue if {_current-version}.isSmallerThan({_compared-version})
else:
continue if {_current-version}.isLargerThan({_compared-version})
# Methods that change in specific versions
on script load:
if minecraft version is less than 1.12:
set {NBT::entity:save} to "e"
else:
set {NBT::entity::save} to "save"
#local effect:
# patterns:
# append %strings% to %javaobjects%
# detach %strings% from %javaobjects%
# convert %javaobjects% to %strings%
# trigger:
# expressions-1 and expression-2 are an instance of NBTTagCompound
#
# loop expression-1:
# loop expression-2:
# if matched pattern is 1:
# loop-value-1.a(MojangsonParser.parse(loop-value-2))
# else if matched pattern is 2:
# loop-value-1.remove(MojangsonParser.parse(loop-value-2))
# else:
# loop-value-1.setTag(MojangsonParser.parse(loop-value-3))
local itemtypes property item NBT:
get:
set {_item} to CraftItemStack.asNMSCopy(expression-1.getRandom())
set {_nbt} to {_item}.getTag() if {_item}.hasTag() is true, else new NBTTagCompound()
return {_nbt}
add string:
set {_appended-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_item} to CraftItemStack.asNMSCopy(loop-value.getRandom())
if {_appended-nbt} is set:
if {_item}.hasTag() is true:
{_item}.a({_appended-nbt})
else:
{_item}.setTag({_appended-nbt})
set {_meta} to CraftItemStack.getItemMeta({_item})
loop-value.setItemMeta({_meta})
remove string:
set {_removed-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_item} to CraftItemStack.asNMSCopy(expression-1.getRandom())
if {_removed-nbt} is set:
{_item}.hasTag() is true
{_item}.remove({_removed-nbt})
set {_meta} to CraftItemStack.getItemMeta({_item})
loop-value.setItemMeta({_meta})
set string:
set {_new-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_item} to CraftItemStack.asNMSCopy(loop-value.getRandom())
if {_new-nbt} is set:
{_item}.setTag({_new-nbt})
set {_meta} to CraftItemStack.getItemMeta({_item})
loop-value.setItemMeta({_meta})
local entities property entity NBT:
get:
set {_entity} to expresion-1.getHandle()
set {_nbt} to new NBTTagCompound()
if {_entity}.c({_nbt}) is false:
{_entity}..{NBT::entity::save}({_nbt})
return {_nbt}
add string:
set {_appended-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_entity} to loop-value.getHandle()
set {_old-nbt} to entity nbt of loop-value
{_old-nbt}.a({_appended-nbt})
{_entity}.f({_appended-nbt})
remove string:
set {_removed-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_entity} to loop-value.getHandle()
set {_old-nbt} to entity nbt of loop-value
{_old-nbt}.remove({removed-nbt})
{_entity}.f({_removed-nbt})
set string:
set {_new-nbt} to MojangsonParser.parse(change value)
loop expressions-1:
set {_entity} to loop-value.getHandle()
{_entity}.f({_new-nbt})
blocks, itemtypes, entities property NBT:
get:
if expression-1 is an instance of ItemType:
return item nbt of expression-1
else if expression-1 is an instance of Entity:
return entity nbt of expression-1
set string:
if expressions-1 are an instance of ItemType:
set item nbt of expressions-1 to change value
else if expressions-1 are an instance of Entity:
set entity nbt of expressions-1 to change value
add string:
if expressions-1 are an instance of ItemType:
add change value to item nbt of expressions-1
else if expressions-1 are an instance of Entity:
add change value to entity nbt of expressions-1
remove string:
if expressions-1 are an instance of ItemType:
remove change value from item nbt of expressions-1
else if expressions-1 are an instance of Entity:
remove change value from entity nbt of expressions-1
expression %itemtype% with nbt %string%:
get:
set {_item} to expression-1
set item nbt of {_item} to expression-2
return {_item}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment