Skip to content
This repository has been archived by the owner on Jan 18, 2021. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
onebone committed Aug 13, 2015
0 parents commit 9ff09e7
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
5 changes: 5 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Boat
main: onebone\boat\Main
version: "1.0"
author: onebone
api: [1.12.0]
90 changes: 90 additions & 0 deletions src/onebone/boat/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace onebone\boat;

use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\inventory\BigShapelessRecipe;
use pocketmine\item\Item;
use pocketmine\entity\Entity;
use pocketmine\event\server\DataPacketReceiveEvent;
use pocketmine\network\protocol\InteractPacket;
use pocketmine\network\protocol\SetEntityLinkPacket;
use pocketmine\network\protocol\MovePlayerPacket;
use pocketmine\event\player\PlayerQuitEvent;

use onebone\boat\item\Boat as BoatItem;
use onebone\boat\packet\PlayerInputPacket;
use onebone\boat\entity\Boat;

class Main extends PluginBase implements Listener{
private $riding = [];

public function onEnable(){
$this->getServer()->getPluginManager()->registerEvents($this, $this);

Item::$list[333] = BoatItem::class;
Item::addCreativeItem(new Item(333));
$this->getServer()->addRecipe((new BigShapelessRecipe(Item::get(333, 0, 1)))->addIngredient(Item::get(Item::WOODEN_PLANK, null, 5))->addIngredient(Item::get(Item::WOODEN_SHOVEL, null, 1)));

Entity::registerEntity("\\onebone\\boat\\entity\\Boat", true);

$this->getServer()->getNetwork()->registerPacket(0xae, PlayerInputPacket::class);
}

public function onQuit(PlayerQuitEvent $event){
if(isset($this->riding[$event->getPlayer()->getName()])){
unset($this->riding[$event->getPlayer()->getName()]);
}
}

public function onPacketReceived(DataPacketReceiveEvent $event){
$packet = $event->getPacket();
$player = $event->getPlayer();
if($packet instanceof InteractPacket){
$boat = $player->getLevel()->getEntity($packet->target);
if($boat instanceof Boat){
if($packet->action === 1){
$pk = new SetEntityLinkPacket();
$pk->from = $boat->getId();
$pk->to = $player->getId();
$pk->type = 2;

$this->getServer()->broadcastPacket($player->getLevel()->getPlayers(), $pk);
$pk = new SetEntityLinkPacket();
$pk->from = $boat->getId();
$pk->to = 0;
$pk->type = 2;
$player->dataPacket($pk);

$this->riding[$player->getName()] = $packet->target;
}elseif($packet->action === 3){
$pk = new SetEntityLinkPacket();
$pk->from = $boat->getId();
$pk->to = $player->getId();
$pk->type = 3;

$this->getServer()->broadcastPacket($player->getLevel()->getPlayers(), $pk);
$pk = new SetEntityLinkPacket();
$pk->from = $boat->getId();
$pk->to = 0;
$pk->type = 3;
$player->dataPacket($pk);

if(isset($this->riding[$event->getPlayer()->getName()])){
unset($this->riding[$event->getPlayer()->getName()]);
}
}
}
}elseif($packet instanceof MovePlayerPacket){
if(isset($this->riding[$player->getName()])){
$boat = $player->getLevel()->getEntity($this->riding[$player->getName()]);
if($boat instanceof Boat){
$boat->x = $packet->x;
$boat->y = $packet->y;
$boat->z = $packet->z;
}
}
}
}
}
65 changes: 65 additions & 0 deletions src/onebone/boat/entity/Boat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace onebone\boat\entity;

use pocketmine\entity\Entity;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\Player;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\network\protocol\EntityEventPacket;
use pocketmine\item\Item;

class Boat extends Entity{
const NETWORK_ID = 90;

public function spawnTo(Player $player){
$pk = new AddEntityPacket();
$pk->eid = $this->getId();
$pk->type = self::NETWORK_ID;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->speedX = 0;
$pk->speedY = 0;
$pk->speedZ = 0;
$pk->yaw = 0;
$pk->pitch = 0;
$pk->metadata = $this->dataProperties;
$player->dataPacket($pk);

parent::spawnTo($player);
}

public function attack($damage, EntityDamageEvent $source){
parent::attack($damage, $source);

if(!$source->isCancelled()){
$pk = new EntityEventPacket();
$pk->eid = $this->id;
$pk->event = EntityEventPacket::HURT_ANIMATION;
foreach($this->getLevel()->getPlayers() as $player){
$player->dataPacket($pk);
}
}
}

public function kill(){
parent::kill();

foreach($this->getDrops() as $item){
$this->getLevel()->dropItem($this, $item);
}
}

public function getDrops(){
return [
Item::get(333, 0, 1)
];
}

public function getSaveId(){
$class = new \ReflectionClass(static::class);
return $class->getShortName();
}
}
57 changes: 57 additions & 0 deletions src/onebone/boat/item/Boat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace onebone\boat\item;

use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\block\Block;
use pocketmine\Player;
use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Double;
use pocketmine\nbt\tag\Float;

use onebone\boat\entity\Boat as BoatEntity;

class Boat extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(333, $meta, $count, "Boat");
}

public function canBeActivated(){
return true;
}

public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$realPos = $block->getSide($face);

$boat = new BoatEntity($player->getLevel()->getChunk($realPos->getX() >> 4, $realPos->getZ() >> 4), new Compound("", [
"Pos" => new Enum("Pos", [
new Double("", $realPos->getX()),
new Double("", $realPos->getY()),
new Double("", $realPos->getZ())
]),
"Motion" => new Enum("Motion", [
new Double("", 0),
new Double("", 0),
new Double("", 0)
]),
"Rotation" => new Enum("Rotation", [
new Float("", 0),
new Float("", 0)
]),
]));
$boat->spawnToAll();

$item = $player->getInventory()->getItemInHand();
$count = $item->getCount();
if(--$count <= 0){
$player->getInventory()->setItemInHand(Item::get(Item::AIR));
return;
}

$item->setCount($count);
$player->getInventory()->setItemInHand($item);
return true;
}
}
47 changes: 47 additions & 0 deletions src/onebone/boat/packet/PlayerInputPacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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 onebone\boat\packet;

use pocketmine\network\protocol\DataPacket;

class PlayerInputPacket extends DataPacket{
const NETWORK_ID = 0xae;

public $motX;
public $motY;

public $jumping;
public $sneaking;

public function decode(){
$this->motX = $this->getFloat();
$this->motY = $this->getFloat();
$flags = $this->getByte();
$this->jumping = (($flags & 0x80) > 0);
$this->sneaking = (($flags & 0x40) > 0);
}

public function encode(){

}

}

0 comments on commit 9ff09e7

Please sign in to comment.