Added a way to send Player's inventory directly

This commit is contained in:
Shoghi Cervantes Pueyo 2013-04-17 19:25:53 +02:00
parent 5e4ef9732b
commit 09301f0e5f
6 changed files with 85 additions and 24 deletions

View File

@ -151,7 +151,8 @@ class BlockAPI{
} }
if($player instanceof Player){ if($player instanceof Player){
$this->drop(new Vector3($player->entity->x - 0.5, $player->entity->y, $player->entity->z - 0.5), $item, true); $player->addItem($item->getID(), $item->getMetadata(), $item->count);
//$this->drop(new Vector3($player->entity->x - 0.5, $player->entity->y, $player->entity->z - 0.5), $item, true);
$output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username."\n"; $output .= "Giving ".$item->count." of ".$item->getName()." (".$item->getID().":".$item->getMetadata().") to ".$player->username."\n";
}else{ }else{
$output .= "Unknown player\n"; $output .= "Unknown player\n";
@ -170,6 +171,7 @@ class BlockAPI{
"block" => $block->getID(), "block" => $block->getID(),
"meta" => $block->getMetadata() "meta" => $block->getMetadata()
)); ));
$player->sendInventory();
return false; return false;
} }

View File

@ -225,6 +225,7 @@ class Player{
if($data[0] === AIR){ if($data[0] === AIR){
$add = min(64, $count); $add = min(64, $count);
$this->inventory[$s] = array($type, $damage, $add); $this->inventory[$s] = array($type, $damage, $add);
$this->sendInventorySlot($s);
break; break;
}elseif($data[0] === $type and $data[1] === $damage){ }elseif($data[0] === $type and $data[1] === $damage){
$add = min(64 - $data[2], $count); $add = min(64 - $data[2], $count);
@ -232,6 +233,7 @@ class Player{
continue; continue;
} }
$this->inventory[$s] = array($type, $damage, $data[2] + $add); $this->inventory[$s] = array($type, $damage, $data[2] + $add);
$this->sendInventorySlot($s);
break; break;
} }
} }
@ -254,6 +256,7 @@ class Player{
}else{ }else{
$this->inventory[$s] = array(0, 0, 0); $this->inventory[$s] = array(0, 0, 0);
} }
$this->sendInventorySlot($s);
break; break;
} }
} }
@ -265,6 +268,29 @@ class Player{
return true; return true;
} }
public function sendInventorySlot($s){
$s = (int) $s;
if(!isset($this->inventory[$s])){
return false;
}
$this->sendInventory(); //Fallback
return true;
//Can't do this :(
/*$slot = BlockAPI::getItem($this->inventory[$s][0], $this->inventory[$s][1], $this->inventory[$s][2]);
$this->dataPacket(MC_CONTAINER_SET_SLOT, array(
"windowid" => 0,
"slot" => (int) $s,
"block" => $slot->getID(),
"stack" => $slot->count,
"meta" => $slot->getMetadata(),
));
return true;*/
}
public function hasItem($type, $damage = false){ public function hasItem($type, $damage = false){
if($type === AIR){ if($type === AIR){
return true; return true;
@ -1105,6 +1131,8 @@ class Player{
} }
public function sendInventory(){ public function sendInventory(){
/*
//OLD WAY
foreach($this->inventory as $s => $data){ foreach($this->inventory as $s => $data){
if($data[0] > 0 and $data[2] >= 0){ if($data[0] > 0 and $data[2] >= 0){
$e = $this->server->api->entity->add(ENTITY_ITEM, $data[0], array( $e = $this->server->api->entity->add(ENTITY_ITEM, $data[0], array(
@ -1117,7 +1145,21 @@ class Player{
$this->server->api->entity->spawnTo($e->eid, $this); $this->server->api->entity->spawnTo($e->eid, $this);
} }
$this->inventory[$s] = array(AIR, 0, 0); $this->inventory[$s] = array(AIR, 0, 0);
}*/
$inv = array();
foreach($this->inventory as $s => $data){
if($data[0] > 0 and $data[2] >= 0){
$inv[] = BlockAPI::getItem($data[0], $data[1], $data[2]);
}else{
$inv[] = BlockAPI::getItem(AIR, 0, 0);
$this->inventory[$s] = array(AIR, 0, 0);
} }
}
$this->dataPacket(MC_CONTAINER_SET_CONTENT, array(
"windowid" => 0,
"count" => count($inv),
"slots" => $inv
));
/* /*
//Future //Future
$inv = array(); $inv = array();

View File

@ -88,7 +88,7 @@ define("MC_CONTAINER_OPEN", 0xae);
define("MC_CONTAINER_CLOSE", 0xaf); define("MC_CONTAINER_CLOSE", 0xaf);
define("MC_CONTAINER_SET_SLOT", 0xb0); define("MC_CONTAINER_SET_SLOT", 0xb0);
//define("MC_CONTAINER_SET_DATA", 0xb1); //define("MC_CONTAINER_SET_DATA", 0xb1);
//define("MC_CONTAINER_SET_CONTENT", 0xb2); define("MC_CONTAINER_SET_CONTENT", 0xb2);
//define("MC_CONTAINER_ACK", 0xb3); //define("MC_CONTAINER_ACK", 0xb3);
define("MC_CLIENT_MESSAGE", 0xb4); define("MC_CLIENT_MESSAGE", 0xb4);
define("MC_SIGN_UPDATE", 0xb5); define("MC_SIGN_UPDATE", 0xb5);

View File

@ -93,26 +93,27 @@ class BurningFurnaceBlock extends SolidBlock{
if($furnace->class !== TILE_FURNACE){ if($furnace->class !== TILE_FURNACE){
return true; return true;
} }
$id = $player->windowCnt = $player->windowCnt++ % 255; $player->windowCnt++;
$player->windows[$id] = $furnace; $player->windowCnt = $id = max(1, $player->windowCnt % 255);
$player->windows[$id] = $chest;
$player->dataPacket(MC_CONTAINER_OPEN, array( $player->dataPacket(MC_CONTAINER_OPEN, array(
"windowid" => $id, "windowid" => $id,
"type" => WINDOW_FURNACE, "type" => WINDOW_FURNACE,
"slots" => 3, "slots" => FURNACE_SLOTS,
"title" => "Furnace", "title" => "Furnace",
)); ));
$slots = array();
for($s = 0; $s < FURNACE_SLOTS; ++$s){ for($s = 0; $s < FURNACE_SLOTS; ++$s){
$slot = $furnace->getSlot($s); $slot = $chest->getSlot($s);
if($slot->getID() > 0 and $slot->count > 0){ if($slot->getID() > 0 and $slot->count > 0){
$player->dataPacket(MC_CONTAINER_SET_SLOT, array( $slots[] = $slot;
}
}
$player->dataPacket(MC_CONTAINER_SET_CONTENT, array(
"windowid" => $id, "windowid" => $id,
"slot" => $s, "count" => count($slots),
"block" => $slot->getID(), "slots" => $slots
"stack" => $slot->count,
"meta" => $slot->getMetadata(),
)); ));
}
}
return true; return true;
} }

View File

@ -110,27 +110,27 @@ class ChestBlock extends TransparentBlock{
if($chest->class !== TILE_CHEST){ if($chest->class !== TILE_CHEST){
return true; return true;
} }
$player->windowCnt++;
$id = $player->windowCnt = $player->windowCnt++ % 255; $player->windowCnt = $id = max(1, $player->windowCnt % 255);
$player->windows[$id] = $chest; $player->windows[$id] = $chest;
$player->dataPacket(MC_CONTAINER_OPEN, array( $player->dataPacket(MC_CONTAINER_OPEN, array(
"windowid" => $id, "windowid" => $id,
"type" => WINDOW_CHEST, "type" => WINDOW_CHEST,
"slots" => 27, "slots" => CHEST_SLOTS,
"title" => "Chest", "title" => "Chest",
)); ));
$slots = array();
for($s = 0; $s < CHEST_SLOTS; ++$s){ for($s = 0; $s < CHEST_SLOTS; ++$s){
$slot = $chest->getSlot($s); $slot = $chest->getSlot($s);
if($slot->getID() > 0 and $slot->count > 0){ if($slot->getID() > 0 and $slot->count > 0){
$player->dataPacket(MC_CONTAINER_SET_SLOT, array( $slots[] = $slot;
}
}
$player->dataPacket(MC_CONTAINER_SET_CONTENT, array(
"windowid" => $id, "windowid" => $id,
"slot" => $s, "count" => count($slots),
"block" => $slot->getID(), "slots" => $slots
"stack" => $slot->count,
"meta" => $slot->getMetadata(),
)); ));
}
}
return true; return true;
} }

View File

@ -675,6 +675,22 @@ class CustomPacketHandler{
$this->raw .= Utils::writeShort($this->data["meta"]); $this->raw .= Utils::writeShort($this->data["meta"]);
} }
break; break;
case MC_CONTAINER_SET_CONTENT:
if($this->c === false){
$this->data["windowid"] = ord($this->get(1));
$this->data["count"] = Utils::readShort($this->get(2), false);
$this->data["slots"] = array();
for($s = 0; $s < $this->data["count"]; ++$s){
$this->data["slots"][$s] = Utils::readSlot($this->get(5));
}
}else{
$this->raw .= chr($this->data["windowid"]);
$this->raw .= Utils::writeShort(count($this->data["slots"]));
foreach($this->data["slots"] as $slot){
$this->raw .= Utils::writeSlot($slot);
}
}
break;
case MC_CLIENT_MESSAGE: case MC_CLIENT_MESSAGE:
if($this->c === false){ if($this->c === false){
$this->data["message"] = $this->get(Utils::readShort($this->get(2), false)); $this->data["message"] = $this->get(Utils::readShort($this->get(2), false));