Added some wrapper methods to make data flags less of a pain in the ass to work with

This commit is contained in:
Dylan K. Taylor 2017-08-17 16:52:17 +01:00
parent 83af4dcd59
commit e825ebd8fa
10 changed files with 75 additions and 35 deletions

View File

@ -1147,7 +1147,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->sleeping = clone $pos;
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [$pos->x, $pos->y, $pos->z]);
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, true, self::DATA_TYPE_BYTE);
$this->setPlayerFlag(self::DATA_PLAYER_FLAG_SLEEP, true);
$this->setSpawn($pos);
@ -1188,7 +1188,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->sleeping = null;
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0]);
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, false, self::DATA_TYPE_BYTE);
$this->setPlayerFlag(self::DATA_PLAYER_FLAG_SLEEP, false);
$this->level->sleepTicks = 0;
@ -2166,7 +2166,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
$this->craftingType = 0;
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false); //TODO: check if this should be true
$this->setGenericFlag(self::DATA_FLAG_ACTION, false); //TODO: check if this should be true
switch($packet->event){
case EntityEventPacket::USE_ITEM: //Eating
@ -2218,7 +2218,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->inventory->equipItem($packet->hotbarSlot, $packet->inventorySlot);
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
$this->setGenericFlag(self::DATA_FLAG_ACTION, false);
return true;
}
@ -2387,7 +2387,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->craftingType = 0;
if($packet->face >= 0 and $packet->face <= 5){ //Use Block, place
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
$this->setGenericFlag(self::DATA_FLAG_ACTION, false);
if(!$this->canInteract($blockVector->add(0.5, 0.5, 0.5), 13) or $this->isSpectator()){
}elseif($this->isCreative()){
@ -2482,7 +2482,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
}
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, true);
$this->setGenericFlag(self::DATA_FLAG_ACTION, true);
$this->startAction = $this->server->getTick();
}
@ -2532,7 +2532,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->level->broadcastLevelEvent($pos, LevelEventPacket::EVENT_BLOCK_STOP_BREAK);
break;
case PlayerActionPacket::ACTION_RELEASE_ITEM:
if($this->startAction > -1 and $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION)){
if($this->startAction > -1 and $this->getGenericFlag(self::DATA_FLAG_ACTION)){
if($this->inventory->getItemInHand()->getId() === Item::BOW){
$bow = $this->inventory->getItemInHand();
if($this->isSurvival() and !$this->inventory->contains(Item::get(Item::ARROW, 0, 1))){
@ -2728,7 +2728,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
$this->startAction = -1;
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
$this->setGenericFlag(self::DATA_FLAG_ACTION, false);
return true;
}
@ -2774,7 +2774,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
$this->level->dropItem($this->add(0, 1.3, 0), $item, $motion, 40);
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ACTION, false);
$this->setGenericFlag(self::DATA_FLAG_ACTION, false);
return true;
}

View File

@ -27,6 +27,6 @@ namespace pocketmine\entity;
abstract class Animal extends Creature implements Ageable{
public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
return $this->getGenericFlag(self::DATA_FLAG_BABY);
}
}

View File

