Extended new char data API

Added some additional load methods and util functions
Added some backwards compat functions to grab item index, etc
D2S#copyTo(CharData) calls D2S#loadRemaining automatically
Added CharData#obtain to return an uninitialized (possibly dirty) CharData
Added CharData#preloadItems which loads player and merc items
CharData#load(D2S) will set CharData to managed
This commit is contained in:
Collin Smith 2020-02-03 15:25:00 -08:00
parent 63c46de6bf
commit c4d2bc58a5
3 changed files with 48 additions and 2 deletions

View File

@ -93,7 +93,12 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable {
* @param managed whether or not this data is backed by a file
*/
public static CharData obtain(int diff, boolean managed, String name, byte charClass) {
return new CharData().set(diff, managed, name, charClass);
return obtain().set(diff, managed, name, charClass);
}
/** Constructs an uninitialized CharData -- must be initialized via #set */
public static CharData obtain() {
return new CharData();
}
/** Constructs an unmanaged instance. Used for remote players with only partial save data. */
@ -129,6 +134,8 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable {
}
public CharData load(D2S d2s) {
managed = true;
data = d2s.file.readBytes();
d2s.copyTo(this);
preprocessItems();
itemData.addUpdateListener(this);
@ -227,6 +234,11 @@ public class CharData implements ItemData.UpdateListener, Pool.Poolable {
base.put(Stat.maxpoisonresist, 75);
}
public void preloadItems() {
itemData.load();
mercData.itemData.load();
}
public boolean isManaged() {
return managed;
}

View File

@ -147,6 +147,7 @@ public class D2S {
}
CharData copyTo(CharData data) {
loadRemaining();
data.softReset();
data.name = header.name;
data.charClass = header.charClass;

View File

@ -58,6 +58,13 @@ public class ItemData {
updateListeners.clear();
}
public void load() {
Item[] items = itemData.items;
for (int i = 0, s = itemData.size; i < s; i++) {
items[i].load();
}
}
void preprocessItems() {
cursor = ItemData.INVALID_ITEM;
Item[] items = itemData.items;
@ -92,6 +99,14 @@ public class ItemData {
updateStats();
}
public Array<Item> getItems() {
return itemData;
}
public int indexOf(Item item) {
return itemData.indexOf(item, true);
}
public Item getItem(int i) {
return itemData.get(i);
}
@ -179,6 +194,16 @@ public class ItemData {
return getLocation(Location.STORED, storeLoc);
}
public Array<Item> toItemArray(IntArray items) {
Array<Item> copy = new Array<>(false, items.size, Item.class);
int[] cache = items.items;
for (int i = 0, s = items.size, j; i < s; i++) {
j = cache[i];
copy.add(itemData.get(j));
}
return copy;
}
void pickup(int i) {
assert cursor == INVALID_ITEM;
Item item = itemData.get(i);
@ -269,7 +294,15 @@ public class ItemData {
}
}
public boolean addEquipmentListener(EquipListener l) {
public IntIntMap getEquippedSets() {
return equippedSets;
}
public int getOwnedSetCount(int setId) {
return setItemsOwned.get(setId, 0);
}
public boolean addEquipListener(EquipListener l) {
equipListeners.add(l);
return true;
}