thedudeguy / PHP-Minecraft-Rcon

Simple Rcon class for php.
MIT License
155 stars 54 forks source link

Processing messages #1

Closed Subnero closed 8 years ago

Subnero commented 10 years ago

Adding this code will make it send messages :8ball:

public function send_message($message,$target) { if (!$this->is_connected()) {return false;} return $this->send_command($this->parse($message,$target));}

private function parse($str,$target) { $ar=Array();$k=0;$str=str_replace(Array("\r","\n"),"",$str); foreach(explode("§",$str) as $code) {$c=$this->parsecolor($code);if($c!=NULL) {$ar[$k]["text"]=str_replace("Â","",substr($code,1));$ar[$k]["color"]=$c;$k++;}}return "tellraw ".$target." ".json_encode(Array("text"=>"","extra"=>$ar));}

private function parsecolor($str) { if($str=="") {return "";} $cl=ord(substr($str,0,1)); $co2=Array("black","dark_blue","dark_green","dark_aqua","dark_red","dark_purple","gold","gray","dark_gray","blue","green","aqua","red","light_purple","yellow","white"); if ($cl>=48 && $cl<=57) {return $co2[$cl-48];}if ($cl>=97 && $cl<=102) {return $co2[10+$cl-97];}if ($cl>=65 && $cl<=70) {return $co2[10+$cl-65];}if ($cl==ord("r")) {return "gray";}return "err";}

thedudeguy commented 8 years ago

Thanks. I'd prefer to keep this library as a general purpose utility, so that other can indeed use it exactly like you have: building their own features and protocols on top of it.