Improved Chat API

This commit is contained in:
Shoghi Cervantes Pueyo
2013-01-12 12:25:27 +01:00
parent b398b9daa2
commit 02950474af
6 changed files with 104 additions and 23 deletions

View File

@@ -25,7 +25,7 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
require_once("src/common/dependencies.php"); require_once(dirname(__FILE__)."/src/common/dependencies.php");
require_once("classes/PocketMinecraftServer.class.php"); require_once("classes/PocketMinecraftServer.class.php");
require_once("API/ServerAPI.php"); require_once("API/ServerAPI.php");

View File

@@ -35,8 +35,17 @@ class ChatAPI{
} }
public function send($a, $b){//a == name of owner. b == message public function broadcast($message){
$this->server->chat($a, $b); $this->send(false, $message);
return true; }
public function send($owner, $text, $whitelist = false, $blacklist = false){
$message = "";
if($owner !== false){
$message = "<".$owner."> ";
}
$message .= $text;
console("[CHAT] ".$message);
$this->server->handle("server.chat", new Container($message, $whitelist, $blacklist));
} }
} }

View File

@@ -147,7 +147,7 @@ class ConsoleAPI{
console("[INFO] Usage: /say <message>"); console("[INFO] Usage: /say <message>");
break; break;
} }
$this->server->chat(false, $s); $this->server->api->chat->broadcast($s);
break; break;
case "whitelist": case "whitelist":
$p = strtolower(array_shift($params)); $p = strtolower(array_shift($params));

View File

@@ -98,7 +98,7 @@ class Player{
foreach($this->evid as $ev){ foreach($this->evid as $ev){
$this->server->deleteEvent($ev); $this->server->deleteEvent($ev);
} }
$this->eventHandler("You have been kicked. Reason: ".$reason, "server.chat"); $this->eventHandler(new Container("You have been kicked. Reason: ".$reason), "server.chat");
$this->dataPacket(MC_LOGIN_STATUS, array( $this->dataPacket(MC_LOGIN_STATUS, array(
"status" => 1, "status" => 1,
)); ));
@@ -106,7 +106,7 @@ class Player{
$this->connected = false; $this->connected = false;
if($msg === true){ if($msg === true){
$this->server->api->dhandle("server.chat", $this->username." left the game"); $this->server->api->chat->broadcast($this->username." left the game");
} }
console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason); console("[INFO] Session with ".$this->ip.":".$this->port." Client ID ".$this->clientID." closed due to ".$reason);
$this->server->api->player->remove($this->CID); $this->server->api->player->remove($this->CID);
@@ -166,8 +166,17 @@ class Player{
)); ));
break; break;
case "server.chat": case "server.chat":
if(($data instanceof Container) === true){
if(!$data->check($this->username)){
return;
}else{
$message = $data->get();
}
}else{
$message = (string) $data;
}
$this->dataPacket(MC_CHAT, array( $this->dataPacket(MC_CHAT, array(
"message" => str_replace("@username", $this->username, $data), "message" => str_replace("@username", $this->username, $message),
)); ));
break; break;
} }

View File

@@ -94,15 +94,13 @@ class PocketMinecraftServer{
} }
public function loadEvents(){ public function loadEvents(){
$this->event("server.chat", array($this, "eventHandler"));
$this->event("player.new", array($this, "eventHandler")); $this->event("player.new", array($this, "eventHandler"));
$this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->api->dhandle("server.time.change", $this->time);'); $this->action(500000, '$this->time += (int) ($this->timePerSecond / 2);$this->api->dhandle("server.time.change", $this->time);');
$this->action(5000000, 'if($this->difficulty < 2){$this->api->dhandle("server.regeneration", 1);}'); $this->action(5000000, 'if($this->difficulty < 2){$this->api->dhandle("server.regeneration", 1);}');
$this->action(1000000 * 60, '$this->reloadConfig();'); $this->action(1000000 * 60, '$this->reloadConfig();');
$this->action(1000000 * 60 * 10, '$this->custom = array();'); $this->action(1000000 * 60 * 10, '$this->custom = array();');
if($this->api !== false){ if($this->api !== false){
$this->action(1000000 * 80, '$cnt = count($this->clients); if($cnt > 1){$this->chat(false, "Online (".$cnt."): ".implode(", ",$this->api->player->online()));}'); $this->action(1000000 * 80, '$cnt = count($this->clients); if($cnt > 1){$this->api->chat->broadcast("Online (".$cnt."): ".implode(", ",$this->api->player->online()));}');
} }
$this->action(1000000 * 120, '$this->debugInfo(true);'); $this->action(1000000 * 120, '$this->debugInfo(true);');
} }
@@ -163,7 +161,7 @@ class PocketMinecraftServer{
public function close($reason = "stop"){ public function close($reason = "stop"){
if($this->stop !== true){ if($this->stop !== true){
$this->chat(false, "Stopping server..."); $this->api->chat->send(false, "Stopping server...");
$this->ticker->stop = true; $this->ticker->stop = true;
$this->save(true); $this->save(true);
$this->stop = true; $this->stop = true;
@@ -172,13 +170,8 @@ class PocketMinecraftServer{
} }
} }
public function chat($owner, $text, $target = true){ public function chat($owner, $text, $target = false){
$message = ""; $this->api->chat->send($owner, $text, $target);
if($owner !== false){
$message = "<".$owner."> ";
}
$message .= $text;
$this->handle("server.chat", $message);
} }
public function setType($type = "normal"){ public function setType($type = "normal"){
@@ -234,9 +227,6 @@ class PocketMinecraftServer{
case "player.new": case "player.new":
console("[DEBUG] Player \"".$data["username"]."\" EID ".$data["eid"]." spawned at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2); console("[DEBUG] Player \"".$data["username"]."\" EID ".$data["eid"]." spawned at X ".$data["x"]." Y ".$data["y"]." Z ".$data["z"], true, true, 2);
break; break;
case "server.chat":
console("[CHAT] $data");
break;
} }
} }
@@ -303,7 +293,7 @@ class PocketMinecraftServer{
} }
} }
console("[DEBUG] Loaded ".count($this->entities)." Entities", true, true, 2); console("[DEBUG] Loaded ".count($this->entities)." Entities", true, true, 2);
$this->action(1000000 * 60 * 15, '$this->chat(false, "Forcing save...");$this->save();$this->chat(false, "Done");'); $this->action(1000000 * 60 * 15, '$this->api->chat->broadcast("Forcing save...");$this->save();');
} }
} }

View File

@@ -0,0 +1,73 @@
<?php
/*
-
/ \
/ \
/ PocketMine \
/ MP \
|\ @shoghicp /|
|. \ / .|
| .. \ / .. |
| .. | .. |
| .. | .. |
\ | /
\ | /
\ | /
\ | /
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.
*/
class Container{
private $payload = "", $whitelist = false, $blacklist = false;
public function __construct($payload = "", $whitelist = false, $blacklist = false){
$this->payload = $payload;
if(is_array($whitelist)){
$this->whitelist = $whitelist;
}
if(is_array($blacklist)){
$this->blacklist = $blacklist;
}
}
public function get(){
return $this->payload;
}
public function check($target){
$w = true;
$b = false;
if($this->whitelist !== false){
$w = false;
if(in_array($target, $this->whitelist, true)){
$w = true;
}
}else{
$w = true;
}
if($this->blacklist !== false){
$b = true;
if(in_array($target, $this->blacklist, true)){
$b = false;
}
}else{
$b = false;
}
if($w === false or $b === true){
return false;
}
return true;
}
public function __toString(){
return $this->payload;
}
}