Skip to content

Commit

Permalink
mostly fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
boi1216 authored Apr 3, 2022
1 parent 7321c53 commit a08baaa
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 72 deletions.
128 changes: 69 additions & 59 deletions src/BedWars/command/DefaultCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@
class DefaultCommand extends \pocketmine\command\Command
{

private $commandInfo = [
'create' => ['desc' => "Create new game", 'usage' => "<game_id> <min_players> <players_per_team> <start_time> <map_name> <world_name>"],
'delete' => ['desc' => "Delete game", 'usage' => "<game_id>"],
'setlobby' => ['desc' => "Set lobby of a game", 'usage' => "<game_id>"],
'addteam' => ['desc' => "Add team to a game", 'usage' => "<game_id> <blue|red|yellow|green|aqua|gold|white> "],
'setpos' => ['desc' => "Set spawn & shop positions", 'usage' => "<game_id> <team_name> <1|2|3> - 3 = Player Spawn, 2 = Upgrade Shop, 1 = Item Shop"],
'addgenerator' => ['desc' => "Add generator", 'usage' => "<game_id> <iron|gold|diamond|emerald> "],
'setbed' => ['desc' => "Set team's bed", 'usage' => "<game_id> <team>"],
'addsafearea' => ['desc' => "Add area restricted for placing blocks", 'usage' => "<game_id>"],
'load' => ['desc' => "Load arena after finishing setup", 'usage' => '<game_id>'],
'join' => ['desc' => "Join arena by id", 'usage' => '<game_id>']
];

/**
* DefaultCommand constructor.
* @param BedWars $owner
Expand Down Expand Up @@ -58,7 +71,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
switch($args[0]){
case 'create';
if(count($args) < 7){
$sender->sendMessage(TextFormat::RED . ":create <game_id> <min_players> <players_per_team> <start_time> <map_name> <world_name>");
$sender->sendMessage($this->getSubUsage('create'));
return;
}
$game_id = $args[1];
Expand Down Expand Up @@ -89,20 +102,20 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
}

if(strlen($map_name) < 1){
$sender->sendMessage(TextFormat::RED . "map_name - too short!");
$sender->sendMessage(TextFormat::RED . "map_name - name too short!");
}

$world = $this->getPlugin()->getServer()->getWorldManager()->getWorldByName($world_name);
if(!$world instanceof World){
$sender->sendMessage("World " . $world_name . " does not exist");
$sender->sendMessage(TextFormat::RED . "World " . $world_name . " does not exist");
return;
}
$sender->sendMessage(TextFormat::GREEN . "Game created");
$this->getPlugin()->createGame($game_id, $min_players, $players_per_team, $start_time, $map_name, $world_name);
break;
case 'delete';
if(count($args) < 1){
$sender->sendMessage(":delete <game_id>");
if(count($args) < 2){
$sender->sendMessage($this->getSubUsage('delete'));
return;
}
$game_id = $args[1];
Expand All @@ -112,15 +125,15 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
return;
}

$sender->sendMessage($game_id . " - game not found");
$sender->sendMessage(TextFormat::RED . $game_id . " - game not found");
break;
case 'setlobby';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}
if(count($args) < 2){
$sender->sendMessage(":setlobby <game_id>");
$sender->sendMessage($this->getSubUsage('setlobby'));
return;
}

Expand All @@ -132,32 +145,32 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
}
break;
case 'setposition';
case 'setpos';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}

if(count($args) < 4){
$sender->sendMessage(":setposition <game_id> <team> <1|2|3>");
$sender->sendMessage("1 - Shop Classic, 2 - Shop Upgrade, 3 - Team Spawn");
$sender->sendMessage($this->getSubUsage('setpos'));
return;
}

$game_id = $args[1];
if(!$this->getPlugin()->gameExists($game_id)){
$sender->sendMessage($game_id . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $game_id . " - Invalid game id");
return;
}
$team = $args[2];

if(!$this->getPlugin()->teamExists($game_id, $team)){
$sender->sendMessage("Invalid team - " . $team);
$sender->sendMessage(TextFormat::RED . "Invalid team - " . $team);
return;
}

$positionType = array(1 => 'Shop', 2 => 'Upgrade', 3 => 'Spawn')[intval($args[3])];
if($positionType == null){
$sender->sendMessage("Invalid position");
$sender->sendMessage(TextFormat::RED . "Invalid position");
return;
}
$pos = $sender->getPosition();
Expand All @@ -166,74 +179,73 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
break;
case 'setbed';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}

if(count($args) < 2){
$sender->sendMessage(":setbed <game_id> <team>");
$sender->sendMessage($this->getSubUsage('setbed'));
return;
}

$game_id = $args[1];
if(!$this->getPlugin()->gameExists($game_id)){
$sender->sendMessage($game_id . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $game_id . " - Invalid game id");
return;
}

$team = strtolower($args[2]);
if(!$this->getPlugin()->teamExists($game_id, $team)){
$sender->sendMessage("Invalid team - " . $team);
$sender->sendMessage(TextFormat::RED . "Invalid team - " . $team);
return;
}
$this->getPlugin()->bedSetup[$sender->getName()] = ['game' => $game_id, 'team' => $team , 'step' => 1];
$sender->sendMessage(TextFormat::GREEN . "Please touch the 1st part of the bed");
break;
case 'addteam';
if(count($args) < 2){
$sender->sendMessage(TextFormat::RED . ":addteam <game_id> <team>");
$sender->sendMessage($this->getSubUsage('addteam'));
return;
}
$game_id = $args[1];
if(!$this->getPlugin()->gameExists($game_id)){
$sender->sendMessage($game_id . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $game_id . " - Invalid game id");
return;
}
$team = strtolower($args[2]);
if(!isset(BedWars::TEAMS[$team])){
$sender->sendMessage($team . " - Invalid team");
$sender->sendMessage("Available: " . implode(" ", array_keys(BedWars::TEAMS)));
$sender->sendMessage(TextFormat::RED . $team . " - Invalid team");
$sender->sendMessage(TextFormat::RED . "Available: " . implode(" ", array_keys(BedWars::TEAMS)));
return;
}
$this->getPlugin()->addTeam($game_id, $team);
break;
case 'addgenerator';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}
if(count($args) < 3){
$sender->sendMessage(":addgenerator <game_id> <generator> (team)");
$sender->sendMessage($this->getSubUsage('addgenerator'));
return;
}

$game_id = $args[1];
if(!$this->getPlugin()->gameExists($game_id)){
$sender->sendMessage($game_id . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $game_id . " - Invalid game id");
return;
}

$generatorTypes = array(1 => 'iron', 2 => 'gold', 3 => 'diamond', 4 => 'emerald');
if(!isset($generatorTypes[intval($args[2])])){
$sender->sendMessage($args[2] . " - Invalid generator type");
$sender->sendMessage("Available: 1 - iron, 2 - gold, 3 - diamond, 4 - emerald");
$generatorTypes = array('iron', 'gold', 'diamond', 'emerald');
if(!in_array($args[2], $generatorTypes)){
$sender->sendMessage(TextFormat::RED . $args[2] . " - Invalid generator type");
return;
}
$generatorType = $generatorTypes[intval($args[2])];
$generatorType = $args[2];
$pos = $sender->getPosition();
$team = isset($args[3]) ? $args[3] : "";
if(!$this->getPlugin()->teamExists($game_id, $team) && $team !== ""){
$sender->sendMessage("Invalid team - " . $team);
$sender->sendMessage(TextFormat::RED . "Invalid team - " . $team);
return;
}
$this->getPlugin()->addGenerator($game_id, $team, $generatorType, $pos->getX(), $pos->getY(), $pos->getZ(), $team);
Expand All @@ -242,18 +254,18 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
break;
case 'addsafearea';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}

if(count($args) < 2){
$sender->sendMessage(":addsafearea <game_id> <ignored_block_ids>");
$sender->sendMessage($this->getSubUsage('addsafearea'));
return;
}

$game_id = $args[1];
if(!$this->getPlugin()->gameExists($game_id)){
$sender->sendMessage($game_id . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $game_id . " - Invalid game id");
return;
}

Expand All @@ -266,8 +278,8 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
}else{
if(!strpos($args[2], ':')){
err:
$sender->sendMessage("Invalid format of ignored blocks!");
$sender->sendMessage("Example: addsafearea <game_id> 365:2,13,1:0");
$sender->sendMessage(TextFormat::RED . "Invalid format of ignored blocks!");
$sender->sendMessage(TextFormat::YELLOW . "Example: <game_id> 365:2,13,1:0");
return;
}
$e = explode(":", $args[2]);
Expand Down Expand Up @@ -313,16 +325,17 @@ public function execute(CommandSender $sender, string $commandLabel, array $args

if(!$block instanceof Block){
invalid:
$sender->sendMessage($blockID . " is not a valid block id");
$sender->sendMessage(TextFormat::RED . $blockID . " is not a valid block id");
return;
}
}
}
$this->getPlugin()->saSetup[$sender->getName()] = ['step' => 1, 'pos1' => null, 'pos2' => null, 'ignoredIds' => implode(",", $ignored), 'game_id' => $game_id];
$sender->sendMessage(TextFormat::GREEN . "Break a block to set the 1st position");
break;
case 'load';
if(count($args) < 1){
$sender->sendMessage(":load <game_id>");
if(count($args) < 2){
$sender->sendMessage($this->getSubUsage('load'));
return;
}

Expand All @@ -332,7 +345,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
}

if(!$this->getPlugin()->validateGame($gameData)){
$sender->sendMessage("Setup not finished for game - " . $args[1]);
$sender->sendMessage(TextFormat::RED . "Setup not finished for game - " . $args[1]);
return;
}

Expand All @@ -356,15 +369,16 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
break;
case 'join';
if(!$sender instanceof Player){
$sender->sendMessage("in-game command");
$sender->sendMessage(TextFormat::GREEN . "This command can be used only in-game");
return;
}
if(count($args) < 1){
$sender->sendMessage(":join <game_id>");
if(count($args) < 2){
$sender->sendMessage($this->getSubUsage('join'));
return;
}

if(!$this->getPlugin()->gameExists($args[1])){
$sender->sendMessage($args[1] . " - Invalid game id");
$sender->sendMessage(TextFormat::RED . $args[1] . " - Invalid game id");
return;
}
$this->getPlugin()->games[$args[1]]->join($sender);
Expand All @@ -377,27 +391,23 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
$this->getPlugin()->games[$random]->join($sender);*/
break;
}
}


