Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

understanding inventory, chest, crafting bugs

Comments in 'Plugin Development' started by HotPepperDaddy, Dec 13, 2014.

  1. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    Hi all, I want to have some discussion on understanding what's going on behind the scenes, possible client / server interaction, and bugs with inventory, chests, and crafting.

    Background: My daughter wanted to bake a cake. I found some code on github where someone implemented cows and chickens. I fixed it up the code so it worked and did a couple quick hacks to get milk and eggs in short order (added as item drops). Milking the cow was giving me problems, it only attacked the cow with the bucket. I started fixing that but put it on the back burner for now.

    So now I have all the ingredients for cake and can craft it. However, as soon as it's crafted (can see it for a moment in inventory) it disappears. IIRC my ingredients come back after leaving the crafting table.

    I've also noticed problems with everything disappearing from some chests.

    Possibly related, sometimes when selecting an item in inventory (the on screen one) the item doesn't show up in hand.

    I guess I'm looking for a better understanding of what's going on so if anyone could shed some light it would be appreciated. Yes, I've looked through the code and I'm about to have another go at it this morning. Thought it may help speed things up by posting a message.

    Thanks.
  2. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    Here's a diff so far.

    Code:
    diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php
    index a79f1d2..d7f1ef1 100644
    --- a/src/pocketmine/Server.php
    +++ b/src/pocketmine/Server.php
    @@ -32,10 +32,13 @@ use pocketmine\command\ConsoleCommandSender;
     use pocketmine\command\PluginIdentifiableCommand;
     use pocketmine\command\SimpleCommandMap;
     use pocketmine\entity\Arrow;
    +use pocketmine\entity\Cow;
    +use pocketmine\entity\Chicken;
     use pocketmine\entity\Entity;
     use pocketmine\entity\FallingSand;
     use pocketmine\entity\Human;
     use pocketmine\entity\Item as DroppedItem;
    +use pocketmine\entity\PigZombie;
     use pocketmine\entity\PrimedTNT;
     use pocketmine\entity\Snowball;
     use pocketmine\entity\Villager;
    @@ -2217,8 +2220,11 @@ class Server{
       private function registerEntities(){
         Entity::registerEntity(Arrow::class);
    +     Entity::registerEntity(Chicken::class);
    +     Entity::registerEntity(Cow::class);
         Entity::registerEntity(DroppedItem::class);
         Entity::registerEntity(FallingSand::class);
    +     Entity::registerEntity(PigZombie::class);
         Entity::registerEntity(PrimedTNT::class);
         Entity::registerEntity(Snowball::class);
         Entity::registerEntity(Villager::class);
    diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php
    index a8d4731..b737f62 100644
    --- a/src/pocketmine/block/Block.php
    +++ b/src/pocketmine/block/Block.php
    @@ -24,7 +24,10 @@
      */
     namespace pocketmine\block;
    +use pocketmine\entity\Chicken;
    +use pocketmine\entity\Cow;
     use pocketmine\entity\Entity;
    +use pocketmine\entity\PigZombie;
     use pocketmine\entity\Villager;
     use pocketmine\entity\Zombie;
     use pocketmine\item\Item;
    @@ -481,8 +484,8 @@ class Block extends Position implements Metadatable{
         [Item::COMPASS, 0],
         [Item::MINECART, 0],
         [Item::SPAWN_EGG, Villager::NETWORK_ID],
    -     //[Item::SPAWN_EGG, 10], //Chicken
    -     //[Item::SPAWN_EGG, 11], //Cow
    +     [Item::SPAWN_EGG, Chicken::NETWORK_ID],
    +     [Item::SPAWN_EGG, Cow::NETWORK_ID],
         //[Item::SPAWN_EGG, 12], //Pig
         //[Item::SPAWN_EGG, 13], //Sheep
         //TODO: Wolf
    @@ -493,7 +496,7 @@ class Block extends Position implements Metadatable{
         //TODO: Skeleton
         //TODO: Slime
         [Item::SPAWN_EGG, Zombie::NETWORK_ID],
    -     //TODO: PigZombie
    +     [Item::SPAWN_EGG, PigZombie::NETWORK_ID],
         //TODO: Replace with Entity constants
    @@ -1186,4 +1189,4 @@ class Block extends Position implements Metadatable{
           $this->getLevel()->getBlockMetadata()->removeMetadata($this, $metadataKey, $plugin);
         }
       }
    -}
    \ No newline at end of file
    +}
    diff --git a/src/pocketmine/entity/Chicken.php b/src/pocketmine/entity/Chicken.php
    index 56190eb..36eb88f 100644
    --- a/src/pocketmine/entity/Chicken.php
    +++ b/src/pocketmine/entity/Chicken.php
    @@ -21,7 +21,76 @@
     namespace pocketmine\entity;
    +use pocketmine\event\entity\EntityDamageByEntityEvent;
    +use pocketmine\item\Item as ItemItem;
    +use pocketmine\network\protocol\AddMobPacket;
    +use pocketmine\network\protocol\SetEntityMotionPacket;
    +use pocketmine\Player;
     class Chicken extends Animal{
    +   const NETWORK_ID = 10;
    +   public $width = 1.0;
    +   public $length = 0.8;
    +   public $height = 0.8;
    -}
    \ No newline at end of file
    +   protected function initEntity(){
    +     $this->setMaxHealth(5);
    +   }
    +
    +   public function getName(){
    +     return "Chicken";
    +   }
    +
    +   public function spawnTo(Player $player){
    +     $pk = new AddMobPacket();
    +     $pk->eid = $this->getID();
    +     $pk->type = Chicken::NETWORK_ID;
    +     $pk->x = $this->x;
    +     $pk->y = $this->y;
    +     $pk->z = $this->z;
    +     $pk->yaw = $this->yaw;
    +     $pk->pitch = $this->pitch;
    +     $pk->metadata = $this->getData();
    +     $player->dataPacket($pk);
    +     $pk = new SetEntityMotionPacket();
    +     $pk->entities = [
    +       [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
    +     ];
    +     $player->dataPacket($pk);
    +     parent::spawnTo($player);
    +   }
    +
    +   public function getData(){ //TODO
    +     $flags = 0;
    +     $flags |= $this->fireTicks > 0 ? 1 : 0;
    +     //$flags |= ($this->crouched === true ? 0b10:0) << 1;
    +     //$flags |= ($this->inAction === true ? 0b10000:0);
    +     $d = [
    +       0 => ["type" => 0, "value" => $flags],
    +       1 => ["type" => 1, "value" => $this->airTicks],
    +       16 => ["type" => 0, "value" => 0],
    +       17 => ["type" => 6, "value" => [0, 0, 0]],
    +     ];
    +     return $d;
    +   }
    +
    +   public function getDrops(){
    +     $drops = [
    +       ItemItem::get(ItemItem::FEATHER, 0, 1),
    +       ItemItem::get(ItemItem::EGG, 0, 1)
    +     ];
    +     if($this->lastDamageCause instanceof EntityDamageByEntityEvent and $this->lastDamageCause->getEntity() instanceof Player){
    +       $rnd = mt_rand(0, 199);
    +       if($rnd < 5){
    +         switch(mt_rand(0, 0)){
    +           case 0:
    +             $drops[] = ItemItem::get(ItemItem::RAW_CHICKEN, 0, 1);
    +             break;
    +         }
    +       } else if($rnd < 10) {
    +         $drops[] = ItemItem::get(ItemItem::EGG, 0, 1);
    +       }
    +     }
    +     return $drops;
    +   }
    +}
    diff --git a/src/pocketmine/entity/Cow.php b/src/pocketmine/entity/Cow.php
    index c954d80..7f919f7 100644
    --- a/src/pocketmine/entity/Cow.php
    +++ b/src/pocketmine/entity/Cow.php
    @@ -22,6 +22,66 @@
     namespace pocketmine\entity;
    -class Cow extends Animal{
    +use pocketmine\event\entity\EntityDamageByEntityEvent;
    +use pocketmine\item\Item as ItemItem;
    +use pocketmine\network\protocol\AddMobPacket;
    +use pocketmine\network\protocol\SetEntityMotionPacket;
    +use pocketmine\Player;
    +
    -}
    \ No newline at end of file
    +class Cow extends Animal{
    +   const NETWORK_ID = 11;
    +   public $width = 0.7;
    +   public $length = 1.6;
    +   public $height = 1.0;
    +   public function getName(){
    +     return "Cow";
    +   }
    +   public function spawnTo(Player $player){
    +     $pk = new AddMobPacket();
    +     $pk->eid = $this->getID();
    +     $pk->type = Cow::NETWORK_ID;
    +     $pk->x = $this->x;
    +     $pk->y = $this->y;
    +     $pk->z = $this->z;
    +     $pk->yaw = $this->yaw;
    +     $pk->pitch = $this->pitch;
    +     $pk->metadata = $this->getData();
    +     $player->dataPacket($pk);
    +     $pk = new SetEntityMotionPacket();
    +     $pk->entities = [
    +       [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
    +     ];
    +     $player->dataPacket($pk);
    +     parent::spawnTo($player);
    +   }
    +   public function getData(){ //TODO
    +     $flags = 0;
    +     $flags |= $this->fireTicks > 0 ? 1 : 0;
    +     //$flags |= ($this->crouched === true ? 0b10:0) << 1;
    +     //$flags |= ($this->inAction === true ? 0b10000:0);
    +     $d = [
    +       0 => ["type" => 0, "value" => $flags],
    +       1 => ["type" => 1, "value" => $this->airTicks],
    +       16 => ["type" => 0, "value" => 0],
    +       17 => ["type" => 6, "value" => [0, 0, 0]],
    +     ];
    +     return $d;
    +   }
    +   public function getDrops(){
    +     $drops = [
    +       ItemItem::get(ItemItem::LEATHER, 0, 1),
    +       ItemItem::get(ItemItem::BUCKET, 1, 1)
    +     ];
    +     if($this->lastDamageCause instanceof EntityDamageByEntityEvent and $this->lastDamageCause->getEntity() instanceof Player){
    +       if(mt_rand(0, 199) < 5){
    +         switch(mt_rand(0, 0)){
    +           case 0:
    +             $drops[] = ItemItem::get(ItemItem::RAW_BEEF, 0, 1);
    +             break;
    +         }
    +       }
    +     }
    +     return $drops;
    +   }
    +}
    diff --git a/src/pocketmine/entity/PigZombie.php b/src/pocketmine/entity/PigZombie.php
    index fdc8272..2378c60 100644
    --- a/src/pocketmine/entity/PigZombie.php
    +++ b/src/pocketmine/entity/PigZombie.php
    @@ -21,7 +21,71 @@
     namespace pocketmine\entity;
    +use pocketmine\event\entity\EntityDamageByEntityEvent;
    +use pocketmine\item\Item as ItemItem;
    +use pocketmine\network\protocol\AddMobPacket;
    +use pocketmine\network\protocol\SetEntityMotionPacket;
    +use pocketmine\Player;
    -class PigZombie extends Zombie{
    -}
    \ No newline at end of file
    +class PigZombie extends Zombie{
    +   const NETWORK_ID = 36;
    +   public $width = 0.6;
    +   public $length = 0.6;
    +   public $height = 1.8;
    +   public function getName(){
    +     return "PigZombie";
    +   }
    +   public function spawnTo(Player $player){
    +     $pk = new AddMobPacket();
    +     $pk->eid = $this->getID();
    +     $pk->type = PigZombie::NETWORK_ID;
    +     $pk->x = $this->x;
    +     $pk->y = $this->y;
    +     $pk->z = $this->z;
    +     $pk->yaw = $this->yaw;
    +     $pk->pitch = $this->pitch;
    +     $pk->metadata = $this->getData();
    +     $player->dataPacket($pk);
    +     $pk = new SetEntityMotionPacket();
    +     $pk->entities = [
    +       [$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
    +     ];
    +     $player->dataPacket($pk);
    +     parent::spawnTo($player);
    +   }
    +   public function getData(){ //TODO
    +     $flags = 0;
    +     $flags |= $this->fireTicks > 0 ? 1 : 0;
    +     //$flags |= ($this->crouched === true ? 0b10:0) << 1;
    +     //$flags |= ($this->inAction === true ? 0b10000:0);
    +     $d = [
    +       0 => ["type" => 0, "value" => $flags],
    +       1 => ["type" => 1, "value" => $this->airTicks],
    +       16 => ["type" => 0, "value" => 0],
    +       17 => ["type" => 6, "value" => [0, 0, 0]],
    +     ];
    +     return $d;
    +   }
    +   public function getDrops(){
    +     $drops = [
    +       ItemItem::get(ItemItem::FEATHER, 0, 1)
    +     ];
    +     if($this->lastDamageCause instanceof EntityDamageByEntityEvent and $this->lastDamageCause->getEntity() instanceof Player){
    +       if(mt_rand(0, 199) < 5){
    +         switch(mt_rand(0, 2)){
    +           case 0:
    +             $drops[] = ItemItem::get(ItemItem::GOLD_SWORD, 0, 1);
    +             break;
    +           case 1:
    +             $drops[] = ItemItem::get(ItemItem::CARROT, 0, 1);
    +             break;
    +           case 2:
    +             $drops[] = ItemItem::get(ItemItem::POTATO, 0, 1);
    +             break;
    +         }
    +       }
    +     }
    +     return $drops;
    +   }
    +}
    diff --git a/src/pocketmine/item/Bucket.php b/src/pocketmine/item/Bucket.php
    index 2d4e482..b78ea5b 100644
    --- a/src/pocketmine/item/Bucket.php
    +++ b/src/pocketmine/item/Bucket.php
    @@ -24,9 +24,11 @@ namespace pocketmine\item;
     use pocketmine\block\Air;
     use pocketmine\block\Block;
     use pocketmine\block\Liquid;
    +use pocketmine\entity\Cow;
     use pocketmine\event\player\PlayerBucketFillEvent;
     use pocketmine\level\Level;
     use pocketmine\Player;
    +use pocketmine\Server;
     class Bucket extends Item{
       public function __construct($meta = 0, $count = 1){
    @@ -41,8 +43,17 @@ class Bucket extends Item{
         return true;
       }
    +   public function useOn($object){
    +     Server::getInstance()->broadcastMessage('useOn: ' . print_r($object));
    +     return false;
    +   }
    +
       public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
         $targetBlock = Block::get($this->meta);
    +// Server::getInstance()->broadcastMessage('meta: ' . $this->meta);
    +// Server::getInstance()->broadcastMessage('block: ' . print_r($block));
    +// Server::getInstance()->broadcastMessage('target: ' . print_r($target));
    +// Server::getInstance()->broadcastMessage('targetBlock: ' . print_r($targetBlock));
         if($targetBlock instanceof Air){
           if($target instanceof Liquid and $target->getDamage() === 0){
    @@ -72,8 +83,22 @@ class Bucket extends Item{
           }else{
             $player->getInventory()->sendContents($player);
           }
    +     }elseif($targetBlock instanceof Cow){
    +       $result = clone $this;
    +       $result->setDamage(0);
    +       $result->meta = 1;
    +       $player->getServer()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $block, $face, $this, $result));
    +       if(!$ev->isCancelled()){
    +         $player->getLevel()->setBlock($block, $targetBlock, true, true);
    +         if($player->isSurvival()){
    +           $player->getInventory()->setItemInHand($ev->getItem(), $player);
    +         }
    +         return true;
    +       }else{
    +         $player->getInventory()->sendContents($player);
    +       }
         }
         return false;
       }
    -}
    \ No newline at end of file
    +}
    diff --git a/src/pocketmine/item/Egg.php b/src/pocketmine/item/Egg.php
    new file mode 100644
    index 00000000..798ce6b
    --- /dev/null
    +++ b/src/pocketmine/item/Egg.php
    @@ -0,0 +1,30 @@
    +<?php
    +
    +/*
    + *
    + *  ____  _  _  __  __ _  __  __ ____  
    + * |  _ \ ___  ___| | _____| |_|  \/  (_)_ __  ___  |  \/  |  _ \
    + * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
    + * |  __/ (_) | (__|  <  __/ |_| |  | | | | | |  __/_____| |  | |  __/
    + * |_|  \___/ \___|_|\_\___|\__|_|  |_|_|_| |_|\___|  |_|  |_|_|
    + *
    + * This program is free software: you can redistribute it and/or modify
    + * it under the terms of the GNU Lesser General Public License as published by
    + * the Free Software Foundation, either version 3 of the License, or
    + * (at your option) any later version.
    + *
    + * @author PocketMine Team
    + * @link http://www.pocketmine.net/
    + *
    + *
    +*/
    +
    +namespace pocketmine\item;
    +
    +
    +class Egg extends Item{
    +   public function __construct($meta = 0, $count = 1){
    +     parent::__construct(self::EGG, 0, $count, "Egg");
    +   }
    +
    +}
    diff --git a/src/pocketmine/item/Feather.php b/src/pocketmine/item/Feather.php
    index c73ce56..07a7407 100644
    --- a/src/pocketmine/item/Feather.php
    +++ b/src/pocketmine/item/Feather.php
    @@ -27,4 +27,4 @@ class Feather extends Item{
         parent::__construct(self::FEATHER, 0, $count, "Feather");
       }
    -}
    \ No newline at end of file
    +}
    diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php
    index 4a47d6d..8a65263 100644
    --- a/src/pocketmine/item/Item.php
    +++ b/src/pocketmine/item/Item.php
    @@ -429,6 +429,7 @@ class Item{
           self::$list[self::STICK] = Stick::class;
           self::$list[self::SNOWBALL] = Snowball::class;
           self::$list[self::BOWL] = Bowl::class;
    +       self::$list[self::EGG] = Egg::class;
           self::$list[self::FEATHER] = Feather::class;
           self::$list[self::BRICK] = Brick::class;
           self::$list[self::LEATHER_CAP] = LeatherCap::class;
    @@ -659,4 +660,4 @@ class Item{
         return $this->id === $item->getId() and ($checkDamage === false or $this->getDamage() === $item->getDamage());
       }
    -}
    \ No newline at end of file
    +}
    
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    From what tree is this diff from?
  4. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    That's a diff between master (up to date) and a working branch of mine (also up to date with master).
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But master has PigZombie.php but you are creating that file in the diff.
  6. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    Nope, look closer. PigZombie (and the other entities) were there but only stubs.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yeah but you create the files instead of filling the stubs.
  8. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    Code:
    diff --git a/src/pocketmine/entity/PigZombie.php b/src/pocketmine/entity/PigZombie.php
    index fdc8272..2378c60 100644
    --- a/src/pocketmine/entity/PigZombie.php
    +++ b/src/pocketmine/entity/PigZombie.php
    @@ -21,7 +21,71 @@
    namespace pocketmine\entity;
    +use pocketmine\event\entity\EntityDamageByEntityEvent;
    +use pocketmine\item\Item as ItemItem;
    +use pocketmine\network\protocol\AddMobPacket;
    +use pocketmine\network\protocol\SetEntityMotionPacket;
    +use pocketmine\Player;
    -class PigZombie extends Zombie{
    -}
    
    That's a diff to an existing file. The files exist in both branches here as well.
  9. HotPepperDaddy
    Offline

    HotPepperDaddy New Member

    Joined:
    Nov 9, 2014
    Posts:
    23
    Minecraft User:
    HotPepperDaddy
    Egg was a new file. Maybe there's something wierd with your branch.

    I'm noticing the same thing with snow. Get 3 snow blocks, goto crafting table, make a snow layer. The first time I tried I actually got it in my inventory. I went to put it down and it disappeared, reverting back to 3 snow blocks. All subsequent crafts of the snow layer flash and revert back to snow blocks right away.

    Can anyone repeat that?

    Thanks.

Share This Page

Advertisement