{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathlib.rs
More file actions
430 lines (382 loc) · 12.3 KB
/
Copy pathlib.rs
File metadata and controls
430 lines (382 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Module for creating and modifying inventories of any type.
use feather_items::{Item, ItemStack};
use once_cell::sync::Lazy;
use smallvec::{Array, SmallVec};
use std::cmp::min;
pub type SlotIndex = usize;
// Constants representing various standard inventory slot indices
pub const SLOT_CRAFTING_OUTPUT: SlotIndex = 0;
pub const SLOT_CRAFTING_INPUT_X0_Y0: SlotIndex = 1;
pub const SLOT_CRAFTING_INPUT_X1_Y0: SlotIndex = 2;
pub const SLOT_CRAFTING_INPUT_X0_Y1: SlotIndex = 3;
pub const SLOT_CRAFTING_INPUT_X1_Y1: SlotIndex = 4;
pub const SLOT_ARMOR_MIN: SlotIndex = 5;
pub const SLOT_ARMOR_MAX: SlotIndex = 8;
pub const SLOT_ARMOR_HEAD: SlotIndex = 5;
pub const SLOT_ARMOR_CHEST: SlotIndex = 6;
pub const SLOT_ARMOR_LEGS: SlotIndex = 7;
pub const SLOT_ARMOR_FEET: SlotIndex = 8;
pub const SLOT_OFFHAND: SlotIndex = 45;
pub const SLOT_INVENTORY_OFFSET: SlotIndex = 9;
pub const SLOT_HOTBAR_OFFSET: SlotIndex = 36;
pub const HOTBAR_SIZE: SlotIndex = 9;
pub const INVENTORY_SIZE: SlotIndex = 27;
pub const SLOT_ENTITY_EQUIPMENT_MAIN_HAND: SlotIndex = 0;
pub const SLOT_ENTITY_EQUIPMENT_OFF_HAND: SlotIndex = 1;
pub const SLOT_ENTITY_EQUIPMENT_BOOTS: SlotIndex = 2;
pub const SLOT_ENTITY_EQUIPMENT_LEGGINGS: SlotIndex = 3;
pub const SLOT_ENTITY_EQUIPMENT_CHESTPLATE: SlotIndex = 4;
pub const SLOT_ENTITY_EQUIPMENT_HELMET: SlotIndex = 5;
pub type Slot = Option<ItemStack>;
static COLLECT_SEARCH_ORDER: Lazy<Vec<SlotIndex>> = Lazy::new(|| {
let mut result = vec![];
for x in SLOT_HOTBAR_OFFSET..SLOT_HOTBAR_OFFSET + HOTBAR_SIZE {
result.push(x);
}
for x in SLOT_INVENTORY_OFFSET..SLOT_INVENTORY_OFFSET + INVENTORY_SIZE {
result.push(x);
}
result
});
pub fn armor_slot_to_entity_equipment(slot: SlotIndex) -> SlotIndex {
assert!(slot >= 5 && slot <= 8);
match slot {
SLOT_ARMOR_HEAD => SLOT_ENTITY_EQUIPMENT_HELMET,
SLOT_ARMOR_CHEST => SLOT_ENTITY_EQUIPMENT_CHESTPLATE,
SLOT_ARMOR_LEGS => SLOT_ENTITY_EQUIPMENT_LEGGINGS,
SLOT_ARMOR_FEET => SLOT_ENTITY_EQUIPMENT_BOOTS,
_ => unreachable!(),
}
}
/// Returns the max size of a stack with the given
/// type.
pub fn max_size(item: Item) -> u8 {
match item {
Item::WoodenSword
| Item::GoldenSword
| Item::StoneSword
| Item::IronSword
| Item::DiamondSword
| Item::WoodenAxe
| Item::GoldenAxe
| Item::StoneAxe
| Item::IronAxe
| Item::DiamondAxe
| Item::WoodenHoe
| Item::GoldenHoe
| Item::StoneHoe
| Item::IronHoe
| Item::DiamondHoe
| Item::WoodenPickaxe
| Item::GoldenPickaxe
| Item::StonePickaxe
| Item::IronPickaxe
| Item::DiamondPickaxe
| Item::WoodenShovel
| Item::GoldenShovel
| Item::StoneShovel
| Item::IronShovel
| Item::DiamondShovel
| Item::LeatherChestplate
| Item::GoldenChestplate
| Item::ChainmailChestplate
| Item::IronChestplate
| Item::DiamondChestplate
| Item::LeatherLeggings
| Item::GoldenLeggings
| Item::ChainmailLeggings
| Item::IronLeggings
| Item::DiamondLeggings
| Item::LeatherBoots
| Item::GoldenBoots
| Item::ChainmailBoots
| Item::IronBoots
| Item::DiamondBoots
| Item::LeatherHelmet
| Item::GoldenHelmet
| Item::ChainmailHelmet
| Item::IronHelmet
| Item::DiamondHelmet
| Item::Bow
| Item::WritableBook
| Item::FlintAndSteel
| Item::WhiteBed
| Item::OrangeBed
| Item::MagentaBed
| Item::LightBlueBed
| Item::YellowBed
| Item::LimeBed
| Item::PinkBed
| Item::GrayBed
| Item::LightGrayBed
| Item::CyanBed
| Item::PurpleBed
| Item::BlueBed
| Item::BrownBed
| Item::GreenBed
| Item::RedBed
| Item::BlackBed
| Item::ShulkerBox
| Item::TurtleEgg
| Item::TurtleHelmet
| Item::FishingRod
| Item::EnchantedBook
| Item::Potion
| Item::LingeringPotion
| Item::SplashPotion
| Item::WaterBucket
| Item::LavaBucket
| Item::TropicalFishBucket
| Item::CodBucket
| Item::MilkBucket
| Item::PufferfishBucket
| Item::SalmonBucket
| Item::CarrotOnAStick
| Item::Elytra
| Item::Shield
| Item::Trident
| Item::MusicDisc13
| Item::MusicDiscCat
| Item::MusicDiscBlocks
| Item::MusicDiscChirp
| Item::MusicDiscFar
| Item::MusicDiscMall
| Item::MusicDiscMellohi
| Item::MusicDiscStal
| Item::MusicDiscStrad
| Item::MusicDiscWard
| Item::MusicDisc11
| Item::MusicDiscWait
| Item::TotemOfUndying
| Item::Shears
| Item::AcaciaBoat
| Item::DarkOakBoat
| Item::OakBoat
| Item::SpruceBoat
| Item::BirchBoat
| Item::JungleBoat
| Item::MushroomStew
| Item::BeetrootSoup
| Item::RabbitStew
| Item::Cake
| Item::Minecart
| Item::ChestMinecart
| Item::CommandBlockMinecart
| Item::FurnaceMinecart
| Item::HopperMinecart
| Item::TntMinecart
| Item::DiamondHorseArmor
| Item::GoldenHorseArmor
| Item::Saddle
| Item::KnowledgeBook
| Item::DebugStick
| Item::IronHorseArmor => 1,
Item::EnderPearl
| Item::Snowball
| Item::WhiteBanner
| Item::OrangeBanner
| Item::MagentaBanner
| Item::LightBlueBanner
| Item::YellowBanner
| Item::LimeBanner
| Item::PinkBanner
| Item::GrayBanner
| Item::LightGrayBanner
| Item::CyanBanner
| Item::PurpleBanner
| Item::BlueBanner
| Item::BrownBanner
| Item::GreenBanner
| Item::RedBanner
| Item::BlackBanner
| Item::Sign
| Item::ArmorStand
| Item::Bucket
| Item::WrittenBook
| Item::Egg => 16,
_ => 64,
}
}
/// The various types of inventories ("windows").
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum InventoryType {
Player,
Container,
Chest,
CraftingTable,
Furnace,
Dispenser,
EnchantingTable,
BrewingStand,
Villager,
Beacon,
Anvil,
Hopper,
Dropper,
ShulkerBox,
Horse,
}
/// An inventory, consisting of a vector
/// of `Slot`s and a type.
#[derive(Debug, Clone)]
pub struct Inventory {
/// The item vector.
///
/// The vector always contains an entry
/// for each slot in the inventory, indexed
/// by the slot IDs. When an entry is set to
/// `None`, there is no item in the slot.
items: Vec<Option<ItemStack>>,
/// The type of this inventory.
pub ty: InventoryType,
}
impl Inventory {
/// Creates a new inventory of the given
/// type and number of slots.
pub fn new(ty: InventoryType, num_slots: u32) -> Self {
Self {
items: vec![None; num_slots as usize],
ty,
}
}
/// Retrieves a reference to the item at the given slot index.
///
/// # Panics
/// Panics if the index is out of bounds.
pub fn item_at(&self, index: SlotIndex) -> Option<&ItemStack> {
self.items[index].as_ref()
}
pub fn item_at_mut(&mut self, index: SlotIndex) -> Option<&mut ItemStack> {
self.items[index].as_mut()
}
/// Sets the item at the given slot index.
pub fn set_item_at(&mut self, index: SlotIndex, item: ItemStack) {
if item.amount == 0 {
self.items[index] = None;
} else {
self.items[index] = Some(item);
}
}
/// Clears the item at the given slot index, returning
/// the old item.
pub fn clear_item_at(&mut self, index: SlotIndex) -> Option<ItemStack> {
self.items[index].take()
}
/// Attempts to insert the given item into a player
/// inventory.
///
/// Returns the affected slots and the number of remaining
/// items which were not added to the inventory.
pub fn collect_item(&mut self, mut item: ItemStack) -> (SmallVec<[SlotIndex; 2]>, u8) {
let mut affected_slots = SmallVec::new();
// First, look for slots already having the type.
for slot in COLLECT_SEARCH_ORDER.iter() {
if let Some(slot_item) = self.item_at(*slot).cloned() {
if slot_item.ty == item.ty {
self.add_to_stack(&mut item, slot_item, *slot, &mut affected_slots);
if item.amount == 0 {
return (affected_slots, 0);
}
}
}
}
for slot in COLLECT_SEARCH_ORDER.iter() {
let slot_item = self.item_at(*slot).cloned();
if slot_item.is_none() {
let fake = ItemStack::new(item.ty, 0);
self.add_to_stack(&mut item, fake, *slot, &mut affected_slots);
if item.amount == 0 {
return (affected_slots, 0);
}
}
if let Some(slot_item) = slot_item {
if slot_item.ty == item.ty {
self.add_to_stack(&mut item, slot_item, *slot, &mut affected_slots);
if item.amount == 0 {
return (affected_slots, 0);
}
}
}
}
(affected_slots, item.amount)
}
/// Adds an item to a stack.
fn add_to_stack<A: Array<Item = SlotIndex>>(
&mut self,
item: &mut ItemStack,
slot_item: ItemStack,
slot: SlotIndex,
affected_slots: &mut SmallVec<A>,
) {
let added = min(item.amount, max_size(item.ty) - slot_item.amount);
item.amount -= added;
self.set_item_at(slot, ItemStack::new(slot_item.ty, slot_item.amount + added));
affected_slots.push(slot);
}
/// Returns the number of slots in this inventory.
pub fn slot_count(&self) -> u16 {
self.items.len() as u16
}
/// Returns a reference to this inventory's items.
pub fn items(&self) -> &[Option<ItemStack>] {
&self.items
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_inventory() {
let mut inv = Inventory::new(InventoryType::Chest, 36);
assert_eq!(inv.slot_count(), 36);
inv.set_item_at(0, ItemStack::new(Item::Air, 1));
let item = inv.item_at(0).unwrap();
assert_eq!(item.ty, Item::Air);
assert_eq!(item.amount, 1);
let item = inv.item_at_mut(0).unwrap();
item.ty = Item::Sponge;
assert_eq!(inv.item_at(0).unwrap().ty, Item::Sponge);
inv.clear_item_at(0);
assert!(inv.item_at(0).is_none());
}
#[test]
fn test_collect_item_basic() {
let mut inv = Inventory::new(InventoryType::Player, 46);
let item = ItemStack::new(Item::Cobblestone, 32);
inv.collect_item(item.clone());
assert_eq!(inv.item_at(SLOT_HOTBAR_OFFSET).unwrap(), &item);
}
#[test]
fn test_collect_item_full() {
let mut inv = Inventory::new(InventoryType::Player, 46);
let item = ItemStack::new(Item::DriedKelpBlock, 64);
for i in 0..46 {
inv.set_item_at(i, item.clone());
}
inv.collect_item(ItemStack::new(Item::Cobblestone, 16));
for i in 0..46 {
assert_eq!(inv.item_at(i).unwrap(), &item);
}
}
#[test]
fn test_collect_item_type_already_in() {
let mut inv = Inventory::new(InventoryType::Player, 46);
let item = ItemStack::new(Item::Cobblestone, 33);
inv.set_item_at(31, item.clone());
inv.collect_item(item.clone());
assert_eq!(inv.item_at(31).unwrap(), &ItemStack::new(item.ty, 64));
assert_eq!(
inv.item_at(SLOT_HOTBAR_OFFSET).unwrap(),
&ItemStack::new(item.ty, 2)
);
}
#[test]
fn test_collect_item_overstack() {
let mut inv = Inventory::new(InventoryType::Player, 46);
let item = ItemStack::new(Item::DiamondSword, 1);
inv.set_item_at(SLOT_HOTBAR_OFFSET, item.clone());
inv.collect_item(item.clone());
assert_eq!(inv.item_at(SLOT_HOTBAR_OFFSET).unwrap(), &item);
assert_eq!(inv.item_at(SLOT_HOTBAR_OFFSET + 1).unwrap(), &item);
}
}
You can’t perform that action at this time.