private function getSubUsage($command){
return TextFormat::RED . "Usage: " . $this->commandInfo[$command]['usage'];
}

/**
* @param CommandSender $sender
*/
private function sendHelp(CommandSender $sender) {
$sender->sendMessage("BedWars commands");
$sender->sendMessage("Setup - ");
$sender->sendMessage(":create - Create new arena");
$sender->sendMessage(":delete - Delete existing arena");
$sender->sendMessage(":setlobby - Set lobby position");
$sender->sendMessage(":setposition - Set other locations");
$sender->sendMessage(":setbed - Set team's bed position");
$sender->sendMessage(":setgenerator - Set generator position");
$sender->sendMessage(":addsafearea - Create area restricted for placing blocks (for example team's spawn)");
$sender->sendMessage(":load - Load game after finishing setup");
$sender->sendMessage("Other - ");
$sender->sendMessage(":list - Info about existing arenas");
$sender->sendMessage(":join - Join arena via command");
$sender->sendMessage(":joinrandom - Join random arena prioritized by players count etc..");
$sender->sendMessage(TextFormat::BOLD . TextFormat::YELLOW . "BedWars Commands");
$sender->sendMessage(TextFormat::GRAY . "Type the sub-command for usage info");
$sender->sendMessage(TextFormat::BOLD . TextFormat::WHITE . "[] - required parameters");
$sender->sendMessage(TextFormat::BOLD . TextFormat::WHITE . "[] - optional parameters");
foreach($this->commandInfo as $command => $info){
$sender->sendMessage(TextFormat::RED . $command . TextFormat::GRAY . " - " . $info['desc']);
}

}
}
9 changes: 2 additions & 7 deletions src/BedWars/game/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use BedWars\game\entity\FakeItemEntity;
use BedWars\utils\Utils;
use BedWars\BedWars;
use pocketmine\entity\Entity;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
Expand Down Expand Up @@ -95,7 +96,7 @@ public function __construct(int $itemID, int $repeatRate, Position $position, bo
}

if($this->spawnBlock){
$path = Server::getInstance()->getDataPath() . "plugin_data/BedWars/skins/" . $itemID . ".png";
$path = BedWars::getInstance()->getDataFolder() . "/skins/" . $itemID . ".png";
$skin = Utils::getSkinFromFile($path, 'geometry.player_head', FakeItemEntity::GEOMETRY);
$position->add(0.5, 2.3, 0.5);
$fakeItem = new FakeItemEntity(new Location($position->getX() + 0.5, $position->getY() + 2.3, $position->getZ() + 0.5, $position->getWorld(), 0, 0), $skin);
Expand Down Expand Up @@ -127,8 +128,6 @@ public function setMultiply(int $multiply) : void{
public function tick() : void{
if($this->team instanceof Team){
if(count($this->team->getPlayers()) <= $this->team->dead){
/*var_dump($this->team->dead);
var_dump(count($this->team->getPlayers())) */
return;
}
}
Expand All @@ -148,10 +147,6 @@ public function tick() : void{

if($this->dynamicSpawnTime == 0){
$this->dynamicSpawnTime = $this->repeatRate;
if($this->itemID == ItemIds::EMERALD && $this->multiply == 200 && $this->team instanceof Team){
$this->setRepeatRate($this->getRepeatRate() / 4);
return;
}
if($this->multiply == 50){
if($this->multiplied == 2){
$this->multiplied = 0;
Expand Down
Loading

0 comments on commit a08baaa

Please sign in to comment.