@arubiku/utils-protocol
Version:
A utility library for the Grakkit and Protocol API
100 lines (94 loc) • 3.76 kB
TypeScript
import { core } from '@grakkit/stdlib'
import type { obiItemStack, obePlayer, obMaterial } from '@grakkit/types-paper'
const NBTBase = Java.type('com.comphenix.protocol.wrappers.nbt.NbtBase')
const NbtFactory = Java.type('com.comphenix.protocol.wrappers.nbt.NbtFactory')
const NbtList = Java.type('com.comphenix.protocol.wrappers.nbt.NbtList')
const NbtType = Java.type('com.comphenix.protocol.wrappers.nbt.NbtType')
const ItemStack = core.type('org.bukkit.inventory.ItemStack')
const NamespacedKey = core.type('org.bukkit.NamespacedKey')
const PersistentDataType = core.type('org.bukkit.persistence.PersistentDataType')
const Material = core.type('org.bukkit.Material')
declare module '@arubiku/utils-protocol' {
interface AllowedTypes {
String: string,
Integer: number,
Double: number,
Float: number,
Long: number,
Short: number,
Byte: number,
Boolean: boolean,
JsonElement: any,
NBTTag: any
}
export function buildStack(material: string): ItemStack{
return new ItemStack(Material.valueOf(material))
}
export class NBTTag{
key : string
type : AllowedTypes
value : any
constructor(key : string, type : AllowedTypes, value : any){
this.key = key
this.type = type
this.value = value
}
getKey() : string{
return this.key
}
getType() : AllowedTypes{
return this.type
}
getValue() : any{
return this.value
}
}
export class NBTItem{
item : ItemStack
modified : ItemStack
constructor(item : any){
this.item = item
this.modified = item
}
fromItemStack(item : any) : NBTItem{
return new NBTItem(item)
}
getOriginalItem() : any{
return this.item
}
setInternal(key : string, value : any, type : AllowedTypes) : void{
const compound = NbtFactory.asCompound(NbtFactory.fromItemTag(this.modified))
compound.put(key, NbtFactory.ofWrapper(type, value))
NbtFactory.setItemTag(this.modified, compound)
}
getInternal(key : string, type : AllowedTypes) : any{
const compound = NbtFactory.asCompound(NbtFactory.fromItemTag(this.modified))
if (compound.containsKey(key)) {
return compound.getValue(key).getValue()
}
return null
}
put(tag: NBTTag) : void{
this.setInternal(tag.getKey(), tag.getValue(), tag.getType())
}
get(key: string) : NBTTag{
const compound = NbtFactory.asCompound(NbtFactory.fromItemTag(this.modified))
if (compound.containsKey(key)) {
return new NBTTag(key, compound.getValue(key).getType().name(), compound.getValue(key).getValue())
}
return null
}
remove(key: string) : void{
const compound = NbtFactory.asCompound(NbtFactory.fromItemTag(this.modified))
compound.remove(key)
NbtFactory.setItemTag(this.modified, compound)
}
putData(key: string, value: any, type: PersistentDataType) : void{
this.modified.getItemMeta().getPersistentDataContainer().set(new NamespacedKey('grakkit', key), type, value)
}
getData(key: string, type: PersistentDataType) : any{
return this.modified.getItemMeta().getPersistentDataContainer().get(new NamespacedKey('grakkit', key), type)
}
}
}
export * from '@arubiku/utils-protocol'