@@ -35,11 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
35
35
36
36
static content_t content_translate_from_19_to_internal (content_t c_from)
37
37
{
38
- for (u32 i=0 ; i<sizeof (trans_table_19)/sizeof (trans_table_19[0 ]); i++)
39
- {
40
- if (trans_table_19[i][1 ] == c_from)
41
- {
42
- return trans_table_19[i][0 ];
38
+ for (const auto &tt : trans_table_19) {
39
+ if (tt[1 ] == c_from) {
40
+ return tt[0 ];
43
41
}
44
42
}
45
43
return c_from;
@@ -116,7 +114,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
116
114
NameIdMapping legacy_nimap;
117
115
content_mapnode_get_name_id_mapping (&legacy_nimap);
118
116
legacy_nimap.getName (material, name);
119
- if (name == " " )
117
+ if (name. empty () )
120
118
name = " unknown_block" ;
121
119
if (itemdef)
122
120
name = itemdef->getAlias (name);
@@ -136,7 +134,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
136
134
NameIdMapping legacy_nimap;
137
135
content_mapnode_get_name_id_mapping (&legacy_nimap);
138
136
legacy_nimap.getName (material, name);
139
- if (name == " " )
137
+ if (name. empty () )
140
138
name = " unknown_block" ;
141
139
if (itemdef)
142
140
name = itemdef->getAlias (name);
@@ -207,21 +205,20 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
207
205
// Read the count
208
206
std::string count_str;
209
207
std::getline (is, count_str, ' ' );
210
- if (count_str.empty ())
211
- {
208
+ if (count_str.empty ()) {
212
209
count = 1 ;
213
210
break ;
214
211
}
215
- else
216
- count = stoi (count_str);
212
+
213
+ count = stoi (count_str);
217
214
218
215
// Read the wear
219
216
std::string wear_str;
220
217
std::getline (is, wear_str, ' ' );
221
218
if (wear_str.empty ())
222
219
break ;
223
- else
224
- wear = stoi (wear_str);
220
+
221
+ wear = stoi (wear_str);
225
222
226
223
// Read metadata
227
224
metadata.deSerialize (is);
@@ -388,17 +385,12 @@ InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager
388
385
clearItems ();
389
386
}
390
387
391
- InventoryList::~InventoryList ()
392
- {
393
- }
394
-
395
388
void InventoryList::clearItems ()
396
389
{
397
390
m_items.clear ();
398
391
399
- for (u32 i=0 ; i<m_size; i++)
400
- {
401
- m_items.push_back (ItemStack ());
392
+ for (u32 i=0 ; i < m_size; i++) {
393
+ m_items.emplace_back ();
402
394
}
403
395
404
396
// setDirty(true);
@@ -427,15 +419,10 @@ void InventoryList::serialize(std::ostream &os) const
427
419
428
420
os<<" Width " <<m_width<<" \n " ;
429
421
430
- for (u32 i=0 ; i<m_items.size (); i++)
431
- {
432
- const ItemStack &item = m_items[i];
433
- if (item.empty ())
434
- {
422
+ for (const auto &item : m_items) {
423
+ if (item.empty ()) {
435
424
os<<" Empty" ;
436
- }
437
- else
438
- {
425
+ } else {
439
426
os<<" Item " ;
440
427
item.serialize (os);
441
428
}
@@ -464,17 +451,16 @@ void InventoryList::deSerialize(std::istream &is)
464
451
std::string name;
465
452
std::getline (iss, name, ' ' );
466
453
467
- if (name == " EndInventoryList" )
468
- {
454
+ if (name == " EndInventoryList" ) {
469
455
break ;
470
456
}
457
+
471
458
// This is a temporary backwards compatibility fix
472
- else if (name == " end" )
473
- {
459
+ if (name == " end" ) {
474
460
break ;
475
461
}
476
- else if (name == " Width " )
477
- {
462
+
463
+ if (name == " Width " ) {
478
464
iss >> m_width;
479
465
if (iss.fail ())
480
466
throw SerializationError (" incorrect width property" );
@@ -551,9 +537,8 @@ u32 InventoryList::getWidth() const
551
537
u32 InventoryList::getUsedSlots () const
552
538
{
553
539
u32 num = 0 ;
554
- for (u32 i=0 ; i<m_items.size (); i++)
555
- {
556
- if (!m_items[i].empty ())
540
+ for (const auto &m_item : m_items) {
541
+ if (!m_item.empty ())
557
542
num++;
558
543
}
559
544
return num;
@@ -672,20 +657,17 @@ bool InventoryList::roomForItem(const ItemStack &item_) const
672
657
bool InventoryList::containsItem (const ItemStack &item, bool match_meta) const
673
658
{
674
659
u32 count = item.count ;
675
- if (count == 0 )
660
+ if (count == 0 )
676
661
return true ;
677
- for (std::vector<ItemStack>::const_reverse_iterator
678
- i = m_items.rbegin ();
679
- i != m_items.rend (); ++i)
680
- {
681
- if (count == 0 )
662
+
663
+ for (auto i = m_items.rbegin (); i != m_items.rend (); ++i) {
664
+ if (count == 0 )
682
665
break ;
683
- if (i->name == item.name
684
- && (!match_meta || (i->metadata == item.metadata ))) {
666
+ if (i->name == item.name && (!match_meta || (i->metadata == item.metadata ))) {
685
667
if (i->count >= count)
686
668
return true ;
687
- else
688
- count -= i->count ;
669
+
670
+ count -= i->count ;
689
671
}
690
672
}
691
673
return false ;
@@ -694,15 +676,11 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
694
676
ItemStack InventoryList::removeItem (const ItemStack &item)
695
677
{
696
678
ItemStack removed;
697
- for (std::vector<ItemStack>::reverse_iterator
698
- i = m_items.rbegin ();
699
- i != m_items.rend (); ++i)
700
- {
701
- if (i->name == item.name )
702
- {
679
+ for (auto i = m_items.rbegin (); i != m_items.rend (); ++i) {
680
+ if (i->name == item.name ) {
703
681
u32 still_to_remove = item.count - removed.count ;
704
682
removed.addItem (i->takeItem (still_to_remove), m_itemdef);
705
- if (removed.count == item.count )
683
+ if (removed.count == item.count )
706
684
break ;
707
685
}
708
686
}
@@ -815,21 +793,17 @@ Inventory::~Inventory()
815
793
void Inventory::clear ()
816
794
{
817
795
m_dirty = true ;
818
- for (u32 i=0 ; i<m_lists.size (); i++)
819
- {
820
- delete m_lists[i];
796
+ for (auto &m_list : m_lists) {
797
+ delete m_list;
821
798
}
822
799
m_lists.clear ();
823
800
}
824
801
825
802
void Inventory::clearContents ()
826
803
{
827
804
m_dirty = true ;
828
- for (u32 i=0 ; i<m_lists.size (); i++)
829
- {
830
- InventoryList *list = m_lists[i];
831
- for (u32 j=0 ; j<list->getSize (); j++)
832
- {
805
+ for (InventoryList *list : m_lists) {
806
+ for (u32 j=0 ; j<list->getSize (); j++) {
833
807
list->deleteItem (j);
834
808
}
835
809
}
@@ -855,9 +829,8 @@ Inventory & Inventory::operator = (const Inventory &other)
855
829
m_dirty = true ;
856
830
clear ();
857
831
m_itemdef = other.m_itemdef ;
858
- for (u32 i=0 ; i<other.m_lists .size (); i++)
859
- {
860
- m_lists.push_back (new InventoryList (*other.m_lists [i]));
832
+ for (InventoryList *list : other.m_lists ) {
833
+ m_lists.push_back (new InventoryList (*list));
861
834
}
862
835
}
863
836
return *this ;
@@ -878,9 +851,7 @@ bool Inventory::operator == (const Inventory &other) const
878
851
879
852
void Inventory::serialize (std::ostream &os) const
880
853
{
881
- for (u32 i=0 ; i<m_lists.size (); i++)
882
- {
883
- InventoryList *list = m_lists[i];
854
+ for (InventoryList *list : m_lists) {
884
855
os<<" List " <<list->getName ()<<" " <<list->getSize ()<<" \n " ;
885
856
list->serialize (os);
886
857
}
@@ -902,17 +873,16 @@ void Inventory::deSerialize(std::istream &is)
902
873
std::string name;
903
874
std::getline (iss, name, ' ' );
904
875
905
- if (name == " EndInventory" )
906
- {
876
+ if (name == " EndInventory" ) {
907
877
break ;
908
878
}
879
+
909
880
// This is a temporary backwards compatibility fix
910
- else if (name == " end" )
911
- {
881
+ if (name == " end" ) {
912
882
break ;
913
883
}
914
- else if (name == " List " )
915
- {
884
+
885
+ if (name == " List " ) {
916
886
std::string listname;
917
887
u32 listsize;
918
888
@@ -944,15 +914,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
944
914
}
945
915
return m_lists[i];
946
916
}
947
- else
948
- {
949
- // don't create list with invalid name
950
- if (name.find (" " ) != std::string::npos) return NULL ;
951
917
952
- InventoryList *list = new InventoryList (name, size, m_itemdef);
953
- m_lists.push_back (list);
954
- return list;
955
- }
918
+
919
+ // don't create list with invalid name
920
+ if (name.find (' ' ) != std::string::npos) return NULL ;
921
+
922
+ InventoryList *list = new InventoryList (name, size, m_itemdef);
923
+ m_lists.push_back (list);
924
+ return list;
956
925
}
957
926
958
927
InventoryList * Inventory::getList (const std::string &name)
@@ -966,9 +935,7 @@ InventoryList * Inventory::getList(const std::string &name)
966
935
std::vector<const InventoryList*> Inventory::getLists ()
967
936
{
968
937
std::vector<const InventoryList*> lists;
969
- for (u32 i=0 ; i<m_lists.size (); i++)
970
- {
971
- InventoryList *list = m_lists[i];
938
+ for (auto list : m_lists) {
972
939
lists.push_back (list);
973
940
}
974
941
return lists;
0 commit comments