Skip to content

Commit

Permalink
Merge pull request #33 from Laith98Dev/master
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
boi1216 authored Apr 5, 2022
2 parents 6338528 + 6694b25 commit 504d0bd
Show file tree
Hide file tree
Showing 8 changed files with 591 additions and 416 deletions.
5 changes: 4 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ version: "2.0pre-release"
api: 4.2.3
main: BedWars\BedWars
author: boi1216
dependencies: ["FormAPI"]

dependencies: ["FormAPI"]
permissions:
bedwars.command.start:
default: op
832 changes: 458 additions & 374 deletions src/BedWars/command/DefaultCommand.php

Large diffs are not rendered by default.

66 changes: 48 additions & 18 deletions src/BedWars/game/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ class Game
/** @var array $safeAreas */
private $safeAreas = array();

/** @var bool $forceStart */
private $forceStart = false;

/**
* Game constructor.
* @param BedWars $plugin
Expand Down Expand Up @@ -201,6 +204,24 @@ public function getMapName() : string{
return $this->mapName;
}

public function getStartTime(): int{
return $this->startTime;
}

public function setStartTime(int $newVal){
if($this->startTime > $newVal){
$this->startTime = $newVal;
}
}

public function isForcedStart(): bool{
return $this->forceStart;
}

public function setForcedStart(bool $newVal){
$this->forceStart = $newVal;
}

/**
* @return int
*/
Expand Down Expand Up @@ -298,6 +319,7 @@ public function stop() : void{
$this->cachedPlayers = array();
$this->state = self::STATE_LOBBY;
$this->starting = false;
$this->forceStart = false;
$this->plugin->getServer()->getWorldManager()->unloadWorld($this->plugin->getServer()->getWorldManager()->getWorldByName($this->worldName));
$this->reload();

Expand Down Expand Up @@ -357,7 +379,7 @@ private function initGenerators() : void{
$delay = $generatorData['refreshRate'];

$vector = Utils::stringToVector(":", $generator['position']);
$position = new Position($vector->x, $vector->y, $vector->z,$this->plugin->getServer()->getWorldManager()->getWorldByName($this->worldName));
$position = new Position($vector->x + 0.5, $vector->y, $vector->z + 0.5, $this->plugin->getServer()->getWorldManager()->getWorldByName($this->worldName));

$this->generators[] = new Generator($item, $delay,$position, $spawnText, $spawnBlock, $generator['team'] == "" ? null : $this->teams[$generator['team']]);
}
Expand Down Expand Up @@ -409,8 +431,14 @@ public function join(Player $player) : void{

$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();
$player->getArmorInventory()->clearAll();
$player->getCraftingGrid()->clearAll();
$player->getOffHandInventory()->clearAll();
foreach($this->teams as $team){
$player->getInventory()->addItem($i =new Item(new ItemIdentifier(ItemIds::WOOL, Utils::colorIntoWool($team->getColor()))));
// $player->getInventory()->addItem($i = new Item(new ItemIdentifier(ItemIds::WOOL, Utils::colorIntoWool($team->getColor()))));
$item = ItemFactory::getInstance()->get(ItemIds::WOOL, Utils::colorIntoWool($team->getColor()));
$item->setCustomName($team->getColor() . ucfirst($team->getName()) . "'s " . TextFormat::WHITE . "Team");
$player->getInventory()->addItem($item);
}
$player->getInventory()->setItem(8, ItemFactory::getInstance()->get(ItemIds::COMPASS)->setCustomName(TextFormat::YELLOW . "Leave"));
$player->setGamemode(GameMode::ADVENTURE());
Expand All @@ -427,7 +455,7 @@ public function join(Player $player) : void{
Scoreboard::setLine($player, 7, " " . TextFormat::WHITE . "Mode: " . TextFormat::GREEN . substr(str_repeat($this->playersPerTeam . "v", count($this->teams)), 0, -1) . str_repeat(" ", 3));
Scoreboard::setLine($player, 8, " " . TextFormat::WHITE . "Version: " . TextFormat::GRAY . "v1.0" . str_repeat(" ", 3));
Scoreboard::setLine($player, 9, " ");
Scoreboard::setLine($player, 10, " " . TextFormat::YELLOW . "www.example.net");
Scoreboard::setLine($player, 10, " " . TextFormat::YELLOW . $this->plugin->serverWebsite);
}

/**
Expand Down Expand Up @@ -516,7 +544,7 @@ public function quit(Player $player) : void{
}

private function checkLobby() : void{
if(!$this->starting && count($this->players) >= $this->minPlayers) {
if(!$this->starting && count($this->players) >= $this->minPlayers && !$this->isForcedStart()) {
$this->starting = true;
$this->broadcastMessage(TextFormat::GREEN . "Countdown started");
}
Expand All @@ -543,21 +571,22 @@ public function killPlayer(Player $player) : void{
$this->deadQueue[$player->getName()] = 5;
}

$playerGame = $this->plugin->getPlayerTeam($player);
$cause = $player->getLastDamageCause();
if($cause == null)return;
if($cause == null || $playerGame == null) return;
switch($cause->getCause()){
case EntityDamageEvent::CAUSE_ENTITY_ATTACK;
if($cause instanceof EntityDamageByEntityEvent){
$damager = $cause->getDamager();
if($damager instanceof Player){
$this->broadcastMessage($this->plugin->getPlayerTeam($player)->getColor() . $player->getName() . " " . TextFormat::GRAY . "was killed by " . $this->plugin->getPlayerTeam($damager)->getColor() . $damager->getName());
if($damager instanceof Player && $this->plugin->getPlayerTeam($damager) !== null){
$this->broadcastMessage($this->plugin->getPlayerTeam($player)->getColor() . $player->getName() . " " . TextFormat::GRAY . "was killed by " . $this->plugin->getPlayerTeam($damager)->getColor() . $damager->getName());
}
}
break;
case EntityDamageEvent::CAUSE_PROJECTILE;
if($cause instanceof EntityDamageByChildEntityEvent){
$damager = $cause->getDamager();
if($damager instanceof Player){
if($damager instanceof Player && $this->plugin->getPlayerTeam($damager) !== null){
$this->broadcastMessage($this->plugin->getPlayerTeam($player)->getColor() . $player->getName() . " " . TextFormat::GRAY . "was shot by " . $this->plugin->getPlayerTeam($damager)->getColor() . $damager->getName());
}
}
Expand Down Expand Up @@ -663,8 +692,8 @@ public function respawnPlayer(Player $player) : void{
public function tick() : void{
switch($this->state) {
case self::STATE_LOBBY;
if ($this->starting) {
if(count($this->players) < $this->minPlayers) {
if ($this->starting || $this->isForcedStart()) {
if(count($this->players) < $this->minPlayers && !$this->isForcedStart()) {
$this->starting = false;
$this->broadcastMessage(TextFormat::YELLOW . "Countdown stopped");
$this->startTime = $this->startTimeStatic;
Expand Down Expand Up @@ -710,12 +739,12 @@ public function tick() : void{
\BedWars\utils\Scoreboard::setLine($player, 2, " " . TextFormat::WHITE . "Map: " . TextFormat::GREEN . $this->mapName . str_repeat(" ", 3));
\BedWars\utils\Scoreboard::setLine($player, 3, " " . TextFormat::WHITE . "Players: " . TextFormat::GREEN . count($this->players) . "/" . $this->maxPlayers . str_repeat(" ", 3));
\BedWars\utils\Scoreboard::setLine($player, 4, " ");
\BedWars\utils\Scoreboard::setLine($player, 5, " " . ($this->starting ? TextFormat::WHITE . "Starting in " . TextFormat::GREEN . $this->startTime . str_repeat(" ", 3) : TextFormat::GREEN . "Waiting for players..." . str_repeat(" ", 3)));
\BedWars\utils\Scoreboard::setLine($player, 5, " " . ($this->starting || $this->isForcedStart() ? TextFormat::WHITE . "Starting in " . TextFormat::GREEN . $this->startTime . str_repeat(" ", 3) : TextFormat::GREEN . "Waiting for players..." . str_repeat(" ", 3)));
\BedWars\utils\Scoreboard::setLine($player, 6, " ");
\BedWars\utils\Scoreboard::setLine($player, 7, " " . TextFormat::WHITE . "Mode: " . TextFormat::GREEN . substr(str_repeat($this->playersPerTeam . "v", count($this->teams)), 0, -1) . str_repeat(" ", 3));
\BedWars\utils\Scoreboard::setLine($player, 8, " " . TextFormat::WHITE . "Version: " . TextFormat::GRAY . "v1.0" . str_repeat(" ", 3));
\BedWars\utils\Scoreboard::setLine($player, 9, " ");
\BedWars\utils\Scoreboard::setLine($player, 10, " " . TextFormat::YELLOW . "www.example.net");
\BedWars\utils\Scoreboard::setLine($player, 10, " " . TextFormat::YELLOW . $this->plugin->serverWebsite);
}

break;
Expand Down Expand Up @@ -773,7 +802,7 @@ public function tick() : void{
}
\BedWars\utils\Scoreboard::setLine($player, " " . $currentLine, " ");
$currentLine++;
\BedWars\utils\Scoreboard::setLine($player, " " . $currentLine, " " . TextFormat::YELLOW . "www.example.net");
\BedWars\utils\Scoreboard::setLine($player, " " . $currentLine, " " . TextFormat::YELLOW . $this->plugin->serverWebsite);
}


Expand Down Expand Up @@ -811,11 +840,12 @@ public function tick() : void{
Scoreboard::new($player, 'bedwars', TextFormat::BOLD . TextFormat::YELLOW . "Bed Wars");
Scoreboard::setLine($player, 1, " ");
Scoreboard::setLine($player, 2, "Winner team: " . TextFormat::GREEN . $this->winnerTeam->getName());
Scoreboard::setLine($player, 3, "Thanks for playing!");
Scoreboard::setLine($player, 4, " ");
Scoreboard::setLine($player, 5, "Restart in " . TextFormat::GREEN . $this->rebootTime);
Scoreboard::setLine($player, 6, " ");
Scoreboard::setLine($player, 7, " " . TextFormat::YELLOW . "www.example.net");
Scoreboard::setLine($player, 3, " ");
Scoreboard::setLine($player, 4, "Thanks for playing!");
Scoreboard::setLine($player, 5, " ");
Scoreboard::setLine($player, 6, "Restart in " . TextFormat::GREEN . $this->rebootTime);
Scoreboard::setLine($player, 7, " ");
Scoreboard::setLine($player, 8, " " . TextFormat::YELLOW . $this->plugin->serverWebsite);
}


Expand Down
45 changes: 42 additions & 3 deletions src/BedWars/game/GameListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use pocketmine\entity\effect\VanillaEffects;
use pocketmine\entity\effect\EffectInstance;
use pocketmine\event\player\PlayerCommandPreprocessEvent;

use pocketmine\event\player\PlayerDropItemEvent;
use pocketmine\item\enchantment\ItemFlags;
use pocketmine\item\ItemFactory;

class GameListener implements Listener
{
Expand Down Expand Up @@ -69,6 +71,14 @@ public function onSignChange(SignChangeEvent $event) : void{
return;
}
$pos = $sign->getPosition();
$pos_ = $pos->getX() . ":" . $pos->getY() . ":" . $pos->getZ() . ":" . $player->getWorld()->getFolderName();

foreach ($this->signs[$text->getLine(1)] as $key => $val){
if($val == $pos_){
return;
}
}

$this->plugin->createSign($text->getLine(1), $pos->getX(), $pos->getY(), $pos->getZ(), $player->getWorld()->getFolderName());
$player->sendMessage(BedWars::PREFIX . TextFormat::GREEN . "Sign created");

Expand Down Expand Up @@ -329,7 +339,7 @@ public function onBreak(BlockBreakEvent $event) : void

if($teamName !== ""){
$teamObject = $game->teams[$name];
if($name == $this->plugin->getPlayerTeam($player)->getName()){
if($teamName == $this->plugin->getPlayerTeam($player)->getName()){
$player->sendMessage(TextFormat::RED . "You can't break your bed!");
$event->cancel();
return;
Expand All @@ -347,6 +357,15 @@ public function onBreak(BlockBreakEvent $event) : void
}
}
}
} else {
foreach ($this->plugin->games as $arena){
if($arena->worldName == $player->getWorld()->getFolderName()){
if(!isset($arena->getPlayers()[$player->getName()])){
$event->cancel();
return;
}
}
}
}
}

Expand Down Expand Up @@ -386,8 +405,18 @@ public function onPlace(BlockPlaceEvent $event) : void{
}

if($event->getBlock()->getId() == BlockLegacyIds::CHEST && !$isCancelled && $player->getInventory()->getItemInHand()->getCustomName() == TextFormat::AQUA . "Popup Tower"){
$player->getInventory()->removeItem(ItemFactory::getInstance()->get(BlockLegacyIds::CHEST, 0, 1));
$event->cancel();
new PopupTower($event->getBlock(), $playerGame, $player, $this->plugin->getPlayerTeam($player));
(new PopupTower($event->getBlock(), $playerGame, $player, $this->plugin->getPlayerTeam($player)));
}
}
} else {
foreach ($this->plugin->games as $arena){
if($arena->worldName == $player->getWorld()->getFolderName()){
if(!isset($arena->getPlayers()[$player->getName()])){
$event->cancel();
return;
}
}
}
}
Expand Down Expand Up @@ -474,6 +503,16 @@ public function onCommandPreprocess(PlayerCommandPreprocessEvent $event) : void{
}
}

public function onDrop(PlayerDropItemEvent $event){
$player = $event->getPlayer();
if(!$player instanceof Player) return;
if(($arena = $this->plugin->getPlayerGame($player)) !== null){
if($arena->getState() == Game::STATE_LOBBY || $arena->getState() == Game::STATE_REBOOT){
$event->cancel();
}
}
}



/**
Expand Down
15 changes: 10 additions & 5 deletions src/BedWars/game/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,17 @@ public function __construct(int $itemID, int $repeatRate, Position $position, bo
if($this->spawnBlock){
$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);
$fakeItem->setScale(1.4);
$fakeItem->spawnToAll();

$this->blockEntity = $fakeItem;
if($skin == null){
BedWars::getInstance()->getLogger()->error("'" . $path . "' not exist!");
} else {
$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);
$fakeItem->setScale(1.4);
$fakeItem->spawnToAll();

$this->blockEntity = $fakeItem;
}
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/BedWars/game/entity/BridgeEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use pocketmine\entity\Location;

use BedWars\BedWars;
use BedWars\game\Game;
use BedWars\game\Team;
use BedWars\utils\Utils;

Expand Down Expand Up @@ -59,11 +60,13 @@ protected function move(float $dx, float $dy, float $dz) : void{
parent::move($dx, $dy, $dz);
return;
}
$game = BedWars::getInstance()->getPlayerGame($this->getOwningEntity());
$team = BedWars::getInstance()->getPlayerTeam($this->getOwningEntity());
if(!$team instanceof Team){
if(!$team instanceof Team || !$game instanceof Game){
parent::move($dx, $dy, $dz);
return;
}

$world = $this->getWorld();
$pos = $this->getPosition();
$placePos = $pos->asVector3()->subtract(0, 1, 0);
Expand All @@ -79,11 +82,18 @@ protected function move(float $dx, float $dy, float $dz) : void{

parent::move($dx, $dy, $dz);
if($this->skippedFirst){ //simple skip for players position
$world->setBlock($placePos, BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
$world->setBlock($placePos->subtract(0, 0, 1), BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
$world->setBlock($placePos->subtract(1, 0, 0), BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
$world->setBlock($placePos->add(1, 0, 0), BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
$world->setBlock($placePos->add(0, 0, 1), BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
foreach ([
$placePos,
$placePos->subtract(0, 0, 1),
$placePos->subtract(1, 0, 0),
$placePos->add(1, 0, 0),
$placePos->add(0, 0, 1)
] as $pos){
if($world->getBlock($pos)->getId() !== BlockLegacyIds::BED_BLOCK){
$world->setBlock($pos, BlockFactory::getInstance()->get(BlockLegacyIds::WOOL, Utils::colorIntoWool($team->getColor())));
$game->placedBlocks[] = Utils::vectorToString(":", $world->getBlock($pos)->getPosition()->asVector3());
}
}
}
$this->skippedFirst = true;
}
Expand Down
5 changes: 4 additions & 1 deletion src/BedWars/game/entity/FakeItemEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public function onUpdate(int $currentTick): bool
if($this->getLocation()->yaw >= 360){
$this->getLocation()->yaw = 0;
}
$this->getLocation()->yaw+=5.5;

$this->getLocation()->yaw+=5.5;

$this->updateMovement();
$this->scheduleUpdate();

//TODO: Add bouncing
$this->move($this->motion->x, $this->motion->y, $this->motion->z);

Expand Down
17 changes: 9 additions & 8 deletions src/BedWars/utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class Utils
{

const WOOL_COLOR = [
'§a' => 5,
'§c' => 14,
'§e' => 4,
'§6' => 1,
'§f' => 0,
'§b' => 3,
'§1' => 11
TextFormat::GREEN => 5,
TextFormat::RED => 14,
TextFormat::YELLOW => 4,
TextFormat::GOLD => 1,
TextFormat::WHITE => 0,
TextFormat::AQUA => 3,
TextFormat::DARK_BLUE => 11
];

public static function colorIntoWool(string $color) : int{
Expand Down Expand Up @@ -128,7 +128,8 @@ public static function rome($number){
* @return Skin|null
*/
public static function getSkinFromFile(string $path, $geometryName = null, $geometryData = null) : ?Skin{

if(!file_exists($path))
return null;
$img = @imagecreatefrompng($path);
$bytes = '';
$l = (int) @getimagesize($path)[1];
Expand Down

0 comments on commit 504d0bd

Please sign in to comment.