Added jsonSerialize() to Item and fixed some crafting data decode issues

This commit is contained in:
Dylan K. Taylor 2016-11-08 11:19:51 +00:00
parent bee6c546dc
commit 911529a6cc
2 changed files with 40 additions and 39 deletions

View File

@ -38,7 +38,7 @@ use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\Config;
class Item implements ItemIds{
class Item implements ItemIds, \JsonSerializable{
/** @var NBT */
private static $cachedParser = null;
@ -738,10 +738,6 @@ class Item implements ItemIds{
return false;
}
final public function __toString() : string{
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x" . $this->count . ($this->hasCompoundTag() ? " tags:0x" . bin2hex($this->getCompoundTag()) : "");
}
public function getDestroySpeed(Block $block, Player $player){
return 1;
}
@ -764,4 +760,17 @@ class Item implements ItemIds{
return false;
}
final public function __toString() : string{
return "Item " . $this->name . " (" . $this->id . ":" . ($this->meta === null ? "?" : $this->meta) . ")x" . $this->count . ($this->hasCompoundTag() ? " tags:0x" . bin2hex($this->getCompoundTag()) : "");
}
final public function jsonSerialize(){
return [
"id" => $this->id,
"damage" => $this->meta,
"count" => $this->count, //TODO: separate items and stacks
"nbt" => $this->tags
];
}
}

View File

@ -52,68 +52,60 @@ class CraftingDataPacket extends DataPacket{
}
public function decode(){
$count = $this->getUnsignedVarInt();
for($i = 0; $i < $count; ++$i){
$recipeType = $this->getUnsignedVarInt();
$entries = [];
$recipeCount = $this->getUnsignedVarInt();
for($i = 0; $i < $recipeCount; ++$i){
$entry = [];
$entry["type"] = $recipeType = $this->getVarInt();
switch($recipeType){
case self::ENTRY_SHAPELESS:
$ingredientCount = $this->getUnsignedVarInt();
/** @var Item */
$ingredients = [];
$entry["input"] = [];
for($j = 0; $j < $ingredientCount; ++$j){
$ingredients[] = $this->getSlot();
$entry["input"][] = $this->getSlot();
}
$resultCount = $this->getUnsignedVarInt();
$results = [];
$entry["output"] = [];
for($k = 0; $k < $resultCount; ++$k){
$results[] = $this->getSlot();
}
$uuid = $this->getUUID();
$recipe = new ShapelessRecipe($results[0]); //ouch...
$recipe->setId($uuid);
foreach($ingredients as $ingr){
$recipe->addIngredient($ingr);
$entry["output"][] = $this->getSlot();
}
$entry["uuid"] = $this->getUUID()->toString();
break;
case self::ENTRY_SHAPED:
$width = $this->getVarInt();
$height = $this->getVarInt();
$count = $width * $height;
$ingredients = [];
$entry["width"] = $this->getVarInt();
$entry["height"] = $this->getVarInt();
$count = $entry["width"] * $entry["height"];
$entry["input"] = [];
for($j = 0; $j < $count; ++$j){
$ingredients[] = $this->getSlot();
$entry["input"][] = $this->getSlot();
}
$resultCount = $this->getUnsignedVarInt();
$results = [];
$entry["output"] = [];
for($k = 0; $k < $resultCount; ++$k){
$results[] = $this->getSlot();
}
$uuid = $this->getUUID();
$recipe = new ShapedRecipe($results[0], $height, $width); //yes, blatant copy-paste...
$recipe->setId($uuid);
foreach($ingredients as $ingr){
$recipe->addIngredient($ingr);
$entry["output"][] = $this->getSlot();
}
$entry["uuid"] = $this->getUUID()->toString();
break;
case self::ENTRY_FURNACE:
case self::ENTRY_FURNACE_DATA:
$inputId = $this->getVarInt();
$entry["inputId"] = $this->getVarInt();
if($recipeType === self::ENTRY_FURNACE_DATA){
$inputData = $this->getVarInt();
$entry["inputDamage"] = $this->getVarInt();
}
$result = $this->getSlot();
$recipe = new FurnaceRecipe(Item::get($inputId, $inputData ?? null), $result);
$entry["output"] = $this->getSlot();
break;
case self::ENTRY_MULTI:
$uuid = $this->getUUID();
$recipe = new MultiRecipe($uuid);
$entry["uuid"] = $this->getUUID()->toString();
break;
default:
throw new \UnexpectedValueException("Unhandled recipe type $recipeType!"); //do not continue attempting to decode
}
$this->entries[] = $recipe;
$entries[] = $entry;
}
//TODO: serialize to json
$this->getBool(); //cleanRecipes
}
private static function writeEntry($entry, BinaryStream $stream){