Skip to content

Commit 4e659d5

Browse files
committed
Fix players receiving multiple messages when joining a game
1 parent 11ddd0b commit 4e659d5

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

src/sergittos/bedwars/game/entity/shop/Villager.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ protected function getInitialDragMultiplier(): float {
3939
return 0.02;
4040
}
4141

42+
public function canSaveWithChunk(): bool {
43+
return true;
44+
}
45+
4246
protected function initEntity(CompoundTag $nbt): void {
4347
parent::initEntity($nbt);
4448

@@ -51,6 +55,8 @@ public function attack(EntityDamageEvent $source): void {
5155
return;
5256
}
5357

58+
$source->cancel();
59+
5460
$damager = $source->getDamager();
5561
if($damager instanceof Player and SessionFactory::hasSession($damager)) {
5662
$damager->sendForm($this->getForm(SessionFactory::getSession($damager)));

src/sergittos/bedwars/game/stage/StartingStage.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77

88

99
use pocketmine\world\sound\ClickSound;
10-
use sergittos\bedwars\session\scoreboard\WaitingScoreboard;
10+
use sergittos\bedwars\game\stage\trait\JoinableTrait;
1111
use sergittos\bedwars\session\Session;
12-
use sergittos\bedwars\utils\ConfigGetter;
1312
use sergittos\bedwars\utils\GameUtils;
1413
use function str_replace;
15-
use function strtoupper;
1614

1715
class StartingStage extends Stage {
16+
use JoinableTrait {
17+
onJoin as onSessionJoin;
18+
onQuit as onSessionQuit;
19+
}
1820

1921
private int $time = 10;
2022

@@ -23,21 +25,13 @@ public function getTime(): int {
2325
}
2426

2527
public function onJoin(Session $session): void {
26-
$session->showBossBar("{YELLOW}Playing {WHITE}BED WARS {YELLOW}on {GREEN}" . strtoupper(ConfigGetter::getIP()));
27-
$session->getPlayer()->getEffects()->clear();
28-
$session->giveWaitingItems();
29-
$session->setGame($this->game);
30-
$session->setScoreboard(new WaitingScoreboard());
31-
$session->teleportToWaitingWorld();
32-
33-
$this->game->broadcastMessage(
34-
"{GRAY}" . $session->getUsername() . " {YELLOW}has joined ({AQUA}" .
35-
$this->game->getPlayersCount() . "{YELLOW}/{AQUA}" . $this->game->getMap()->getMaxCapacity() . "{YELLOW})!"
36-
);
28+
if(!$this->justStarted()) {
29+
$this->onSessionJoin($session);
30+
}
3731
}
3832

3933
public function onQuit(Session $session): void {
40-
$this->game->broadcastMessage("{GRAY}" . $session->getUsername() . " {YELLOW}has quit!");
34+
$this->onSessionQuit($session);
4135

4236
if(!$this->game->isFull()) {
4337
$this->game->setStage(new WaitingStage());
@@ -78,4 +72,8 @@ private function broadcastCountdownTitle(): void {
7872
$this->game->broadcastSound(new ClickSound());
7973
}
8074

75+
private function justStarted(): bool {
76+
return $this->time === 10;
77+
}
78+
8179
}

src/sergittos/bedwars/game/stage/WaitingStage.php

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,24 @@
77

88

99
use sergittos\bedwars\game\Game;
10-
use sergittos\bedwars\session\scoreboard\WaitingScoreboard;
10+
use sergittos\bedwars\game\stage\trait\JoinableTrait;
1111
use sergittos\bedwars\session\Session;
12-
use sergittos\bedwars\utils\ConfigGetter;
13-
use function strtoupper;
1412

1513
class WaitingStage extends Stage {
14+
use JoinableTrait {
15+
onJoin as onSessionJoin;
16+
}
1617

1718
public function start(Game $game): void {
1819
$this->game = $game;
1920
}
2021

2122
public function onJoin(Session $session): void {
22-
$session->showBossBar("{YELLOW}Playing {WHITE}BED WARS {YELLOW}on {GREEN}" . strtoupper(ConfigGetter::getIP()));
23-
$session->getPlayer()->getEffects()->clear();
24-
$session->giveWaitingItems();
25-
$session->setGame($this->game);
26-
$session->setScoreboard(new WaitingScoreboard());
27-
$session->teleportToWaitingWorld();
28-
29-
$this->game->broadcastMessage(
30-
"{GRAY}" . $session->getUsername() . " {YELLOW}has joined ({AQUA}" .
31-
$this->game->getPlayersCount() . "{YELLOW}/{AQUA}" . $this->game->getMap()->getMaxCapacity() . "{YELLOW})!"
32-
);
23+
$this->onSessionJoin($session);
24+
$this->startIfReady();
25+
}
3326

27+
private function startIfReady(): void {
3428
$map = $this->game->getMap();
3529
$count = $this->game->getPlayersCount();
3630

@@ -39,10 +33,6 @@ public function onJoin(Session $session): void {
3933
}
4034
}
4135

42-
public function onQuit(Session $session): void {
43-
$this->game->broadcastMessage("{GRAY}" . $session->getUsername() . " {YELLOW}has quit!");
44-
}
45-
4636
public function tick(): void {}
4737

4838
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
4+
namespace sergittos\bedwars\game\stage\trait;
5+
6+
7+
use sergittos\bedwars\session\scoreboard\WaitingScoreboard;
8+
use sergittos\bedwars\session\Session;
9+
use sergittos\bedwars\utils\ConfigGetter;
10+
use function strtoupper;
11+
12+
trait JoinableTrait {
13+
14+
public function onJoin(Session $session): void {
15+
$session->showBossBar("{YELLOW}Playing {WHITE}BED WARS {YELLOW}on {GREEN}" . strtoupper(ConfigGetter::getIP()));
16+
$session->getPlayer()->getEffects()->clear();
17+
$session->giveWaitingItems();
18+
$session->setGame($this->game);
19+
$session->setScoreboard(new WaitingScoreboard());
20+
$session->teleportToWaitingWorld();
21+
22+
$this->game->broadcastMessage(
23+
"{GRAY}" . $session->getUsername() . " {YELLOW}has joined ({AQUA}" .
24+
$this->game->getPlayersCount() . "{YELLOW}/{AQUA}" . $this->game->getMap()->getMaxCapacity() . "{YELLOW})!"
25+
);
26+
}
27+
28+
public function onQuit(Session $session): void {
29+
$this->game->broadcastMessage("{GRAY}" . $session->getUsername() . " {YELLOW}has quit!");
30+
}
31+
32+
}

0 commit comments

Comments
 (0)