Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ComponentCreativeInventory.cs
浏览该文件的文档.
1using System.Globalization;
2using Engine;
5
6namespace Game {
8 internal class Order {
9 public Block block;
10 public int order;
11 public int value;
12
13 public Order(Block b, int o, int v) {
14 block = b;
15 order = o;
16 value = v;
17 }
18 }
19
20 public List<int> m_slots = [];
21
23
24 public int m_visibleSlotsCount = 10;
25
26 public const int m_largeNumber = 0x1fffffff;
27 public int OpenSlotsCount { get; set; }
28
29 public int CategoryIndex { get; set; }
30
31 public int PageIndex { get; set; }
32
34
35 public int ActiveSlotIndex {
36 get => m_activeSlotIndex;
37 set => m_activeSlotIndex = Math.Clamp(value, 0, VisibleSlotsCount - 1);
38 }
39
40 public int SlotsCount => m_slots.Count;
41
42 public int VisibleSlotsCount {
44 set {
45 value = Math.Clamp(value, 0, 10);
46 if (value == m_visibleSlotsCount) {
47 return;
48 }
49 m_visibleSlotsCount = value;
51 ComponentFrame componentFrame = Entity.FindComponent<ComponentFrame>();
52 if (componentFrame != null) {
53 Vector3 position = componentFrame.Position + new Vector3(0f, 0.5f, 0f);
54 Vector3 velocity = 1f * componentFrame.Rotation.GetForwardVector();
55 for (int i = m_visibleSlotsCount; i < 10; i++) {
56 ComponentInventoryBase.DropSlotItems(this, i, position, velocity);
57 }
58 }
59 }
60 }
61
62 public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap) {
63 m_activeSlotIndex = valuesDictionary.GetValue<int>("ActiveSlotIndex");
64 OpenSlotsCount = valuesDictionary.GetValue<int>("OpenSlotsCount");
65 CategoryIndex = valuesDictionary.GetValue<int>("CategoryIndex");
66 PageIndex = valuesDictionary.GetValue<int>("PageIndex");
67 for (int i = 0; i < OpenSlotsCount; i++) {
68 m_slots.Add(0);
69 }
70 List<Order> orders = [];
71 foreach (Block item in BlocksManager.Blocks) {
72 foreach (int creativeValue in item.GetCreativeValues()) {
73 orders.Add(new Order(item, item.GetDisplayOrder(creativeValue), creativeValue));
74 }
75 }
76 IOrderedEnumerable<Order> orderList = orders.OrderBy(o => o.order);
77 foreach (Order c in orderList) {
78 m_slots.Add(c.value);
79 }
80 ValuesDictionary value = valuesDictionary.GetValue<ValuesDictionary>("Slots", null);
81 if (value == null) {
82 return;
83 }
84 for (int j = 0; j < OpenSlotsCount; j++) {
85 ValuesDictionary value2 = value.GetValue<ValuesDictionary>($"Slot{j.ToString(CultureInfo.InvariantCulture)}", null);
86 if (value2 != null) {
87 m_slots[j] = value2.GetValue<int>("Contents");
88 }
89 }
90 }
91
92 public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap) {
93 valuesDictionary.SetValue("ActiveSlotIndex", m_activeSlotIndex);
94 valuesDictionary.SetValue("CategoryIndex", CategoryIndex);
95 valuesDictionary.SetValue("PageIndex", PageIndex);
96 ValuesDictionary valuesDictionary2 = new();
97 valuesDictionary.SetValue("Slots", valuesDictionary2);
98 for (int i = 0; i < OpenSlotsCount; i++) {
99 if (m_slots[i] != 0) {
100 ValuesDictionary valuesDictionary3 = new();
101 valuesDictionary2.SetValue($"Slot{i.ToString(CultureInfo.InvariantCulture)}", valuesDictionary3);
102 valuesDictionary3.SetValue("Contents", m_slots[i]);
103 }
104 }
105 }
106
107 public virtual int GetSlotValue(int slotIndex) {
108 if (slotIndex >= 0
109 && slotIndex < m_slots.Count) {
110 return m_slots[slotIndex];
111 }
112 return 0;
113 }
114
115 public virtual int GetSlotCount(int slotIndex) {
116 if (slotIndex >= 0
117 && slotIndex < m_slots.Count) {
118 if (m_slots[slotIndex] == 0) {
119 return 0;
120 }
121 return m_largeNumber;
122 }
123 return 0;
124 }
125
126 public virtual int GetSlotCapacity(int slotIndex, int value) {
127 if (slotIndex >= VisibleSlotsCount
128 && slotIndex < 10) {
129 return 0;
130 }
131 if (slotIndex >= 0
132 && slotIndex < OpenSlotsCount) {
133 return m_largeNumber << 1;
134 }
135 int num = Terrain.ExtractContents(value);
136 if (BlocksManager.Blocks[num].IsNonDuplicable_(value)) {
137 return m_largeNumber;
138 }
139 return m_largeNumber << 1;
140 }
141
142 public virtual int GetSlotProcessCapacity(int slotIndex, int value) {
143 int slotCount = GetSlotCount(slotIndex);
144 int slotValue = GetSlotValue(slotIndex);
145 if (slotCount > 0
146 && slotValue != 0) {
147 SubsystemBlockBehavior[] blockBehaviors = Project.FindSubsystem<SubsystemBlockBehaviors>(true)
148 .GetBlockBehaviors(Terrain.ExtractContents(slotValue));
149 for (int i = 0; i < blockBehaviors.Length; i++) {
150 int processInventoryItemCapacity = blockBehaviors[i].GetProcessInventoryItemCapacity(this, slotIndex, value);
151 if (processInventoryItemCapacity > 0) {
152 return processInventoryItemCapacity;
153 }
154 }
155 }
156 if (slotIndex < OpenSlotsCount) {
157 return 0;
158 }
159 return m_largeNumber;
160 }
161
162 public virtual void AddSlotItems(int slotIndex, int value, int count) {
163 if (slotIndex >= 0
164 && slotIndex < OpenSlotsCount) {
165 m_slots[slotIndex] = value;
166 }
167 }
168
169 public virtual void ProcessSlotItems(int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount) {
170 int slotCount = GetSlotCount(slotIndex);
171 int slotValue = GetSlotValue(slotIndex);
172 if (slotCount > 0
173 && slotValue != 0) {
174 SubsystemBlockBehavior[] blockBehaviors = Project.FindSubsystem<SubsystemBlockBehaviors>(true)
175 .GetBlockBehaviors(Terrain.ExtractContents(slotValue));
176 foreach (SubsystemBlockBehavior subsystemBlockBehavior in blockBehaviors) {
177 int processInventoryItemCapacity = subsystemBlockBehavior.GetProcessInventoryItemCapacity(this, slotIndex, value);
178 if (processInventoryItemCapacity > 0) {
179 subsystemBlockBehavior.ProcessInventoryItem(
180 this,
181 slotIndex,
182 value,
183 count,
184 MathUtils.Min(processInventoryItemCapacity, processCount),
185 out processedValue,
186 out processedCount
187 );
188 return;
189 }
190 }
191 }
192 if (slotIndex >= OpenSlotsCount) {
193 processedValue = 0;
194 processedCount = 0;
195 }
196 else {
197 processedValue = value;
198 processedCount = count;
199 }
200 }
201
202 public virtual int RemoveSlotItems(int slotIndex, int count) {
203 int num = Terrain.ExtractContents(m_slots[slotIndex]);
204 int maxStacking = BlocksManager.Blocks[num].GetMaxStacking(m_slots[slotIndex]);
205 if (slotIndex >= 0
206 && slotIndex < OpenSlotsCount) {
207 if (BlocksManager.Blocks[num].IsNonDuplicable_(m_slots[slotIndex])) {
208 m_slots[slotIndex] = 0;
209 return 1;
210 }
211 if (count >= m_largeNumber) {
212 m_slots[slotIndex] = 0;
213 return 1;
214 }
215 }
217 return MathUtils.Min(maxStacking, count);
218 }
219 return 1;
220 }
221
222 public virtual void DropAllItems(Vector3 position) { }
223 }
224}
Engine.Vector3 Vector3
static int Min(int x1, int x2)
virtual bool IsNonDuplicable_(int value)
virtual int GetDisplayOrder(int value)
virtual int GetMaxStacking(int value)
virtual IEnumerable< int > GetCreativeValues()
virtual void AddSlotItems(int slotIndex, int value, int count)
virtual void ProcessSlotItems(int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount)
virtual int GetSlotCapacity(int slotIndex, int value)
override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
virtual int GetSlotProcessCapacity(int slotIndex, int value)
override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
virtual int RemoveSlotItems(int slotIndex, int count)
实际移除的数量
static void DropSlotItems(IInventory inventory, int slotIndex, Vector3 position, Vector3 velocity)
virtual void ProcessInventoryItem(IInventory inventory, int slotIndex, int value, int count, int processCount, out int processedValue, out int processedCount)
virtual int GetProcessInventoryItemCapacity(IInventory inventory, int slotIndex, int value)
static int ExtractContents(int value)
ValuesDictionary ValuesDictionary