Added TextWrapper

This commit is contained in:
Shoghi Cervantes 2014-10-11 00:01:53 +02:00
parent 0dd46c835c
commit 645c00b2f7
4 changed files with 90 additions and 3 deletions

View File

@ -105,6 +105,7 @@ use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile;
use pocketmine\utils\ReversePriorityQueue;
use pocketmine\utils\TextFormat;
use pocketmine\utils\TextWrapper;
/**
* Main class that handles networking, recovery, and packet sending to the server part
@ -2239,7 +2240,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return true;
}
return false;
return false;git
}
/**
@ -2248,6 +2249,9 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
* @param string $message
*/
public function sendMessage($message){
if($this->removeFormat !== false){
$message = TextWrapper::wrap($message);
}
$mes = explode("\n", $message);
foreach($mes as $m){
if($m !== ""){

View File

@ -84,6 +84,7 @@ use pocketmine\utils\Cache;
use pocketmine\utils\Config;
use pocketmine\utils\MainLogger;
use pocketmine\utils\TextFormat;
use pocketmine\utils\TextWrapper;
use pocketmine\utils\Utils;
use pocketmine\utils\VersionString;
@ -1550,6 +1551,7 @@ class Server{
InventoryType::init();
Block::init();
Item::init();
TextWrapper::init();
$this->craftingManager = new CraftingManager();
$this->pluginManager = new PluginManager($this, $this->commandMap);

View File

@ -21,11 +21,10 @@
namespace pocketmine\utils;
/**
* Class used to handle Minecraft chat format, and convert it to other formats like ANSI or HTML
*/
class TextFormat{
abstract class TextFormat{
const BLACK = "§0";
const DARK_BLUE = "§1";
const DARK_GREEN = "§2";

View File

@ -0,0 +1,82 @@
<?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/
*
*
*/
namespace pocketmine\utils;
abstract class TextWrapper{
private static $characterWidths = [
4, 2, 5, 6, 6, 6, 6, 3, 5, 5, 5, 6, 2, 6, 2, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 2, 2, 5, 6, 5, 6,
7, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 4, 6, 4, 6, 6,
6, 6, 6, 6, 6, 5, 6, 6, 2, 6, 5, 3, 6, 6, 6, 6,
6, 6, 6, 4, 6, 6, 6, 6, 6, 6, 5, 2, 5, 7
];
const CHAT_WINDOW_WIDTH = 320;
const CHAT_STRING_LENGTH = 119;
private static $allowedChars = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_abcdefghijklmnopqrstuvwxyz{|}~";
private static $allowedCharsArray = [];
public static function init(){
self::$allowedCharsArray = [];
$len = strlen(self::$allowedChars);
for($i = 0; $i < $len; ++$i){
self::$allowedCharsArray[self::$allowedChars{$i}] = self::$characterWidths[$i];
}
}
public static function wrap($text){
$result = "";
$len = strlen($text);
$lineWidth = 0;
$lineLength = 0;
for($i = 0; $i < $len; ++$i){
$char = $text{$i};
if($char === "\n"){
$result .= "\n";
$lineLength = 0;
$lineWidth = 0;
}elseif(isset(self::$allowedCharsArray[$char])){
$width = self::$allowedCharsArray[$char];
if($lineLength + 1 > self::CHAT_STRING_LENGTH or $lineWidth + $width > self::CHAT_WINDOW_WIDTH){
$result .= "\n";
$lineLength = 0;
$lineWidth = 0;
}
++$lineLength;
$lineWidth += $width;
$result .= $char;
}else{
$result .= "\n";
}
}
return $result;
}
}