Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported Slapper to latest PM4 #6

Merged
merged 9 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified .travis.yml
100755 → 100644
Empty file.
55 changes: 28 additions & 27 deletions plugin.yml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,35 @@ commands:
slapper:
description: Create a Slapper NPC, or edit an existing one!
usage: "/slapper [args]..."
permission: slapper.command

permission: slapper.create;slapper.edit;slapper.remove;slapper.id;slapper.help
rca:
description: Execute a command as someone else!
usage: "/rca <player> <command>"
permission: slapper.rca
nothing:
description: Do nothing!
usage: "/nothing"
permission: slapper.nothing

permissions:
slapper:
description: Allow using all Slapper commands
slapper.create:
description: Allow using command /slapper create
default: op
slapper.edit:
description: Allow editing Slapper entity data with /slapper edit
default: op
slapper.nothing:
description: Allow doing nothing
default: op
slapper.rca:
description: Allow running commands as other players
default: op
slapper.remove:
description: Allow removing Slapper entities with /slapper remove [id]
default: op
slapper.id:
description: Allow viewing entity ID
default: op
slapper.command:
description: Allow using /slapper
slapper.help:
description: Allow viewing Slapper help page
default: op
slapper.create:
description: Allow using command /slapper create
default: op
slapper.edit:
description: Allow editing Slapper entity data with /slapper edit
default: op
slapper.nothing:
description: Allow doing nothing
default: op
slapper.rca:
description: Allow running commands as other players
default: op
slapper.remove:
description: Allow removing Slapper entities with /slapper remove [id]
default: op
slapper.id:
description: Allow viewing entity ID
default: op
slapper.help:
description: Allow viewing Slapper help page
default: op
256 changes: 137 additions & 119 deletions src/slapper/Main.php

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions src/slapper/SlapperCommandSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace slapper;

use pocketmine\command\CommandSender;
use pocketmine\lang\Language;
use pocketmine\lang\Translatable;
use pocketmine\permission\DefaultPermissions;
use pocketmine\permission\PermissibleBase;
use pocketmine\permission\PermissibleDelegateTrait;
use pocketmine\Server;

class SlapperCommandSender implements CommandSender{
use PermissibleDelegateTrait;

protected $lineHeight = null;

private Server $server;
private Language $language;

public function __construct(private Main $plugin){
$this->server = $plugin->getServer();
$this->language = $this->server->getLanguage();
$this->perm = new PermissibleBase([DefaultPermissions::ROOT_OPERATOR => true]);
}

public function getServer() : Server{
return $this->server;
}

public function getLanguage() : Language{
return $this->language;
}

public function sendMessage(Translatable|string $message) : void{
if($message instanceof Translatable){
$message = $this->language->translate($message);
}

$logger = $this->plugin->getLogger();
foreach(explode("\n", trim($message)) as $line){
$logger->info($line);
}
}

public function getName() : string{
return $this->plugin->getName();
}

public function getScreenLineHeight() : int{
return $this->lineHeight ?? PHP_INT_MAX;
}

public function setScreenLineHeight(?int $height) : void{
if($height !== null and $height < 1){
throw new \InvalidArgumentException("Line height must be at least 1");
}
$this->lineHeight = $height;
}
}
61 changes: 8 additions & 53 deletions src/slapper/SlapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,81 +4,36 @@

namespace slapper;

use pocketmine\entity\DataPropertyManager;
use pocketmine\entity\Entity;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\network\mcpe\protocol\SetActorDataPacket as SetEntityDataPacket;
use pocketmine\Player;
use pocketmine\player\Player;

