Skip to content

Commit

Permalink
1.0.1 update 👌
Browse files Browse the repository at this point in the history
- config
- new /1vs1 join command
- customizable kits
- new PlayerEquipEvent
  • Loading branch information
GamakCZ committed Feb 10, 2019
1 parent ff8ce5c commit aeafe6b
Show file tree
Hide file tree
Showing 12 changed files with 296 additions and 28 deletions.
7 changes: 5 additions & 2 deletions 1vs1/plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,4 +25,7 @@ permissions:
default: op
1vs1.cmd.arenas:
description: Permission for /1vs1 arenas
default: op
default: op
1vs1.cmd.join:
description: Permission for /1vs1 join
default: true
20 changes: 20 additions & 0 deletions 1vs1/resources/config.yml
Original file line number Diff line number Diff line change
@@ -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
...
96 changes: 96 additions & 0 deletions 1vs1/src/vixikhd/onevsone/EmptyArenaChooser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* Copyright 2018-2019 GamakCZ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace vixikhd\onevsone;

use vixikhd\onevsone\arena\Arena;

/**
* Class EmptyArenaChooser
* @package vixikhd\onevsone
*/
class EmptyArenaChooser {

/** @var OneVsOne $plugin */
public $plugin;

/**
* EmptyArenaQueue constructor.
* @param OneVsOne $plugin
*/
public function __construct(OneVsOne $plugin) {
$this->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)]];
}
}
27 changes: 24 additions & 3 deletions 1vs1/src/vixikhd/onevsone/OneVsOne.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2018 GamakCZ
* Copyright 2018-2019 GamakCZ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@
use pocketmine\event\block\BlockBreakEvent;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerChatEvent;
use pocketmine\Player;
use pocketmine\plugin\PluginBase;
use vixikhd\onevsone\arena\Arena;
use vixikhd\onevsone\commands\OneVsOneCommand;
Expand All @@ -39,6 +40,9 @@ class OneVsOne extends PluginBase implements Listener {
/** @var YamlDataProvider */
public $dataProvider;

/** @var EmptyArenaChooser $emptyArenaChooser */
public $emptyArenaChooser;

/** @var Command[] $commands */
public $commands = [];

Expand All @@ -51,9 +55,14 @@ class OneVsOne extends PluginBase implements Listener {
/** @var int[] $setupData */
public $setupData = [];

public function onLoad() {
$this->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));
}

Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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!");
}
}
49 changes: 38 additions & 11 deletions 1vs1/src/vixikhd/onevsone/arena/Arena.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2018 GamakCZ
* Copyright 2018-2019 GamakCZ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@
use pocketmine\Player;
use pocketmine\tile\Tile;
use vixikhd\onevsone\event\PlayerArenaWinEvent;
use vixikhd\onevsone\event\PlayerEquipEvent;
use vixikhd\onevsone\math\Vector3;
use vixikhd\onevsone\OneVsOne;

Expand Down Expand Up @@ -77,6 +78,9 @@ class Arena implements Listener {
/** @var Level $level */
public $level = null;

/** @var string $kit */
public $kit;

/**
* Arena constructor.
* @param OneVsOne $plugin
Expand Down Expand Up @@ -129,6 +133,8 @@ public function joinToArena(Player $player) {
}
}

$this->broadcastMessage("§a> Player {$player->getName()} joined the match! §7[".count($this->players)."/{$this->data["slots"]}]");

$player->getInventory()->clearAll();
$player->getArmorInventory()->clearAll();
$player->getCursorInventory()->clearAll();
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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 = [];
}
Expand Down
2 changes: 1 addition & 1 deletion 1vs1/src/vixikhd/onevsone/arena/ArenaScheduler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2018 GamakCZ
* Copyright 2018-2019 GamakCZ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
20 changes: 14 additions & 6 deletions 1vs1/src/vixikhd/onevsone/commands/OneVsOneCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Copyright 2018 GamakCZ
* Copyright 2018-2019 GamakCZ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -54,10 +54,6 @@ public function __construct(OneVsOne $plugin) {
* @return mixed|void
*/
public function execute(CommandSender $sender, string $commandLabel, array $args) {
if(!$sender->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;
Expand All @@ -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":
Expand Down Expand Up @@ -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!");
Expand Down
Loading

0 comments on commit aeafe6b

Please sign in to comment.