rewrite of interact system, bugfixes, ai tweaks

This commit is contained in:
GameHerobrine 2022-11-01 00:55:49 +03:00
parent f5222be9d2
commit ac1a1b2449
62 changed files with 649 additions and 491 deletions

View file

@ -8,6 +8,7 @@ class Chicken extends Animal{
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken');
$this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7);
$this->setSpeed(0.25);
$this->update();
}
public function isFood($id){

View file

@ -7,6 +7,7 @@ class Cow extends Animal{
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
$this->setSize($this->isBaby() ? 0.45 : 0.9 , $this->isBaby() ? 0.7 : 1.4);
$this->setName("Cow");
$this->setSpeed(0.2);
$this->update();
}
@ -14,6 +15,18 @@ class Cow extends Animal{
return $id === WHEAT;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === BUCKET && $slot->getMetadata() === 0){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); //remove only 1 bucket
$e->player->addItem(BUCKET, 1, 1, true);
return true;
}
}
parent::interactWith($e, $action);
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : [
[LEATHER, 0, mt_rand(0,2)],

View file

@ -1,30 +1,68 @@
<?php
class Pig extends Animal implements Rideable{
const TYPE = MOB_PIG;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
$this->server = ServerAPI::request();
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
$this->setName("Pig");
$this->setSpeed(0.25);
$this->update();
}
public function getMetadata(){
$d = parent::getMetadata();
if(!isset($this->data["Saddled"])){
$this->data["Saddled"] = 0;
}
$d[16]["value"] = ((int)$this->data["Saddled"]);
return $d;
/**
* @return boolean
*/
public function isSaddled(){
return (boolean) $this->getState();
}
/**
* @param boolean $value
*/
public function setSaddled($value = null){
$this->setState($value === null ? !$this->getState() : $value);
}
public function canRide($e){
return $this->isSaddled() && !($this->linkedEntity instanceof Entity);
}
public function interactWith(Entity $e, $action)
{
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($this->canRide($e)){
$this->linkedEntity = $e;
$e->isRiding = true;
$this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE);
return true;
}
if($e->isRiding && $this->linkedEntity->eid = $e->eid){
$this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE);
$e->isRiding = false;
$this->linkedEntity = 0;
return true;
}
if($slot->getID() === SADDLE){
if(!$this->isSaddled()){
$e->player->removeItem($slot->getID(), 0, 1);
$this->setSaddled(1);
}
return true; //avoid further interactions
}
}
return parent::interactWith($e, $action);
}
public function isFood($id){
return $id === POTATO || $id === CARROT || $id === BEETROOT;
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : ($this->data["Saddled"] ? [
return $this->isBaby() ? parent::getDrops() : ($this->isSaddled() ? [
[($this->fire > 0 ? COOKED_PORKCHOP : RAW_PORKCHOP), 0, mt_rand(0,2)],
[SADDLE, 0, 1]
] : [

View file

@ -1,18 +1,34 @@
<?php
class Sheep extends Animal{
public $color;
const TYPE = MOB_SHEEP;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
$this->setName("Sheep");
$this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor();
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3);
$this->setSpeed(0.25);
$this->update();
}
public function setSheared($v = null){
$this->data["Sheared"] = $v === null ? !$this->isSheared() : $v;
$this->updateMetadata();
}
public function isSheared(){
return $this->data["Sheared"];
}
public function getColor(){
return $this->data["Color"] & 16; //color === 16 -> color = 0, color === 17 -> color = 1 ...
}
public function getDrops(){
return $this->isBaby() ? parent::getDrops() : [
[WOOL, $this->data["Color"] & 0x0F, 1],
[WOOL, $this->getColor() & 0x0F, 1],
];
}
@ -20,40 +36,49 @@ class Sheep extends Animal{
return $id === WHEAT;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === SHEARS){
if(!$this->isSheared()){
$this->setSheared(1);
$this->server->api->entity->drop($this, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)));
if($slot->getMetadata() >= $slot->getMaxDurability()){
$this->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
}
}
return true;
}
}
return parent::interactWith($e, $action);
}
public function getMetadata(){
$d = parent::getMetadata();
if(!isset($this->data["Sheared"])){
$this->data["Sheared"] = 0;
}
if(!isset($this->data["Color"])){ //Make a new random color if it is not in data
$this->data["Color"] = $this->sheepColor();
}
$d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->data["Color"] & 0x0F); //dark manipulations are happening here...
$d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->getColor() & 0x0F);
return $d;
}
public function sheepColor(){
$pink = 0.1558;
$brown = 2.85;
$lightgrayBlackGray = 14.25;
$chance = Utils::randomFloat() * 100;
switch($chance){
case($chance <= $pink):
$color = 6;
break;
case($chance > $pink and $chance <= ($brown+$pink)):
$color = 12;
break;
case($chance > ($brown+$pink) and $chance <= ($lightgrayBlackGray+$brown+$pink)):
$rand = mt_rand(1, 3);
if($rand == 1) $color = 15;
elseif($rand == 2) $color = 7;
else $color = 8;
break;
default:
$color = 0;
break;
public function sheepColor(){ //a method from 0.8.1
$c = mt_rand(0,100);
if($c <= 4){
return 0xF; //white
}
return $color;
if($c <= 9){
return 0x7;
}
if($c <= 14){
return 0x8;
}
if($c <= 17){
return 0xC;
}
if(mt_rand(0, 500)){
return 0x0;
}
return 0x0;
}
}