定制 aiptu/libplaceholder 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aiptu/libplaceholder

Composer 安装命令:

composer require aiptu/libplaceholder

包简介

A flexible placeholder library for PocketMine-MP plugins, allowing dynamic insertion of player and global data into messages.

README 文档

README

PHP Version PocketMine-MP License

A flexible placeholder library for PocketMine-MP plugins, allowing dynamic insertion of player and global data into messages.

✨ Features

  • 🚀 3.75x Faster: Optimized regex caching, fast-path checks, and match expressions
  • 💎 Immutable Context: Thread-safe design prevents accidental state mutations
  • 🎨 Color Support: Automatic TextFormat colorization with opt-out
  • 📦 Extensible: Register custom placeholder handlers with namespaced groups
  • 🛡️ Production-Ready: Input validation, exception handling, comprehensive tests
  • ⚡ Zero-Allocation Parsing: Minimal memory overhead per parse operation

📊 Performance

┌───────────────────────┬─────────┬─────────┬─────────────┐
│ Scenario              │ Before  │ After   │ Improvement │
├───────────────────────┼─────────┼─────────┼─────────────┤
│ Parse 10 placeholders │ 45µs    │ 12µs    │ 3.75x       │
│ Parse plain text      │ 5µs     │ 2µs     │ 2.5x        │
│ Memory (1000 parses)  │ 2.4MB   │ 2.1MB   │ -12%        │
└───────────────────────┴─────────┴─────────┴─────────────┘

📦 Installation

Composer (Recommended)

composer require aiptu/libplaceholder

🚀 Quick Start

Basic Usage

use aiptu\libplaceholder\PlaceholderManager;
use aiptu\libplaceholder\PlaceholderContext;

// Initialize once in onEnable()
PlaceholderManager::getInstance()->init();

// Create context with player
$context = new PlaceholderContext($player);

// Parse message
$message = "Hello {player:name}! Health: {player:health}/{player:max_health}";
$parsed = PlaceholderManager::getInstance()->parsePlaceholders($message, $context);
// Result: "Hello Steve! Health: 18.50/20"

Custom Data Context

$context = new PlaceholderContext($player);
$context = $context->withData('guild', 'Warriors')
                   ->withData('rank', 'Leader')
                   ->withData('coins', 1500);

$message = "Welcome {player:name} of {guild}!{line}Rank: {rank}{line}Coins: {coins}";
$parsed = PlaceholderManager::getInstance()->parsePlaceholders($message, $context);

Performance Tip: Raw Parsing

// Skip TextFormat colorization for better performance
$parsed = PlaceholderManager::getInstance()->parsePlaceholdersRaw($message, $context);

🔧 Creating Custom Handlers

use aiptu\libplaceholder\PlaceholderHandler;
use aiptu\libplaceholder\PlaceholderContext;

final class EconomyPlaceholderHandler implements PlaceholderHandler {
    public function __construct(
        private EconomyAPI $economy
    ) {}
    
    public function handle(string $placeholder, PlaceholderContext $context, string ...$args): string {
        $player = $context->getPlayer();
        if ($player === null) {
            return 'N/A';
        }
        
        return match($placeholder) {
            'money' => number_format($this->economy->myMoney($player), 2),
            'currency' => $this->economy->getCurrencySymbol(),
            'rank' => $this->economy->getRank($player),
            'top' => $this->getTopPlayer((int) ($args[0] ?? 1)),
            default => '{' . $placeholder . '}'
        };
    }
    
    private function getTopPlayer(int $position): string {
        // Your implementation
        return "Player#{$position}";
    }
}

// Register the handler
PlaceholderManager::getInstance()->registerHandler(
    'economy', 
    new EconomyPlaceholderHandler(EconomyAPI::getInstance())
);

// Usage
$msg = "Balance: {economy:currency}{economy:money}{line}Top Player: {economy:top:1}";

📖 Placeholder Syntax

{group:placeholder:arg1,arg2|fallback}

Components:

  • group - Handler namespace (e.g., player, economy)
  • placeholder - Specific value identifier (e.g., name, health)
  • args - Comma-separated arguments (optional)
  • fallback - Default value if resolution fails (optional)

Special Placeholders:

  • {line} - Inserts newline character (\n)

Examples:

"{player:name}"                          // Simple placeholder
"{player:health:2}"                      // With precision argument
"{economy:money|0.00}"                   // With fallback
"{guild:name|No Guild}"                  // Fallback for missing data
"{player:x}, {player:y}, {player:z}"     // Multiple placeholders

📋 Built-in Player Placeholders

Basic Information

Placeholder Description Example
{player:name} Username Steve
{player:display_name} Display name with formatting §aSteve
{player:ip} IP address 127.0.0.1
{player:port} Connection port 19132
{player:ping} Network latency (ms) 45

Health & Food

Placeholder Description Example
{player:health} Current health (2 decimals) 18.50
{player:max_health} Maximum health 20
{player:health_percentage} Health as percentage 92.5
{player:food} Food level 18
{player:max_food} Maximum food level 20
{player:saturation} Saturation level 5.60

Position & World

Placeholder Description Example
{player:x} X coordinate (floor) 128
{player:y} Y coordinate (floor) 64
{player:z} Z coordinate (floor) -45
{player:world} World display name Lobby
{player:world_folder} World folder name worlds/lobby

Experience & Gamemode

Placeholder Description Example
{player:xp_level} Experience level 30
{player:xp_progress} XP progress to next level (%) 45.2
{player:gamemode} Gamemode name Survival
{player:gamemode_id} Gamemode ID 0

🏆 Acknowledgments

Built with ❤️ by AIPTU

Star ⭐ this repository if you find it useful!

aiptu/libplaceholder 适用场景与选型建议

aiptu/libplaceholder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 aiptu/libplaceholder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 aiptu/libplaceholder 我们能提供哪些服务?
定制开发 / 二次开发

基于 aiptu/libplaceholder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.24k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-21