Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
SubsystemEditableItemBehavior.cs
浏览该文件的文档.
1using Engine;
4
5namespace Game {
6 public abstract class SubsystemEditableItemBehavior<T> : SubsystemBlockBehavior where T : IEditableItemData, new() {
8
9 public int m_contents;
10
11 public Dictionary<int, T> m_itemsData = [];
12
13 public Dictionary<Point3, T> m_blocksData = [];
14 public Dictionary<MovingBlock, T> m_movingBlocksData = new();
15
16 public SubsystemEditableItemBehavior(int contents) => m_contents = contents;
17
18 public T GetBlockData(Point3 point) {
19 m_blocksData.TryGetValue(point, out T value);
20 return value;
21 }
22
23 public T GetBlockData(MovingBlock movingBlock) {
24 m_movingBlocksData.TryGetValue(movingBlock, out T value);
25 return value;
26 }
27
28 public void SetBlockData(Point3 point, T t) {
29 if (t != null) {
30 m_blocksData[point] = t;
31 }
32 else {
33 m_blocksData.Remove(point);
34 }
35 }
36
37 public void SetBlockData(MovingBlock movingBlock, T t) {
38 if (t != null) {
39 m_movingBlocksData[movingBlock] = t;
40 }
41 else {
42 m_movingBlocksData.Remove(movingBlock);
43 }
44 }
45
46 public T GetItemData(int id) {
47 m_itemsData.TryGetValue(id, out T value);
48 return value;
49 }
50
51 public int StoreItemDataAtUniqueId(T t) {
52 int num = FindFreeItemId();
53 m_itemsData[num] = t;
54 return num;
55 }
56
57 public override void OnItemPlaced(int x, int y, int z, ref BlockPlacementData placementData, int itemValue) {
58 int id = Terrain.ExtractData(itemValue);
59 T itemData = GetItemData(id);
60 if (itemData != null) {
61 m_blocksData[new Point3(x, y, z)] = (T)itemData.Copy();
62 }
63 }
64
65 public override void OnItemHarvested(int x, int y, int z, int blockValue, ref BlockDropValue dropValue, ref int newBlockValue) {
66 T blockData = GetBlockData(new Point3(x, y, z));
67 if (blockData != null) {
68 int num = FindFreeItemId();
69 m_itemsData.Add(num, (T)blockData.Copy());
70 dropValue.Value = Terrain.ReplaceData(dropValue.Value, num);
71 }
72 }
73
74 public override void OnBlockRemoved(int value, int newValue, int x, int y, int z) {
75 m_blocksData.Remove(new Point3(x, y, z));
76 }
77
78 public override void OnBlockStartMoving(int value, int newValue, int x, int y, int z, MovingBlock movingBlock) {
79 Point3 point = new(x, y, z);
80 T blockData = m_blocksData[point];
81 m_blocksData.Remove(point);
82 m_movingBlocksData[movingBlock] = blockData;
83 }
84
85 public override void OnBlockStopMoving(int value, int oldValue, int x, int y, int z, MovingBlock movingBlock) {
86 Point3 point = new(x, y, z);
87 T blockData = m_movingBlocksData[movingBlock];
88 m_movingBlocksData.Remove(movingBlock);
89 m_blocksData[point] = blockData;
90 }
91
92 public override void Load(ValuesDictionary valuesDictionary) {
93 base.Load(valuesDictionary);
95 foreach (KeyValuePair<string, object> item in valuesDictionary.GetValue<ValuesDictionary>("Blocks")) {
96 T value = new();
97 value.LoadString((string)item.Value);
98 MovingBlock movingBlock = MovingBlock.LoadFromString(Project, item.Key, out Exception exception);
99 if (exception == null) {
100 m_movingBlocksData[movingBlock] = value;
101 }
102 else {
104 m_blocksData[key] = value;
105 }
106 }
107 foreach (KeyValuePair<string, object> item2 in valuesDictionary.GetValue<ValuesDictionary>("Items")) {
108 int key2 = HumanReadableConverter.ConvertFromString<int>(item2.Key);
109 T value2 = new();
110 value2.LoadString((string)item2.Value);
111 m_itemsData[key2] = value2;
112 }
113 m_subsystemItemsScanner.ItemsScanned += GarbageCollectItems;
114 }
115
116 public override void Save(ValuesDictionary valuesDictionary) {
117 base.Save(valuesDictionary);
118 ValuesDictionary valuesDictionary2 = new();
119 valuesDictionary.SetValue("Blocks", valuesDictionary2);
120 foreach (KeyValuePair<Point3, T> blocksDatum in m_blocksData) {
121 valuesDictionary2.SetValue(HumanReadableConverter.ConvertToString(blocksDatum.Key), blocksDatum.Value.SaveString());
122 }
123 foreach (KeyValuePair<MovingBlock, T> movingBlocksDatum in m_movingBlocksData) {
124 valuesDictionary2.SetValue(movingBlocksDatum.Key.ToString(), movingBlocksDatum.Value.SaveString());
125 }
126 ValuesDictionary valuesDictionary3 = new();
127 valuesDictionary.SetValue("Items", valuesDictionary3);
128 foreach (KeyValuePair<int, T> itemsDatum in m_itemsData) {
129 valuesDictionary3.SetValue(HumanReadableConverter.ConvertToString(itemsDatum.Key), itemsDatum.Value.SaveString());
130 }
131 }
132
133 public int FindFreeItemId() {
134 for (int i = 1; i < 1000; i++) {
135 if (!m_itemsData.ContainsKey(i)) {
136 return i;
137 }
138 }
139 return 0;
140 }
141
142 public void GarbageCollectItems(ReadOnlyList<ScannedItemData> allExistingItems) {
143 HashSet<int> hashSet = new();
144 foreach (ScannedItemData item in allExistingItems) {
145 if (Terrain.ExtractContents(item.Value) == m_contents) {
146 hashSet.Add(Terrain.ExtractData(item.Value));
147 }
148 }
149 List<int> list = new();
150 foreach (KeyValuePair<int, T> itemsDatum in m_itemsData) {
151 if (!hashSet.Contains(itemsDatum.Key)) {
152 list.Add(itemsDatum.Key);
153 }
154 }
155 foreach (int item2 in list) {
156 m_itemsData.Remove(item2);
157 }
158 }
159 }
160}
static object ConvertFromString(Type type, string data)
static MovingBlock LoadFromString(Project project, string movingBlockInfo, out Exception exception)
override void OnBlockStartMoving(int value, int newValue, int x, int y, int z, MovingBlock movingBlock)
override void Load(ValuesDictionary valuesDictionary)
void GarbageCollectItems(ReadOnlyList< ScannedItemData > allExistingItems)
override void OnItemHarvested(int x, int y, int z, int blockValue, ref BlockDropValue dropValue, ref int newBlockValue)
override void OnItemPlaced(int x, int y, int z, ref BlockPlacementData placementData, int itemValue)
override void OnBlockRemoved(int value, int newValue, int x, int y, int z)
override void OnBlockStopMoving(int value, int oldValue, int x, int y, int z, MovingBlock movingBlock)
override void Save(ValuesDictionary valuesDictionary)
static int ExtractContents(int value)
static int ReplaceData(int value, int data)
static int ExtractData(int value)
ValuesDictionary ValuesDictionary