Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
Update to 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
inxomnyaa committed Mar 10, 2017
2 parents cec138f + 0a6b59d commit 55e71a8
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 155 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### Issue description
<!--- use our forum https://forums.pmmp.io for questions -->
<!--- Any issues requesting updates to new versions of MCPE will be treated as spam. We do not need issues to tell us that there is a new version available. -->
Write a short description about the issue

### Steps to reproduce the issue
Expand Down
128 changes: 37 additions & 91 deletions src/pocketmine/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
const SPECTATOR = 3;
const VIEW = Player::SPECTATOR;

const SURVIVAL_SLOTS = 36;
const CREATIVE_SLOTS = 112;

/** @var SourceInterface */
protected $interface;

Expand Down Expand Up @@ -436,7 +433,7 @@ public function getViewDistance() : int{
public function setViewDistance(int $distance){
$this->viewDistance = $this->server->getAllowedViewDistance($distance);

$this->spawnThreshold = (int) (min($this->viewDistance, $this->server->getProperty("chunk-sending.spawn-radius")) ** 2 * M_PI);
$this->spawnThreshold = (int) (min($this->viewDistance, $this->server->getProperty("chunk-sending.spawn-radius", 4)) ** 2 * M_PI);

$pk = new ChunkRadiusUpdatedPacket();
$pk->radius = $this->viewDistance;
Expand Down Expand Up @@ -702,21 +699,12 @@ private function unloadChunk($x, $z, Level $level = null){
$level = $level === null ? $this->level : $level;
$index = Level::chunkHash($x, $z);
if(isset($this->usedChunks[$index])){
$chunk = $level->getChunk($x, $z);
foreach($chunk->getEntities() as $entity){
foreach($level->getChunkEntities($x, $z) as $entity){
if($entity !== $this){
$entity->despawnFrom($this);
}
}

if($level !== $this->level){
$pk = new FullChunkDataPacket();
$pk->chunkX = $x;
$pk->chunkZ = $z;
$pk->data = chr($chunk->getSubChunkSendCount());
$this->dataPacket($pk);
}

unset($this->usedChunks[$index]);
}
$level->unregisterChunkLoader($this, $x, $z);
Expand Down Expand Up @@ -819,6 +807,7 @@ protected function doFirstSpawn(){
$this->sendData($this);
$this->inventory->sendContents($this);
$this->inventory->sendArmorContents($this);
$this->inventory->sendHeldItem($this);

$pk = new SetTimePacket();
$pk->time = $this->level->getTime();
Expand Down Expand Up @@ -1790,12 +1779,6 @@ protected function processLogin(){
return;
}

if($this->isCreative()){
$this->inventory->setHeldItemSlot(0);
}else{
$this->inventory->setHeldItemSlot($this->inventory->getHotbarSlotIndex(0));
}

$this->dataPacket(new ResourcePacksInfoPacket());

if(!$this->hasValidSpawnPosition() and isset($this->namedtag->SpawnLevel) and ($level = $this->server->getLevelByName($this->namedtag["SpawnLevel"])) instanceof Level){
Expand Down Expand Up @@ -2020,64 +2003,20 @@ public function handleDataPacket(DataPacket $packet){
break;
}

if($packet->slot === 0x28 or $packet->slot === 0 or $packet->slot === 255){ //0 for 0.8.0 compatibility
$packet->slot = -1; //Air
if($packet->inventorySlot === 255){
$packet->inventorySlot = -1; //Cleared slot
}else{
$packet->slot -= 9; //Get real block slot
}
$packet->inventorySlot -= 9; //Get real inventory slot
$item = $this->inventory->getItem($packet->inventorySlot);

/** @var Item $item */
$item = null;

if($this->isCreative()){ //Creative mode match
$item = $packet->item;
$slot = Item::getCreativeItemIndex($item);
}else{
$item = $this->inventory->getItem($packet->slot);
$slot = $packet->slot;
}

if($packet->slot === -1){ //Air
if($this->isCreative()){
$found = false;
for($i = 0; $i < $this->inventory->getHotbarSize(); ++$i){
if($this->inventory->getHotbarSlotIndex($i) === -1){
$this->inventory->setHeldItemIndex($i);
$found = true;
break;
}
}

if(!$found){ //couldn't find a empty slot (error)
$this->inventory->sendContents($this);
break;
}
}else{
if($packet->selectedSlot >= 0 and $packet->selectedSlot < 9){
$this->inventory->setHeldItemIndex($packet->selectedSlot, false);
$this->inventory->setHeldItemSlot($packet->slot);
}else{
$this->inventory->sendContents($this);
break;
}
}
}elseif($item === null or $slot === -1 or !$item->deepEquals($packet->item)){ // packet error or not implemented
$this->inventory->sendContents($this);
break;
}elseif($this->isCreative()){
$this->inventory->setHeldItemIndex($packet->selectedSlot, false);
$this->inventory->setItem($packet->selectedSlot, $item);
$this->inventory->setHeldItemSlot($packet->selectedSlot);
}else{
if($packet->selectedSlot >= 0 and $packet->selectedSlot < $this->inventory->getHotbarSize()){
$this->inventory->setHeldItemIndex($packet->selectedSlot, false);
$this->inventory->setHeldItemSlot($slot);
}else{
if(!$item->equals($packet->item)){
$this->inventory->sendContents($this);
break;
}
}

$this->inventory->equipItem($packet->hotbarSlot, $packet->inventorySlot);

$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
break;
case ProtocolInfo::USE_ITEM_PACKET:
Expand Down Expand Up @@ -3028,33 +2967,38 @@ public function handleDataPacket(DataPacket $packet){
break;
}

if($packet->windowid === 0){ //Our inventory
if($packet->slot >= $this->inventory->getSize()){
switch($packet->windowid){
case ContainerSetContentPacket::SPECIAL_INVENTORY: //Normal inventory change
if($packet->slot >= $this->inventory->getSize()){
break 2;
}

$transaction = new BaseTransaction($this->inventory, $packet->slot, $this->inventory->getItem($packet->slot), $packet->item);
break;
}
if($this->isCreative()){
if(Item::getCreativeItemIndex($packet->item) !== -1){
$this->inventory->setItem($packet->slot, $packet->item);
$this->inventory->setHotbarSlotIndex($packet->slot, $packet->slot); //links $hotbar[$packet->slot] to $slots[$packet->slot]
case ContainerSetContentPacket::SPECIAL_ARMOR: //Armour change
if($packet->slot >= 4){
break 2;
}
}
$transaction = new BaseTransaction($this->inventory, $packet->slot, $this->inventory->getItem($packet->slot), $packet->item);
}elseif($packet->windowid === ContainerSetContentPacket::SPECIAL_ARMOR){ //Our armor
if($packet->slot >= 4){

$transaction = new BaseTransaction($this->inventory, $packet->slot + $this->inventory->getSize(), $this->inventory->getArmorItem($packet->slot), $packet->item);
break;
}
case ContainerSetContentPacket::SPECIAL_HOTBAR: //Hotbar link update
//hotbarSlot 0-8, slot 9-44
$this->inventory->setHotbarSlotIndex($packet->hotbarSlot, $packet->slot - 9);
break 2;
default:
if(!isset($this->windowIndex[$packet->windowid])){
break 2; //unknown windowID and/or not matching any open windows
}

$transaction = new BaseTransaction($this->inventory, $packet->slot + $this->inventory->getSize(), $this->inventory->getArmorItem($packet->slot), $packet->item);
}elseif(isset($this->windowIndex[$packet->windowid])){
$this->craftingType = 0;
$inv = $this->windowIndex[$packet->windowid];
$transaction = new BaseTransaction($inv, $packet->slot, $inv->getItem($packet->slot), $packet->item);
}else{
break;
$this->craftingType = 0;
$inv = $this->windowIndex[$packet->windowid];
$transaction = new BaseTransaction($inv, $packet->slot, $inv->getItem($packet->slot), $packet->item);
break;
}

if($transaction->getSourceItem()->deepEquals($transaction->getTargetItem()) and $transaction->getTargetItem()->getCount() === $transaction->getSourceItem()->getCount()){ //No changes!
//No changes, just a local inventory update sent by the server
//No changes, just a local inventory update sent by the client
break;
}

Expand Down Expand Up @@ -3547,6 +3491,8 @@ public function kill(){

if($this->inventory !== null){
$this->inventory->clearAll();
$this->inventory->setHeldItemIndex(0);
$this->inventory->resetHotbar(true);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/pocketmine/entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function getValue(){
return $this->currentValue;
}

public function setValue($value, $fit = false){
public function setValue($value, $fit = false, bool $forceSend = false){
if($value > $this->getMaxValue() or $value < $this->getMinValue()){
if(!$fit){
throw new \InvalidArgumentException("Value $value exceeds the range!");
Expand All @@ -181,7 +181,10 @@ public function setValue($value, $fit = false){
if($this->currentValue != $value){
$this->desynchronized = true;
$this->currentValue = $value;
}elseif($forceSend){
$this->desynchronized = true;
}

return $this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/pocketmine/entity/Effect.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public function add(Entity $entity, $modify = false, Effect $oldEffect = null){
$speed = $attr->getValue();
}
$speed *= (1 - 0.15 * $this->amplifier);
$attr->setValue($speed);
$attr->setValue($speed, true);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/pocketmine/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ abstract class Entity extends Location implements Metadatable{
* 58 (byte)
* 59 (float)
* 60 (float) */
const DATA_AREA_EFFECT_CLOUD_RADIUS = 61; //float
const DATA_AREA_EFFECT_CLOUD_WAITING = 62; //int
const DATA_AREA_EFFECT_CLOUD_PARTICLE_ID = 63; //int
/* 64 (int), shulker-related
* 65 (byte), shulker-related
* 66 (short) shulker-related
* 67 (unknown), shulker-related */
const DATA_TRADING_PLAYER_EID = 68; //long


const DATA_FLAG_ONFIRE = 0;
Expand Down Expand Up @@ -470,7 +478,7 @@ public function setSprinting($value = true){
if($value !== $this->isSprinting()){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING, (bool) $value);
$attr = $this->attributeMap->getAttribute(Attribute::MOVEMENT_SPEED);
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3));
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true);
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/pocketmine/entity/Human.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,12 @@ protected function initEntity(){
}
}

if(isset($this->namedtag->SelectedInventorySlot) and $this->namedtag->SelectedInventorySlot instanceof IntTag){
$this->inventory->setHeldItemIndex($this->namedtag->SelectedInventorySlot->getValue(), false);
}else{
$this->inventory->setHeldItemIndex(0, false);
}

parent::initEntity();

if(!isset($this->namedtag->foodLevel) or !($this->namedtag->foodLevel instanceof IntTag)){
Expand Down Expand Up @@ -448,9 +454,8 @@ public function saveNBT(){
}

//Normal inventory
$slotCount = Player::SURVIVAL_SLOTS + 9;
//$slotCount = (($this instanceof Player and ($this->gamemode & 0x01) === 1) ? Player::CREATIVE_SLOTS : Player::SURVIVAL_SLOTS) + 9;
for($slot = 9; $slot < $slotCount; ++$slot){
$slotCount = $this->inventory->getSize() + $this->inventory->getHotbarSize();
for($slot = $this->inventory->getHotbarSize(); $slot < $slotCount; ++$slot){
$item = $this->inventory->getItem($slot - 9);
$this->namedtag->Inventory[$slot] = $item->nbtSerialize($slot);
}
Expand All @@ -462,6 +467,8 @@ public function saveNBT(){
$this->namedtag->Inventory[$slot] = $item->nbtSerialize($slot);
}
}

$this->namedtag->SelectedInventorySlot = new IntTag("SelectedInventorySlot", $this->inventory->getHeldItemIndex());
}

if(strlen($this->getSkinData()) > 0){
Expand Down
40 changes: 23 additions & 17 deletions src/pocketmine/inventory/InventoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@

namespace pocketmine\inventory;

use pocketmine\network\protocol\types\InventoryNetworkIds;

/**
* Saves all the information regarding default inventory sizes and types
*/
class InventoryType{

//NOTE: Do not confuse these with the network IDs.
const CHEST = 0;
const DOUBLE_CHEST = 1;
const PLAYER = 2;
Expand Down Expand Up @@ -63,23 +67,25 @@ public static function init(){
return;
}

static::$default[static::CHEST] = new InventoryType(27, "Chest", 0);
static::$default[static::DOUBLE_CHEST] = new InventoryType(27 + 27, "Double Chest", 0);
static::$default[static::PLAYER] = new InventoryType(36 + 4, "Player", 0); //36 CONTAINER, 4 ARMOR
static::$default[static::CRAFTING] = new InventoryType(5, "Crafting", 1); //4 CRAFTING slots, 1 RESULT
static::$default[static::WORKBENCH] = new InventoryType(10, "Crafting", 1); //9 CRAFTING slots, 1 RESULT
static::$default[static::FURNACE] = new InventoryType(3, "Furnace", 2); //2 INPUT, 1 OUTPUT
static::$default[static::ENCHANT_TABLE] = new InventoryType(2, "Enchant", 3); //1 INPUT/OUTPUT, 1 LAPIS
static::$default[static::BREWING_STAND] = new InventoryType(4, "Brewing", 4); //1 INPUT, 3 POTION
static::$default[static::ANVIL] = new InventoryType(3, "Anvil", 5); //2 INPUT, 1 OUTPUT
//TODO: add the below
static::$default[static::DISPENSER] = new InventoryType(9, "Dispenser", 6); //9 CONTAINER
static::$default[static::DROPPER] = new InventoryType(9, "Dropper", 7); //9 CONTAINER
static::$default[static::HOPPER] = new InventoryType(5, "Hopper", 8); //5 CONTAINER
static::$default[static::ENDER_CHEST] = new InventoryType(27, "Ender Chest", 0);
static::$default[static::BEACON] = new InventoryType(0, "Beacon", 13);

static::$default[static::PLAYER_FLOATING] = new InventoryType(36, "Floating", null); //Mirror all slots of main inventory (needed for large item pickups)
//TODO: move network stuff out of here
//TODO: move inventory data to json
static::$default = [
static::CHEST => new InventoryType(27, "Chest", InventoryNetworkIds::CONTAINER),
static::DOUBLE_CHEST => new InventoryType(27 + 27, "Double Chest", InventoryNetworkIds::CONTAINER),
static::PLAYER => new InventoryType(36 + 4, "Player", InventoryNetworkIds::INVENTORY), //36 CONTAINER, 4 ARMOR
static::CRAFTING => new InventoryType(5, "Crafting", InventoryNetworkIds::INVENTORY), //yes, the use of INVENTORY is intended! 4 CRAFTING slots, 1 RESULT
static::WORKBENCH => new InventoryType(10, "Crafting", InventoryNetworkIds::WORKBENCH), //9 CRAFTING slots, 1 RESULT
static::FURNACE => new InventoryType(3, "Furnace", InventoryNetworkIds::FURNACE), //2 INPUT, 1 OUTPUT
static::ENCHANT_TABLE => new InventoryType(2, "Enchant", InventoryNetworkIds::ENCHANTMENT), //1 INPUT/OUTPUT, 1 LAPIS
static::BREWING_STAND => new InventoryType(4, "Brewing", InventoryNetworkIds::BREWING_STAND), //1 INPUT, 3 POTION
static::ANVIL => new InventoryType(3, "Anvil", InventoryNetworkIds::ANVIL), //2 INPUT, 1 OUTPUT
//TODO: add the below
static::DISPENSER => new InventoryType(9, "Dispenser", InventoryNetworkIds::DISPENSER), //9 CONTAINER
static::DROPPER => new InventoryType(9, "Dropper", InventoryNetworkIds::DROPPER), //9 CONTAINER
static::HOPPER => new InventoryType(5, "Hopper", InventoryNetworkIds::HOPPER), //5 CONTAINER
static::ENDER_CHEST => new InventoryType(27, "Ender Chest", InventoryNetworkIds::CONTAINER),
static::BEACON => new InventoryType(0, "Beacon", InventoryNetworkIds::BEACON),
];
}

/**
Expand Down
Loading

0 comments on commit 55e71a8

Please sign in to comment.