1: <?php
2:
3: namespace aliuly\common;
4:
5: use pocketmine\Player;
6:
7: use aliuly\common\Session;
8: use aliuly\common\MPMU;
9:
10: use pocketmine\plugin\PluginBase;
11: use pocketmine\event\player\PlayerChatEvent;
12:
13:
14: 15: 16: 17: 18: 19: 20:
21: class ChatSession extends Session {
22: protected $apis;
23: protected $chat;
24: 25: 26:
27: public function __construct(PluginBase $owner) {
28: $bag = $owner->getServer()->getPluginManager()->getPlugin("GrabBag");
29: $this->apis = [ null, null ];
30: if ($bag && $bag->isEnabled() && MPMU::apiCheck($bag->getDescription()->getVersion(),"2.3")) {
31: if ($bag->api->getFeature("chat-utils")) $this->apis[0] = $bag->api;
32: if ($bag->api->getFeature("mute-unmute")) $this->apis[1] = $bag->api;
33: return;
34: }
35: parent::__construct($owner);
36: $this->chat = true;
37: }
38:
39: 40: 41: 42:
43: public function setGlobalChat($mode) {
44: if ($this->apis[0] !== null) {
45: $this->apis[0]->setGlobalChat($mode);
46: return;
47: }
48: $this->chat = $mode;
49: }
50: 51: 52: 53:
54: public function getGlobalChat() {
55: if ($this->apis[0] !== null) return $this->apis[0]->getGlobalChat();
56: return $this->chat;
57: }
58: 59: 60: 61: 62:
63: public function setPlayerChat(Player $player,$mode) {
64: if ($this->apis[0] !== null) {
65: $this->apis[0]->setPlayerChat($player, $mode);
66: return;
67: }
68: if ($mode) {
69: $this->unsetState("chat",$player);
70: } else {
71: $this->setState("chat",$player,false);
72: }
73: }
74: 75: 76: 77: 78:
79: public function getPlayerChat(Player $player) {
80: if ($this->apis[0] !== null) return $this->apis[0]->getPlayerChat($player);
81:
82: return $this->getState("chat",$player,true);
83: }
84: 85: 86: 87: 88:
89: public function setMute(Player $player,$mode) {
90: if ($this->apis[1] !== null) {
91: $this->apis[1]->setMute($player, $mode);
92: return;
93: }
94: if ($mode) {
95: $this->setState("muted",$player,true);
96: } else {
97: $this->unsetState("muted",$player);
98: }
99: }
100: 101: 102: 103: 104:
105: public function getMute(Player $player) {
106: if ($this->apis[1] !== null) return $this->apis[1]->getMute($player);
107: return $this->getState("muted",$player,false);
108: }
109:
110: public function onChat(PlayerChatEvent $ev) {
111:
112: if ($ev->isCancelled()) return;
113: $p = $ev->getPlayer();
114:
115: if (!$this->chat) {
116: $p->sendMessage(mc::_("Chat has been globally disabled!"));
117: $ev->setCancelled();
118: return;
119: }
120: if ($this->getState("muted",$p,false)) {
121: $p->sendMessage(mc::_("You have been muted!"));
122: $ev->setCancelled();
123: return;
124: }
125: if (!$this->getState("chat",$p,true)) {
126: $p->sendMessage(mc::_("Chat is disabled for you"));
127: $ev->setCancelled();
128: return;
129: }
130: $recvr = [];
131: foreach ($ev->getRecipients() as $to) {
132: if ($this->getState("chat",$to,true)) $recvr[] = $to;
133: }
134: $ev->setRecipients($recvr);
135: }
136: }
137: