Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Scriptname _avarisc_metachest_script extends ObjectReference
- int array
- int barray
- bool mutex = false
- Event OnInit()
- BlockActivation()
- endEvent
- Event OnLoad()
- BlockActivation()
- endEvent
- Event OnClose(ObjectReference akActionRef)
- saveMetachest()
- EndEvent
- Event OnItemAdded(Form akBaseItem, int aiItemCount, ObjectReference akItemReference, ObjectReference akSourceContainer)
- if(akBaseItem.GetFormID() < 0) ; dynamic item id
- Debug.Notification("This item cannot exist in other realities.")
- refuseItem(akBaseItem, akItemReference, aiItemCount, akSourceContainer)
- endif
- endEvent
- function refuseItem(Form refusedItemBase, ObjectReference refusedItemReference, int refusedItemCount, ObjectReference refusedSource)
- Self.RemoveItem(refusedItemBase, refusedItemCount, false, refusedSource)
- endfunction
- Event OnActivate(ObjectReference akActionRef)
- if(!mutex) ; a simplistic concurrency lock to prevent crosswriting errors
- loadMetachest()
- Activate(akActionRef, true)
- endif
- EndEvent
- function loadMetachest()
- mutex = true
- ; clear save-local (unwanted) data
- Self.RemoveAllItems()
- if(array != 0)
- array = JValue.release(array)
- endif
- ; load data
- array = JValue.readFromFile(JContainers.userDirectory() + "avarisc_metachest.json")
- if(array == 0)
- array = JArray.object()
- endif
- ; restore the chest
- int itemCounter = JArray.count(array)
- while itemCounter > 0
- itemCounter -= 1
- DeserializeItemIntoInventory(JArray.getObj(array, itemCounter))
- endwhile
- JValue.release(array)
- mutex = false
- endfunction
- function saveMetachest()
- mutex = true
- if(array != 0)
- array = JValue.release(array)
- endif
- array = JArray.object();
- if(barray != 0)
- barray = JValue.release(barray)
- endif
- barray = JMap.object();
- Int iFormIndex = Self.GetNumItems()
- While iFormIndex > 0
- iFormIndex -= 1
- Form kForm = Self.GetNthForm(iFormIndex)
- int itemcount = Self.GetItemCount(kForm)
- int itemcount2 = itemcount
- while(itemcount2 > 0)
- ObjectReference amor = Self.DropObject(kForm, 1)
- if(ItemHasMetadata(amor) != 0)
- JArray.addObj(array, SerializeItemWithMetadata(amor))
- else
- JMap.SetInt(barray, amor.GetBaseObject().GetFormID() as string, JMap.GetInt(barray, amor.GetBaseObject().GetFormID(), 0) + 1)
- endif
- amor.Delete()
- itemcount2 -= 1
- endwhile
- EndWhile
- ;add teh simple items
- string simpleid = JMap.nextKey(barray)
- while simpleid
- JArray.addObj(array, SerializeItem(simpleid as int, JMap.GetInt(barray, simpleid)))
- simpleid = JMap.nextKey(barray, simpleid)
- endwhile
- JValue.writeToFile(array, JContainers.userDirectory() + "avarisc_metachest.json")
- mutex = false
- endfunction
- ;should take a JMap int and return an objectreference
- function DeserializeItemIntoInventory(int avmsmap)
- if(JMap.GetInt(avmsmap, "hasMetadata") > 0)
- ObjectReference tobj = Self.PlaceAtMe(Game.GetForm(JMap.GetInt(avmsmap, "baseItemId", 0)))
- int numEnchantEffects = JMap.GetInt(avmsmap, "numEnchantEffects", 0)
- if(numEnchantEffects > 0)
- MagicEffect[] avmsMagicEffects
- float[] avmsMags
- int[] avmsAreas
- int[] avmsDurations
- ; SO ridiculous that this is necessary. but arrays require a literal.
- ; If you have more than 20 enchants on one item, God help you- I won't.
- if(numEnchantEffects == 1)
- avmsMagicEffects = new MagicEffect[1]
- avmsMags = new float[1]
- avmsAreas = new int[1]
- avmsDurations = new int[1]
- elseif(numEnchantEffects == 2)
- avmsMagicEffects = new MagicEffect[2]
- avmsMags = new float[2]
- avmsAreas = new int[2]
- avmsDurations = new int[2]
- elseif(numEnchantEffects == 3)
- avmsMagicEffects = new MagicEffect[3]
- avmsMags = new float[3]
- avmsAreas = new int[3]
- avmsDurations = new int[3]
- elseif(numEnchantEffects == 4)
- avmsMagicEffects = new MagicEffect[4]
- avmsMags = new float[4]
- avmsAreas = new int[4]
- avmsDurations = new int[4]
- elseif(numEnchantEffects == 5)
- avmsMagicEffects = new MagicEffect[5]
- avmsMags = new float[5]
- avmsAreas = new int[5]
- avmsDurations = new int[5]
- elseif(numEnchantEffects == 6)
- avmsMagicEffects = new MagicEffect[6]
- avmsMags = new float[6]
- avmsAreas = new int[6]
- avmsDurations = new int[6]
- elseif(numEnchantEffects == 7)
- avmsMagicEffects = new MagicEffect[7]
- avmsMags = new float[7]
- avmsAreas = new int[7]
- avmsDurations = new int[7]
- elseif(numEnchantEffects == 8)
- avmsMagicEffects = new MagicEffect[8]
- avmsMags = new float[8]
- avmsAreas = new int[8]
- avmsDurations = new int[8]
- elseif(numEnchantEffects == 9)
- avmsMagicEffects = new MagicEffect[9]
- avmsMags = new float[9]
- avmsAreas = new int[9]
- avmsDurations = new int[9]
- elseif(numEnchantEffects == 10)
- avmsMagicEffects = new MagicEffect[10]
- avmsMags = new float[10]
- avmsAreas = new int[10]
- avmsDurations = new int[10]
- elseif(numEnchantEffects == 11)
- avmsMagicEffects = new MagicEffect[11]
- avmsMags = new float[11]
- avmsAreas = new int[11]
- avmsDurations = new int[11]
- elseif(numEnchantEffects == 12)
- avmsMagicEffects = new MagicEffect[12]
- avmsMags = new float[12]
- avmsAreas = new int[12]
- avmsDurations = new int[12]
- elseif(numEnchantEffects == 13)
- avmsMagicEffects = new MagicEffect[13]
- avmsMags = new float[13]
- avmsAreas = new int[13]
- avmsDurations = new int[13]
- elseif(numEnchantEffects == 14)
- avmsMagicEffects = new MagicEffect[14]
- avmsMags = new float[14]
- avmsAreas = new int[14]
- avmsDurations = new int[14]
- elseif(numEnchantEffects == 15)
- avmsMagicEffects = new MagicEffect[15]
- avmsMags = new float[15]
- avmsAreas = new int[15]
- avmsDurations = new int[15]
- elseif(numEnchantEffects == 16)
- avmsMagicEffects = new MagicEffect[16]
- avmsMags = new float[16]
- avmsAreas = new int[16]
- avmsDurations = new int[16]
- elseif(numEnchantEffects == 17)
- avmsMagicEffects = new MagicEffect[17]
- avmsMags = new float[17]
- avmsAreas = new int[17]
- avmsDurations = new int[17]
- elseif(numEnchantEffects == 18)
- avmsMagicEffects = new MagicEffect[18]
- avmsMags = new float[18]
- avmsAreas = new int[18]
- avmsDurations = new int[18]
- elseif(numEnchantEffects == 19)
- avmsMagicEffects = new MagicEffect[19]
- avmsMags = new float[19]
- avmsAreas = new int[19]
- avmsDurations = new int[19]
- else
- avmsMagicEffects = new MagicEffect[20]
- avmsMags = new float[20]
- avmsAreas = new int[20]
- avmsDurations = new int[20]
- endif
- float avmsMaxCharge = JMap.GetInt(avmsmap, "enchantMaxCharge", 0)
- int avmsMagicEffectsJObj = JMap.GetObj(avmsmap, "enchantMagicEffectIds")
- int avmsMagsJObj = JMap.GetObj(avmsmap, "enchantMagnitudes")
- int avmsAreasJObj = JMap.GetObj(avmsmap, "enchantAreas")
- int avmsDurationsJObj = JMap.GetObj(avmsmap, "enchantDurations")
- int enEfC = 0
- while(enEfC < numEnchantEffects)
- avmsMagicEffects[enEfC] = Game.GetForm(JArray.GetInt(avmsMagicEffectsJObj, enEfC)) as MagicEffect
- avmsMags[enEfC] = JArray.GetFlt(avmsMagsJObj, enEfC)
- avmsAreas[enEfC] = JArray.GetInt(avmsAreasJObj, enEfC)
- avmsDurations[enEfC] = JArray.GetInt(avmsDurationsJObj, enEfC)
- enEfC += 1
- endwhile
- tobj.CreateEnchantment(avmsMaxCharge, avmsMagicEffects, avmsMags, avmsAreas, avmsDurations)
- endif
- tobj.SetItemHealthPercent(JMap.GetFlt(avmsmap, "smithStatus"));
- tobj.SetDisplayName(JMap.GetStr(avmsmap, "itemName"))
- Self.AddItem(tobj as Form)
- else
- Self.AddItem(Game.GetForm(JMap.GetInt(avmsmap, "baseItemId", 0)), JMap.GetInt(avmsmap, "quantity", 1))
- endif
- endfunction
- ;returns 1 if it has metadata, 0 otherwise
- int function ItemHasMetadata(ObjectReference aor)
- if(aor.GetItemHealthPercent() > 1.0) ; metadata: smithed
- return 1
- elseif(aor.GetEnchantment() != None) ; metadata: enchanted
- return 1
- elseif(aor.GetPoison() != None) ; metadata: potion effects
- return 1
- else
- return 0
- endif
- endfunction
- ;takes simple item id/quantity pairs and serializes them
- int function SerializeItem(int id, int count)
- int retval = JMap.Object()
- JMap.SetInt(retval, "baseItemId", id)
- JMap.SetInt(retval, "hasMetadata", 0)
- JMap.SetInt(retval, "quantity", count)
- return retval
- endfunction
- ; parameter - a name containing a smithy string such as (Legendary) or (Fine)
- ; return - the parameter, trimmed to the last instance of "("
- string function StripSmithingTagFromItemName(string displayName)
- return StringUtil.Substring(displayName, 0, FindLastOccurence(displayName, "("))
- endfunction
- ; paramater - haystack - the string to search
- ; parameter - needle - the character to find
- ; return - int position of the last occurence of needle in haystack
- int function FindLastOccurence(string haystack, string needle)
- int search = StringUtil.GetLength(haystack) ; find in reverse from end
- while search > 0
- search -= 1
- if(StringUtil.GetNthChar(haystack, search) == needle)
- return search
- endif
- endwhile
- return -1; not found
- endfunction
- ;should take an objectreference and return a JMap int
- int function SerializeItemWithMetadata(ObjectReference avmsitem)
- int retval = JMap.object()
- JMap.SetInt(retval, "baseItemId", avmsitem.GetBaseObject().GetFormID())
- JMap.SetFlt(retval, "smithStatus", avmsitem.GetItemHealthPercent())
- if(avmsitem.GetItemHealthPercent() > 1.0); if the item is smithed, the display name contains a smithy string
- JMap.SetStr(retval, "itemName", StripSmithingTagFromItemName(avmsitem.GetDisplayName()))
- else
- JMap.SetStr(retval, "itemName", avmsitem.GetDisplayName()) ; this _should_ work for everything else?
- endif
- JMap.SetInt(retval, "hasMetadata", 1)
- JMap.SetInt(retval, "quantity", 1)
- ; serialize enchantment if present
- if(avmsitem.GetEnchantment() != None)
- Enchantment eForm = avmsitem.GetEnchantment()
- int numEffects = eForm.GetNumEffects()
- JMap.SetInt(retval, "numEnchantEffects", numEffects)
- int avmsEnIds = JArray.object()
- int avmsEnMags = JArray.object()
- int avmsEnAreas = JArray.object()
- int avmsEnDurations = JArray.object()
- int currentEffect = 0
- while currentEffect < numEffects
- JArray.AddInt(avmsEnIds, eForm.GetNthEffectMagicEffect(currentEffect).GetFormID())
- JArray.AddFlt(avmsEnMags, eForm.GetNthEffectMagnitude(currentEffect))
- JArray.AddInt(avmsEnAreas, eForm.GetNthEffectArea(currentEffect))
- JArray.AddInt(avmsEnDurations, eForm.GetNthEffectDuration(currentEffect))
- currentEffect += 1
- endwhile
- JMap.SetFlt(retval, "enchantMaxCharge", avmsitem.GetItemMaxCharge())
- JMap.SetObj(retval, "enchantMagicEffectIds", avmsEnIds)
- JMap.SetObj(retval, "enchantMagnitudes", avmsEnMags)
- JMap.SetObj(retval, "enchantAreas", avmsEnAreas)
- JMap.SetObj(retval, "enchantDurations", avmsEnDurations)
- else
- JMap.SetInt(retval, "numEnchantEffects", 0)
- endif
- return retval
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement