From aeafe6bdfe94ed2fb99045d209ba8c505dcf6b6d Mon Sep 17 00:00:00 2001 From: VixikHD Date: Sun, 10 Feb 2019 20:06:56 +0100 Subject: [PATCH] 1.0.1 update :ok_hand: - config - new /1vs1 join command - customizable kits - new PlayerEquipEvent --- 1vs1/plugin.yml | 7 +- 1vs1/resources/config.yml | 20 ++++ .../vixikhd/onevsone/EmptyArenaChooser.php | 96 +++++++++++++++++++ 1vs1/src/vixikhd/onevsone/OneVsOne.php | 27 +++++- 1vs1/src/vixikhd/onevsone/arena/Arena.php | 49 +++++++--- .../vixikhd/onevsone/arena/ArenaScheduler.php | 2 +- .../onevsone/commands/OneVsOneCommand.php | 20 ++-- .../onevsone/event/PlayerArenaWinEvent.php | 18 +++- .../onevsone/event/PlayerEquipEvent.php | 70 ++++++++++++++ 1vs1/src/vixikhd/onevsone/math/Time.php | 2 +- 1vs1/src/vixikhd/onevsone/math/Vector3.php | 2 +- .../onevsone/provider/YamlDataProvider.php | 11 ++- 12 files changed, 296 insertions(+), 28 deletions(-) create mode 100644 1vs1/resources/config.yml create mode 100644 1vs1/src/vixikhd/onevsone/EmptyArenaChooser.php create mode 100644 1vs1/src/vixikhd/onevsone/event/PlayerEquipEvent.php diff --git a/1vs1/plugin.yml b/1vs1/plugin.yml index 9a88126..ee81011 100644 --- a/1vs1/plugin.yml +++ b/1vs1/plugin.yml @@ -1,6 +1,6 @@ name: 1vs1 api: 3.0.0 -version: 1.0.0 +version: 1.0.1 main: vixikhd\onevsone\OneVsOne author: VixikHD description: 1vs1 minigame plugin @@ -25,4 +25,7 @@ permissions: default: op 1vs1.cmd.arenas: description: Permission for /1vs1 arenas - default: op \ No newline at end of file + default: op + 1vs1.cmd.join: + description: Permission for /1vs1 join + default: true \ No newline at end of file diff --git a/1vs1/resources/config.yml b/1vs1/resources/config.yml new file mode 100644 index 0000000..7b421d7 --- /dev/null +++ b/1vs1/resources/config.yml @@ -0,0 +1,20 @@ +--- +# +## 1vs1 v1.0.1 configuration file +# + +# Kits are randomly selected +kits: + diamond: + helmet: [310, 0, 1] # [id, damage, count] + chestplate: [311, 0, 1] + leggings: [312, 0, 1] + boots: [313, 0, 1] + 0: [267, 0, 1] # 1. inventory slot + 1: [322, 0, 5] # 2. inventory slot + 8: [364, 0, 16] # 9. inventory slot + #steve: [] # add more kits like 'steve' kit here without any items + +# Set false to disable hunger in game +hunger: true +... \ No newline at end of file diff --git a/1vs1/src/vixikhd/onevsone/EmptyArenaChooser.php b/1vs1/src/vixikhd/onevsone/EmptyArenaChooser.php new file mode 100644 index 0000000..747ddf3 --- /dev/null +++ b/1vs1/src/vixikhd/onevsone/EmptyArenaChooser.php @@ -0,0 +1,96 @@ +plugin = $plugin; + } + + + + /** + * @return null|Arena + * + * 1. Choose all arenas + * 2. Remove in-game arenas + * 3. Sort arenas by players + * 4. Sort arenas by rand() + */ + public function getRandomArena(): ?Arena { + //1. + + /** @var Arena[] $availableArenas */ + $availableArenas = []; + foreach ($this->plugin->arenas as $index => $arena) { + $availableArenas[$index] = $arena; + } + + //2. + foreach ($availableArenas as $index => $arena) { + if($arena->phase !== 0 || $arena->setup) { + unset($availableArenas[$index]); + } + } + + //3. + $arenasByPlayers = []; + foreach ($availableArenas as $index => $arena) { + $arenasByPlayers[$index] = count($arena->players); + } + + arsort($arenasByPlayers); + $top = -1; + $availableArenas = []; + + foreach ($arenasByPlayers as $index => $players) { + if($top == -1) { + $top = $players; + $availableArenas[] = $index; + } + else { + if($top == $players) { + $availableArenas[] = $index; + } + } + } + + if(empty($availableArenas)) { + return null; + } + + return $this->plugin->arenas[$availableArenas[array_rand($availableArenas, 1)]]; + } +} \ No newline at end of file diff --git a/1vs1/src/vixikhd/onevsone/OneVsOne.php b/1vs1/src/vixikhd/onevsone/OneVsOne.php index bfe79c4..e6c1dda 100644 --- a/1vs1/src/vixikhd/onevsone/OneVsOne.php +++ b/1vs1/src/vixikhd/onevsone/OneVsOne.php @@ -1,7 +1,7 @@ dataProvider = new YamlDataProvider($this); + } + public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); - $this->dataProvider = new YamlDataProvider($this); + $this->dataProvider->loadArenas(); + $this->emptyArenaChooser = new EmptyArenaChooser($this); $this->getServer()->getCommandMap()->register("1vs1", $this->commands[] = new OneVsOneCommand($this)); } @@ -116,7 +125,7 @@ public function onChat(PlayerChatEvent $event) { $player->sendMessage("§a> Spawn $args[1] set to X: " . (string)round($player->getX()) . " Y: " . (string)round($player->getY()) . " Z: " . (string)round($player->getZ())); break; case "joinsign": - $player->sendMessage("§a> Break block to set joinsign!"); + $player->sendMessage("§a> Break block to set join sign!"); $this->setupData[$player->getName()] = 0; break; case "enable": @@ -162,4 +171,16 @@ public function onBreak(BlockBreakEvent $event) { } } } + + /** + * @param Player $player + */ + public function joinToRandomArena(Player $player) { + $arena = $this->emptyArenaChooser->getRandomArena(); + if(!is_null($arena)) { + $arena->joinToArena($player); + return; + } + $player->sendMessage("§c> All the arenas are full!"); + } } \ No newline at end of file diff --git a/1vs1/src/vixikhd/onevsone/arena/Arena.php b/1vs1/src/vixikhd/onevsone/arena/Arena.php index f2cb046..31e943f 100644 --- a/1vs1/src/vixikhd/onevsone/arena/Arena.php +++ b/1vs1/src/vixikhd/onevsone/arena/Arena.php @@ -1,7 +1,7 @@ broadcastMessage("§a> Player {$player->getName()} joined the match! §7[".count($this->players)."/{$this->data["slots"]}]"); + $player->getInventory()->clearAll(); $player->getArmorInventory()->clearAll(); $player->getCursorInventory()->clearAll(); @@ -138,15 +144,35 @@ public function joinToArena(Player $player) { $player->setFood(20); $inv = $player->getArmorInventory(); - $inv->setHelmet(Item::get(Item::DIAMOND_HELMET)); - $inv->setChestplate(Item::get(Item::DIAMOND_CHESTPLATE)); - $inv->setLeggings(Item::get(Item::DIAMOND_LEGGINGS)); - $inv->setBoots(Item::get(Item::DIAMOND_BOOTS)); + if(empty($this->plugin->dataProvider->config["kits"]) || !is_array($this->plugin->dataProvider->config["kits"]) || $this->kit === null) { + $inv->setHelmet(Item::get(Item::DIAMOND_HELMET)); + $inv->setChestplate(Item::get(Item::DIAMOND_CHESTPLATE)); + $inv->setLeggings(Item::get(Item::DIAMOND_LEGGINGS)); + $inv->setBoots(Item::get(Item::DIAMOND_BOOTS)); + + $player->getInventory()->addItem(Item::get(Item::IRON_SWORD)); + $player->getInventory()->addItem(Item::get(Item::GOLDEN_APPLE, 0, 5)); + $event = new PlayerEquipEvent($this->plugin, $player, $this); + $event->call(); + return; + } - $player->getInventory()->addItem(Item::get(Item::IRON_SWORD)); - $player->getInventory()->addItem(Item::get(Item::GOLDEN_APPLE, 0, 5)); - $this->broadcastMessage("§a> Player {$player->getName()} joined the match! §7[".count($this->players)."/{$this->data["slots"]}]"); + $kitData = $this->plugin->dataProvider->config["kits"][$this->kit]; + if(isset($kitData["helmet"])) $inv->setHelmet(Item::get($kitData["helmet"][0], $kitData["helmet"][1], $kitData["helmet"][2])); + if(isset($kitData["chestplate"])) $inv->setChestplate(Item::get($kitData["chestplate"][0], $kitData["chestplate"][1], $kitData["chestplate"][2])); + if(isset($kitData["leggings"])) $inv->setLeggings(Item::get($kitData["leggings"][0], $kitData["leggings"][1], $kitData["leggings"][2])); + if(isset($kitData["boots"])) $inv->setBoots(Item::get($kitData["boots"][0], $kitData["boots"][1], $kitData["boots"][2])); + + foreach ($kitData as $slot => [$id, $damage, $count]) { + if(is_numeric($slot)) { + $slot = (int)$slot; + $player->getInventory()->setItem($slot, Item::get($id, $damage, $count)); + } + } + + $event = new PlayerEquipEvent($this->plugin, $player, $this); + $event->call(); } /** @@ -302,7 +328,7 @@ public function onExhaust(PlayerExhaustEvent $event) { if(!$player instanceof Player) return; - if($this->inGame($player) && $this->phase == self::PHASE_LOBBY) { + if($this->inGame($player) && $this->phase == self::PHASE_LOBBY && !$this->plugin->dataProvider->config["hunger"]) { $event->setCancelled(true); } } @@ -413,14 +439,15 @@ public function loadArena(bool $restart = false) { $this->level = $this->plugin->getServer()->getLevelByName($this->data["level"]); } - - else { $this->scheduler->reloadTimer(); } if(!$this->level instanceof Level) $this->level = $this->plugin->getServer()->getLevelByName($this->data["level"]); + $keys = array_keys($this->plugin->dataProvider->config["kits"]); + $this->kit = $keys[array_rand($keys, 1)]; + $this->phase = static::PHASE_LOBBY; $this->players = []; } diff --git a/1vs1/src/vixikhd/onevsone/arena/ArenaScheduler.php b/1vs1/src/vixikhd/onevsone/arena/ArenaScheduler.php index b0a798c..6d26a18 100644 --- a/1vs1/src/vixikhd/onevsone/arena/ArenaScheduler.php +++ b/1vs1/src/vixikhd/onevsone/arena/ArenaScheduler.php @@ -1,7 +1,7 @@ hasPermission("1vs1.cmd")) { - $sender->sendMessage("§cYou have not permissions to use this command!"); - return; - } if(!isset($args[0])) { $sender->sendMessage("§cUsage: §7/1vs1 help"); return; @@ -74,7 +70,8 @@ public function execute(CommandSender $sender, string $commandLabel, array $args "§7/1vs1 create : Create OneVsOne arena\n". "§7/1vs1 remove : Remove OneVsOne arena\n". "§7/1vs1 set : Set OneVsOne arena\n". - "§7/1vs1 arenas : Displays list of arenas"); + "§7/1vs1 arenas : Displays list of arenas\n" . + "§7/1vs1 join : Connect player to the random arena"); break; case "create": @@ -165,6 +162,17 @@ public function execute(CommandSender $sender, string $commandLabel, array $args } $sender->sendMessage($list); break; + case "join": + if(!$sender->hasPermission("1vs1.cmd.join")) { + $sender->hasPermission("§cYou have not permissions to use this command!"); + break; + } + if(!$sender instanceof Player) { + $sender->sendMessage("§c> This command can be used only in game!"); + break; + } + $this->plugin->joinToRandomArena($sender); + break; default: if(!$sender->hasPermission("1vs1.cmd.help")) { $sender->sendMessage("§cYou have not permissions to use this command!"); diff --git a/1vs1/src/vixikhd/onevsone/event/PlayerArenaWinEvent.php b/1vs1/src/vixikhd/onevsone/event/PlayerArenaWinEvent.php index 50900c3..ce6934b 100644 --- a/1vs1/src/vixikhd/onevsone/event/PlayerArenaWinEvent.php +++ b/1vs1/src/vixikhd/onevsone/event/PlayerArenaWinEvent.php @@ -1,5 +1,21 @@ player; diff --git a/1vs1/src/vixikhd/onevsone/event/PlayerEquipEvent.php b/1vs1/src/vixikhd/onevsone/event/PlayerEquipEvent.php new file mode 100644 index 0000000..9d1e35b --- /dev/null +++ b/1vs1/src/vixikhd/onevsone/event/PlayerEquipEvent.php @@ -0,0 +1,70 @@ +player = $player; + $this->arena = $arena; + parent::__construct($plugin); + } + + /** + * @return Player $player + */ + public function getPlayer(): Player { + return $this->player; + } + + /** + * @return Arena $arena + */ + public function getArena(): Arena { + return $this->arena; + } +} \ No newline at end of file diff --git a/1vs1/src/vixikhd/onevsone/math/Time.php b/1vs1/src/vixikhd/onevsone/math/Time.php index 4e7dd36..e672495 100644 --- a/1vs1/src/vixikhd/onevsone/math/Time.php +++ b/1vs1/src/vixikhd/onevsone/math/Time.php @@ -1,7 +1,7 @@ plugin = $plugin; $this->init(); - $this->loadArenas(); } public function init() { @@ -54,6 +56,11 @@ public function init() { if(!is_dir($this->getDataFolder() . "saves")) { @mkdir($this->getDataFolder() . "saves"); } + if(!is_file($this->getDataFolder() . "/config.yml")) { + $this->plugin->saveResource("/config.yml"); + } + $this->config = (new Config($this->getDataFolder() . "/config.yml", Config::YAML))->getAll(); + var_dump($this->config); } public function loadArenas() {