@ -46,11 +46,11 @@ class Arrow extends Projectile{
}
public function isCritical() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL);
return $this->getGenericFlag(self::DATA_FLAG_CRITICAL);
}
public function setCritical(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL, $value);
$this->setGenericFlag(self::DATA_FLAG_CRITICAL, $value);
}
public function getResultDamage() : int{

View File

@ -422,7 +422,7 @@ class Effect{
switch($this->id){
case Effect::INVISIBILITY:
$entity->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_INVISIBLE, true);
$entity->setGenericFlag(Entity::DATA_FLAG_INVISIBLE, true);
$entity->setNameTagVisible(false);
break;
case Effect::SPEED:
@ -487,7 +487,7 @@ class Effect{
switch($this->id){
case Effect::INVISIBILITY:
$entity->setDataFlag(Entity::DATA_FLAGS, Entity::DATA_FLAG_INVISIBLE, false);
$entity->setGenericFlag(Entity::DATA_FLAG_INVISIBLE, false);
$entity->setNameTagVisible(true);
break;
case Effect::SPEED:

View File

@ -453,14 +453,14 @@ abstract class Entity extends Location implements Metadatable{
* @return bool
*/
public function isNameTagVisible() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG);
return $this->getGenericFlag(self::DATA_FLAG_CAN_SHOW_NAMETAG);
}
/**
* @return bool
*/
public function isNameTagAlwaysVisible() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG);
return $this->getGenericFlag(self::DATA_FLAG_ALWAYS_SHOW_NAMETAG);
}
@ -475,14 +475,14 @@ abstract class Entity extends Location implements Metadatable{
* @param bool $value
*/
public function setNameTagVisible(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_SHOW_NAMETAG, $value);
$this->setGenericFlag(self::DATA_FLAG_CAN_SHOW_NAMETAG, $value);
}
/**
* @param bool $value
*/
public function setNameTagAlwaysVisible(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ALWAYS_SHOW_NAMETAG, $value);
$this->setGenericFlag(self::DATA_FLAG_ALWAYS_SHOW_NAMETAG, $value);
}
/**
@ -521,31 +521,31 @@ abstract class Entity extends Location implements Metadatable{
public function isSneaking() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING);
return $this->getGenericFlag(self::DATA_FLAG_SNEAKING);
}
public function setSneaking(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SNEAKING, $value);
$this->setGenericFlag(self::DATA_FLAG_SNEAKING, $value);
}
public function isSprinting() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING);
return $this->getGenericFlag(self::DATA_FLAG_SPRINTING);
}
public function setSprinting(bool $value = true){
if($value !== $this->isSprinting()){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_SPRINTING, $value);
$this->setGenericFlag(self::DATA_FLAG_SPRINTING, $value);
$attr = $this->attributeMap->getAttribute(Attribute::MOVEMENT_SPEED);
$attr->setValue($value ? ($attr->getValue() * 1.3) : ($attr->getValue() / 1.3), false, true);
}
}
public function isImmobile() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE);
return $this->getGenericFlag(self::DATA_FLAG_IMMOBILE);
}
public function setImmobile(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IMMOBILE, $value);
$this->setGenericFlag(self::DATA_FLAG_IMMOBILE, $value);
}
/**
@ -553,7 +553,7 @@ abstract class Entity extends Location implements Metadatable{
* @return bool
*/
public function canClimb() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_CLIMB);
return $this->getGenericFlag(self::DATA_FLAG_CAN_CLIMB);
}
/**
@ -561,7 +561,7 @@ abstract class Entity extends Location implements Metadatable{
* @param bool $value
*/
public function setCanClimb(bool $value){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CAN_CLIMB, $value);
$this->setGenericFlag(self::DATA_FLAG_CAN_CLIMB, $value);
}
/**
@ -570,7 +570,7 @@ abstract class Entity extends Location implements Metadatable{
* @return bool
*/
public function canClimbWalls() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_WALLCLIMBING);
return $this->getGenericFlag(self::DATA_FLAG_WALLCLIMBING);
}
/**
@ -579,7 +579,7 @@ abstract class Entity extends Location implements Metadatable{
* @param bool $value
*/
public function setCanClimbWalls(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_WALLCLIMBING, $value);
$this->setGenericFlag(self::DATA_FLAG_WALLCLIMBING, $value);
}
/**
@ -1266,12 +1266,12 @@ abstract class Entity extends Location implements Metadatable{
$this->fireTicks = $ticks;
}
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ONFIRE, true);
$this->setGenericFlag(self::DATA_FLAG_ONFIRE, true);
}
public function extinguish(){
$this->fireTicks = 0;
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ONFIRE, false);
$this->setGenericFlag(self::DATA_FLAG_ONFIRE, false);
}
public function isFireProof() : bool{
@ -1930,6 +1930,26 @@ abstract class Entity extends Location implements Metadatable{
return (((int) $this->getDataProperty($propertyId)) & (1 << $flagId)) > 0;
}
/**
* Wrapper around {@link Entity#getDataFlag} for generic data flag reading.
*
* @param int $flagId
* @return bool
*/
public function getGenericFlag(int $flagId) : bool{
return $this->getDataFlag(self::DATA_FLAGS, $flagId);
}
/**
* Wrapper around {@link Entity#setDataFlag} for generic data flag setting.
*
* @param int $flagId
* @param bool $value
*/
public function setGenericFlag(int $flagId, bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, $flagId, $value, self::DATA_TYPE_LONG);
}
public function __destruct(){
$this->close();
}

View File

@ -283,7 +283,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
protected function initEntity(){
$this->setDataFlag(self::DATA_PLAYER_FLAGS, self::DATA_PLAYER_FLAG_SLEEP, false, self::DATA_TYPE_BYTE);
$this->setPlayerFlag(self::DATA_PLAYER_FLAG_SLEEP, false);
$this->setDataProperty(self::DATA_PLAYER_BED_POSITION, self::DATA_TYPE_POS, [0, 0, 0], false);
$this->inventory = new PlayerInventory($this);
@ -542,4 +542,24 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
parent::close();
}
}
/**
* Wrapper around {@link Entity#getDataFlag} for player-specific data flag reading.
*
* @param int $flagId
* @return bool
*/
public function getPlayerFlag(int $flagId){
return $this->getDataFlag(self::DATA_PLAYER_FLAGS, $flagId);
}
/**
* Wrapper around {@link Entity#setDataFlag} for player-specific data flag setting.
*
* @param int $flagId
* @param bool $value
*/
public function setPlayerFlag(int $flagId, bool $value = true){
$this->setDataFlag(self::DATA_PLAYER_FLAGS, $flagId, $value, self::DATA_TYPE_BYTE);
}
}

View File

@ -418,7 +418,7 @@ abstract class Living extends Entity implements Damageable{
public function entityBaseTick(int $tickDiff = 1) : bool{
Timings::$timerLivingEntityBaseTick->startTiming();
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BREATHING, !$this->isInsideOfWater());
$this->setGenericFlag(self::DATA_FLAG_BREATHING, !$this->isInsideOfWater());
$hasUpdate = parent::entityBaseTick($tickDiff);

View File

@ -63,7 +63,7 @@ class PrimedTNT extends Entity implements Explosive{
$this->fuse = 80;
}
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_IGNITED, true);
$this->setGenericFlag(self::DATA_FLAG_IGNITED, true);
$this->setDataProperty(self::DATA_FUSE_LENGTH, self::DATA_TYPE_INT, $this->fuse);
$this->level->broadcastLevelEvent($this, LevelEventPacket::EVENT_SOUND_IGNITE);

View File

@ -84,6 +84,6 @@ class Villager extends Creature implements NPC, Ageable{
}
public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
return $this->getGenericFlag(self::DATA_FLAG_BABY);
}
}

View File

@ -26,6 +26,6 @@ namespace pocketmine\entity;
abstract class WaterAnimal extends Creature implements Ageable{
public function isBaby() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_BABY);
return $this->getGenericFlag(self::DATA_FLAG_BABY);
}
}