PocketMine-MP 5.35.1 git-f412a390b8f63d0311cc1d1c81046404153b8440
Loading...
Searching...
No Matches
TimeCommand.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\command\defaults;
25
33use function count;
34
36
37 public function __construct(string $namespace, string $name){
38 parent::__construct(
39 $namespace,
40 $name,
41 KnownTranslationFactory::pocketmine_command_time_description(),
42 KnownTranslationFactory::pocketmine_command_time_usage()
43 );
44 $this->setPermissions([
45 DefaultPermissionNames::COMMAND_TIME_ADD,
46 DefaultPermissionNames::COMMAND_TIME_SET,
47 DefaultPermissionNames::COMMAND_TIME_START,
48 DefaultPermissionNames::COMMAND_TIME_STOP,
49 DefaultPermissionNames::COMMAND_TIME_QUERY
50 ]);
51 }
52
53 public function execute(CommandSender $sender, string $commandLabel, array $args){
54 if(count($args) < 1){
56 }
57 $testPermissionCtx = $commandLabel . " " . $args[0];
58
59 if($args[0] === "start"){
60 if(!$this->testPermission($testPermissionCtx, $sender, DefaultPermissionNames::COMMAND_TIME_START)){
61 return true;
62 }
63 foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){
64 $world->startTime();
65 }
66 Command::broadcastCommandMessage($sender, "Restarted the time");
67 return true;
68 }elseif($args[0] === "stop"){
69 if(!$this->testPermission($testPermissionCtx, $sender, DefaultPermissionNames::COMMAND_TIME_STOP)){
70 return true;
71 }
72 foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){
73 $world->stopTime();
74 }
75 Command::broadcastCommandMessage($sender, "Stopped the time");
76 return true;
77 }elseif($args[0] === "query"){
78 if(!$this->testPermission($testPermissionCtx, $sender, DefaultPermissionNames::COMMAND_TIME_QUERY)){
79 return true;
80 }
81 if($sender instanceof Player){
82 $world = $sender->getWorld();
83 }else{
84 $world = $sender->getServer()->getWorldManager()->getDefaultWorld();
85 }
86 $sender->sendMessage($sender->getLanguage()->translate(KnownTranslationFactory::commands_time_query((string) $world->getTime())));
87 return true;
88 }
89
90 if(count($args) < 2){
92 }
93
94 if($args[0] === "set"){
95 if(!$this->testPermission($testPermissionCtx, $sender, DefaultPermissionNames::COMMAND_TIME_SET)){
96 return true;
97 }
98
99 switch($args[1]){
100 case "day":
101 $value = World::TIME_DAY;
102 break;
103 case "noon":
104 $value = World::TIME_NOON;
105 break;
106 case "sunset":
107 $value = World::TIME_SUNSET;
108 break;
109 case "night":
110 $value = World::TIME_NIGHT;
111 break;
112 case "midnight":
113 $value = World::TIME_MIDNIGHT;
114 break;
115 case "sunrise":
116 $value = World::TIME_SUNRISE;
117 break;
118 default:
119 $value = $this->getInteger($sender, $args[1], 0);
120 break;
121 }
122
123 foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){
124 $world->setTime($value);
125 }
126 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_time_set((string) $value));
127 }elseif($args[0] === "add"){
128 if(!$this->testPermission($testPermissionCtx, $sender, DefaultPermissionNames::COMMAND_TIME_ADD)){
129 return true;
130 }
131
132 $value = $this->getInteger($sender, $args[1], 0);
133 foreach($sender->getServer()->getWorldManager()->getWorlds() as $world){
134 $world->setTime($world->getTime() + $value);
135 }
136 Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_time_added((string) $value));
137 }else{
139 }
140
141 return true;
142 }
143}
testPermission(string $context, CommandSender $target, ?string $permission=null)
Definition Command.php:125
setPermissions(array $permissions)
Definition Command.php:106
execute(CommandSender $sender, string $commandLabel, array $args)