本文整理汇总了PHP中pocketmine\event\player\PlayerItemConsumeEvent::isCancelled方法的典型用法代码示例。如果您正苦于以下问题:PHP PlayerItemConsumeEvent::isCancelled方法的具体用法?PHP PlayerItemConsumeEvent::isCancelled怎么用?PHP PlayerItemConsumeEvent::isCancelled使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pocketmine\event\player\PlayerItemConsumeEvent
的用法示例。
在下文中一共展示了PlayerItemConsumeEvent::isCancelled方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet->pid() === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
return;
}
switch ($packet->pid()) {
case ProtocolInfo::LOGIN_PACKET:
/*
* A/N: Not going to implement any session code until it actually does stuff.
* Single line functions are obnoxious to follow.
*/
if ($this->loggedIn === true) {
break;
}
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->setNameTag($this->username);
$this->iusername = strtolower($this->username);
$this->randomClientId = $packet->clientId;
$this->loginData = ["clientId" => $packet->clientId, "loginData" => null];
$this->uuid = $packet->clientUUID;
$this->rawUUID = $this->uuid->toBinary();
$this->clientSecret = $packet->clientSecret;
$this->protocol = $packet->protocol1;
if (count($this->server->getOnlinePlayers()) > $this->server->getMaxPlayers() and $this->kick("Server is full")) {
break;
}
if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "";
if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "Please update Minecraft PE to join.";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->dataPacket($pk);
} else {
$message = "Please use an older version of Minecraft PE.";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->dataPacket($pk);
}
$this->close("", $message, false);
return;
}
if (strpos($packet->username, "") !== false or preg_match('#^[a-zA-Z0-9_]{3,16}$#', $packet->username) == 0 or $this->username === "" or $this->iusername === "rcon" or $this->iusername === "console" or strlen($packet->username) > 16 or strlen($packet->username) < 3) {
$this->close("", "Please choose a valid username.");
return;
}
if (strlen($packet->skin) < 64 * 32 * 4) {
$this->close("", "Invalid skin.", false);
return;
}
$this->setSkin($packet->skin, $packet->slim);
$this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason"));
if ($ev->isCancelled()) {
$this->close("", $ev->getKickMessage());
return;
}
if (!$this->server->isWhitelisted(strtolower($this->getName()))) {
$this->close(TextFormat::YELLOW . $this->username . " has left the game", "Server is private.");
return;
} elseif ($this->server->getNameBans()->isBanned(strtolower($this->getName())) or $this->server->getIPBans()->isBanned($this->getAddress())) {
$this->close(TextFormat::YELLOW . $this->username . " has left the game", "You have been banned.");
return;
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this);
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this);
}
foreach ($this->server->getOnlinePlayers() as $p) {
if ($p !== $this and strtolower($p->getName()) === strtolower($this->getName())) {
if ($p->kick("You connected from somewhere else.") === false) {
$this->close(TextFormat::YELLOW . $this->getName() . " has left the game", "You connected from somewhere else.");
return;
} else {
return;
}
}
}
$nbt = $this->server->getOfflinePlayerData($this->username);
if (!isset($nbt->NameTag)) {
//.........这里部分代码省略.........
示例2: eatFoodInHand
public function eatFoodInHand()
{
if ($this->eatCoolDown + 1800 >= time()) {
return;
}
$eatenItem = $this->inventory->getItemInHand();
if ($eatenItem instanceof Food) {
if ($this->getRealFood() >= 20 && $eatenItem->getId() !== Item::GOLDEN_APPLE && $eatenItem->getId() !== Item::ENCHANTED_GOLDEN_APPLE && $eatenItem->getId() !== Item::POTION && !($eatenItem->getId() === Item::BUCKET && $eatenItem->getDamage() === 1)) {
$this->server->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($this, $eatenItem));
$ev->setCancelled();
} elseif ($this->getRealFood() < 20 || ($eatenItem->getId() === Item::GOLDEN_APPLE || $eatenItem->getId() === Item::ENCHANTED_GOLDEN_APPLE || $eatenItem->getId() === Item::POTION || $eatenItem->getId() === Item::BUCKET && $eatenItem->getDamage() === 1)) {
$this->server->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($this, $eatenItem));
if ($ev->isCancelled()) {
$this->inventory->sendContents($this);
return;
}
$pk = new EntityEventPacket();
$pk->eid = $this->getId();
$pk->event = EntityEventPacket::USE_ITEM;
$this->dataPacket($pk);
Server::broadcastPacket($this->getViewers(), $pk);
$this->setFood($this->getFood() + $eatenItem->getSaturation());
--$eatenItem->count;
$this->inventory->setItemInHand($eatenItem);
if ($eatenItem->getId() === Item::MUSHROOM_STEW || $eatenItem->getId() === Item::BEETROOT_SOUP || $eatenItem->getId() === Item::RABBIT_STEW) {
$this->inventory->addItem(Item::get(Item::BOWL, 0, 1));
} elseif ($eatenItem->getId() === Item::POTION) {
$this->inventory->addItem(Item::get(Item::GLASS_BOTTLE, 0, 1));
} elseif ($eatenItem->getId() === Item::BUCKET && $eatenItem->getDamage() === 1) {
$this->inventory->addItem(Item::get(Item::BUCKET, 0, 1));
$this->removeAllEffects();
}
if (!empty($eatenItem->getEffects())) {
foreach ($eatenItem->getEffects() as $effects => $keys) {
if (mt_rand(0, 100) <= $keys[1] * 100) {
$this->addEffect($keys[0]);
}
}
}
}
}
}
示例3: onConsume
public function onConsume(PlayerItemConsumeEvent $ev)
{
if ($ev->isCancelled()) {
return;
}
$pl = $ev->getPlayer();
if ($pl->hasPermission("wp.banitem.exempt")) {
return;
}
$world = $pl->getLevel()->getName();
if (!isset($this->wcfg[$world])) {
return;
}
$item = $ev->getItem();
if (!isset($this->wcfg[$world][$item->getId()])) {
return;
}
$pl->sendMessage(mc::_("You can not use that item here!"));
$ev->setCancelled();
}
示例4: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
return;
}
switch ($packet->pid()) {
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn === true) {
break;
}
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->nameTag = $this->username;
$this->iusername = strtolower($this->username);
$this->loginData = ["clientId" => $packet->clientId, "loginData" => $packet->loginData];
if (count($this->server->getOnlinePlayers()) > $this->server->getMaxPlayers()) {
if ($this->kick("server full") === true) {
return;
}
}
if ($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL) {
if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) {
$pk = new LoginStatusPacket();
$pk->status = 1;
$this->dataPacket($pk);
} else {
$pk = new LoginStatusPacket();
$pk->status = 2;
$this->dataPacket($pk);
}
$this->close("", "Incorrect protocol #" . $packet->protocol1, false);
return;
}
if (strpos($packet->username, "") !== false or preg_match('#^[a-zA-Z0-9_]{3,16}$#', $packet->username) == 0 or $this->username === "" or $this->iusername === "rcon" or $this->iusername === "console" or strlen($packet->username) > 16 or strlen($packet->username) < 3) {
$this->close("", "Bad username");
return;
}
$this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason"));
if ($ev->isCancelled()) {
$this->close("", $ev->getKickMessage());
return;
}
if (!$this->server->isWhitelisted(strtolower($this->getName()))) {
$this->close(TextFormat::YELLOW . $this->username . " has left the game", "Server is white-listed");
return;
} elseif ($this->server->getNameBans()->isBanned(strtolower($this->getName())) or $this->server->getIPBans()->isBanned($this->getAddress())) {
$this->close(TextFormat::YELLOW . $this->username . " has left the game", "You are banned");
return;
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this);
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this);
}
foreach ($this->server->getOnlinePlayers() as $p) {
if ($p !== $this and strtolower($p->getName()) === strtolower($this->getName())) {
if ($p->kick("logged in from another location") === false) {
$this->close(TextFormat::YELLOW . $this->getName() . " has left the game", "Logged in from another location");
return;
} else {
break;
}
}
}
$nbt = $this->server->getOfflinePlayerData($this->username);
if (!isset($nbt->NameTag)) {
$nbt->NameTag = new String("NameTag", $this->username);
} else {
$nbt["NameTag"] = $this->username;
}
$this->gamemode = $nbt["playerGameType"] & 0x3;
if ($this->server->getForceGamemode()) {
$this->gamemode = $this->server->getGamemode();
$nbt->playerGameType = new Int("playerGameType", $this->gamemode);
}
if (($level = $this->server->getLevelByName($nbt["Level"])) === null) {
$this->setLevel($this->server->getDefaultLevel(), true);
$nbt["Level"] = $this->level->getName();
$nbt["Pos"][0] = $this->level->getSpawnLocation()->x;
$nbt["Pos"][1] = $this->level->getSpawnLocation()->y;
$nbt["Pos"][2] = $this->level->getSpawnLocation()->z;
} else {
$this->setLevel($level, true);
}
if (!$nbt instanceof Compound) {
$this->close(TextFormat::YELLOW . $this->username . " has left the game", "Invalid data");
//.........这里部分代码省略.........
示例5: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet::NETWORK_ID === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
$timings->stopTiming();
return;
}
switch ($packet::NETWORK_ID) {
case ProtocolInfo::ITEM_FRAME_DROP_ITEM_PACKET:
$tile = $this->level->getTile($this->temporalVector->setComponents($packet->x, $packet->y, $packet->z));
if ($tile instanceof ItemFrame) {
$block = $this->level->getBlock($tile);
$this->server->getPluginManager()->callEvent($ev = new BlockBreakEvent($this, $block, $this->getInventory()->getItemInHand(), true));
if (!$ev->isCancelled()) {
$item = $tile->getItem();
$this->server->getPluginManager()->callEvent($ev = new ItemFrameDropItemEvent($this, $block, $tile, $item));
if (!$ev->isCancelled()) {
if ($item->getId() !== Item::AIR) {
if (mt_rand(0, 10) / 10 < $tile->getItemDropChance()) {
$this->level->dropItem($tile, $item);
}
$tile->setItem(Item::get(Item::AIR));
$tile->setItemRotation(0);
}
} else {
$tile->spawnTo($this);
}
} else {
$tile->spawnTo($this);
}
}
break;
case ProtocolInfo::REQUEST_CHUNK_RADIUS_PACKET:
/*if($this->spawned){
$this->viewDistance = $packet->radius ** 2;
}*/
$pk = new ChunkRadiusUpdatedPacket();
$pk->radius = $this->server->chunkRadius != -1 ? $this->server->chunkRadius : $packet->radius;
$this->dataPacket($pk);
break;
case ProtocolInfo::PLAYER_INPUT_PACKET:
break;
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn) {
break;
}
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_SUCCESS;
$this->dataPacket($pk);
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->setNameTag($this->username);
$this->iusername = strtolower($this->username);
$this->protocol = $packet->protocol;
if ($this->server->getConfigBoolean("online-mode", false) && $packet->identityPublicKey === null) {
$this->kick("disconnectionScreen.notAuthenticated", false);
break;
}
if (count($this->server->getOnlinePlayers()) >= $this->server->getMaxPlayers() and $this->kick("disconnectionScreen.serverFull", false)) {
break;
}
if (!in_array($packet->protocol, ProtocolInfo::ACCEPTED_PROTOCOLS)) {
if ($packet->protocol < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "disconnectionScreen.outdatedClient";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->directDataPacket($pk);
} else {
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->directDataPacket($pk);
}
$this->close("", $message, false);
break;
}
$this->randomClientId = $packet->clientId;
$this->uuid = UUID::fromString($packet->clientUUID);
$this->rawUUID = $this->uuid->toBinary();
$valid = true;
$len = strlen($packet->username);
//.........这里部分代码省略.........
示例6: onDataPacketReceiveEvent
public function onDataPacketReceiveEvent(DataPacketReceiveEvent $event)
{
$packet = $event->getPacket();
$player = $event->getPlayer();
if ($packet instanceof EntityEventPacket) {
if ($player->spawned === \true or $player->blocked === \false or $player->isAlive()) {
$player->craftingType = 0;
$player->setDataFlag(Player::DATA_FLAGS, Player::DATA_FLAG_ACTION, \false);
// TODO: check if this should be true
switch ($packet->event) {
case 9:
// Eating
$items = [Item::APPLE => 4, Item::MUSHROOM_STEW => 10, Item::BEETROOT_SOUP => 10, Item::BREAD => 5, Item::RAW_PORKCHOP => 3, Item::COOKED_PORKCHOP => 8, Item::RAW_BEEF => 3, Item::STEAK => 8, Item::COOKED_CHICKEN => 6, Item::RAW_CHICKEN => 2, Item::MELON_SLICE => 2, Item::GOLDEN_APPLE => 10, Item::PUMPKIN_PIE => 8, Item::CARROT => 4, Item::POTATO => 1, Item::BAKED_POTATO => 6, Item::COOKIE => 2, Item::COOKED_FISH => [0 => 5, 1 => 6], Item::RAW_FISH => [0 => 2, 1 => 2, 2 => 1, 3 => 1]];
$slot = $player->getInventory()->getItemInHand();
if (isset($items[$slot->getId()])) {
$this->getServer()->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($player, $slot));
if ($ev->isCancelled()) {
$player->getInventory()->sendContents($this);
break;
}
$pk = new EntityEventPacket();
$pk->eid = $player->getId();
$pk->event = EntityEventPacket::USE_ITEM;
$player->dataPacket($pk);
Server::broadcastPacket($player->getViewers(), $pk);
$amount = $items[$slot->getId()];
if (\is_array($amount)) {
$amount = isset($amount[$slot->getDamage()]) ? $amount[$slot->getDamage()] : 0;
}
$ev = new EntityRegainHealthEvent($player, $amount, EntityRegainHealthEvent::CAUSE_EATING);
$this->getServer()->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
return;
}
--$slot->count;
$player->getInventory()->setItemInHand($slot, $player);
if ($slot->getId() === Item::MUSHROOM_STEW or $slot->getId() === Item::BEETROOT_SOUP) {
$this->getInventory()->addItem(Item::get(Item::BOWL, 0, 1));
} elseif ($slot->getId() === Item::RAW_FISH and $slot->getDamage() === 3) {
// Pufferfish
$player->addEffect(Effect::getEffect(Effect::HUNGER)->setAmplifier(2)->setDuration(15 * 20));
$player->addEffect(Effect::getEffect(Effect::POISON)->setAmplifier(3)->setDuration(60 * 20));
}
}
$event->setCancelled();
break;
}
}
if ($packet instanceof PlayerActionPacket) {
if ($packet->action == PlayerActionPacket::ACTION_JUMP) {
if ($event->getPlayer()->isSprinting()) {
HungerSystem::exhaustion($event->getPlayer(), HungerSystem::JUMPING_WHILE_SPRINTING);
} else {
if ($event->getPlayer()->isSprinting()) {
HungerSystem::exhaustion($event->getPlayer(), HungerSystem::JUMPING);
}
}
}
if ($packet->action == PlayerActionPacket::ACTION_START_SPRINT) {
$attribute = AttributeProvider::getInstance()->getAttribute($event->getPlayer());
if ($attribute->getHunger() < 6) {
$event->setCancelled();
}
}
}
}
}
示例7: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet::NETWORK_ID === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
$timings->stopTiming();
return;
}
switch ($packet::NETWORK_ID) {
case ProtocolInfo::ITEM_FRAME_DROP_ITEM_PACKET:
$tile = $this->level->getTile($this->temporalVector->setComponents($packet->x, $packet->y, $packet->z));
if ($tile instanceof ItemFrame) {
if ($tile->getItem()->getId() !== Item::AIR) {
$this->getServer()->getPluginManager()->callEvent($ev = new ItemFrameDropItemEvent($this->getLevel()->getBlock($tile), $this, $tile->getItem(), $tile->getItemDropChance()));
if (!$ev->isCancelled()) {
if (mt_rand(0, 10) / 10 <= $tile->getItemDropChance()) {
$this->level->dropItem($tile, $tile->getItem());
}
$tile->setItem(Item::get(Item::AIR));
$tile->setItemRotation(0);
}
}
}
break;
case ProtocolInfo::PLAYER_INPUT_PACKET:
break;
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn) {
break;
}
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->iusername = strtolower($this->username);
#$this->setDataProperty(self::DATA_NAMETAG, self::DATA_TYPE_STRING, $this->username, false);
if ($this->server->getMaxPlayers() !== -1) {
if (count($this->server->getOnlinePlayers()) >= $this->server->getMaxPlayers() and $this->kick("disconnectionScreen.serverFull", false)) {
break;
}
}
if (!in_array($packet->protocol, ProtocolInfo::ACCEPT_PROTOCOL)) {
if ($packet->protocol < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "disconnectionScreen.outdatedClient";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->directDataPacket($pk);
} else {
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->directDataPacket($pk);
}
$this->close("", $message, false);
break;
}
$this->randomClientId = $packet->clientId;
$this->uuid = UUID::fromString($packet->clientUUID);
$this->rawUUID = $this->uuid->toBinary();
$this->identityPublicKey = $packet->identityPublicKey;
$this->chainData = $packet->chainData;
$valid = true;
$len = strlen($packet->username);
if ($len > 16 or $len < 3) {
$valid = false;
}
for ($i = 0; $i < $len and $valid; ++$i) {
$c = ord($packet->username[$i]);
if ($c >= ord("a") and $c <= ord("z") or $c >= ord("A") and $c <= ord("Z") or $c >= ord("0") and $c <= ord("9") or $c === ord("_")) {
continue;
}
$valid = false;
break;
}
if (!$valid or $this->iusername === "rcon" or $this->iusername === "console") {
$this->close("", "disconnectionScreen.invalidName");
break;
}
if (strlen($packet->skin) !== 64 * 32 * 4 and strlen($packet->skin) !== 64 * 64 * 4) {
$this->close("", "disconnectionScreen.invalidSkin");
break;
}
$this->setSkin($packet->skin, $packet->skinId);
$this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason"));
//.........这里部分代码省略.........
示例8: onDataPacketReceiveEvent
public function onDataPacketReceiveEvent(DataPacketReceiveEvent $event)
{
$packet = $event->getPacket();
$player = $event->getPlayer();
if ($packet instanceof EntityEventPacket) {
if ($player->spawned === \true or $player->blocked === \false or $player->isAlive()) {
$player->craftingType = 0;
$player->setDataFlag(Player::DATA_FLAGS, Player::DATA_FLAG_ACTION, \false);
// TODO: check if this should be true
switch ($packet->event) {
case 9:
// Eating
$items = [Item::GOLDEN_APPLE => 8];
$slot = $player->getInventory()->getItemInHand();
if (isset($items[$slot->getId()])) {
$this->getServer()->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($player, $slot));
if ($ev->isCancelled()) {
$player->getInventory()->sendContents($player);
break;
}
$pk = new EntityEventPacket();
$pk->eid = $player->getId();
$pk->event = EntityEventPacket::USE_ITEM;
$player->dataPacket($pk);
Server::broadcastPacket($player->getViewers(), $pk);
$amount = $items[$slot->getId()];
if (\is_array($amount)) {
$amount = isset($amount[$slot->getDamage()]) ? $amount[$slot->getDamage()] : 0;
}
$ev = new EntityRegainHealthEvent($player, $amount, EntityRegainHealthEvent::CAUSE_EATING);
$this->getServer()->getPluginManager()->callEvent($ev);
if ($ev->isCancelled()) {
return;
}
--$slot->count;
$player->getInventory()->setItemInHand($slot, $player);
}
$event->setCancelled();
break;
}
}
if ($packet instanceof PlayerActionPacket) {
if ($packet->action == PlayerActionPacket::ACTION_JUMP) {
if ($event->getPlayer()->isSprinting()) {
HungerSystem::exhaustion($event->getPlayer(), HungerSystem::JUMPING_WHILE_SPRINTING);
} else {
if ($event->getPlayer()->isSprinting()) {
HungerSystem::exhaustion($event->getPlayer(), HungerSystem::JUMPING);
}
}
}
if ($packet->action == PlayerActionPacket::ACTION_START_SPRINT) {
$attribute = AttributeProvider::getInstance()->getAttribute($event->getPlayer());
if ($attribute->getHunger() < 6) {
$event->setCancelled();
}
}
}
}
}
示例9: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet->pid() === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
return;
}
switch ($packet->pid()) {
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn === true) {
break;
}
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->nameTag = $this->username;
$this->iusername = strtolower($this->username);
$this->randomClientId = $packet->clientId;
$this->loginData = ["clientId" => $packet->clientId, "loginData" => null];
$this->uuid = Utils::dataToUUID($this->randomClientId, $this->iusername, $this->getAddress());
if (count($this->server->getOnlinePlayers()) > $this->server->getMaxPlayers() and $this->kick("disconnectionScreen.serverFull", false)) {
return;
}
if ($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL) {
if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "disconnectionScreen.outdatedClient";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
} else {
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->dataPacket($pk->setChannel(Network::CHANNEL_PRIORITY));
}
$this->close("", $message, false);
return;
}
if (strpos($packet->username, "") !== false or preg_match('#^[a-zA-Z0-9_]{3,16}$#', $packet->username) == 0 or $this->username === "" or $this->iusername === "rcon" or $this->iusername === "console" or strlen($packet->username) > 16 or strlen($packet->username) < 3) {
$this->close("", "disconnectionScreen.invalidName");
return;
}
if (strlen($packet->skin) < 64 * 32 * 4) {
$this->close("", "disconnectionScreen.invalidSkin", false);
return;
}
$this->setSkin($packet->skin, $packet->slim);
$this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason"));
if ($ev->isCancelled()) {
$this->close("", $ev->getKickMessage());
return;
}
if (!$this->server->isWhitelisted(strtolower($this->getName()))) {
$this->close($this->getLeaveMessage(), "Server is white-listed");
return;
} elseif ($this->server->getNameBans()->isBanned(strtolower($this->getName())) or $this->server->getIPBans()->isBanned($this->getAddress())) {
$this->close($this->getLeaveMessage(), "You are banned");
return;
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_USERS)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_USERS, $this);
}
if ($this->hasPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE)) {
$this->server->getPluginManager()->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this);
}
foreach ($this->server->getOnlinePlayers() as $p) {
if ($p !== $this and strtolower($p->getName()) === strtolower($this->getName())) {
if ($p->kick("logged in from another location") === false) {
$this->close($this->getLeaveMessage(), "Logged in from another location");
return;
} else {
return;
}
}
}
$nbt = $this->server->getOfflinePlayerData($this->username);
if (!isset($nbt->NameTag)) {
$nbt->NameTag = new String("NameTag", $this->username);
} else {
$nbt["NameTag"] = $this->username;
}
$this->gamemode = $nbt["playerGameType"] & 0x3;
if ($this->server->getForceGamemode()) {
$this->gamemode = $this->server->getGamemode();
$nbt->playerGameType = new Int("playerGameType", $this->gamemode);
//.........这里部分代码省略.........
示例10: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet::NETWORK_ID === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
$timings->stopTiming();
return;
}
switch ($packet::NETWORK_ID) {
case ProtocolInfo::ITEM_FRAME_DROP_ITEM_PACKET:
$tile = $this->level->getTile($this->temporalVector->setComponents($packet->x, $packet->y, $packet->z));
if ($tile instanceof ItemFrame) {
$block = $this->level->getBlock($tile);
$this->server->getPluginManager()->callEvent($ev = new BlockBreakEvent($this, $block, $this->getInventory()->getItemInHand(), true));
if (!$ev->isCancelled()) {
$item = $tile->getItem();
$this->server->getPluginManager()->callEvent($ev = new ItemFrameDropItemEvent($this, $block, $tile, $item));
if (!$ev->isCancelled()) {
if ($item->getId() !== Item::AIR) {
if (mt_rand(0, 10) / 10 < $tile->getItemDropChance()) {
$this->level->dropItem($tile, $item);
}
$tile->setItem(Item::get(Item::AIR));
$tile->setItemRotation(0);
}
} else {
$tile->spawnTo($this);
}
} else {
$tile->spawnTo($this);
}
}
break;
case ProtocolInfo::REQUEST_CHUNK_RADIUS_PACKET:
/*if($this->spawned){
$this->viewDistance = $packet->radius ** 2;
}*/
$pk = new ChunkRadiusUpdatedPacket();
$pk->radius = $this->server->chunkRadius != -1 ? $this->server->chunkRadius : $packet->radius;
$this->dataPacket($pk);
break;
case ProtocolInfo::PLAYER_INPUT_PACKET:
break;
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn) {
break;
}
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_SUCCESS;
$this->dataPacket($pk);
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->setNameTag($this->username);
$this->iusername = strtolower($this->username);
$this->protocol = $packet->protocol;
if (count($this->server->getOnlinePlayers()) >= $this->server->getMaxPlayers() and $this->kick("disconnectionScreen.serverFull", false)) {
break;
}
if (!in_array($packet->protocol, ProtocolInfo::ACCEPTED_PROTOCOLS)) {
if ($packet->protocol < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "disconnectionScreen.outdatedClient";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->directDataPacket($pk);
} else {
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->directDataPacket($pk);
}
$this->close("", $message, false);
break;
}
$this->randomClientId = $packet->clientId;
$this->loginData = ["clientId" => $packet->clientId, "loginData" => null];
$this->uuid = UUID::fromString($packet->clientUUID);
$this->rawUUID = $this->uuid->toBinary();
$valid = true;
$len = strlen($packet->username);
if ($len > 16 or $len < 3) {
$valid = false;
}
//.........这里部分代码省略.........
示例11: onDataPacketReceive
public function onDataPacketReceive(DataPacketReceiveEvent $event)
{
$pk = $event->getPacket();
$p = $event->getPlayer();
if ($pk->pid() == ProtocolInfo::ENTITY_EVENT_PACKET && $pk->event == 9) {
$i = $p->getInventory()->getItemInHand();
$items = [Item::APPLE => 4, Item::MUSHROOM_STEW => 10, Item::BEETROOT_SOUP => 10, Item::BREAD => 5, Item::RAW_PORKCHOP => 3, Item::COOKED_PORKCHOP => 8, Item::RAW_BEEF => 3, Item::STEAK => 8, Item::COOKED_CHICKEN => 6, Item::RAW_CHICKEN => 2, Item::MELON_SLICE => 2, Item::GOLDEN_APPLE => 10, Item::PUMPKIN_PIE => 8, Item::CARROT => 4, Item::POTATO => 1, Item::BAKED_POTATO => 6];
if ($p->getHealth() >= 20 && $p->getHealth() < $p->getMaxHealth() && isset($items[$i->getID()])) {
$this->getServer()->getPluginManager()->callEvent($ev = new PlayerItemConsumeEvent($p, $i));
if ($ev->isCancelled()) {
$p->getInventory()->sendContents($p);
break;
}
$pk = new EntityEventPacket();
$pk->eid = 0;
$pk->event = 9;
$p->dataPacket($pk);
$pk->eid = $p->getId();
$this->getServer()->broadcastPacket($p->getViewers(), $pk);
$amount = $items[$i->getID()];
$this->getServer()->getPluginManager()->callEvent($ev = new EntityRegainHealthEvent($p, $amount, EntityRegainHealthEvent::CAUSE_EATING));
if (!$ev->isCancelled()) {
$p->heal($ev->getAmount(), $ev);
}
--$i->count;
$p->getInventory()->setItemInHand($i, $p);
if ($i->getID() === Item::MUSHROOM_STEW or $i->getID() === Item::BEETROOT_SOUP) {
$p->getInventory()->addItem(Item::get(Item::BOWL, 0, 1), $p);
}
}
} elseif ($pk->pid() == ProtocolInfo::RESPAWN_PACKET && $p->spawned && $p->dead) {
$p->craftingType = 0;
$this->getServer()->getPluginManager()->callEvent($ev = new PlayerRespawnEvent($p, $p->getSpawn()));
$p->teleport($ev->getRespawnPosition());
$p->fireTicks = 0;
$p->airTicks = 300;
$p->deadTicks = 0;
$p->noDamageTicks = 0;
$p->dead = false;
$p->setHealth($p->getMaxHealth());
$p->sendMetadata($p->getViewers());
$p->sendMetadata($p);
$p->sendSettings();
$p->getInventory()->sendContents($p);
$p->getInventory()->sendArmorContents($p);
$p->blocked = false;
$p->spawnToAll();
$p->scheduleUpdate();
$event->setCancelled();
}
}
示例12: handleDataPacket
/**
* Handles a Minecraft packet
* TODO: Separate all of this in handlers
*
* WARNING: Do not use this, it's only for internal use.
* Changes to this function won't be recorded on the version.
*
* @param DataPacket $packet
*/
public function handleDataPacket(DataPacket $packet)
{
if ($this->connected === false) {
return;
}
if ($packet::NETWORK_ID === ProtocolInfo::BATCH_PACKET) {
/** @var BatchPacket $packet */
$this->server->getNetwork()->processBatch($packet, $this);
return;
}
$timings = Timings::getReceiveDataPacketTimings($packet);
$timings->startTiming();
$this->server->getPluginManager()->callEvent($ev = new DataPacketReceiveEvent($this, $packet));
if ($ev->isCancelled()) {
$timings->stopTiming();
return;
}
switch ($packet::NETWORK_ID) {
case ProtocolInfo::LOGIN_PACKET:
if ($this->loggedIn) {
break;
}
$this->username = TextFormat::clean($packet->username);
$this->displayName = $this->username;
$this->setNameTag($this->username);
$this->iusername = strtolower($this->username);
if (count($this->server->getOnlinePlayers()) >= $this->server->getMaxPlayers() and $this->kick("disconnectionScreen.serverFull", false)) {
break;
}
if ($packet->protocol1 !== ProtocolInfo::CURRENT_PROTOCOL) {
if ($packet->protocol1 < ProtocolInfo::CURRENT_PROTOCOL) {
$message = "disconnectionScreen.outdatedClient";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_CLIENT;
$this->directDataPacket($pk);
} else {
$message = "disconnectionScreen.outdatedServer";
$pk = new PlayStatusPacket();
$pk->status = PlayStatusPacket::LOGIN_FAILED_SERVER;
$this->directDataPacket($pk);
}
$this->close("", $message, false);
break;
}
$this->randomClientId = $packet->clientId;
$this->loginData = ["clientId" => $packet->clientId, "loginData" => null];
$this->uuid = $packet->clientUUID;
$this->rawUUID = $this->uuid->toBinary();
$this->clientSecret = $packet->clientSecret;
$valid = true;
$len = strlen($packet->username);
if ($len > 16 or $len < 3) {
$valid = false;
}
for ($i = 0; $i < $len and $valid; ++$i) {
$c = ord($packet->username[$i]);
if ($c >= ord("a") and $c <= ord("z") or $c >= ord("A") and $c <= ord("Z") or $c >= ord("0") and $c <= ord("9") or $c === ord("_")) {
continue;
}
$valid = false;
break;
}
if (!$valid or $this->iusername === "rcon" or $this->iusername === "console") {
$this->close("", "disconnectionScreen.invalidName");
break;
}
if (strlen($packet->skin) !== 64 * 32 * 4 and strlen($packet->skin) !== 64 * 64 * 4) {
$this->close("", "disconnectionScreen.invalidSkin");
break;
}
$this->setSkin($packet->skin, $packet->skinname, $packet->oldclient, $packet->slim, $packet->transparent);
$this->server->getPluginManager()->callEvent($ev = new PlayerPreLoginEvent($this, "Plugin reason"));
if ($ev->isCancelled()) {
$this->close("", $ev->getKickMessage());
break;
}
$this->onPlayerPreLogin();
break;
case ProtocolInfo::MOVE_PLAYER_PACKET:
$newPos = new Vector3($packet->x, $packet->y - $this->getEyeHeight(), $packet->z);
$revert = false;
if (!$this->isAlive() or $this->spawned !== true) {
$revert = true;
$this->forceMovement = new Vector3($this->x, $this->y, $this->z);
}
if ($this->teleportPosition !== null or $this->forceMovement instanceof Vector3 and (($dist = $newPos->distanceSquared($this->forceMovement)) > 0.1 or $revert)) {
$this->sendPosition($this->forceMovement, $packet->yaw, $packet->pitch);
} else {
$packet->yaw %= 360;
$packet->pitch %= 360;
if ($packet->yaw < 0) {
//.........这里部分代码省略.........