Skip to content

Commit

Permalink
Fix bed breaking + added player cache
Browse files Browse the repository at this point in the history
  • Loading branch information
boi1216 committed Mar 3, 2020
1 parent 16c2723 commit 7129ace
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 48 deletions.
16 changes: 6 additions & 10 deletions src/BedWars/BedWars.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class BedWars extends PluginBase
'emerald' => ['item' => Item::EMERALD, 'spawnText' => true, 'spawnBlock' => true, 'refreshRate' => 60]
];


public function onEnable() : void
{
@mkdir($this->getDataFolder());
Expand All @@ -75,14 +74,9 @@ public function onEnable() : void

$this->games[$jsonData['name']] = $game = new Game($this, $jsonData['name'], intval($jsonData['minPlayers']), intval($jsonData['playersPerTeam']), $jsonData['world'], $jsonData['lobby'], $jsonData['teamInfo'], $jsonData['generatorInfo']);

if(!empty($jsonData['lobby'])){
$split = explode(":", $jsonData['lobby']);
$game->setLobby(new Vector3(intval($split[0]), intval($split[1]), intval($split[2])), $split[3]);
}

if(!empty($jsonData['void_y'])){
$game->setVoidLimit(intval($jsonData['void_y']));
}
$split = explode(":", $jsonData['lobby']);
$game->setLobby(new Vector3(intval($split[0]), intval($split[1]), intval($split[2])), $split[3]);
$game->setVoidLimit(intval($jsonData['void_y']));
}

$this->getServer()->getCommandMap()->register("bedwars", new DefaultCommand($this));
Expand Down Expand Up @@ -145,7 +139,9 @@ public function validateGame(array $arenaData) : bool{
'lobby',
'world',
'teamInfo',
'generatorInfo'
'generatorInfo',
'lobby',
'void_y'
];

$error = 0;
Expand Down
49 changes: 11 additions & 38 deletions src/BedWars/game/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace BedWars\game;


use BedWars\game\player\PlayerCache;
use BedWars\game\Team;
use BedWars\utils\Utils;
use pocketmine\entity\Entity;
Expand Down Expand Up @@ -36,7 +37,6 @@ class Game
const STATE_RUNNING = 1;
const STATE_REBOOT = 2;


/** @var BedWars $plugin */
private $plugin;

Expand Down Expand Up @@ -112,14 +112,13 @@ class Game
/** @var string $tierUpdateGen */
private $tierUpdateGen = "diamond";

/** @var array $dynamicStats */
private $dynamicStats = array();

/** @var array $placedBlocks */
public $placedBlocks = array();

/** @var bool $needLoad */
private $needLoad = false;
/** @var PlayerCache[] $cachedPlayers */
private $cachedPlayers = array();




/**
Expand Down Expand Up @@ -241,27 +240,6 @@ public function broadcastMessage(string $message) : void{
* @return array
*/
public function getAliveTeams() : array{
/* $teams = [];
/* for($i = 1; $i < (count($this->teams)); $i++){
$players = [];
$team = array_values($this->teams)[$i];
foreach($team->getPlayers() as $p){
if(!$p->isOnline() || $p->level->getFolderName() !== $this->worldName){
$this->quit($p);
continue;
}
if($p->isAlive() && $team->hasBed()){
$players[] = $p;
}
}
if(count($players) >= 1){
$teams[] = $team->getName();
}
}*/
$teams = [];
foreach($this->teams as $team){
if(count($team->getPlayers()) <= 0 || !$team->hasBed())continue;
Expand All @@ -283,10 +261,7 @@ public function getAliveTeams() : array{

public function stop() : void{
foreach(array_merge($this->players, $this->spectators) as $player){
$player->setHealth($player->getMaxHealth());
$player->setFood(20);
$player->setGamemode(0);
$player->setNameTag($player->getName()); //TODO: save this before starting the game
$this->cachedPlayers[$player->getRawUniqueId()]->load();
\BedWars\utils\Scoreboard::remove($player);
}

Expand All @@ -309,12 +284,13 @@ public function stop() : void{
}
}

$this->spectators = [];
$this->players = [];
$this->spectators = array();
$this->players = array();
$this->winnerTeam = '';
$this->startTime = 30;
$this->rebootTime = 10;
$this->generators = array();
$this->cachedPlayers = array();
$this->state = self::STATE_LOBBY;
$this->starting = false;
$this->plugin->getServer()->unloadLevel($this->plugin->getServer()->getLevelByName($this->worldName));
Expand Down Expand Up @@ -418,11 +394,11 @@ public function join(Player $player) : void{
return;
}

$this->cachedPlayers[$player->getRawUniqueId()] = new PlayerCache($player);
$player->teleport($this->lobby);
$this->players[$player->getRawUniqueId()] = $player;

$this->broadcastMessage(TextFormat::GRAY . $player->getName() . " " . TextFormat::YELLOW . "has joined the game " . TextFormat::GOLD . "(" . TextFormat::AQUA . count($this->players) . TextFormat::YELLOW . "/" . TextFormat::AQUA . $this->maxPlayers . TextFormat::YELLOW . ")");

$player->getInventory()->clearAll();
$a = 0;
$items = array_fill(0, count($this->teams), Item::get(Item::WOOL));
Expand Down Expand Up @@ -476,7 +452,6 @@ public function trackCompass(Player $player) : void{
$player->getInventory()->setItem($slot, Item::get(Item::COMPASS)->setCustomName(TextFormat::GREEN . "Tap to switch"));
}
}

}

/**
Expand Down Expand Up @@ -739,9 +714,7 @@ public function tick() : void{
}




if(count($team = $this->getAliveTeams()) === 1){
if(count($team = $this->getAliveTeams()) === 1 && count($this->players) <= 1){
$this->winnerTeam = $team[0];

$this->state = self::STATE_REBOOT;
Expand Down
53 changes: 53 additions & 0 deletions src/BedWars/game/player/PlayerCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php


namespace BedWars\game\player;

use pocketmine\Player;
use pocketmine\level\Position;
use pocketmine\entity\EffectInstance;


class PlayerCache
{
/** @var Player $player */
private $player;
/** @var string $nametag */
private $nametag;
/** @var array $inventoryContents */
private $inventoryContents = array();
/** @var int $health */
private $health;
/** @var int $maxHealth */
private $maxHealth;
/** @var int $food */
private $food;
/** @var Position $position */
private $position;
/** @var EffectInstance[] $effects */
private $effects;

public function __construct(Player $player)
{
$this->nametag = $player->getNameTag();
$this->inventoryContents = $player->getInventory()->getContents();
$this->health = $player->getHealth();
$this->maxHealth = $player->getMaxHealth();
$this->food = $player->getMaxFood();
$this->position = $player->asPosition();
$this->effects = $player->getEffects();
}

public function load(){
$this->player->setNameTag($this->nametag);
$this->player->getInventory()->setContents($this->inventoryContents);
$this->player->setHealth($this->health);
$this->player->setMaxHealth($this->maxHealth);
$this->player->setFood($this->food);
$this->player->teleport($this->position);
foreach($this->effects as $effect){
$this->player->addEffect($effect);
}
}

}

0 comments on commit 7129ace

Please sign in to comment.