2016-03-16 14:25:35 -04:00
|
|
|
// Copyright 2016 Matthew Collins
|
2015-09-17 11:21:56 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-09-12 15:31:26 -04:00
|
|
|
|
|
|
|
use nbt;
|
2015-10-07 14:36:59 -04:00
|
|
|
use protocol::Serializable;
|
2015-09-12 15:31:26 -04:00
|
|
|
use std::io;
|
2015-09-17 11:04:25 -04:00
|
|
|
use byteorder::{BigEndian, WriteBytesExt, ReadBytesExt};
|
2015-09-12 15:31:26 -04:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Stack {
|
|
|
|
id: isize,
|
|
|
|
count: isize,
|
|
|
|
damage: isize,
|
|
|
|
tag: Option<nbt::NamedTag>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for Stack {
|
|
|
|
fn default() -> Stack {
|
|
|
|
Stack {
|
|
|
|
id: -1,
|
|
|
|
count: 0,
|
|
|
|
damage: 0,
|
|
|
|
tag: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serializable for Option<Stack> {
|
|
|
|
fn read_from(buf: &mut io::Read) -> Result<Option<Stack>, io::Error> {
|
|
|
|
let id = try!(buf.read_i16::<BigEndian>());
|
|
|
|
if id == -1 {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2015-10-07 14:36:59 -04:00
|
|
|
Ok(Some(Stack {
|
2015-09-12 15:31:26 -04:00
|
|
|
id: id as isize,
|
|
|
|
count: try!(buf.read_u8()) as isize,
|
|
|
|
damage: try!(buf.read_i16::<BigEndian>()) as isize,
|
|
|
|
tag: try!(Serializable::read_from(buf)),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
fn write_to(&self, buf: &mut io::Write) -> Result<(), io::Error> {
|
|
|
|
match *self {
|
|
|
|
Some(ref val) => {
|
|
|
|
try!(buf.write_i16::<BigEndian>(val.id as i16));
|
|
|
|
try!(buf.write_u8(val.count as u8));
|
|
|
|
try!(buf.write_i16::<BigEndian>(val.damage as i16));
|
|
|
|
try!(val.tag.write_to(buf));
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2015-09-12 15:31:26 -04:00
|
|
|
None => try!(buf.write_i16::<BigEndian>(-1)),
|
|
|
|
}
|
|
|
|
Result::Ok(())
|
|
|
|
}
|
|
|
|
}
|