Skip to content
Open
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
20 changes: 20 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Configuration for ItemStacker

# 1. Configuration for Stack Radius
stacking_radius: 5.0

# 2. Configuration for Max Stack Size Enforcement
# If set to 0, no global limit is applied (uses item's default max stack size).
max_global_stack_size: 1024

# 3. Custom Tag Display Format
# Define the format for the item stack name tag.
# Available placeholders:
# - {COUNT}: The total number of stacked items.
# - {ITEM_NAME}: The item's default name (e.g., "Stone").
# You can use Minecraft color codes (e.g., §r, §l, §a)
tag_format: "§r§l§7{ITEM_NAME} §r§7x§b{COUNT}"

# 4. Default Name Tag Display (Optional fallback)
# If true, the name tag will still be set even if the stack size is 1.
display_single_stack_tag: false
178 changes: 128 additions & 50 deletions src/david/itemStacker/EventListener.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,128 @@
<?php

declare(strict_types=1);

namespace david\itemStacker;

use pocketmine\event\Listener;
use pocketmine\event\entity\EntitySpawnEvent;

use pocketmine\entity\object\ItemEntity;

class EventListener implements Listener {

public function onEntitySpawn(EntitySpawnEvent $event) : void{
$entity = $event->getEntity();

if (!$entity instanceof ItemEntity) {
return;
}

$position = $entity->getPosition();
$world = $position->getWorld();
$entities = $world->getNearbyEntities($entity->getBoundingBox()->expandedCopy(5, 5, 5));

if (empty($entities)) {
return;
}

$originalItem = $entity->getItem();
$totalItemCount = $originalItem->getCount();

foreach ($entities as $e) {
if ($e instanceof ItemEntity && $entity !== $e) {
$itemE = $e->getItem();
if ($itemE->equals($originalItem)) {
$e->flagForDespawn();
$originalItem->setCount($originalItem->getCount() + $itemE->getCount());
$totalItemCount += $itemE->getCount();
}
}
}

if ($totalItemCount > 1) {
$itemName = $originalItem->getName();
$tag = "§7" . $itemName . " §7x§b" . $totalItemCount;
$entity->setNameTag($tag);
$entity->setNameTagAlwaysVisible(true);
}
}
}
<?php

declare(strict_types=1);

namespace david\itemStacker;

use pocketmine\entity\Entity;
use pocketmine\entity\object\ItemEntity;
use pocketmine\event\entity\EntitySpawnEvent;
use pocketmine\event\Listener;
use pocketmine\math\AxisAlignedBB;
use function count;
use function str_replace;

class EventListener implements Listener {
private Loader $plugin;

public function __construct(Loader $plugin) {
$this->plugin = $plugin;
}

public function onEntitySpawn(EntitySpawnEvent $event) : void {
$entity = $event->getEntity();

if (!$entity instanceof ItemEntity) {
return;
}

$originalItem = $entity->getItem();

$world = $entity->getWorld();
$position = $entity->getPosition();

$stackRadius = $this->plugin->getStackRadius();
$maxGlobalStackSize = $this->plugin->getMaxGlobalStackSize();
$displaySingleStackTag = $this->plugin->displaySingleStackTag();

$halfRadius = $stackRadius / 2;

$aabb = new AxisAlignedBB(
$position->x - $halfRadius,
$position->y - $halfRadius,
$position->z - $halfRadius,
$position->x + $halfRadius,
$position->y + $halfRadius,
$position->z + $halfRadius
);

/** @var array<Entity> $entities */
$entities = $world->getNearbyEntities($aabb, $entity);

if (count($entities) === 0) {
$finalCount = $originalItem->getCount();
if ($finalCount > 0 && $displaySingleStackTag) {
$this->updateNameTag($entity, $originalItem->getName(), $finalCount);
}

return;
}

$tempStack = clone $originalItem;
$itemsToSpawn = [];
$entitiesToDespawn = [];

$maxItemStack = $maxGlobalStackSize > 0 ? $maxGlobalStackSize : $originalItem->getMaxStackSize();

foreach ($entities as $nearbyEntity) {
if ($nearbyEntity instanceof ItemEntity) {
$nearbyItem = $nearbyEntity->getItem();

if ($nearbyItem->equals($originalItem)) {
$nearbyCount = $nearbyItem->getCount();
$entitiesToDespawn[] = $nearbyEntity;

$currentCount = $tempStack->getCount();

if ($currentCount + $nearbyCount > $maxItemStack) {
$canAdd = $maxItemStack - $currentCount;
$remainder = $nearbyCount - $canAdd;

$tempStack->setCount($maxItemStack);

$spillItem = clone $originalItem;
$spillItem->setCount($remainder);
$itemsToSpawn[] = $spillItem;

break;
}

$tempStack->setCount($currentCount + $nearbyCount);
}
}
}

foreach ($entitiesToDespawn as $nearbyEntity) {
$nearbyEntity->flagForDespawn();
}

$entity->setStackSize($tempStack->getCount());

$entity->setDespawnDelay(ItemEntity::DEFAULT_DESPAWN_DELAY);

$finalCount = $tempStack->getCount();

if ($finalCount > 1 || $displaySingleStackTag) {
$this->updateNameTag($entity, $tempStack->getName(), $finalCount);
} else {
$entity->setNameTag('');
}

foreach ($itemsToSpawn as $spillItem) {
$world->dropItem($position, $spillItem);
}
}

private function updateNameTag(ItemEntity $entity, string $itemName, int $count) : void {
$tagFormat = $this->plugin->getTagFormat();

$tag = str_replace(
['{COUNT}', '{ITEM_NAME}'],
[(string) $count, $itemName],
$tagFormat
);

$entity->setNameTag($tag);
$entity->setNameTagAlwaysVisible(true);
}
}
104 changes: 86 additions & 18 deletions src/david/itemStacker/Loader.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,86 @@
<?php

