|
| 1 | +package qowyn.ark.tools.data; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.SortedMap; |
| 7 | +import java.util.TreeMap; |
| 8 | + |
| 9 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 10 | + |
| 11 | +import qowyn.ark.GameObject; |
| 12 | +import qowyn.ark.GameObjectContainer; |
| 13 | +import qowyn.ark.tools.CreatureData; |
| 14 | +import qowyn.ark.tools.DataManager; |
| 15 | +import qowyn.ark.types.ArkName; |
| 16 | +import qowyn.ark.types.LocationData; |
| 17 | +import qowyn.ark.types.ObjectReference; |
| 18 | + |
| 19 | +public class DroppedItem { |
| 20 | + |
| 21 | + public ArkName className; |
| 22 | + |
| 23 | + public String type; |
| 24 | + |
| 25 | + public LocationData location; |
| 26 | + |
| 27 | + public GameObject myItem; |
| 28 | + |
| 29 | + public String droppedByName; |
| 30 | + |
| 31 | + public int targetingTeam; |
| 32 | + |
| 33 | + public double originalCreationTime; |
| 34 | + |
| 35 | + public float initialLifeSpan; |
| 36 | + |
| 37 | + public DroppedItem() {} |
| 38 | + |
| 39 | + public DroppedItem(GameObject droppedItem, GameObjectContainer saveFile) { |
| 40 | + className = droppedItem.getClassName(); |
| 41 | + CreatureData structureData = DataManager.getStructure(droppedItem.getClassString()); |
| 42 | + type = structureData != null ? structureData.getName() : droppedItem.getClassString(); |
| 43 | + |
| 44 | + location = droppedItem.getLocation(); |
| 45 | + |
| 46 | + myItem = droppedItem.findPropertyValue("MyItem", ObjectReference.class).map(saveFile::getObject).orElse(null); |
| 47 | + droppedByName = droppedItem.findPropertyValue("DroppedByName", String.class).orElse(""); |
| 48 | + targetingTeam = droppedItem.findPropertyValue("TargetingTeam", Integer.class).orElse(0); |
| 49 | + originalCreationTime = droppedItem.findPropertyValue("OriginalCreationTime", Double.class).orElse(0.0); |
| 50 | + initialLifeSpan = droppedItem.findPropertyValue("InitialLifeSpan", Float.class).orElse(Float.POSITIVE_INFINITY); |
| 51 | + } |
| 52 | + |
| 53 | + public static final SortedMap<String, WriterFunction<DroppedItem>> PROPERTIES = new TreeMap<>(); |
| 54 | + |
| 55 | + static { |
| 56 | + /** |
| 57 | + * Creature Properties |
| 58 | + */ |
| 59 | + PROPERTIES.put("type", (droppedItem, generator, context, writeEmpty) -> { |
| 60 | + if (context instanceof DataCollector) { |
| 61 | + generator.writeStringField("type", droppedItem.className.toString()); |
| 62 | + } else { |
| 63 | + generator.writeStringField("type", droppedItem.type); |
| 64 | + } |
| 65 | + }); |
| 66 | + PROPERTIES.put("location", (droppedItem, generator, context, writeEmpty) -> { |
| 67 | + if (writeEmpty || droppedItem.location != null) { |
| 68 | + if (droppedItem.location == null) { |
| 69 | + generator.writeNullField("location"); |
| 70 | + } else { |
| 71 | + generator.writeObjectFieldStart("location"); |
| 72 | + generator.writeNumberField("x", droppedItem.location.getX()); |
| 73 | + generator.writeNumberField("y", droppedItem.location.getY()); |
| 74 | + generator.writeNumberField("z", droppedItem.location.getZ()); |
| 75 | + if (context.getLatLonCalculator() != null) { |
| 76 | + generator.writeNumberField("lat", context.getLatLonCalculator().calculateLat(droppedItem.location.getY())); |
| 77 | + generator.writeNumberField("lon", context.getLatLonCalculator().calculateLon(droppedItem.location.getX())); |
| 78 | + } |
| 79 | + generator.writeEndObject(); |
| 80 | + } |
| 81 | + } |
| 82 | + }); |
| 83 | + PROPERTIES.put("myItem", (droppedItem, generator, context, writeEmpty) -> { |
| 84 | + if (droppedItem.myItem != null) { |
| 85 | + generator.writeNumberField("myItem", droppedItem.myItem.getId()); |
| 86 | + } else if (writeEmpty) { |
| 87 | + generator.writeNullField("myItem"); |
| 88 | + } |
| 89 | + }); |
| 90 | + PROPERTIES.put("droppedByName", (droppedItem, generator, context, writeEmpty) -> { |
| 91 | + if (writeEmpty || !droppedItem.droppedByName.isEmpty()) { |
| 92 | + generator.writeStringField("droppedByName", droppedItem.droppedByName); |
| 93 | + } |
| 94 | + }); |
| 95 | + PROPERTIES.put("targetingTeam", (droppedItem, generator, context, writeEmpty) -> { |
| 96 | + if (writeEmpty || droppedItem.targetingTeam != 0) { |
| 97 | + generator.writeNumberField("targetingTeam", droppedItem.targetingTeam); |
| 98 | + } |
| 99 | + }); |
| 100 | + PROPERTIES.put("originalCreationTime", (droppedItem, generator, context, writeEmpty) -> { |
| 101 | + if (writeEmpty || droppedItem.originalCreationTime != 0.0) { |
| 102 | + generator.writeNumberField("originalCreationTime", droppedItem.originalCreationTime); |
| 103 | + } |
| 104 | + }); |
| 105 | + PROPERTIES.put("initialLifeSpan", (droppedItem, generator, context, writeEmpty) -> { |
| 106 | + if (writeEmpty || droppedItem.initialLifeSpan != Float.POSITIVE_INFINITY) { |
| 107 | + generator.writeNumberField("initialLifeSpan", droppedItem.initialLifeSpan); |
| 108 | + } |
| 109 | + }); |
| 110 | + PROPERTIES.put("lifeSpanLeft", (droppedItem, generator, context, writeEmpty) -> { |
| 111 | + if (context.getSavegame() != null && droppedItem.initialLifeSpan != Float.POSITIVE_INFINITY) { |
| 112 | + generator.writeNumberField("lifeSpanLeft", droppedItem.initialLifeSpan - (context.getSavegame().getGameTime() - droppedItem.originalCreationTime)); |
| 113 | + } else if (writeEmpty) { |
| 114 | + if (droppedItem.initialLifeSpan != Float.POSITIVE_INFINITY) { |
| 115 | + generator.writeNumberField("lifeSpanLeft", Float.NaN); |
| 116 | + } else { |
| 117 | + generator.writeNumberField("lifeSpanLeft", Float.POSITIVE_INFINITY); |
| 118 | + } |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + public static final List<WriterFunction<DroppedItem>> PROPERTIES_LIST = new ArrayList<>(PROPERTIES.values()); |
| 124 | + |
| 125 | + public void writeAllProperties(JsonGenerator generator, DataContext context, boolean writeEmpty) throws IOException { |
| 126 | + for (WriterFunction<DroppedItem> writer: PROPERTIES_LIST) { |
| 127 | + writer.accept(this, generator, context, writeEmpty); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public void writeInventory(JsonGenerator generator, DataContext context, boolean writeEmpty, boolean inventorySummary) throws IOException { |
| 132 | + if (this.myItem != null) { |
| 133 | + Item item = new Item(myItem); |
| 134 | + |
| 135 | + generator.writeFieldName("item"); |
| 136 | + if (inventorySummary) { |
| 137 | + generator.writeStartObject(); |
| 138 | + |
| 139 | + generator.writeStringField("name", item.type); |
| 140 | + generator.writeNumberField("count", item.quantity); |
| 141 | + |
| 142 | + generator.writeEndObject(); |
| 143 | + } else { |
| 144 | + generator.writeStartObject(); |
| 145 | + |
| 146 | + item.writeAllProperties(generator, context, writeEmpty); |
| 147 | + |
| 148 | + generator.writeEndObject(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments