Bug Fixes about scenarios ... and a lot more stuff.

Starting the howtoplay
This commit is contained in:
Ad5001 2016-08-21 10:40:41 +03:00
parent be870bc924
commit 6cd5bec03a
10 changed files with 102 additions and 65 deletions

View file

@ -22,6 +22,8 @@ commands:
permission: uhc.command.scenarios
permissions:
uhc.command.main:
default: true
uhc.command.start&stop:
default: op
uhc.command.scenarios:
default: true

16
scenarios/Example.php Normal file
View file

@ -0,0 +1,16 @@
<?php
use Ad5001\UHC\scenario\Scenario;
use pocketmine\Player;
class ExampleScenario extends Scenario {
public function onStart() {
$this->getLogger()->info("Started !");
}
public function onJoin(Player $player) {
$player->sendMessage("Welcome to this example UHC Scenario !");
}
}

View file

@ -65,7 +65,6 @@ class Main extends PluginBase implements Listener{
$this->getServer()->getPluginManager()->registerEvent("Ad5001\\UHC\\event\\GameStartEvent", $this, \pocketmine\event\EventPriority::NORMAL, new \pocketmine\plugin\MethodEventExecutor("onGameStart"), $this, true);
$this->getServer()->getPluginManager()->registerEvent("Ad5001\\UHC\\event\\GameStopEvent", $this, \pocketmine\event\EventPriority::NORMAL, new \pocketmine\plugin\MethodEventExecutor("onGameStop"), $this, false);
$this->UHCManager = new UHCManager($this);
$this->UHCManager->getStartedUHCs() = [];
$this->quit = [];
}
@ -78,6 +77,21 @@ class Main extends PluginBase implements Listener{
}
public function getClasses(string $file) {
$tokens = token_get_all($file);
$class_token = false;
foreach ($tokens as $token) {
if (is_array($token)) {
if ($token[0] == T_CLASS) {
$class_token = true;
} else if ($class_token && $token[0] == T_STRING) {
return $token[1];
}
}
}
}
public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
switch($cmd->getName()){
case "uhc":
@ -112,7 +126,7 @@ switch($cmd->getName()){
break;
case "scenarios":
if(isset($args[0]) and $sender instanceof Player and $sender->hasPermission("uhc.scenarios.modify")) {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedGames()[$sender->getLevel()->getName()])) {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
switch($args[0]) {
case "add":
if(isset($args[1])) {
@ -124,7 +138,7 @@ switch($cmd->getName()){
}
$sender->sendMessage(self::PREFIX . C::GREEN . " Succefully added scenario $args[1] !");
} else {
$sender->sendMessage(slef::PREFIX . C::DARK_RED . "Scenario $args[1] has already been added !");
$sender->sendMessage(self::PREFIX . C::DARK_RED . "Scenario $args[1] has already been added !");
}
} else {
$sender->sendMessage(self::PREFIX . C::DARK_RED . "Scenario $args[1] does not exists !");
@ -138,7 +152,7 @@ switch($cmd->getName()){
case "delete":
case "del":
if(isset($args[1])) {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getScenarios()[$args[1]])) {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()[$args[1]])) {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()[$args[1]])) {
$this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->rmScenario($args[1]);
foreach($sender->getLevel()->getPlayers() as $p) {
@ -163,8 +177,9 @@ switch($cmd->getName()){
$sender->sendMessage(self::PREFIX . "You're not in a UHC world !");
}
} else {
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedGames()[$sender->getLevel()->getName()])) {
$sender->sendMessage(self::PREFIX . "Current enabled scenarios : " . implode(", ", $this->UHCManager[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios()));
if(isset($this->UHCManager->getLevels()[$sender->getLevel()->getName()]) and !isset($this->UHCManager->getStartedUHCs()[$sender->getLevel()->getName()])) {
$scs = $this->UHCManager->getLevels()[$sender->getLevel()->getName()]->scenarioManager->getUsedScenarios();
$sender->sendMessage(self::PREFIX . "Current enabled scenarios : " . implode(", ", $scs));
}
}
break;
@ -180,28 +195,8 @@ return false;
# Event Listener !
public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {
// echo $event->getBlock()->getId() . "=/=" . Block::SIGN_POST ."=/=" . Block::WALL_SIGN;
if($event->getBlock()->getId() == Block::SIGN_POST or $event->getBlock()->getId() == Block::WALL_SIGN) {
$t = $event->getBlock()->getLevel()->getTile($event->getBlock());
// echo "Sign.";
foreach($this->UHCManager->getLevels() as $class) {
if(str_ireplace("{game}", $class->getName(), $this->getConfig()->get("Game1")) == $t->getText()[0]) {
$lvlex = explode("{level}", $this->getConfig()->get("Game2"));
$lvl = str_ireplace($lvlex[0], "", $t->getText()[1]);
$lvl = str_ireplace($lvlex[1], "", $lvl);
if($class->getLevel()->getName() == $lvl) {
if($this->UHCManager->getLevels()[$lvl]->isStarted()) {
$event->getPlayer()->teleport($class->getLevel()->getSafeSpawn());
$event->getPlayer()->setGamemode(3);
} else {
$event->getPlayer()->teleport($class->getLevel()->getSafeSpawn());
}
}
}
}
}
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onInteract($event);
}
}
@ -210,12 +205,12 @@ return false;
public function onEntityLevelChange(EntityLevelChangeEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getOrigin()->getName()]) and $event->getEntity() instanceof Player) {
foreach($this->UHCManager->getLevels()[$event->getOrigin()->getName()]->scenarioManager->getScenarios() as $sc) {
$sc->onQuit($event->getPlayer());
foreach($this->UHCManager->getLevels()[$event->getOrigin()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onQuit($event->getEntity());
}
}
if(isset($this->UHCManager->getLevels()[$event->getTarget()->getName()]) and $event->getEntity() instanceof Player) {
foreach($this->UHCManager->getLevels()[$event->getTarget()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getTarget()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onJoin($event->getEntity());
}
}
@ -224,7 +219,7 @@ return false;
public function onPlayerChat(\pocketmine\event\player\PlayerChatEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerChat($event);
$sc->onChat($event);
}
@ -234,7 +229,7 @@ return false;
public function onPlayerCommandPreprocess(\pocketmine\event\player\PlayerCommandPreprocessEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerCommand($event);
}
}
@ -243,7 +238,7 @@ return false;
public function onPlayerDeath(\pocketmine\event\player\PlayerDeathEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerDeath($event);
$sc->onDeath($event);
}
@ -253,7 +248,7 @@ return false;
public function onPlayerDropItem(\pocketmine\event\player\PlayerDropItemEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerDropItem($event);
$sc->onPlayerDropItem($event);
}
@ -263,7 +258,7 @@ return false;
public function onPlayerMove(\pocketmine\event\player\PlayerMoveEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerMove($event);
$sc->onMove($event);
}
@ -273,7 +268,7 @@ return false;
public function onPlayerItemConsume(\pocketmine\event\player\PlayerItemConsumeEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerItemConsume($event);
$sc->onItemConsume($event);
}
@ -283,7 +278,7 @@ return false;
public function onPlayerItemHeld(\pocketmine\event\player\PlayerItemHeldEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onPlayerItemHeld($event);
$sc->onItemHeld($event);
}
@ -293,7 +288,7 @@ return false;
public function onBlockBreak(\pocketmine\event\block\BlockBreakEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onBlockBreak($event);
$sc->onBreak($event);
}
@ -303,7 +298,7 @@ return false;
public function onBlockPlace(\pocketmine\event\block\BlockPlaceEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onBlockPlace($event);
$sc->onPlace($event);
}
@ -313,7 +308,7 @@ return false;
public function onEntityDamage(\pocketmine\event\entity\EntityDamageEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onEntityDamage($event);
}
}
@ -322,7 +317,7 @@ return false;
public function onProjectileLaunch(\pocketmine\event\entity\ProjectileLaunchEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onProjectileLaunch($event);
}
}
@ -331,7 +326,7 @@ return false;
public function onProjectileHit(\pocketmine\event\entity\ProjectileHitEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getEntity()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onProjectileHit($event);
}
}
@ -340,7 +335,7 @@ return false;
public function onDataPacketReceive(\pocketmine\event\server\DataPacketReceiveEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onDataPacketReceive($event);
}
}
@ -349,7 +344,7 @@ return false;
public function onDataPacketSend(\pocketmine\event\server\DataPacketSendEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onDataPacketSend($event);
}
}
@ -358,7 +353,7 @@ return false;
public function onServerCommand(\pocketmine\event\server\ServerCommandEvent $event) {
foreach($this->UHCManager->getLevels() as $lvl => $world) {
foreach($world->scenarioManager->getScenarios() as $sc) {
foreach($world->scenarioManager->getUsedScenarios() as $sc) {
$sc->onServerCommand($event);
}
}
@ -366,7 +361,7 @@ return false;
public function onPlayerJoin(\pocketmine\event\player\PlayerJoinEvent $event) {
if(!isset($this->ft)) {
$this->ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new FetchPlayersTask($this, $this->UHCManager->getLevels()), 10);
$this->ft = $this->getServer()->getScheduler()->scheduleRepeatingTask(new FetchPlayersTask($this, $this->UHCManager->getStartedUHCs()), 10);
}
if(isset($this->quit[$event->getPlayer()->getName()])) {
$quit = explode("/", $this->quit[$event->getPlayer()->getName()]);
@ -377,7 +372,7 @@ return false;
}
}
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onJoin($event->getPlayer());
}
}
@ -386,7 +381,7 @@ return false;
public function onPlayerQuit(\pocketmine\event\player\PlayerQuitEvent $event) {
if(isset($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()])) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->UHCManager->getLevels()[$event->getPlayer()->getLevel()->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onQuit($event->getPlayers());
}
}

View file

@ -96,7 +96,7 @@ class UHCGame implements Listener{
public function onPlayerDeath(PlayerDeathEvent $event) {
if($event->getPlayer()->getLevel()->getName() === $this->world->getName() and !$this->cancelled) {
foreach($event->getPlayer()->getLevel()->getPlayers() as $p) {
$p->sendMessage(Main::PREFIX . C::YELLOW . $event->getPlayer()->getName() . " died. " . (count($this->world->getLevel()->getPlayers) - 2)) . " players left !");
$p->sendMessage(Main::PREFIX . C::YELLOW . $event->getPlayer()->getName() . " died. " . (count($this->world->getLevel()->getPlayers()) - 1) . " players left !");
}
$this->respawn[$event->getPlayer()->getName()] = true;
$pls = [];

View file

@ -42,7 +42,7 @@ class UHCManager {
$ft = $this->main->getServer()->getScheduler()->scheduleRepeatingTask($t = new StartGameTask($this->main, $this->levels[$level->getName()]), 20);
$t->setHandler($ft);
$this->startedgames[$level->getName()] = true;
foreach($this->levels[$level->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->levels[$level->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onStart();
}
return true;
@ -55,7 +55,7 @@ class UHCManager {
public function stopUHC(Level $level) {
if(isset($this->startedgames[$level->getName()])) {
unset($this->startedgames[$level->getName()]);
foreach($this->levels[$level->getName()]->scenarioManager->getScenarios() as $sc) {
foreach($this->levels[$level->getName()]->scenarioManager->getUsedScenarios() as $sc) {
$sc->onQuit();
}
return true;
@ -82,4 +82,10 @@ class UHCManager {
}
public function addStartedUHC(string $name, UHCGame $game) {
$this->startedgames[$name] = $game;
return true;
}
}

View file

@ -39,6 +39,11 @@ class UHCWorld {
}
public function isStarted() {
return isset($this->p->UHCManager->getStartedUHCs()[$this->lvl->getName()]);
}
public function getPlayers() {
return $this->players;
}

View file

@ -12,10 +12,12 @@ use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\command\PluginIdentifiableCommand;
use pocketmine\Server;
use pocketmine\Player;
use pocketmine\event\Listener;
use pocketmine\utils\Config;
use Ad5001\UHC\scenario\ScenarioInt;
use Ad5001\UHC\Main;
use Ad5001\UHC\UHCWorld;
abstract class Scenario implements ScenarioInt, Listener {
@ -25,14 +27,13 @@ abstract class Scenario implements ScenarioInt, Listener {
private $level;
public function __construct(Server $server, UHCGame $game, UHCWorld $level) {
public function __construct(Server $server, UHCWorld $level) {
$this->server = $server;
$this->game = $game;
$this->level = $level;
}
public function onStart()
public function onStart() {}
public function onStop() {}
@ -44,6 +45,19 @@ abstract class Scenario implements ScenarioInt, Listener {
public function onQuit(Player $player) {}
public function getLevel() {
return $this->level;
}
public function getGame() {
if(isset($this->main->UHCManager->getStartedUHCs()[$this->level->getName()])) {
return $this->main->UHCManager->getStartedUHCs()[$this->level->getName()];
}
return null;
}
public function onInteract(\pocketmine\event\player\PlayerInteractEvent $event) {}
@ -155,6 +169,6 @@ abstract class Scenario implements ScenarioInt, Listener {
public function getScenarioFolder() {
return realPath($this->getMain()->getDataFolder() . "scenarios/");
return $this->getMain()->getDataFolder() . "scenarios/";
}
}

View file

@ -8,10 +8,12 @@
# The most customisable UHC plugin for Minecraft PE !
namespace Ad5001\UHC\scenario;
use pocketmine\command\CommandExecutor;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
interface ScenarioInt extends CommandExecutor {
interface ScenarioInt {
/* When the scenario is activating */
public function onEnable();
public function onStart();
/* When the scenario is stoping (end of UHC) */
@ -27,12 +29,7 @@ interface ScenarioInt extends CommandExecutor {
/* Save the config */
public function saveConfig();
/* Test when an event is throwed */
public function onPlayerEvent(\pocketmine\event\player\PlayerEvent $event);
public function onEvent(\pocketmine\event\Event $event);
public function saveConfig($cfg);
/* Get the scenario folder */
@ -41,4 +38,6 @@ interface ScenarioInt extends CommandExecutor {
/* Reload the config */
public function reloadConfig();
}

View file

@ -48,7 +48,7 @@ class ScenarioManager {
public function addScenario(string $name) {
if(!isset($this->usedscenarios[$name]) and !$this->level->isStarted()) {
$this->usedscenarios[$name] = new $name($this->main, $this->level);
$this->usedscenarios[$name] = new $name($this->server, $this->level);
return true;
}
return false;

View file

@ -86,7 +86,7 @@ class StartGameTask extends PluginTask {
}
break;
case 0:
$this->main->games[$this->world->getLevel()->getName()] = new UHCGame($this->main, $this->world);
$this->main->UHCManager->addStartedUHC($this->world->getLevel()->getName(), new UHCGame($this->main, $this->world));
$this->close();
$this->seconds = -1;
break;