void BlockChest::onStartedDigging(User* user, int8_t status, int32_t x, int16_t y, int32_t z, int map, int8_t direction) { // Locksystem if (user->inv[36 + user->currentItemSlot()].getType() == ITEM_WOODEN_AXE) { int chunk_x = blockToChunk(x); int chunk_z = blockToChunk(z); sChunk* chunk = ServerInstance->map(map)->loadMap(chunk_x, chunk_z); if (chunk == NULL) { return; } NBT_Value* entityList = (*(*(chunk->nbt))["Level"])["TileEntities"]; if (!entityList) { entityList = new NBT_Value(NBT_Value::TAG_LIST, NBT_Value::TAG_COMPOUND); chunk->nbt->Insert("TileEntities", entityList); } if (entityList->GetType() == NBT_Value::TAG_LIST) { if (entityList->GetListType() != NBT_Value::TAG_COMPOUND) { entityList->SetType(NBT_Value::TAG_LIST, NBT_Value::TAG_COMPOUND); } std::vector<NBT_Value*> *entities = entityList->GetList(); std::vector<NBT_Value*>::iterator iter = entities->begin(), end = entities->end(); //bool done = false; // Unused variable for (; iter != end; iter++) { if ((**iter)["x"] == NULL || (**iter)["y"] == NULL || (**iter)["z"] == NULL || (**iter)["x"]->GetType() != NBT_Value::TAG_INT || (**iter)["y"]->GetType() != NBT_Value::TAG_INT || (**iter)["z"]->GetType() != NBT_Value::TAG_INT) { continue; } if ((int32_t)(*(**iter)["x"]) == x && (int32_t)(*(**iter)["y"]) == y && (int32_t)(*(**iter)["z"]) == z) { int8_t locked; NBT_Value* nbtLockdata = (**iter)["Lockdata"]; if (nbtLockdata != NULL) { std::string player = *(*nbtLockdata)["player"]->GetString(); // Toggle lock if player is the owner of block if (player == user->nick) { locked = *(*nbtLockdata)["locked"]; locked = (locked == 1) ? 0 : 1; *(*nbtLockdata)["locked"] = locked; if (locked == 1) { ServerInstance->chat()->sendMsg(user, MC_COLOR_RED + "Chest locked", Chat::USER); } else { ServerInstance->chat()->sendMsg(user, MC_COLOR_RED + "Chest opened", Chat::USER); } } } else { // If lockdata is missing (old chest) NBT_Value* nbtLock = new NBT_Value(NBT_Value::TAG_COMPOUND); nbtLock->Insert("player", new NBT_Value(user->nick)); nbtLock->Insert("locked", new NBT_Value((int8_t)1)); (*iter)->Insert("Lockdata", nbtLock); } break; } } } } }
bool User::saveData() { std::string outfile = Map::get().mapDirectory+"/players/"+this->nick+".dat"; // Try to create parent directories if necessary struct stat stFileInfo; if(stat(outfile.c_str(), &stFileInfo) != 0) { std::string outdir = Map::get().mapDirectory+"/players"; if(stat(outdir.c_str(), &stFileInfo) != 0) { #ifdef WIN32 if(_mkdir(outdir.c_str()) == -1) #else if(mkdir(outdir.c_str(), 0755) == -1) #endif return false; } } NBT_Value val(NBT_Value::TAG_COMPOUND); val.Insert("OnGround", new NBT_Value((sint8)1)); val.Insert("Air", new NBT_Value((sint16)300)); val.Insert("AttackTime", new NBT_Value((sint16)0)); val.Insert("DeathTime", new NBT_Value((sint16)0)); val.Insert("Fire", new NBT_Value((sint16)-20)); val.Insert("Health", new NBT_Value((sint16)20)); val.Insert("HurtTime", new NBT_Value((sint16)0)); val.Insert("FallDistance", new NBT_Value(54.f)); NBT_Value *nbtInv = new NBT_Value(NBT_Value::TAG_LIST, NBT_Value::TAG_COMPOUND); //Start with main items Item *slots = (Item *)&inv.main; char slotid = 0; char itemslot = 0; for(int i = 0; i < 36+4+4; i++) { //Crafting items after main if(i == 36) { slots = (Item *)&inv.crafting; itemslot = 80; slotid = 0; } //Equipped items last else if(i == 36+4) { slots = (Item *)&inv.equipped; itemslot = 100; slotid = 0; } if(slots[(uint8)slotid].count) { NBT_Value *val = new NBT_Value(NBT_Value::TAG_COMPOUND); val->Insert("Count", new NBT_Value((sint8)slots[(uint8)slotid].count)); val->Insert("Slot", new NBT_Value((sint8)itemslot)); val->Insert("Damage", new NBT_Value((sint16)slots[(uint8)slotid].health)); val->Insert("id", new NBT_Value((sint16)slots[(uint8)slotid].type)); nbtInv->GetList()->push_back(val); } slotid++; itemslot++; } val.Insert("Inventory", nbtInv); NBT_Value *nbtPos = new NBT_Value(NBT_Value::TAG_LIST, NBT_Value::TAG_DOUBLE); nbtPos->GetList()->push_back(new NBT_Value((double)pos.x)); nbtPos->GetList()->push_back(new NBT_Value((double)pos.y)); nbtPos->GetList()->push_back(new NBT_Value((double)pos.z)); val.Insert("Pos", nbtPos); NBT_Value *nbtRot = new NBT_Value(NBT_Value::TAG_LIST, NBT_Value::TAG_FLOAT); nbtRot->GetList()->push_back(new NBT_Value((float)pos.yaw)); nbtRot->GetList()->push_back(new NBT_Value((float)pos.pitch)); val.Insert("Rotation", nbtRot); NBT_Value *nbtMotion = new NBT_Value(NBT_Value::TAG_LIST, NBT_Value::TAG_DOUBLE); nbtMotion->GetList()->push_back(new NBT_Value((double)0.0)); nbtMotion->GetList()->push_back(new NBT_Value((double)0.0)); nbtMotion->GetList()->push_back(new NBT_Value((double)0.0)); val.Insert("Motion", nbtMotion); val.SaveToFile(outfile); return true; }