NBT.gd/nbt/nbtdata.gd
2024-03-08 01:16:17 -05:00

92 lines
2.5 KiB
GDScript

"""
NBT Parser for Godot Engine
Copyright (C) 2022 Jason Table
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
class_name NBTHelper
# This is a helper class to reduce the amount of code needed to build
# NBT files from scratch
# instead of doing:
#var data = {"type":NBT.TAG_String,"data":"hello world"}
# you can do:
#var data = NBTData.string("hello world")
static func byte(value:int):
return {"type":NBT.TAG_Byte,"data":value}
static func short(value:int):
return {"type":NBT.TAG_Short,"data":value}
static func int(value:int):
return {"type":NBT.TAG_Int,"data":value}
static func long(value:int):
return {"type":NBT.TAG_Long,"data":value}
static func float(value:float):
return {"type":NBT.TAG_Float,"data":value}
static func double(value:float):
return {"type":NBT.TAG_Double,"data":value}
static func byte_array(value:PoolByteArray):
return {"type":NBT.TAG_Byte_Array,"data":value}
static func string(value:String):
return {"type":NBT.TAG_String,"data":value}
static func list(value:Array,valuetype:int):
return {"type":NBT.TAG_List,"data":value,"valuetype":valuetype}
static func compund(value:Dictionary):
return {"type":NBT.TAG_Compound,"data":value}
static func int_array(value:PoolIntArray):
return {"type":NBT.TAG_Int_Array,"data":value}
static func long_array(value:Array):
return {"type":NBT.TAG_Long_Array,"data":value}
# Special list helpers
static func quick_list(value:Array,valuetype:int):
# converts an array directly to a NBT list but does not support compounds
# or lists
# WARNING: does not validate list entry types. make sure all items in the
# array are the same type
var output := {"type":NBT.TAG_List,"valuetype":valuetype,"data":[]}
for i in value:
output.data.append({"type":valuetype,"data":i})
return output
# UUID helpers
# maybe make a uuid stringify and unstringify.
# stringify takes the int array and makes it the string
# unstringinfy goes the opposite