declare(strict_types=1);

namespace david\itemStacker;

use pocketmine\plugin\PluginBase;

use DaPigGuy\libPiggyUpdateChecker\libPiggyUpdateChecker;

class Loader extends PluginBase {

protected function onEnable() : void{
$this->getServer()->getPluginManager()->registerEvents(new EventListener(), $this);

libPiggyUpdateChecker::init($this);
}
}
<?php

declare(strict_types=1);

namespace david\itemStacker;

use DaPigGuy\libPiggyUpdateChecker\libPiggyUpdateChecker;
use pocketmine\plugin\PluginBase;
use function is_bool;
use function is_int;
use function is_numeric;
use function is_string;
use function trim;

class Loader extends PluginBase {
private float $stackRadius;
private int $maxGlobalStackSize;
private string $tagFormat;
private bool $displaySingleStackTag;

protected function onEnable() : void {
$this->saveDefaultConfig();
$this->validateConfig($this->getConfig()->getAll());

$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);

libPiggyUpdateChecker::init($this);
}

/**
* Performs strict validation and sets class properties.
*
* @param array<string, mixed> $config
*/
private function validateConfig(array $config) : void {
$logger = $this->getLogger();

$radius = $config['stacking_radius'] ?? 5.0;
if (!is_numeric($radius) || (float) $radius <= 0.0) {
$radius = 5.0;
$logger->warning("Invalid 'stacking_radius' in config.yml. Must be a positive number. Using default: 5.0");
}

$this->stackRadius = (float) $radius;

$maxSize = $config['max_global_stack_size'] ?? 1024;
if (!is_int($maxSize) || $maxSize < 0) {
$maxSize = 1024;
$logger->warning("Invalid 'max_global_stack_size' in config.yml. Must be a non-negative integer. Using default: 1024");
}

$this->maxGlobalStackSize = $maxSize;

$format = $config['tag_format'] ?? '§r§7{ITEM_NAME} §r§7x§b{COUNT}';
if (!is_string($format) || trim($format) === '') {
$format = '§r§7{ITEM_NAME} §r§7x§b{COUNT}';
$logger->warning("Invalid 'tag_format' in config.yml. Must be a non-empty string. Using default format.");
}

$this->tagFormat = $format;

$displaySingle = $config['display_single_stack_tag'] ?? false;
if (!is_bool($displaySingle)) {
$displaySingle = false;
$logger->warning("Invalid 'display_single_stack_tag' in config.yml. Must be a boolean (true/false). Using default: false");
}

$this->displaySingleStackTag = $displaySingle;
}

public function getStackRadius() : float {
return $this->stackRadius;
}

public function getMaxGlobalStackSize() : int {
return $this->maxGlobalStackSize;
}

public function getTagFormat() : string {
return $this->tagFormat;
}

public function displaySingleStackTag() : bool {
return $this->displaySingleStackTag;
}
}