Skip to content

Commit

Permalink
Merge branch 'minor-next' into feat/server-max-players-setter
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldogedev authored Feb 20, 2024
2 parents bbdc5c7 + 9203416 commit 0322335
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ private function register1to1ItemMappings() : void{
$this->map1to1Item(Ids::MUSIC_DISC_WAIT, Items::RECORD_WAIT());
$this->map1to1Item(Ids::MUSIC_DISC_WARD, Items::RECORD_WARD());
$this->map1to1Item(Ids::MUTTON, Items::RAW_MUTTON());
$this->map1to1Item(Ids::NAME_TAG, Items::NAME_TAG());
$this->map1to1Item(Ids::NAUTILUS_SHELL, Items::NAUTILUS_SHELL());
$this->map1to1Item(Ids::NETHER_STAR, Items::NETHER_STAR());
$this->map1to1Item(Ids::NETHERBRICK, Items::NETHER_BRICK());
Expand Down
8 changes: 8 additions & 0 deletions src/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ public function isNameTagAlwaysVisible() : bool{
return $this->alwaysShowNameTag;
}

/**
* Returns whether players can rename this entity using a name tag.
* Note that plugins can still name entities using setNameTag().
*/
public function canBeRenamed() : bool{
return false;
}

public function setNameTag(string $name) : void{
$this->nameTag = $name;
$this->networkPropertiesDirty = true;
Expand Down
4 changes: 4 additions & 0 deletions src/entity/Living.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ protected function getInitialGravity() : float{ return 0.08; }

abstract public function getName() : string;

public function canBeRenamed() : bool{
return true;
}

protected function initEntity(CompoundTag $nbt) : void{
parent::initEntity($nbt);

Expand Down
3 changes: 2 additions & 1 deletion src/item/ItemTypeIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ private function __construct(){
public const EYE_ARMOR_TRIM_SMITHING_TEMPLATE = 20284;
public const SPIRE_ARMOR_TRIM_SMITHING_TEMPLATE = 20285;
public const PITCHER_POD = 20286;
public const NAME_TAG = 20287;

public const FIRST_UNUSED_ITEM_ID = 20287;
public const FIRST_UNUSED_ITEM_ID = 20288;

private static int $nextDynamicId = self::FIRST_UNUSED_ITEM_ID;

Expand Down
40 changes: 40 additions & 0 deletions src/item/NameTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?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/
*
*
*/

declare(strict_types=1);

namespace pocketmine\item;

use pocketmine\entity\Entity;
use pocketmine\math\Vector3;
use pocketmine\player\Player;

class NameTag extends Item{

public function onInteractEntity(Player $player, Entity $entity, Vector3 $clickVector) : bool{
if($entity->canBeRenamed() && $this->hasCustomName()){
$entity->setNameTag($this->getCustomName());
$this->pop();
return true;
}
return false;
}
}
1 change: 1 addition & 0 deletions src/item/StringToItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@ private static function registerItems(self $result) : void{
$result->register("mutton_raw", fn() => Items::RAW_MUTTON());
$result->register("muttoncooked", fn() => Items::COOKED_MUTTON());
$result->register("muttonraw", fn() => Items::RAW_MUTTON());
$result->register("name_tag", fn() => Items::NAME_TAG());
$result->register("nautilus_shell", fn() => Items::NAUTILUS_SHELL());
$result->register("nether_brick", fn() => Items::NETHER_BRICK());
$result->register("nether_quartz", fn() => Items::NETHER_QUARTZ());
Expand Down
2 changes: 2 additions & 0 deletions src/item/VanillaItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@
* @method static MilkBucket MILK_BUCKET()
* @method static Minecart MINECART()
* @method static MushroomStew MUSHROOM_STEW()
* @method static NameTag NAME_TAG()
* @method static Item NAUTILUS_SHELL()
* @method static Axe NETHERITE_AXE()
* @method static Armor NETHERITE_BOOTS()
Expand Down Expand Up @@ -490,6 +491,7 @@ protected static function setup() : void{
self::register("milk_bucket", new MilkBucket(new IID(Ids::MILK_BUCKET), "Milk Bucket"));
self::register("minecart", new Minecart(new IID(Ids::MINECART), "Minecart"));
self::register("mushroom_stew", new MushroomStew(new IID(Ids::MUSHROOM_STEW), "Mushroom Stew"));
self::register("name_tag", new NameTag(new IID(Ids::NAME_TAG), "Name Tag"));
self::register("nautilus_shell", new Item(new IID(Ids::NAUTILUS_SHELL), "Nautilus Shell"));
self::register("nether_brick", new Item(new IID(Ids::NETHER_BRICK), "Nether Brick"));
self::register("nether_quartz", new Item(new IID(Ids::NETHER_QUARTZ), "Nether Quartz"));
Expand Down
4 changes: 4 additions & 0 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ public function setDisplayName(string $name) : void{
$this->displayName = $ev->getNewName();
}

public function canBeRenamed() : bool{
return false;
}

/**
* Returns the player's locale, e.g. en_US.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ public function registerEvent(string $event, \Closure $handler, int $priority, P

$handlerName = Utils::getNiceClosureName($handler);

$reflect = new \ReflectionFunction($handler);
if($reflect->isGenerator()){
throw new PluginException("Generator function $handlerName cannot be used as an event handler");
}

if(!$plugin->isEnabled()){
throw new PluginException("Plugin attempted to register event handler " . $handlerName . "() to event " . $event . " while not enabled");
}
Expand Down

0 comments on commit 0322335

Please sign in to comment.