Skip to content

Commit

Permalink
Properly handle netIDs of recipes, itemstacks, transactions and creat…
Browse files Browse the repository at this point in the history
…ive items

these were all lumped into one previously (incorrectly), which caused various reading errors (which happened to work, but incorrectly), and also missed a decent amount of finesse (such as the request ID/server itemstack ID union stuff in the ItemStackRequest system).
I omitted validation for these since what we really need is wrapper types, but that can wait for another time.
  • Loading branch information
dktapps committed Feb 16, 2024
1 parent 0cebb55 commit 968666b
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/InventoryTransactionPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function create(int $requestId, array $requestChangedSlots, Transa
}

protected function decodePayload(PacketSerializer $in) : void{
$this->requestId = $in->readGenericTypeNetworkId();
$this->requestId = $in->readLegacyItemStackRequestId();
$this->requestChangedSlots = [];
if($this->requestId !== 0){
for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
Expand All @@ -77,7 +77,7 @@ protected function decodePayload(PacketSerializer $in) : void{
}

protected function encodePayload(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->requestId);
$out->writeLegacyItemStackRequestId($this->requestId);
if($this->requestId !== 0){
$out->putUnsignedVarInt(count($this->requestChangedSlots));
foreach($this->requestChangedSlots as $changedSlots){
Expand Down
54 changes: 52 additions & 2 deletions src/serializer/PacketSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,61 @@ public function getNbtCompoundRoot() : CompoundTag{
}
}

public function readGenericTypeNetworkId() : int{
public function readRecipeNetId() : int{
return $this->getUnsignedVarInt();
}

public function writeRecipeNetId(int $id) : void{
$this->putUnsignedVarInt($id);
}

public function readCreativeItemNetId() : int{
return $this->getUnsignedVarInt();
}

public function writeCreativeItemNetId(int $id) : void{
$this->putUnsignedVarInt($id);
}

/**
* This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound
* packets to allow the client to refer to server known items, or items which may have been modified by a previous
* as-yet unacknowledged request from the client.
*/
public function readItemStackNetIdVariant() : int{
return $this->getVarInt();
}

/**
* This is a union of ItemStackRequestId, LegacyItemStackRequestId, and ServerItemStackId, used in serverbound
* packets to allow the client to refer to server known items, or items which may have been modified by a previous
* as-yet unacknowledged request from the client.
*/
public function writeItemStackNetIdVariant(int $id) : void{
$this->putVarInt($id);
}

public function readItemStackRequestId() : int{
return $this->getVarInt();
}

public function writeItemStackRequestId(int $id) : void{
$this->putVarInt($id);
}

public function readLegacyItemStackRequestId() : int{
return $this->getVarInt();
}

public function writeLegacyItemStackRequestId(int $id) : void{
$this->putVarInt($id);
}

public function readServerItemStackId() : int{
return $this->getVarInt();
}

public function writeGenericTypeNetworkId(int $id) : void{
public function writeServerItemStackId(int $id) : void{
$this->putVarInt($id);
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/EnchantOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function read(PacketSerializer $in) : self{
$selfActivatedEnchants = self::readEnchantList($in);

$name = $in->getString();
$optionId = $in->readGenericTypeNetworkId();
$optionId = $in->readRecipeNetId();

return new self($cost, $slotFlags, $equipActivatedEnchants, $heldActivatedEnchants, $selfActivatedEnchants, $name, $optionId);
}
Expand All @@ -94,6 +94,6 @@ public function write(PacketSerializer $out) : void{
self::writeEnchantList($out, $this->selfActivatedEnchantments);

$out->putString($this->name);
$out->writeGenericTypeNetworkId($this->optionId);
$out->writeRecipeNetId($this->optionId);
}
}
4 changes: 2 additions & 2 deletions src/types/inventory/CreativeContentEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public function getEntryId() : int{ return $this->entryId; }
public function getItem() : ItemStack{ return $this->item; }

public static function read(PacketSerializer $in) : self{
$entryId = $in->readGenericTypeNetworkId();
$entryId = $in->readCreativeItemNetId();
$item = $in->getItemStackWithoutStackId();
return new self($entryId, $item);
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->entryId);
$out->writeCreativeItemNetId($this->entryId);
$out->putItemStackWithoutStackId($this->item);
}
}
4 changes: 2 additions & 2 deletions src/types/inventory/ItemStackWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function read(PacketSerializer $in) : self{
$stack = $in->getItemStack(function(PacketSerializer $in) use (&$stackId) : void{
$hasNetId = $in->getBool();
if($hasNetId){
$stackId = $in->readGenericTypeNetworkId();
$stackId = $in->readServerItemStackId();
}
});
return new self($stackId, $stack);
Expand All @@ -45,7 +45,7 @@ public function write(PacketSerializer $out) : void{
$out->putItemStack($this->itemStack, function(PacketSerializer $out) : void{
$out->putBool($this->stackId !== 0);
if($this->stackId !== 0){
$out->writeGenericTypeNetworkId($this->stackId);
$out->writeServerItemStackId($this->stackId);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getRepetitions() : int{ return $this->repetitions; }
public function getIngredients() : array{ return $this->ingredients; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readGenericTypeNetworkId();
$recipeId = $in->readRecipeNetId();
$repetitions = $in->getByte();
$ingredients = [];
for($i = 0, $count = $in->getByte(); $i < $count; ++$i){
Expand All @@ -59,7 +59,7 @@ public static function read(PacketSerializer $in) : self{
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->recipeId);
$out->writeRecipeNetId($this->recipeId);
$out->putByte($this->repetitions);
$out->putByte(count($this->ingredients));
foreach($this->ingredients as $ingredient){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public function getRecipeId() : int{ return $this->recipeId; }
public function getFilterStringIndex() : int{ return $this->filterStringIndex; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readGenericTypeNetworkId();
$recipeId = $in->readRecipeNetId();
$filterStringIndex = $in->getLInt();
return new self($recipeId, $filterStringIndex);
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->recipeId);
$out->writeRecipeNetId($this->recipeId);
$out->putLInt($this->filterStringIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ final public function __construct(
public function getRecipeId() : int{ return $this->recipeId; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readGenericTypeNetworkId();
$recipeId = $in->readRecipeNetId();
return new self($recipeId);
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->recipeId);
$out->writeRecipeNetId($this->recipeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public function __construct(
public function getCreativeItemId() : int{ return $this->creativeItemId; }

public static function read(PacketSerializer $in) : self{
$creativeItemId = $in->readGenericTypeNetworkId();
$creativeItemId = $in->readCreativeItemNetId();
return new self($creativeItemId);
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->creativeItemId);
$out->writeCreativeItemNetId($this->creativeItemId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function getRecipeId() : int{ return $this->recipeId; }
public function getRepairCost() : int{ return $this->repairCost; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readGenericTypeNetworkId();
$recipeId = $in->readRecipeNetId();
$repairCost = $in->getVarInt(); //WHY!!!!

return new self($recipeId, $repairCost);
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->recipeId);
$out->writeRecipeNetId($this->recipeId);
$out->putVarInt($this->repairCost);
}
}
4 changes: 2 additions & 2 deletions src/types/inventory/stackrequest/ItemStackRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static function readAction(PacketSerializer $in, int $typeId) : ItemStac
}

public static function read(PacketSerializer $in) : self{
$requestId = $in->readGenericTypeNetworkId();
$requestId = $in->readItemStackRequestId();
$actions = [];
for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
$typeId = $in->getByte();
Expand All @@ -91,7 +91,7 @@ public static function read(PacketSerializer $in) : self{
}

public function write(PacketSerializer $out) : void{
$out->writeGenericTypeNetworkId($this->requestId);
$out->writeItemStackRequestId($this->requestId);
$out->putUnsignedVarInt(count($this->actions));
foreach($this->actions as $action){
$out->putByte($action->getTypeId());
Expand Down
4 changes: 2 additions & 2 deletions src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function getStackId() : int{ return $this->stackId; }
public static function read(PacketSerializer $in) : self{
$containerId = $in->getByte();
$slotId = $in->getByte();
$stackId = $in->readGenericTypeNetworkId();
$stackId = $in->readItemStackNetIdVariant();
return new self($containerId, $slotId, $stackId);
}

public function write(PacketSerializer $out) : void{
$out->putByte($this->containerId);
$out->putByte($this->slotId);
$out->writeGenericTypeNetworkId($this->stackId);
$out->writeItemStackNetIdVariant($this->stackId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public function getStackId() : int{ return $this->stackId; }
public static function read(PacketSerializer $in) : self{
$hotbarSlot = $in->getVarInt();
$predictedDurability = $in->getVarInt();
$stackId = $in->readGenericTypeNetworkId();
$stackId = $in->readItemStackNetIdVariant();
return new self($hotbarSlot, $predictedDurability, $stackId);
}

public function write(PacketSerializer $out) : void{
$out->putVarInt($this->hotbarSlot);
$out->putVarInt($this->predictedDurability);
$out->writeGenericTypeNetworkId($this->stackId);
$out->writeItemStackNetIdVariant($this->stackId);
}
}
4 changes: 2 additions & 2 deletions src/types/inventory/stackresponse/ItemStackResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function getContainerInfos() : array{ return $this->containerInfos; }

public static function read(PacketSerializer $in) : self{
$result = $in->getByte();
$requestId = $in->readGenericTypeNetworkId();
$requestId = $in->readItemStackRequestId();
$containerInfos = [];
if($result === self::RESULT_OK){
for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
Expand All @@ -58,7 +58,7 @@ public static function read(PacketSerializer $in) : self{

public function write(PacketSerializer $out) : void{
$out->putByte($this->result);
$out->writeGenericTypeNetworkId($this->requestId);
$out->writeItemStackRequestId($this->requestId);
if($this->result === self::RESULT_OK){
$out->putUnsignedVarInt(count($this->containerInfos));
foreach($this->containerInfos as $containerInfo){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function read(PacketSerializer $in) : self{
$slot = $in->getByte();
$hotbarSlot = $in->getByte();
$count = $in->getByte();
$itemStackId = $in->readGenericTypeNetworkId();
$itemStackId = $in->readServerItemStackId();
$customName = $in->getString();
$durabilityCorrection = $in->getVarInt();
return new self($slot, $hotbarSlot, $count, $itemStackId, $customName, $durabilityCorrection);
Expand All @@ -52,7 +52,7 @@ public function write(PacketSerializer $out) : void{
$out->putByte($this->slot);
$out->putByte($this->hotbarSlot);
$out->putByte($this->count);
$out->writeGenericTypeNetworkId($this->itemStackId);
$out->writeServerItemStackId($this->itemStackId);
$out->putString($this->customName);
$out->putVarInt($this->durabilityCorrection);
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/recipe/MultiRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function getRecipeNetId() : int{

public static function decode(int $typeId, PacketSerializer $in) : self{
$uuid = $in->getUUID();
$recipeNetId = $in->readGenericTypeNetworkId();
$recipeNetId = $in->readRecipeNetId();
return new self($typeId, $uuid, $recipeNetId);
}

public function encode(PacketSerializer $out) : void{
$out->putUUID($this->recipeId);
$out->writeGenericTypeNetworkId($this->recipeNetId);
$out->writeRecipeNetId($this->recipeNetId);
}
}
4 changes: 2 additions & 2 deletions src/types/recipe/ShapedRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static function decode(int $recipeType, PacketSerializer $in) : self{
$uuid = $in->getUUID();
$block = $in->getString();
$priority = $in->getVarInt();
$recipeNetId = $in->readGenericTypeNetworkId();
$recipeNetId = $in->readRecipeNetId();

return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $recipeNetId);
}
Expand All @@ -135,6 +135,6 @@ public function encode(PacketSerializer $out) : void{
$out->putUUID($this->uuid);
$out->putString($this->blockName);
$out->putVarInt($this->priority);
$out->writeGenericTypeNetworkId($this->recipeNetId);
$out->writeRecipeNetId($this->recipeNetId);
}
}
4 changes: 2 additions & 2 deletions src/types/recipe/ShapelessRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function decode(int $recipeType, PacketSerializer $in) : self{
$uuid = $in->getUUID();
$block = $in->getString();
$priority = $in->getVarInt();
$recipeNetId = $in->readGenericTypeNetworkId();
$recipeNetId = $in->readRecipeNetId();

return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $recipeNetId);
}
Expand All @@ -104,6 +104,6 @@ public function encode(PacketSerializer $out) : void{
$out->putUUID($this->uuid);
$out->putString($this->blockName);
$out->putVarInt($this->priority);
$out->writeGenericTypeNetworkId($this->recipeNetId);
$out->writeRecipeNetId($this->recipeNetId);
}
}
4 changes: 2 additions & 2 deletions src/types/recipe/SmithingTransformRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function decode(int $typeId, PacketSerializer $in) : self{
$addition = $in->getRecipeIngredient();
$output = $in->getItemStackWithoutStackId();
$blockName = $in->getString();
$recipeNetId = $in->readGenericTypeNetworkId();
$recipeNetId = $in->readRecipeNetId();

return new self(
$typeId,
Expand All @@ -74,6 +74,6 @@ public function encode(PacketSerializer $out) : void{
$out->putRecipeIngredient($this->addition);
$out->putItemStackWithoutStackId($this->output);
$out->putString($this->blockName);
$out->writeGenericTypeNetworkId($this->recipeNetId);
$out->writeRecipeNetId($this->recipeNetId);
}
}
4 changes: 2 additions & 2 deletions src/types/recipe/SmithingTrimRecipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function decode(int $typeId, PacketSerializer $in) : self{
$input = $in->getRecipeIngredient();
$addition = $in->getRecipeIngredient();
$blockName = $in->getString();
$recipeNetId = $in->readGenericTypeNetworkId();
$recipeNetId = $in->readRecipeNetId();

return new self(
$typeId,
Expand All @@ -67,6 +67,6 @@ public function encode(PacketSerializer $out) : void{
$out->putRecipeIngredient($this->input);
$out->putRecipeIngredient($this->addition);
$out->putString($this->blockName);
$out->writeGenericTypeNetworkId($this->recipeNetId);
$out->writeRecipeNetId($this->recipeNetId);
}
}

0 comments on commit 968666b

Please sign in to comment.