Exemplo n.º 1
0
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;
        }
      }
    }
  }
}
Exemplo n.º 2
0
void NBT_Value::Write(std::vector<uint8> &buffer)
{
  int storeAt = buffer.size();;
  switch(m_type)
  {
  case TAG_BYTE:
    buffer.push_back(m_value.byteVal);
    break;
  case TAG_SHORT:
    buffer.resize(storeAt + 2);
    putSint16(&buffer[storeAt], m_value.shortVal);
    break;
  case TAG_INT:
    buffer.resize(storeAt + 4);
    putSint32(&buffer[storeAt], m_value.intVal);
    break;
  case TAG_LONG:
    buffer.resize(storeAt + 8);
    putSint64(&buffer[storeAt], m_value.longVal);
    break;
  case TAG_FLOAT:
    buffer.resize(storeAt + 4);
    putFloat(&buffer[storeAt], m_value.floatVal);
    break;
  case TAG_DOUBLE:
    buffer.resize(storeAt + 8);
    putDouble(&buffer[storeAt], m_value.doubleVal);
    break;
  case TAG_BYTE_ARRAY:
    {
      int arraySize = m_value.byteArrayVal ? m_value.byteArrayVal->size() : 0;
      buffer.resize(storeAt + 4 + arraySize);
      putSint32(&buffer[storeAt], arraySize);
      storeAt += 4;
      if(arraySize)
        memcpy(&buffer[storeAt], &(*m_value.byteArrayVal)[0], arraySize);
      break;
    }
  case TAG_STRING:
    {
      int stringLen = m_value.stringVal ? m_value.stringVal->size() : 0;
      buffer.resize(storeAt + 2 + stringLen);
      putSint16(&buffer[storeAt], (sint16)stringLen);
      storeAt += 2;
      if(stringLen>0)
        memcpy(&buffer[storeAt], m_value.stringVal->c_str(), stringLen);
      break;
    }
  case TAG_LIST:
    {
      buffer.resize(storeAt + 5);
      int listCount = m_value.listVal.data ? m_value.listVal.data->size() : 0;
      buffer[storeAt] = m_value.listVal.type;
      storeAt++;
      putSint32(&buffer[storeAt], listCount);
      for(int i=0;i<listCount;i++)
        (*m_value.listVal.data)[i]->Write(buffer);
      break;
    }
  case TAG_COMPOUND:
    {
      int compoundCount = m_value.compoundVal ? m_value.compoundVal->size() : 0;
      if(compoundCount)
      {
        std::map<std::string, NBT_Value*>::iterator iter = m_value.compoundVal->begin(), end = m_value.compoundVal->end();
        for( ; iter != end; iter++)
        {
          const std::string &key = iter->first;
          int keySize = key.size();
          NBT_Value *val = iter->second;
          int curPos = buffer.size();
          buffer.resize(curPos + 3 + keySize);
          buffer[curPos] = (uint8)val->GetType();
          curPos++;
          putSint16(&buffer[curPos], keySize);
          curPos += 2;
          if(keySize)
            memcpy(&buffer[curPos], key.c_str(), keySize);
          val->Write(buffer);
        }
      }
      buffer.push_back(TAG_END);
      break;
    }
  case TAG_END:
    break; //for completeness
  }
}