/**
* Trait containing methods used in various Slappers.
*/
trait SlapperTrait {
/** @var CompoundTag */
public $namedtag;

/**
* @return DataPropertyManager
*/
abstract public function getDataPropertyManager(): DataPropertyManager;
protected bool $networkPropertiesDirty = false;

/**
* @return string
*/
abstract public function getNameTag(): string;

abstract public function sendNameTag(Player $player): void;

abstract public function setGenericFlag(int $flag, bool $value = true): void;

public function prepareMetadata(): void {
$this->setGenericFlag(Entity::DATA_FLAG_IMMOBILE, true);
if (!$this->namedtag->hasTag("Scale", FloatTag::class)) {
$this->namedtag->setFloat("Scale", 1.0, true);
}
$this->getDataPropertyManager()->setFloat(Entity::DATA_SCALE, $this->namedtag->getFloat("Scale"));
}

public function tryChangeMovement(): void {

//NOOP
}

public function sendData($playerList, array $data = null): void {
if(!is_array($playerList)){
$playerList = [$playerList];
}

foreach($playerList as $p){
$playerData = $data ?? $this->getDataPropertyManager()->getAll();
unset($playerData[self::DATA_NAMETAG]);
$pk = new SetEntityDataPacket();
$pk->entityRuntimeId = $this->getId();
$pk->metadata = $playerData;
$p->dataPacket($pk);

$this->sendNameTag($p);
}
public function prepareMetadata(): void {
$this->networkPropertiesDirty = true;
}

public function saveSlapperNbt(): void {
$visibility = 0;
if ($this->isNameTagVisible()) {
$visibility = 1;
if ($this->isNameTagAlwaysVisible()) {
$visibility = 2;
}
}
$scale = $this->getDataPropertyManager()->getFloat(Entity::DATA_SCALE);
$this->namedtag->setInt("NameVisibility", $visibility, true);
$this->namedtag->setFloat("Scale", $scale, true);
public function sendData(?array $playerList, ?array $data = null): void {
//NOOP
}

public function getDisplayName(Player $player): string {
$vars = [
"{name}" => $player->getName(),
"{display_name}" => $player->getName(),
"{display_name}" => $player->getDisplayName(),
"{nametag}" => $player->getNameTag()
];
return str_replace(array_keys($vars), array_values($vars), $this->getNameTag());
Expand Down
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperBat.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperBat extends SlapperEntity {

const TYPE_ID = 19;
const TYPE_ID = EntityLegacyIds::BAT;
const HEIGHT = 0.9;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperBlaze.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperBlaze extends SlapperEntity {

const TYPE_ID = 43;
const TYPE_ID = EntityLegacyIds::BLAZE;
const HEIGHT = 1.8;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperCaveSpider.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperCaveSpider extends SlapperEntity {

const TYPE_ID = 40;
const TYPE_ID = EntityLegacyIds::CAVE_SPIDER;
const HEIGHT = 0.5;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperChicken.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperChicken extends SlapperEntity {

const TYPE_ID = 10;
const TYPE_ID = EntityLegacyIds::CHICKEN;
const HEIGHT = 0.7;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperCow.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperCow extends SlapperEntity {

const TYPE_ID = 11;
const TYPE_ID = EntityLegacyIds::COW;
const HEIGHT = 1.4;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperCreeper.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperCreeper extends SlapperEntity {

const TYPE_ID = 33;
const TYPE_ID = EntityLegacyIds::COW;
const HEIGHT = 1.7;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperDonkey.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperDonkey extends SlapperEntity {

const TYPE_ID = 24;
const TYPE_ID = EntityLegacyIds::DONKEY;
const HEIGHT = 1.6;

}
13 changes: 8 additions & 5 deletions src/slapper/entities/SlapperElderGuardian.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataCollection;
use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataFlags;

class SlapperElderGuardian extends SlapperEntity {

const TYPE_ID = 50;
const TYPE_ID = EntityLegacyIds::ELDER_GUARDIAN;
const HEIGHT = 1.9975;

public function prepareMetadata(): void {
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_ELDER, true);
parent::prepareMetadata();
protected function syncNetworkData(EntityMetadataCollection $properties) : void{
parent::syncNetworkData($properties);
$properties->setGenericFlag(EntityMetadataFlags::ELDER, true);
}

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperEnderman.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperEnderman extends SlapperEntity {

const TYPE_ID = 38;
const TYPE_ID = EntityLegacyIds::ENDERMAN;
const HEIGHT = 2.9;

}
4 changes: 3 additions & 1 deletion src/slapper/entities/SlapperEndermite.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace slapper\entities;

use pocketmine\data\bedrock\EntityLegacyIds;

class SlapperEndermite extends SlapperEntity {

const TYPE_ID = 55;
const TYPE_ID = EntityLegacyIds::ENDERMITE;
const HEIGHT = 0.3;

}
Loading