├── .poggit.yml
├── LICENSE
├── README.md
├── icon.png
├── plugin.yml
├── resources
├── channels.yml
└── config.yml
└── src
└── ServerChannels
├── Commands
└── Commands.php
├── EventListener.php
├── Events
├── ServerChannelsChatEvent.php
├── ServerChannelsEvent.php
├── ServerChannelsJoinEvent.php
└── ServerChannelsLeaveEvent.php
└── ServerChannels.php
/.poggit.yml:
--------------------------------------------------------------------------------
1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/EvolSoft/ServerChannels
2 | branches:
3 | - master
4 | projects:
5 | ServerChannels:
6 | path: ""
7 | ...
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014-2015 EvolSoft
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # ServerChannels
4 |
5 | A powerful chat channelling plugin for PocketMine-MP
6 |
7 | [](http://gestyy.com/er3s9W)
8 |
9 | ## Category
10 |
11 | PocketMine-MP plugins
12 |
13 | ## Requirements
14 |
15 | PocketMine-MP API 3.0.0
16 |
17 | ## Overview
18 |
19 | **ServerChannels** is a powerful chat channelling plugin for PocketMine-MP.
20 | You can create highly customizable chat channels on your MC:PE server thanks to this plugin.
21 |
22 | ***Features:***
23 |
24 | - *Custom format:* customize the channel format with colors and tags
25 | - *Hidden/Unhidden channels:* customize channel visibility (hidden channels messages will be seen only by players who joined that channels, unhidden channels messages will be seen by all players)
26 | - *Custom channels authentication:* create whitelisted channels, protect them with a password or leave them accessible by all players
27 |
28 | **EvolSoft Website:** https://www.evolsoft.tk
29 |
30 | ***This Plugin uses the New API. You can't install it on old versions of PocketMine.***
31 |
32 | ## Donate
33 |
34 | Please support the development of this plugin with a small donation by clicking [:dollar: here](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=flavius.c.1999@gmail.com&lc=US&item_name=www.evolsoft.tk&no_note=0&cn=&curency_code=EUR&bn=PP-DonationsBF:btn_donateCC_LG.gif:NonHosted).
35 | Your small donation will help me paying web hosting, domains, buying programs (such as IDEs, debuggers, etc...) and new hardware to improve software development. Thank you :smile:
36 |
37 | ## Documentation
38 |
39 | **Text format (Available on PocketMine console and on MCPE v0.11.0 and later):**
40 |
41 | **Colors:**
42 |
43 | Black ("&0");
44 | Dark Blue ("&1");
45 | Dark Green ("&2");
46 | Dark Aqua ("&3");
47 | Dark Red ("&4");
48 | Dark Purple ("&5");
49 | Gold ("&6");
50 | Gray ("&7");
51 | Dark Gray ("&8");
52 | Blue ("&9");
53 | Green ("&a");
54 | Aqua ("&b");
55 | Red ("&c");
56 | Light Purple ("&d");
57 | Yellow ("&e");
58 | White ("&f");
59 |
60 | **Special:**
61 |
62 | Obfuscated ("&k");
63 | Bold ("&l");
64 | Strikethrough ("&m");
65 | Underline ("&n");
66 | Italic ("&o");
67 | Reset ("&r");
68 |
69 | **Create and configure a channel:**
70 |
71 | *Remember that you must have the "serverchannels.channels.<channel>" permission to join the channel*
72 |
73 | 1. Run the command "/sch create <channel>"
74 | 2. Open "channels.yml" file inside plugin configuration folder and open it
75 | This is a channel config file entry:
76 |
77 | ```yaml
78 | # Channel name
79 | channel_name:
80 | # Channel prefix
81 | prefix: "&7[&bExampleChannel&7]"
82 | # Channel suffix
83 | suffix: ""
84 | # Channel format
85 | # Available Tags:
86 | # - {MESSAGE}: Show message
87 | # - {PLAYER}: Show player name
88 | # - {PREFIX}: Show prefix
89 | # - {SUFFIX}: Show suffix
90 | # - {TIME}: Show current time
91 | # - {WORLD}: Show world name
92 | format: "{PREFIX} &7{PLAYER}: &f{MESSAGE}"
93 | # Channel visiblity (true if you want that channel messages will be seen by players in the channel only, false if you want that channel messages will be seen by all players)
94 | hidden: false
95 | # Channel authentication (0 or "none" = no authentication, 1 or "password" = password authentication, 2 or "whitelist" = whitelisted channel)
96 | auth: "none"
97 | # Channel password (this field is ignored unless auth is set to 1 or "password")
98 | password: ""
99 | # Channel whitelisted players array (this field is ignored unless auth is set to 2 or "whitelist")
100 | whitelist: []
101 | ```
102 |
103 | **Configuration (config.yml):**
104 |
105 | ```yaml
106 | ---
107 | # Date\Time format (replaced in {TIME}). For format codes read http://php.net/manual/en/datetime.formats.php
108 | datetime-format: "H:i:s"
109 | # Log channel messages on console
110 | log-on-console: true
111 | # Default channel settings
112 | default-channel:
113 | # Enable default (join) channel
114 | enabled: false
115 | # Default (join) channel
116 | channel: "users"
117 | ...
118 | ```
119 |
120 | **Commands:**
121 |
122 | ***/serverchannels*** *- ServerChannels commands (aliases: [serverch, sch])*
123 | ***/sch info*** *- Show info about this plugin*
124 | ***/sch help*** *- Show help about this plugin*
125 | ***/sch reload*** *- Reload the config*
126 | ***/sch create <channel>*** *- Create a new channel*
127 | ***/sch join <channel> [password]*** *- Join a channel*
128 | ***/sch leave*** *- Leave the current channel*
129 | ***/sch list*** *- Show the list of all channels*
130 |
131 | **Permissions:**
132 |
133 | -
serverchannels.* - ServerChannels permissions.
134 | - serverchannels.channels.* - ServerChannels channels permissions.
135 | - serverchannels.commands.* - ServerChannels commands permissions.
136 | - serverchannels.commands.help - ServerChannels command Help permission.
137 | - serverchannels.commands.info - ServerChannels command Info permission.
138 | - serverchannels.commands.reload - ServerChannels command Reload permission.
139 | - serverchannels.commands.create - ServerChannels command New permission.
140 | - serverchannels.commands.join - ServerChannels command Join permission.
141 | - serverchannels.commands.leave - ServerChannels command Leave permission.
142 | - serverchannels.commands.list - ServerChannels command List permission.
143 |
144 | ## API
145 |
146 | Almost all our plugins have API access to widely extend their features.
147 |
148 | To access ServerChannels API:
149 | *1. Define the plugin dependency in plugin.yml (you can check if ServerChannels is installed in different ways):*
150 |
151 | ```yaml
152 | depend: [ServerChannels]
153 | ```
154 |
155 | *2. Include ServerChannels API in your plugin code:*
156 |
157 | ```php
158 | //ServerChannels API
159 | use ServerChannels\ServerChannels;
160 | ```
161 |
162 | *3. Access the API by doing:*
163 |
164 | ```php
165 | ServerChannels::getAPI()
166 | ```
167 |
--------------------------------------------------------------------------------
/icon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/EvolSoft/ServerChannels/fe7a2b2967a1e026341b87f4a1591aafbfa4a87d/icon.png
--------------------------------------------------------------------------------
/plugin.yml:
--------------------------------------------------------------------------------
1 | name: ServerChannels
2 | main: ServerChannels\ServerChannels
3 | version: 2.3
4 | api: [3.0.0]
5 | load: STARTUP
6 | author: EvolSoft
7 | description: A powerful chat channelling plugin for PocketMine-MP
8 | website: https://www.evolsoft.tk
9 |
10 | commands:
11 | serverchannels:
12 | aliases: [serverch, sch]
13 | description: ServerChannels commands.
14 | permission: serverchannels
15 |
16 | permissions:
17 | serverchannels:
18 | default: true
19 | description: ServerChannels permissions.
20 | children:
21 | serverchannels.channels:
22 | default: true
23 | description: ServerChannels channels permissions.
24 | serverchannels.commands:
25 | default: true
26 | description: ServerChannels commands permissions.
27 | children:
28 | serverchannels.commands.create:
29 | default: op
30 | description: ServerChannels command New permission.
31 | serverchannels.commands.help:
32 | default: true
33 | description: ServerChannels command Help permission.
34 | serverchannels.commands.info:
35 | default: op
36 | description: ServerChannels command Info permission.
37 | serverchannels.commands.reload:
38 | default: op
39 | description: ServerChannels command Reload permission.
40 | serverchannels.commands.list:
41 | default: op
42 | description: ServerChannels command List permission.
43 | serverchannels.commands.join:
44 | default: op
45 | description: ServerChannels command Join permission.
46 | serverchannels.commands.leave:
47 | default: op
48 | description: ServerChannels command Leave permission.
49 |
--------------------------------------------------------------------------------
/resources/channels.yml:
--------------------------------------------------------------------------------
1 | ---
2 | admin:
3 | name: "&b&lAdmin"
4 | prefix: "&c[&bAdmin&c]"
5 | suffix: ""
6 | format: "{PREFIX} <{PLAYER}> {MESSAGE}"
7 | hidden: false
8 | auth: "whitelist"
9 | password: ""
10 | whitelist:
11 | - "Flavius12"
12 | staff:
13 | name: "&a&lStaff"
14 | prefix: "&b[&aStaff&b]"
15 | suffix: ""
16 | format: "{PREFIX} <{PLAYER}> {MESSAGE}"
17 | hidden: false
18 | auth: "password"
19 | password: "password"
20 | whitelist: []
21 | private:
22 | name: "&d&oPrivate"
23 | prefix: "&d[Private]"
24 | suffix: ""
25 | format: "{PREFIX} <{PLAYER}> {MESSAGE}"
26 | hidden: true
27 | auth: "none"
28 | password: ""
29 | whitelist: []
30 | users:
31 | name: "&e&lUsers"
32 | prefix: "&7[&eUsers&7]"
33 | suffix: ""
34 | format: "{PREFIX} <{PLAYER}> {MESSAGE}"
35 | hidden: false
36 | auth: "none"
37 | password: ""
38 | whitelist: []
39 | ...
--------------------------------------------------------------------------------
/resources/config.yml:
--------------------------------------------------------------------------------
1 | ---
2 | # Date\Time format (replaced in {TIME}). For format codes read http://php.net/manual/en/datetime.formats.php
3 | datetime-format: "H:i:s"
4 | # Log channel messages on console
5 | log-on-console: true
6 | # Default channel settings
7 | default-channel:
8 | # Enable default (join) channel
9 | enabled: false
10 | # Default (join) channel
11 | channel: "users"
12 | ...
13 |
--------------------------------------------------------------------------------
/src/ServerChannels/Commands/Commands.php:
--------------------------------------------------------------------------------
1 | plugin = $plugin;
30 | }
31 |
32 | public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) : bool {
33 | if(isset($args[0])){
34 | $args[0] = strtolower($args[0]);
35 | switch($args[0]){
36 | case "create":
37 | case "new":
38 | if($sender->hasPermission("serverchannels.commands.create")){
39 | if(isset($args[1])){
40 | $this->plugin->createChannel($args[1]);
41 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&aChannel &b" . strtolower($args[1]) . "&a created!"));
42 | }else{
43 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cUsage: /sch create "));
44 | }
45 | break;
46 | }
47 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
48 | break;
49 | case "help":
50 | goto help;
51 | case "info":
52 | if($sender->hasPermission("serverchannels.commands.info")){
53 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&eServerChannels &bv" . $this->plugin->getVersion() . "&e developed by &bEvolSoft"));
54 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&eWebsite &b" . $this->plugin->getDescription()->getWebsite()));
55 | break;
56 | }
57 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
58 | break;
59 | case "join":
60 | if($sender instanceof Player){
61 | if($sender->hasPermission("serverchannels.commands.join")){
62 | if(isset($args[1])){
63 | if($sender->hasPermission(strtolower("serverchannels.channels." . $args[1]))){
64 | $cmessage = null;
65 | switch($this->plugin->joinChannel($sender, $args[1], isset($args[2]) ? $args[2] : null, $cmessage)){
66 | case ServerChannels::SUCCESS:
67 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&aYou joined &b" . strtolower($args[1]) . "&a channel"));
68 | break;
69 | case ServerChannels::ERR_WRONG_PASS:
70 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cWrong password"));
71 | break;
72 | case ServerChannels::ERR_NOT_WHITELISTED:
73 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cYou are not whitelisted on this channel"));
74 | break;
75 | case ServerChannels::ERR_CHANNEL_NOT_FOUND:
76 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cChannel not found"));
77 | break;
78 | case ServerChannels::CANCELLED:
79 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . $cmessage));
80 | break;
81 | default:
82 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cAn error has occurred while joining the channel"));
83 | break;
84 | }
85 | }else{
86 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cYou don't have permissions to join in this channel"));
87 | }
88 | }else{
89 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cUsage: /sch join [password]"));
90 | }
91 | }else{
92 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
93 | }
94 | }else{
95 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cYou can only perform this command as a player"));
96 | }
97 | break;
98 | case "leave":
99 | if($sender instanceof Player){
100 | if($sender->hasPermission("serverchannels.commands.leave")){
101 | $channel = $this->plugin->getCurrentChannel($sender);
102 | $cmessage = null;
103 | switch($this->plugin->leaveChannel($sender, $cmessage)){
104 | case ServerChannels::SUCCESS:
105 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&aYou left &b" . $channel . "&a channel"));
106 | break;
107 | case ServerChannels::ERR_NO_CHANNEL:
108 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cYou haven't joined on a channel"));
109 | break;
110 | case ServerChannels::CANCELLED:
111 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . $cmessage));
112 | break;
113 | default:
114 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cAn error has occurred while leaving the channel"));
115 | break;
116 |
117 | }
118 | }else{
119 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
120 | }
121 | }else{
122 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cYou can only perform this command as a player"));
123 | }
124 | break;
125 | case "list":
126 | if($sender->hasPermission("serverchannels.commands.list")){
127 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&b>> &aAvailable Channels &b<<"));
128 | foreach($this->plugin->getAllChannels() as $k => $v){
129 | if($sender->hasPermission(strtolower("serverchannels.channels." . $k))){
130 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&b- &a" . $k));
131 | }
132 | }
133 | break;
134 | }
135 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
136 | break;
137 | case "reload":
138 | if($sender->hasPermission("serverchannels.commands.reload")){
139 | $this->plugin->reload();
140 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&aConfiguration reloaded"));
141 | break;
142 | }
143 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
144 | break;
145 | default:
146 | if($sender->hasPermission("serverchannels")){
147 | $sender->sendMessage(TextFormat::colorize(ServerChannels::PREFIX . "&cSubcommand &a" . $args[0] . "&c not found. Use &a/sch help&c to show available commands"));
148 | break;
149 | }
150 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
151 | break;
152 | }
153 | }else{
154 | help:
155 | if($sender->hasPermission("serverchannels.commands.help")){
156 | $sender->sendMessage(TextFormat::colorize("&b>> &aAvailable Commands &b<<"));
157 | $sender->sendMessage(TextFormat::colorize("&a/sch info &b>>&e Show info about this plugin"));
158 | $sender->sendMessage(TextFormat::colorize("&a/sch help &b>>&e Show help about this plugin"));
159 | $sender->sendMessage(TextFormat::colorize("&a/sch reload &b>>&e Reload the config"));
160 | $sender->sendMessage(TextFormat::colorize("&a/sch create &b>>&e Create new channel"));
161 | $sender->sendMessage(TextFormat::colorize("&a/sch list &b>>&e Show the list of all channels"));
162 | $sender->sendMessage(TextFormat::colorize("&a/sch join &b>>&e Join a channel"));
163 | $sender->sendMessage(TextFormat::colorize("&a/sch leave &b>>&e Leave the current channel"));
164 | }else{
165 | $sender->sendMessage(TextFormat::colorize("&cYou don't have permissions to use this command"));
166 | }
167 | }
168 | return true;
169 | }
170 | }
--------------------------------------------------------------------------------
/src/ServerChannels/EventListener.php:
--------------------------------------------------------------------------------
1 | plugin = $plugin;
25 | }
26 |
27 | /**
28 | * @param PlayerJoinEvent $event
29 | */
30 | public function onPlayerJoin(PlayerJoinEvent $event){
31 | $ch = $this->plugin->getDefaultChannel();
32 | if($this->plugin->isDefaultChannelEnabled() && $this->plugin->channelExists($ch) && $this->plugin->getChannelAuth($ch) == ServerChannels::AUTH_NONE){
33 | $this->plugin->joinChannel($event->getPlayer(), $ch);
34 | }
35 | }
36 |
37 | /**
38 | * @param PlayerChatEvent $event
39 | */
40 | public function onPlayerChat(PlayerChatEvent $event){
41 | $player = $event->getPlayer();
42 | $message = $event->getMessage();
43 | if(($channel = $this->plugin->getCurrentChannel($player))){
44 | $this->plugin->sendChannelMessage($player, $channel, $message);
45 | $event->setCancelled(true);
46 | }
47 | }
48 |
49 | /**
50 | * @param PlayerQuitEvent $event
51 | */
52 | public function onPlayerQuit(PlayerQuitEvent $event){
53 | $this->plugin->leaveChannel($event->getPlayer());
54 | }
55 | }
--------------------------------------------------------------------------------
/src/ServerChannels/Events/ServerChannelsChatEvent.php:
--------------------------------------------------------------------------------
1 | player = $player;
30 | $this->channel = $channel;
31 | $this->message = $message;
32 | }
33 |
34 | /**
35 | * Get player
36 | *
37 | * @return Player
38 | */
39 | public function getPlayer() : Player {
40 | return $this->player;
41 | }
42 |
43 | /**
44 | * Get current channel
45 | *
46 | * @return string
47 | */
48 | public function getChannel(){
49 | return $this->channel;
50 | }
51 |
52 | /**
53 | * Get message
54 | *
55 | * @return string
56 | */
57 | public function getMessage(){
58 | return $this->message;
59 | }
60 |
61 | /**
62 | * Set message
63 | *
64 | * @param string $message
65 | */
66 | public function setMessage($message){
67 | $this->message = $message;
68 | }
69 | }
--------------------------------------------------------------------------------
/src/ServerChannels/Events/ServerChannelsEvent.php:
--------------------------------------------------------------------------------
1 | cmessage;
28 | }
29 |
30 | /**
31 | * Set event cancelled message
32 | *
33 | * @param string $message
34 | */
35 | public function setCancelledMessage($cmessage){
36 | $this->cmessage = $cmessage;
37 | }
38 | }
--------------------------------------------------------------------------------
/src/ServerChannels/Events/ServerChannelsJoinEvent.php:
--------------------------------------------------------------------------------
1 | player = $player;
30 | $this->channel = $channel;
31 | $this->password = $password;
32 | }
33 |
34 | /**
35 | * Get player
36 | *
37 | * @return Player
38 | */
39 | public function getPlayer() : Player {
40 | return $this->player;
41 | }
42 |
43 | /**
44 | * Get channel
45 | *
46 | * @return string
47 | */
48 | public function getChannel(){
49 | return $this->channel;
50 | }
51 |
52 | /**
53 | * Get player typed password
54 | *
55 | * @return string
56 | */
57 | public function getPassword(){
58 | return $this->password;
59 | }
60 | }
--------------------------------------------------------------------------------
/src/ServerChannels/Events/ServerChannelsLeaveEvent.php:
--------------------------------------------------------------------------------
1 | player = $player;
27 | $this->channel = $channel;
28 | }
29 |
30 | /**
31 | * Get player
32 | *
33 | * @return Player
34 | */
35 | public function getPlayer() : Player {
36 | return $this->player;
37 | }
38 |
39 | /**
40 | * Get current channel
41 | *
42 | * @return string
43 | */
44 | public function getChannel(){
45 | return $this->channel;
46 | }
47 | }
--------------------------------------------------------------------------------
/src/ServerChannels/ServerChannels.php:
--------------------------------------------------------------------------------
1 | getDataFolder());
78 | $this->saveDefaultConfig();
79 | $this->saveResource("channels.yml");
80 | $this->cfg = $this->getConfig()->getAll();
81 | $this->channels = new Config($this->getDataFolder() . "channels.yml");
82 | $this->initializeChannelsPermissions();
83 | $this->getCommand("serverchannels")->setExecutor(new Commands\Commands($this));
84 | $this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
85 | }
86 |
87 | /**
88 | * Reload ServerChannels configurations
89 | */
90 | public function reload(){
91 | $this->reloadConfig();
92 | $this->cfg = $this->getConfig()->getAll();
93 | $this->channels->reload();
94 | $this->initializeChannelsPermissions();
95 | }
96 |
97 | /**
98 | * Initialize channels permissions (internal)
99 | */
100 | public function initializeChannelsPermissions(){
101 | foreach($this->getAllChannels() as $k => $v){
102 | $permission = new Permission("serverchannels.channels." . strtolower($k), "ServerChannels " . $k . " channel permission.");
103 | $this->getServer()->getPluginManager()->addPermission($permission);
104 | }
105 | }
106 |
107 | /**
108 | * Replace variables inside a string
109 | *
110 | * @param string $str
111 | * @param array $vars
112 | *
113 | * @return string
114 | */
115 | public function replaceVars($str, array $vars){
116 | foreach($vars as $key => $value){
117 | $str = str_replace("{" . $key . "}", $value, $str);
118 | }
119 | return $str;
120 | }
121 |
122 | /**
123 | * Get ServerChannels API
124 | *
125 | * @return ServerChannels
126 | */
127 | public static function getAPI(){
128 | return self::$instance;
129 | }
130 |
131 | /**
132 | * Get ServerChannels version
133 | *
134 | * @return string
135 | */
136 | public function getVersion(){
137 | return $this->getVersion();
138 | }
139 |
140 | /**
141 | * Get ServerChannels API version
142 | *
143 | * @return string
144 | */
145 | public function getAPIVersion(){
146 | return self::API_VERSION;
147 | }
148 |
149 | /**
150 | * Check if console logging is enabled
151 | */
152 | public function getLogOnConsole(){
153 | return $this->cfg["log-on-console"];
154 | }
155 |
156 | /**
157 | * Check if default (join) channel is enabled
158 | */
159 | public function isDefaultChannelEnabled(){
160 | return $this->cfg["default-channel"]["enabled"];
161 | }
162 |
163 | /**
164 | * Get default (join) channel
165 | */
166 | public function getDefaultChannel(){
167 | return $this->cfg["default-channel"]["channel"];
168 | }
169 |
170 | /**
171 | * Create a new channel
172 | *
173 | * @param string $channel
174 | */
175 | public function createChannel($channel){
176 | $ch = array(
177 | "name" => $channel,
178 | "prefix" => "[" . $channel . "]",
179 | "suffix" => null,
180 | "format" => "{PREFIX} <{PLAYER}> {MESSAGE}",
181 | "hidden" => true,
182 | "auth" => "none",
183 | "password" => null,
184 | "whitelist" => array()
185 | );
186 | $this->channels->set(strtolower($channel), $ch);
187 | $this->channels->save();
188 | $this->initializeChannelsPermissions();
189 | }
190 |
191 | /**
192 | * Check if channel exists
193 | *
194 | * @param string $channel
195 | *
196 | * @return bool
197 | */
198 | public function channelExists($channel) : bool {
199 | return $this->channels->exists(strtolower($channel));
200 | }
201 |
202 | /**
203 | * Get all channels
204 | *
205 | * @return array
206 | */
207 | public function getAllChannels(){
208 | return $this->channels->getAll();
209 | }
210 |
211 | /**
212 | * Get the specified channel
213 | *
214 | * @param string $channel
215 | *
216 | * @return array|null
217 | */
218 | public function getChannel($channel){
219 | $channel = strtolower($channel);
220 | if($this->channelExists($channel)){
221 | return $this->channels->get($channel);
222 | }
223 | return null;
224 | }
225 |
226 |
227 | /**
228 | * Get channel name
229 | *
230 | * @param string $channel
231 | *
232 | * @return string|null
233 | */
234 | public function getChannelName($channel){
235 | if(($ch = $this->getChannel($channel)) && isset($ch["name"])){
236 | return $ch["name"];
237 | }
238 | return null;
239 | }
240 |
241 |
242 | /**
243 | * Get channel prefix
244 | *
245 | * @param string $channel
246 | *
247 | * @return string|null
248 | */
249 | public function getChannelPrefix($channel){
250 | if(($ch = $this->getChannel($channel)) && isset($ch["prefix"])){
251 | return $ch["prefix"];
252 | }
253 | return null;
254 | }
255 |
256 |
257 | /**
258 | * Get channel suffix
259 | *
260 | * @param string $channel
261 | *
262 | * @return string|null
263 | */
264 | public function getChannelSuffix($channel){
265 | if(($ch = $this->getChannel($channel)) && isset($ch["suffix"])){
266 | return $ch["suffix"];
267 | }
268 | return null;
269 | }
270 |
271 | /**
272 | * Get channel format
273 | *
274 | * @param string $channel
275 | *
276 | * @return string|null
277 | */
278 | public function getChannelFormat($channel){
279 | if(($ch = $this->getChannel($channel)) && isset($ch["format"])){
280 | return $ch["format"];
281 | }
282 | return null;
283 | }
284 |
285 | /**
286 | * Check if the specified channel is hidden
287 | *
288 | * @param string $channel
289 | *
290 | * @return bool
291 | */
292 | public function isChannelHidden($channel) : bool {
293 | if(($ch = $this->getChannel($channel)) && isset($ch["hidden"])){
294 | return $ch["hidden"];
295 | }
296 | return true;
297 | }
298 |
299 | /**
300 | * Get channel authentication
301 | *
302 | * @param string $channel
303 | *
304 | * @return int
305 | */
306 | public function getChannelAuth($channel) : int {
307 | if(($ch = $this->getChannel($channel)) && isset($ch["auth"])){
308 | if(is_int($ch["auth"])){
309 | return $ch["auth"];
310 | }else{
311 | switch(strtolower($ch["auth"])){
312 | case "none":
313 | default:
314 | return self::AUTH_NONE;
315 | case "password":
316 | return self::AUTH_PASSWORD;
317 | case "whitelist":
318 | return self::AUTH_WHITELIST;
319 | }
320 | }
321 | }
322 | return self::AUTH_NONE;
323 | }
324 |
325 | /**
326 | * Get channel password
327 | *
328 | * @param string $channel
329 | *
330 | * @return string|null
331 | */
332 | public function getChannelPassword($channel){
333 | if(($ch = $this->getChannel($channel)) && isset($ch["password"])){
334 | return $ch["password"];
335 | }
336 | return null;
337 | }
338 |
339 | /**
340 | * Get channel whitelist
341 | *
342 | * @param string $channel
343 | *
344 | * @return array|null
345 | */
346 | public function getChannelWhitelist($channel){
347 | if(($ch = $this->getChannel($channel)) && isset($ch["whitelist"])){
348 | return $ch["whitelist"];
349 | }
350 | return null;
351 | }
352 |
353 | /**
354 | * Get current player channel
355 | *
356 | * @param Player $player
357 | *
358 | * @return string|null
359 | */
360 | public function getCurrentChannel(Player $player){
361 | if($this->hasJoined($player)){
362 | return $this->users[strtolower($player->getName())];
363 | }
364 | return null;
365 | }
366 |
367 | /**
368 | * Join the specified channel
369 | *
370 | * @param Player $player
371 | * @param string $channel
372 | * @param string $password
373 | * @param string $cmessage
374 | *
375 | * @return int
376 | */
377 | public function joinChannel(Player $player, $channel, $password = null, &$cmessage = null) : int {
378 | $channel = strtolower($channel);
379 | if($this->channelExists($channel)){
380 | $scevent = new ServerChannelsJoinEvent($player, $channel, $password);
381 | $scevent->setCancelledMessage("&cOperation cancelled");
382 | $this->getServer()->getPluginManager()->callEvent($scevent);
383 | if($scevent->isCancelled()){
384 | $cmessage = $scevent->getCancelledMessage();
385 | return self::CANCELLED;
386 | }
387 | switch($this->getChannelAuth($channel)){
388 | default:
389 | case self::AUTH_NONE:
390 | break;
391 | case self::AUTH_PASSWORD:
392 | if(($pwd = $this->getChannelPassword($channel)) && $pwd != $password){
393 | return self::ERR_WRONG_PASS;
394 | }
395 | break;
396 | case self::AUTH_WHITELIST:
397 | if(!in_array($player->getName(), $this->getChannelWhitelist($channel))){
398 | return self::ERR_NOT_WHITELISTED;
399 | }
400 | break;
401 | }
402 | if($this->hasJoined($player)){
403 | $this->leaveChannel($player);
404 | }
405 | $this->users[strtolower($player->getName())] = $channel;
406 | return self::SUCCESS;
407 | }
408 | return self::ERR_CHANNEL_NOT_FOUND;
409 | }
410 |
411 | /**
412 | * Leave current channel
413 | *
414 | * @param Player $player
415 | * @param string $cmessage
416 | *
417 | * @return int
418 | */
419 | public function leaveChannel(Player $player, &$cmessage = null) : int {
420 | if($this->hasJoined($player)){
421 | $pname = strtolower($player->getName());
422 | $channel = $this->users[$pname];
423 | $scevent = new ServerChannelsLeaveEvent($player, $channel);
424 | $scevent->setCancelledMessage("&cOperation cancelled");
425 | $this->getServer()->getPluginManager()->callEvent($scevent);
426 | if($scevent->isCancelled()){
427 | $cmessage = $scevent->getCancelledMessage();
428 | return self::CANCELLED;
429 | }
430 | unset($this->users[$pname]);
431 | return self::SUCCESS;
432 | }
433 | return self::ERR_NO_CHANNEL;
434 | }
435 |
436 | /**
437 | * Check if player has joined a channel
438 | *
439 | * @param Player $player
440 | *
441 | * @return bool
442 | */
443 | public function hasJoined(Player $player) : bool {
444 | return isset($this->users[strtolower($player->getName())]);
445 | }
446 |
447 | /**
448 | * Get all players in the specified channel
449 | *
450 | * @param string $channel
451 | *
452 | * @return Player[]
453 | */
454 | public function getChannelPlayers($channel){
455 | $channel = strtolower($channel);
456 | $array = array_keys($this->users, $channel);
457 | $result = array();
458 | foreach($array as $v){
459 | array_push($result, $this->getServer()->getPlayer($v));
460 | }
461 | return $result;
462 | }
463 |
464 | /**
465 | * Format channel message
466 | *
467 | * @param string $channel
468 | * @param Player $player
469 | * @param string $message
470 | *
471 | * @return string|null
472 | */
473 | public function formatChannelMessage($channel, Player $player, $message){
474 | $channel = strtolower($channel);
475 | if(($ch = $this->getChannel($channel))){
476 | $prefix = $ch["prefix"];
477 | $suffix = $ch["suffix"];
478 | $format = $ch["format"];
479 | $str = $this->replaceVars($format, array(
480 | "MESSAGE" => $message,
481 | "PLAYER" => $player->getName(),
482 | "PREFIX" => $prefix,
483 | "SUFFIX" => $suffix,
484 | "TIME" => date($this->cfg["datetime-format"]),
485 | "WORLD" => $player->getLevel()->getName()
486 | ));
487 | return $str;
488 | }
489 | return null;
490 | }
491 |
492 | /**
493 | * Send message into the specified channel
494 | *
495 | * @param Player $player
496 | * @param string $channel
497 | * @param string $message
498 | * @param string $cmessage
499 | *
500 | * @return int
501 | */
502 | public function sendChannelMessage(Player $player, $channel, $message, &$cmessage = null){
503 | if($this->channelExists($channel)){
504 | $scevent = new ServerChannelsChatEvent($player, $channel, $message);
505 | $scevent->setCancelledMessage("&cOperation cancelled");
506 | $this->getServer()->getPluginManager()->callEvent($scevent);
507 | if($scevent->isCancelled()){
508 | $cmessage = $scevent->getCancelledMessage();
509 | return self::CANCELLED;
510 | }
511 | $message = TextFormat::colorize($this->formatChannelMessage($channel, $player, $scevent->getMessage()));
512 | if($this->isChannelHidden($channel)){
513 | foreach($this->getChannelPlayers($channel) as $cp){
514 | $cp->sendMessage($message);
515 | }
516 | }else{
517 | foreach($this->getServer()->getOnlinePlayers() as $op){
518 | $op->sendMessage($message);
519 | }
520 | }
521 | if($this->getLogOnConsole()){
522 | $this->getServer()->getLogger()->info($message);
523 | }
524 | }
525 | return self::SUCCESS;
526 | }
527 | }
--------------------------------------------------------------------------------