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

PM5 Update #419

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
run: mkdir vendor
- name: Download Virion Dependencies
run: |
wget -O vendor/libFormAPI.phar https://poggit.pmmp.io/r/207248/libFormAPI_dev-4.phar
wget -O vendor/Commando.phar https://poggit.pmmp.io/r/162923/Commando_dev-30.phar
wget -O vendor/libPiggyUpdateChecker.phar https://poggit.pmmp.io/r/163495/libPiggyUpdateChecker_dev-1.phar
wget -O vendor/FormsUI.phar https://poggit.pmmp.io/r/209836/FormsUI_dev-20.phar
wget -O vendor/Commando.phar https://poggit.pmmp.io/r/216831/Commando_dev-34.phar
wget -O vendor/libPiggyUpdateChecker.phar https://poggit.pmmp.io/r/211732/libPiggyUpdateChecker_dev-2.phar
- name: Run PHPStan
uses: paroxity/pmmp-phpstan-action@4.21.0
uses: paroxity/pmmp-phpstan-action@5.3.0
with:
phpstan-config: phpstan.neon.dist
7 changes: 3 additions & 4 deletions .poggit.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
--- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/DaPigGuy/PiggyCustomEnchants
branches:
- master
- pmmp-4.0.0
projects:
PiggyCustomEnchants:
path: ""
icon: "resources/PCE_Icon.png"
libs:
- src: jojoe77777/FormAPI/libFormAPI
version: ^2.1.1
- src: Vecnavium/FormsUI/FormsUI
version: ^1.1.3
- src: Paroxity/Commando/Commando
version: ^3.0.0
version: ^3.1.0
- src: DaPigGuy/libPiggyUpdateChecker/libPiggyUpdateChecker
version: ^1.0.0
lint: false
Expand Down
6 changes: 2 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ parameters:
bootstrapFiles:
- phar:///pocketmine/PocketMine-MP.phar/vendor/autoload.php
scanDirectories:
- phar:///source/vendor/libFormAPI.phar/src/
- phar:///source/vendor/FormsUI.phar/src/
- phar:///source/vendor/Commando.phar/src/
- phar:///source/vendor/libPiggyUpdateChecker.phar/src/
excludePaths:
analyse:
- source/vendor
checkMissingIterableValueType: false
ignoreErrors:
- "#Strict comparison using === between pocketmine\\\\inventory\\\\PlayerInventory and null will always evaluate to false.#"
checkMissingIterableValueType: false
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PiggyCustomEnchants
main: DaPigGuy\PiggyCustomEnchants\PiggyCustomEnchants
version: 3.0.12
api: 4.21.0
version: 3.1.0
api: 5.3.0
load: POSTWORLD
author: DaPigGuy
website: "https://github.com/DaPigGuy/PiggyCustomEnchants/"
Expand Down
20 changes: 12 additions & 8 deletions src/DaPigGuy/PiggyCustomEnchants/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
use DaPigGuy\PiggyCustomEnchants\enchants\tools\DrillerEnchant;
use DaPigGuy\PiggyCustomEnchants\entities\BombardmentTNT;
use DaPigGuy\PiggyCustomEnchants\entities\PiggyTNT;
use DaPigGuy\PiggyCustomEnchants\items\EnchantedBook;
use DaPigGuy\PiggyCustomEnchants\utils\ProjectileTracker;
use DaPigGuy\PiggyCustomEnchants\utils\Utils;
use pocketmine\block\BlockLegacyIds;
use pocketmine\block\BlockTypeIds;
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\entity\EntityBlockChangeEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
Expand All @@ -40,7 +41,7 @@
use pocketmine\inventory\transaction\action\SlotChangeAction;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\Item;
use pocketmine\item\ItemIds;
use pocketmine\item\ItemTypeIds;
use pocketmine\item\VanillaItems;
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
Expand Down Expand Up @@ -208,8 +209,8 @@ public function onJoin(PlayerJoinEvent $event): void
$holder = $inventory->getHolder();
if ($holder instanceof Player) {
if (!$oldItem->equals(($newItem = $inventory->getItem($slot)), !$inventory instanceof ArmorInventory)) {
if ($newItem->getId() === ItemIds::AIR || $inventory instanceof ArmorInventory) foreach ($oldItem->getEnchantments() as $oldEnchantment) ToggleableEnchantment::attemptToggle($holder, $oldItem, $oldEnchantment, $inventory, $slot, false);
if ($oldItem->getId() === ItemIds::AIR || $inventory instanceof ArmorInventory) foreach ($newItem->getEnchantments() as $newEnchantment) ToggleableEnchantment::attemptToggle($holder, $newItem, $newEnchantment, $inventory, $slot);
if ($newItem->getStateId() === BlockTypeIds::AIR || $inventory instanceof ArmorInventory) foreach ($oldItem->getEnchantments() as $oldEnchantment) ToggleableEnchantment::attemptToggle($holder, $oldItem, $oldEnchantment, $inventory, $slot, false);
if ($oldItem->getStateId() === BlockTypeIds::AIR || $inventory instanceof ArmorInventory) foreach ($newItem->getEnchantments() as $newEnchantment) ToggleableEnchantment::attemptToggle($holder, $newItem, $newEnchantment, $inventory, $slot);
Aericio marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand All @@ -235,7 +236,7 @@ public function onMove(PlayerMoveEvent $event): void
{
$player = $event->getPlayer();
if (!Utils::shouldTakeFallDamage($player)) {
if ($player->getWorld()->getBlock($player->getPosition()->floor()->subtract(0, 1, 0))->getId() !== BlockLegacyIds::AIR && Utils::getNoFallDamageDuration($player) <= 0) {
if ($player->getWorld()->getBlock($player->getPosition()->floor()->subtract(0, 1, 0))->getTypeId() !== BlockTypeIds::AIR && Utils::getNoFallDamageDuration($player) <= 0) {
Utils::setShouldTakeFallDamage($player, true);
} else {
Utils::increaseNoFallDamageDuration($player);
Expand Down Expand Up @@ -302,7 +303,10 @@ public function onTransaction(InventoryTransactionEvent $event): void
$actions = array_values($transaction->getActions());
if (count($actions) === 2) {
foreach ($actions as $i => $action) {
if ($action instanceof SlotChangeAction && ($otherAction = $actions[($i + 1) % 2]) instanceof SlotChangeAction && ($itemClickedWith = $action->getTargetItem())->getId() === ItemIds::ENCHANTED_BOOK && ($itemClicked = $action->getSourceItem())->getId() !== ItemIds::AIR && ($itemClicked->getId() !== ItemIds::ENCHANTED_BOOK || count($itemClicked->getEnchantments()) >= count($itemClickedWith->getEnchantments()))) {
if ($action instanceof SlotChangeAction && ($otherAction = $actions[($i + 1) % 2]) instanceof SlotChangeAction &&
($itemClickedWith = $action->getTargetItem())->getTypeId() === EnchantedBook::ENCHANTED_BOOK()->getTypeId() &&
($itemClicked = $action->getSourceItem())->getTypeId() !== BlockTypeIds::AIR &&
($itemClicked->getTypeId() !== EnchantedBook::ENCHANTED_BOOK()->getTypeId() || count($itemClicked->getEnchantments()) >= count($itemClickedWith->getEnchantments()))) {
if (count($itemClickedWith->getEnchantments()) < 1) return;
$enchantmentSuccessful = false;
foreach ($itemClickedWith->getEnchantments() as $enchantment) {
Expand All @@ -324,8 +328,8 @@ public function onTransaction(InventoryTransactionEvent $event): void
) ||
$itemClicked->getCount() !== 1 ||
$newLevel > $enchantmentType->getMaxLevel() ||
($itemClicked->getId() === ItemIds::ENCHANTED_BOOK && count($itemClicked->getEnchantments()) === 0) ||
$itemClicked->getId() === ItemIds::BOOK
($itemClicked->getTypeId() === EnchantedBook::ENCHANTED_BOOK()->getTypeId() && count($itemClicked->getEnchantments()) === 0) ||
$itemClicked->getTypeId() === ItemTypeIds::BOOK
) continue;
$itemClicked->addEnchantment(new EnchantmentInstance($enchantmentType, $newLevel));
if ($willChange) $enchantmentSuccessful = true;
Expand Down
55 changes: 48 additions & 7 deletions src/DaPigGuy/PiggyCustomEnchants/PiggyCustomEnchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use CortexPE\Commando\BaseCommand;
use CortexPE\Commando\PacketHooker;
use DaPigGuy\libPiggyUpdateChecker\libPiggyUpdateChecker;
use DaPigGuy\PiggyCustomEnchants\blocks\PiggyObsidian;
use DaPigGuy\PiggyCustomEnchants\commands\CustomEnchantsCommand;
use DaPigGuy\PiggyCustomEnchants\enchants\CustomEnchant;
use DaPigGuy\PiggyCustomEnchants\enchants\ToggleableEnchantment;
Expand All @@ -18,33 +17,41 @@
use DaPigGuy\PiggyCustomEnchants\entities\PiggyTNT;
use DaPigGuy\PiggyCustomEnchants\entities\PiggyWitherSkull;
use DaPigGuy\PiggyCustomEnchants\entities\PigProjectile;
use DaPigGuy\PiggyCustomEnchants\items\EnchantedBook;
use DaPigGuy\PiggyCustomEnchants\tasks\CheckDisabledEnchantsTask;
use DaPigGuy\PiggyCustomEnchants\tasks\TickEnchantmentsTask;
use jojoe77777\FormAPI\Form;
use pocketmine\block\BlockFactory;
use pocketmine\block\Block;
use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\color\Color;
use pocketmine\data\bedrock\EffectIdMap;
use pocketmine\data\bedrock\item\ItemTypeNames;
use pocketmine\data\bedrock\item\SavedItemData;
use pocketmine\entity\effect\Effect;
use pocketmine\entity\EntityDataHelper;
use pocketmine\entity\EntityFactory;
use pocketmine\item\Item;
use pocketmine\item\StringToItemParser;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\Config;
use pocketmine\world\format\io\GlobalBlockStateHandlers;
use pocketmine\world\format\io\GlobalItemDataHandlers;
use pocketmine\world\World;
use Vecnavium\FormsUI\Form;

class PiggyCustomEnchants extends PluginBase
{
public static Effect $SLOW_FALL;

/** @var mixed[] */
/** @var array */
private array $enchantmentData;

public function onEnable(): void
{
foreach (
[
"Commando" => BaseCommand::class,
"libformapi" => Form::class,
"FormsUI" => Form::class,
"libPiggyUpdateChecker" => libPiggyUpdateChecker::class
] as $virion => $class
) {
Expand All @@ -65,9 +72,9 @@ public function onEnable(): void

CustomEnchantManager::init($this);

BlockFactory::getInstance()->register(new PiggyObsidian(), true);
self::registerItemsAndBlocks();

//TODO: Use real effect
// TODO: Use real effect
self::$SLOW_FALL = new Effect("%potion.slowFalling", new Color(206, 255, 255));
EffectIdMap::getInstance()->register(27, self::$SLOW_FALL);

Expand Down Expand Up @@ -149,4 +156,38 @@ public function areFormsEnabled(): bool
{
return $this->getConfig()->getNested("forms.enabled", true);
}

private static function registerItemsAndBlocks(): void
{
self::registerItem(ItemTypeNames::ENCHANTED_BOOK, EnchantedBook::ENCHANTED_BOOK(), ["enchanted_book"]);
// self::registerBlock(BlockTypeNames::OBSIDIAN, PiggyObsidian::PIGGY_OBSIDIAN(), ["piggy_obsidian"]);
}

/**
* @param string[] $stringToItemParserNames
*/
private static function registerBlock(string $id, Block $block, array $stringToItemParserNames): void
{
RuntimeBlockStateRegistry::getInstance()->register($block);

GlobalBlockStateHandlers::getDeserializer()->mapSimple($id, fn() => clone $block);
GlobalBlockStateHandlers::getSerializer()->mapSimple($block, $id);

foreach ($stringToItemParserNames as $name) {
StringToItemParser::getInstance()->registerBlock($name, fn() => clone $block);
}
}

/**
* @param string[] $stringToItemParserNames
*/
private static function registerItem(string $id, Item $item, array $stringToItemParserNames): void
{
GlobalItemDataHandlers::getDeserializer()->map($id, fn() => clone $item);
GlobalItemDataHandlers::getSerializer()->map($item, fn() => new SavedItemData($id));

foreach ($stringToItemParserNames as $name) {
StringToItemParser::getInstance()->register($name, fn() => clone $item);
}
}
}
74 changes: 24 additions & 50 deletions src/DaPigGuy/PiggyCustomEnchants/blocks/PiggyObsidian.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,40 @@
use pocketmine\block\Block;
use pocketmine\block\BlockBreakInfo;
use pocketmine\block\BlockIdentifier;
use pocketmine\block\BlockLegacyIds;
use pocketmine\block\VanillaBlocks;
use pocketmine\item\Item;
use pocketmine\player\Player;

class PiggyObsidian extends Block
use pocketmine\block\BlockTypeIds;
use pocketmine\block\BlockTypeInfo;
use pocketmine\utils\CloningRegistryTrait;

/**
* @method static PiggyObsidianBlock PIGGY_OBSIDIAN()
*/
final class PiggyObsidian
{
private int $age = 0;

public function __construct()
{
parent::__construct(new BlockIdentifier(BlockLegacyIds::OBSIDIAN, 15), "Magmawalker Obsidian", BlockBreakInfo::instant());
}

public function onScheduledUpdate(): void
{
if (mt_rand(0, 3) === 0 || $this->countNeighbors() < 4) {
$this->slightlyMelt(true);
} else {
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), mt_rand(20, 40));
}
}
use CloningRegistryTrait;

public function onBreak(Item $item, Player $player = null): bool
private function __construct()
{
$this->getPosition()->getWorld()->setBlock($this->getPosition(), VanillaBlocks::LAVA());
return true;
//NOOP
}

public function getDrops(Item $item): array
protected static function register(string $name, Block $block): void
{
return [];
self::_registryRegister($name, $block);
}

public function countNeighbors(): int
/**
* @return Block[]
* @phpstan-return array<string, Block>
*/
public static function getAll(): array
{
$i = 0;
foreach ($this->getAllSides() as $block) {
if ($block instanceof PiggyObsidian) {
$i++;
if ($i >= 4) return $i;
}
}
return $i;
/** @var Block[] $result */
$result = self::_registryGetAll();
return $result;
}

public function slightlyMelt(bool $meltNeighbors): void
protected static function setup(): void
{
if ($this->age < 3) {
$this->age++;
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), mt_rand(20, 40));
} else {
$this->getPosition()->getWorld()->useBreakOn($this->getPosition());
if ($meltNeighbors) {
foreach ($this->getAllSides() as $block) {
if ($block instanceof PiggyObsidian) {
$block->slightlyMelt(false);
}
}
}
}
self::register("piggy_obsidian", new PiggyObsidianBlock(new BlockIdentifier(BlockTypeIds::newId()), "Magmawalker Obsidian", new BlockTypeInfo(BlockBreakInfo::instant())));
}
}
}
66 changes: 66 additions & 0 deletions src/DaPigGuy/PiggyCustomEnchants/blocks/PiggyObsidianBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace DaPigGuy\PiggyCustomEnchants\blocks;

use pocketmine\block\Opaque;
use pocketmine\block\VanillaBlocks;
use pocketmine\item\Item;
use pocketmine\player\Player;

class PiggyObsidianBlock extends Opaque
{
private int $age = 0;

public function onScheduledUpdate(): void
{
if (mt_rand(0, 3) === 0 || $this->countNeighbors() < 4) {
$this->slightlyMelt(true);
} else {
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), mt_rand(20, 40));
}
}

public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []): bool
{
$world = $this->position->getWorld();
if (($t = $world->getTile($this->position)) !== null) {
$t->onBlockDestroyed();
}
$world->setBlock($this->position, VanillaBlocks::LAVA());
return true;
}

public function getDrops(Item $item): array
{
return [];
}

public function countNeighbors(): int
{
$i = 0;
foreach ($this->getAllSides() as $block) {
if ($block instanceof PiggyObsidianBlock) {
$i++;
if ($i >= 4) return $i;
}
}
return $i;
}

public function slightlyMelt(bool $meltNeighbors): void
{
if ($this->age < 3) {
$this->age++;
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), mt_rand(20, 40));
} else {
$this->getPosition()->getWorld()->useBreakOn($this->getPosition());
if ($meltNeighbors) {
foreach ($this->getAllSides() as $block) {
if ($block instanceof PiggyObsidianBlock) {
$block->slightlyMelt(false);
}
}
}
}
}
}
Loading