PageRenderTime 66ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/Plugins/Grenade/src/MineBlock/Grenade/Grenade.php

https://gitlab.com/Skull3x/MineBlock
PHP | 206 lines | 188 code | 18 blank | 0 comment | 26 complexity | 0a7f1f044cbbe67da6f699dc4155cbe4 MD5 | raw file
  1. <?php
  2. namespace MineBlock\Grenade;
  3. use pocketmine\plugin\PluginBase;
  4. use pocketmine\command\Command;
  5. use pocketmine\command\CommandSender;
  6. use pocketmine\event\Listener;
  7. use pocketmine\utils\Config;
  8. use pocketmine\utils\TextFormat;
  9. use pocketmine\event\server\DataPacketReceiveEvent;
  10. use pocketmine\event\entity\ExplosionPrimeEvent;
  11. use pocketmine\network\protocol\AddItemEntityPacket;
  12. use pocketmine\network\protocol\Info as ProtocolInfo;
  13. use pocketmine\item\Item;
  14. use pocketmine\math\AxisAlignedBB;
  15. use pocketmine\math\Vector3;
  16. use pocketmine\Player;
  17. use pocketmine\entity\Living;
  18. use pocketmine\nbt\tag\Compound;
  19. use pocketmine\nbt\tag\Enum;
  20. use pocketmine\nbt\tag\Double;
  21. use pocketmine\nbt\tag\Float;
  22. use pocketmine\entity\Entity;
  23. use pocketmine\entity\Projectile;
  24. use pocketmine\level\format\FullChunk;
  25. use pocketmine\level\Explosion;
  26. class Grenade extends PluginBase implements Listener{
  27. public function onEnable(){
  28. $this->cool = [];
  29. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  30. $this->loadYml();
  31. }
  32. public function onCommand(CommandSender $sender, Command $cmd, $label, array $sub){
  33. if(!isset($sub[0])) return false;
  34. $g = $this->g;
  35. $rm = TextFormat::RED . "Usage: /Grenade ";
  36. $mm = "[Grenade]";
  37. $ik = $this->isKorean();
  38. switch(strtolower($sub[0])){
  39. case "gun":
  40. case "g":
  41. case "수류탄":
  42. case "item":
  43. case "i":
  44. case "아이템":
  45. case "템":
  46. if(!isset($sub[1])){
  47. $r = $rm . ($ik ? "수류탄 <아이템ID>": "Grenade(G) <ItemID>");
  48. }else{
  49. $i = Item::fromString($sub[1]);
  50. $id = $i->getID() . ":" . $i->getDamage();
  51. $g["Grenade"] = $id;
  52. $r = $mm . ($ik ? "수류탄을 $id 로 설정했습니다.": "Grenade is set $id");
  53. }
  54. break;
  55. case "cool":
  56. case "cooltime":
  57. case "ct":
  58. case "쿨타임":
  59. case "쿨":
  60. if(!isset($sub[1])){
  61. $r = $rm . ($ik ? "쿨타임 <시간>": "CoolTime(CT) <Num>");
  62. }else{
  63. if($sub[1] < 0 || !is_numeric($sub[1])) $sub[1] = 0;
  64. if(isset($sub[2]) && $sub[2] > $sub[1] && is_numeric($sub[2]) !== false) $sub[1] = $sub[1] . "~" . $sub[2];
  65. $g["Cool"] = $sub[1];
  66. $r = $mm . ($ik ? "낚시 쿨타임을 [$sub[1]] 로 설정했습니다.": "Grenade cooltime is set [$sub[1]]");
  67. }
  68. break;
  69. default:
  70. return false;
  71. break;
  72. }
  73. if(isset($r)) $sender->sendMessage($r);
  74. if($this != $g){
  75. $this->g = $g;
  76. $this->saveYml();
  77. }
  78. return true;
  79. }
  80. public function onDataPacketReceive(DataPacketReceiveEvent $event){
  81. $pk = $event->getPacket();
  82. if($pk->pid() !== ProtocolInfo::USE_ITEM_PACKET || $pk->face !== 0xff) return false;
  83. $ik = $this->isKorean();
  84. $m = ($ik ? "[수류탄] ": "[Grenade] ");
  85. $p = $event->getPlayer();
  86. $n = $p->getName();
  87. $i = $this->getItem($this->g["Grenade"], 1);
  88. $ii = $p->getInventory()->getItemInHand();
  89. if($ii->getID() !== $i->getID() || $ii->getDamage() !== $i->getDamage()) return;
  90. if(!isset($this->cool[$n])) $this->cool[$n] = 0;
  91. $c = microtime(true) - $this->cool[$n];
  92. if($c < 0){
  93. $m .= $ik ? "쿨타임 :" . round(-$c, 1) . " 초": "Cool : " . round($c * 100) / -100 . " sec";
  94. }else{
  95. $c = explode("~", $this->g["Cool"]);
  96. $this->cool[$n] = microtime(true) + rand($c[0], isset($c[1]) ? $c[1] : $c[0]);
  97. $p->getInventory()->removeItem($i);
  98. $this->getServer()->broadcastPacket($p->getLevel()->getUsingChunk($p->x >> 4, $p->z >> 4), $pk);
  99. $nbt = new Compound("", ["Pos" => new Enum("Pos", [new Double("", $p->x),new Double("", $p->y + $p->getEyeHeight() - 0.2),new Double("", $p->z)]),"Motion" => new Enum("Motion", [new Double("", -sin($p->getyaw() / 180 * M_PI) * cos($p->getPitch() / 180 * M_PI)),new Double("", -sin($p->getPitch() / 180 * M_PI)),new Double("", cos($p->getyaw() / 180 * M_PI) * cos($p->getPitch() / 180 * M_PI))]),"Rotation" => new Enum("Rotation", [new Float("", $p->getyaw()),new Float("", $p->getPitch())])]);
  100. $grenade = new GrenadeEntity($p->chunk, $nbt, $p, $i);
  101. $grenade->spawnToAll();
  102. return;
  103. }
  104. if(isset($m)) $p->sendMessage($m);
  105. $event->setCancelled();
  106. }
  107. public function getItem($id = 0, $cnt = 0){
  108. $id = explode(":", $id);
  109. return Item::get($id[0], isset($id[1]) ? $id[1] : 0, $cnt);
  110. }
  111. public function loadYml(){
  112. @mkdir($this->path = ($this->getServer()->getDataPath() . "/plugins/! MineBlock/"));
  113. $this->g = (new Config($this->file = $this->path . "Grenade.yml", Config::YAML, [
  114. "Grenade" => "341:0",
  115. "Cool" => "3",
  116. ]))->getAll();
  117. }
  118. public function saveYml(){
  119. $g = new Config($this->file, Config::YAML);
  120. $g->setAll($this->g);
  121. $g->save();
  122. }
  123. public function isKorean(){
  124. @mkdir($this->getServer()->getDataPath() . "/plugins/! MineBlock/");
  125. if(!isset($this->ik)) $this->ik = (new Config($this->getServer()->getDataPath() . "/plugins/! MineBlock/" . "! Korean.yml", Config::YAML, ["Korean" => false]))->get("Korean");
  126. return $this->ik;
  127. }
  128. }
  129. class GrenadeEntity extends Projectile{
  130. const NETWORK_ID = 64;
  131. protected $gravity = 0.03;
  132. protected $drag = 0.01;
  133. public function __construct(FullChunk $chunk, Compound $nbt, Entity $shootingEntity = null, Item $item){
  134. $this->shootingEntity = $shootingEntity;
  135. $this->item = $item;
  136. parent::__construct($chunk, $nbt);
  137. }
  138. protected function initEntity(){
  139. }
  140. public function saveNBT(){
  141. }
  142. public function canCollideWith(Entity $entity){
  143. return false;
  144. }
  145. public function explode(){
  146. $this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 3));
  147. if(!$ev->isCancelled()){
  148. $explosion = new Explosion($this, $ev->getForce(), $this);
  149. if($ev->isBlockBreaking()){
  150. $explosion->explodeA();
  151. }
  152. $explosion->explodeB();
  153. }
  154. }
  155. public function onUpdate($currentTick){
  156. if($this->closed){
  157. return false;
  158. }
  159. $this->timings->startTiming();
  160. $hasUpdate = parent::onUpdate($currentTick);
  161. $bb = new AxisAlignedBB($x = $this->x - 0.5, $y = $this->y, $z = $this->z - 0.5, $x + 1, $y + 1, $z + 1);
  162. if($this->age > 100){
  163. $this->kill();
  164. $this->explode();
  165. $this->close();
  166. $hasUpdate = true;
  167. }elseif(count($this->level->getCollisionBlocks($bb = $this->getBoundingBox())) > 0){
  168. if($this->motionX < 0) $this->motionX = -$this->motionX * 0.5;
  169. if($this->motionY < 0) $this->motionY = -$this->motionY * 0.1;
  170. if($this->motionZ < 0) $this->motionZ = -$this->motionZ * 0.5;
  171. }
  172. $this->timings->stopTiming();
  173. return $hasUpdate;
  174. }
  175. public function spawnTo(Player $player){
  176. $pk = new AddItemEntityPacket();
  177. $pk->eid = $this->id;
  178. $pk->x = $this->x;
  179. $pk->y = $this->y;
  180. $pk->z = $this->z;
  181. $pk->yaw = $this->yaw;
  182. $pk->pitch = $this->pitch;
  183. $pk->roll = 0;
  184. $pk->item = $this->item;
  185. $player->dataPacket($pk);
  186. parent::spawnTo($player);
  187. }
  188. }