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\Server;
use pocketmine\utils\Config; use pocketmine\utils\Config;
class Item implements ItemIds{ class Item implements ItemIds, \JsonSerializable{
/** @var NBT */ /** @var NBT */
private static $cachedParser = null; private static $cachedParser = null;
@ -738,10 +738,6 @@ class Item implements ItemIds{
return false; 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){ public function getDestroySpeed(Block $block, Player $player){
return 1; return 1;
} }
@ -764,4 +760,17 @@ class Item implements ItemIds{
return false; 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(){ public function decode(){
$count = $this->getUnsignedVarInt(); $entries = [];
for($i = 0; $i < $count; ++$i){ $recipeCount = $this->getUnsignedVarInt();
$recipeType = $this->getUnsignedVarInt(); for($i = 0; $i < $recipeCount; ++$i){
$entry = [];
$entry["type"] = $recipeType = $this->getVarInt();
switch($recipeType){ switch($recipeType){
case self::ENTRY_SHAPELESS: case self::ENTRY_SHAPELESS:
$ingredientCount = $this->getUnsignedVarInt(); $ingredientCount = $this->getUnsignedVarInt();
/** @var Item */ /** @var Item */
$ingredients = []; $entry["input"] = [];
for($j = 0; $j < $ingredientCount; ++$j){ for($j = 0; $j < $ingredientCount; ++$j){
$ingredients[] = $this->getSlot(); $entry["input"][] = $this->getSlot();
} }
$resultCount = $this->getUnsignedVarInt(); $resultCount = $this->getUnsignedVarInt();
$results = []; $entry["output"] = [];
for($k = 0; $k < $resultCount; ++$k){ for($k = 0; $k < $resultCount; ++$k){
$results[] = $this->getSlot(); $entry["output"][] = $this->getSlot();
}
$uuid = $this->getUUID();
$recipe = new ShapelessRecipe($results[0]); //ouch...
$recipe->setId($uuid);
foreach($ingredients as $ingr){
$recipe->addIngredient($ingr);
} }
$entry["uuid"] = $this->getUUID()->toString();
break; break;
case self::ENTRY_SHAPED: case self::ENTRY_SHAPED:
$width = $this->getVarInt(); $entry["width"] = $this->getVarInt();
$height = $this->getVarInt(); $entry["height"] = $this->getVarInt();
$count = $width * $height; $count = $entry["width"] * $entry["height"];
$ingredients = []; $entry["input"] = [];
for($j = 0; $j < $count; ++$j){ for($j = 0; $j < $count; ++$j){
$ingredients[] = $this->getSlot(); $entry["input"][] = $this->getSlot();
} }
$resultCount = $this->getUnsignedVarInt(); $resultCount = $this->getUnsignedVarInt();
$results = []; $entry["output"] = [];
for($k = 0; $k < $resultCount; ++$k){ for($k = 0; $k < $resultCount; ++$k){
$results[] = $this->getSlot(); $entry["output"][] = $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["uuid"] = $this->getUUID()->toString();
break; break;
case self::ENTRY_FURNACE: case self::ENTRY_FURNACE:
case self::ENTRY_FURNACE_DATA: case self::ENTRY_FURNACE_DATA:
$inputId = $this->getVarInt(); $entry["inputId"] = $this->getVarInt();
if($recipeType === self::ENTRY_FURNACE_DATA){ if($recipeType === self::ENTRY_FURNACE_DATA){
$inputData = $this->getVarInt(); $entry["inputDamage"] = $this->getVarInt();
} }
$result = $this->getSlot(); $entry["output"] = $this->getSlot();
$recipe = new FurnaceRecipe(Item::get($inputId, $inputData ?? null), $result);
break; break;
case self::ENTRY_MULTI: case self::ENTRY_MULTI:
$uuid = $this->getUUID(); $entry["uuid"] = $this->getUUID()->toString();
$recipe = new MultiRecipe($uuid);
break; break;
default: default:
throw new \UnexpectedValueException("Unhandled recipe type $recipeType!"); //do not continue attempting to decode 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){ private static function writeEntry($entry, BinaryStream $stream){