the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 195 lines 4.9 kB view raw
1#include "stdafx.h" 2#include "net.minecraft.world.item.trading.h" 3#include "MerchantRecipeList.h" 4 5MerchantRecipeList::MerchantRecipeList() 6{ 7} 8 9MerchantRecipeList::MerchantRecipeList(CompoundTag *tag) 10{ 11 load(tag); 12} 13 14MerchantRecipeList::~MerchantRecipeList() 15{ 16 for(AUTO_VAR(it, m_recipes.begin()); it != m_recipes.end(); ++it) 17 { 18 delete (*it); 19 } 20} 21 22MerchantRecipe *MerchantRecipeList::getRecipeFor(shared_ptr<ItemInstance> buyA, shared_ptr<ItemInstance> buyB, int selectionHint) 23{ 24 if (selectionHint > 0 && selectionHint < m_recipes.size()) 25 { 26 // attempt to match vs the hint 27 MerchantRecipe *r = m_recipes.at(selectionHint); 28 if (buyA->id == r->getBuyAItem()->id && ((buyB == NULL && !r->hasSecondaryBuyItem()) || (r->hasSecondaryBuyItem() && buyB != NULL && r->getBuyBItem()->id == buyB->id))) 29 { 30 if (buyA->count >= r->getBuyAItem()->count && (!r->hasSecondaryBuyItem() || buyB->count >= r->getBuyBItem()->count)) 31 { 32 return r; 33 } 34 } 35 return NULL; 36 } 37 for (int i = 0; i < m_recipes.size(); i++) 38 { 39 MerchantRecipe *r = m_recipes.at(i); 40 if (buyA->id == r->getBuyAItem()->id && buyA->count >= r->getBuyAItem()->count 41 && ((!r->hasSecondaryBuyItem() && buyB == NULL) || (r->hasSecondaryBuyItem() && buyB != NULL && r->getBuyBItem()->id == buyB->id && buyB->count >= r->getBuyBItem()->count))) 42 { 43 return r; 44 } 45 } 46 return NULL; 47} 48 49bool MerchantRecipeList::addIfNewOrBetter(MerchantRecipe *recipe) 50{ 51 bool added = false; 52 for (int i = 0; i < m_recipes.size(); i++) 53 { 54 MerchantRecipe *r = m_recipes.at(i); 55 if (recipe->isSame(r)) 56 { 57 if (recipe->isSameSameButBetter(r)) 58 { 59 delete m_recipes[i]; 60 m_recipes[i] = recipe; 61 added = true; 62 } 63 return added; 64 } 65 } 66 m_recipes.push_back(recipe); 67 return true; 68} 69 70MerchantRecipe *MerchantRecipeList::getMatchingRecipeFor(shared_ptr<ItemInstance> buy, shared_ptr<ItemInstance> buyB, shared_ptr<ItemInstance> sell) 71{ 72 for (int i = 0; i < m_recipes.size(); i++) 73 { 74 MerchantRecipe *r = m_recipes.at(i); 75 if (buy->id == r->getBuyAItem()->id && buy->count >= r->getBuyAItem()->count && sell->id == r->getSellItem()->id) 76 { 77 if (!r->hasSecondaryBuyItem() || (buyB != NULL && buyB->id == r->getBuyBItem()->id && buyB->count >= r->getBuyBItem()->count)) 78 { 79 return r; 80 } 81 } 82 } 83 return NULL; 84} 85 86void MerchantRecipeList::writeToStream(DataOutputStream *stream) 87{ 88 stream->writeByte((byte) (m_recipes.size() & 0xff)); 89 for (int i = 0; i < m_recipes.size(); i++) 90 { 91 MerchantRecipe *r = m_recipes.at(i); 92 Packet::writeItem(r->getBuyAItem(), stream); 93 Packet::writeItem(r->getSellItem(), stream); 94 95 shared_ptr<ItemInstance> buyBItem = r->getBuyBItem(); 96 stream->writeBoolean(buyBItem != NULL); 97 if (buyBItem != NULL) 98 { 99 Packet::writeItem(buyBItem, stream); 100 } 101 stream->writeBoolean(r->isDeprecated()); 102 stream->writeInt(r->getUses()); 103 stream->writeInt(r->getMaxUses()); 104 } 105} 106 107MerchantRecipeList *MerchantRecipeList::createFromStream(DataInputStream *stream) 108{ 109 MerchantRecipeList *list = new MerchantRecipeList(); 110 111 int count = (int) (stream->readByte() & 0xff); 112 for (int i = 0; i < count; i++) 113 { 114 shared_ptr<ItemInstance> buy = Packet::readItem(stream); 115 shared_ptr<ItemInstance> sell = Packet::readItem(stream); 116 117 shared_ptr<ItemInstance> buyB = nullptr; 118 if (stream->readBoolean()) 119 { 120 buyB = Packet::readItem(stream); 121 } 122 bool isDeprecated = stream->readBoolean(); 123 int uses = stream->readInt(); 124 int maxUses = stream->readInt(); 125 126 MerchantRecipe *recipe = new MerchantRecipe(buy, buyB, sell, uses, maxUses); 127 if (isDeprecated) 128 { 129 recipe->enforceDeprecated(); 130 } 131 list->push_back(recipe); 132 } 133 return list; 134} 135 136void MerchantRecipeList::load(CompoundTag *tag) 137{ 138 ListTag<CompoundTag> *list = (ListTag<CompoundTag> *) tag->getList(L"Recipes"); 139 140 for (int i = 0; i < list->size(); i++) 141 { 142 CompoundTag *recipeTag = list->get(i); 143 m_recipes.push_back(new MerchantRecipe(recipeTag)); 144 } 145} 146 147CompoundTag *MerchantRecipeList::createTag() 148{ 149 CompoundTag *tag = new CompoundTag(); 150 151 ListTag<CompoundTag> *list = new ListTag<CompoundTag>(L"Recipes"); 152 for (int i = 0; i < m_recipes.size(); i++) 153 { 154 MerchantRecipe *merchantRecipe = m_recipes.at(i); 155 list->add(merchantRecipe->createTag()); 156 } 157 tag->put(L"Recipes", list); 158 159 return tag; 160} 161 162void MerchantRecipeList::push_back(MerchantRecipe *recipe) 163{ 164 m_recipes.push_back(recipe); 165} 166 167MerchantRecipe *MerchantRecipeList::at(size_t index) 168{ 169 return m_recipes.at(index); 170} 171 172std::vector<MerchantRecipe *>::iterator MerchantRecipeList::begin() 173{ 174 return m_recipes.begin(); 175} 176 177std::vector<MerchantRecipe *>::iterator MerchantRecipeList::end() 178{ 179 return m_recipes.end(); 180} 181 182std::vector<MerchantRecipe *>::iterator MerchantRecipeList::erase(std::vector<MerchantRecipe *>::iterator it) 183{ 184 return m_recipes.erase(it); 185} 186 187size_t MerchantRecipeList::size() 188{ 189 return m_recipes.size(); 190} 191 192bool MerchantRecipeList::empty() 193{ 194 return m_recipes.empty(); 195}