Skip to content

Instantly share code, notes, and snippets.

@truelinux
Created March 30, 2015 04:35
Show Gist options
  • Save truelinux/a9133814255f53ed19f9 to your computer and use it in GitHub Desktop.
Save truelinux/a9133814255f53ed19f9 to your computer and use it in GitHub Desktop.
Code
<?php
namespace Skywars;
use pocketmine\network\protocol\DataPacket;
use pocketmine\block\Block;
use pocketmine\item\Item;
use pocketmine\utils\Config;
use pocketmine\command\Command;
use pocketmine\command\CommandExecutor;
use pocketmine\command\CommandSender;
use pocketmine\entity\FallingSand;
use pocketmine\event\entity\EntityLevelChangeEvent;
use pocketmine\event\entity\EntityMotionEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\Listener;
use pocketmine\event\player\PlayerDeathEvent;
use pocketmine\event\player\PlayerMoveEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\server\DataPacketSendEvent;
use pocketmine\network\protocol\AddItemEntityPacket;
use pocketmine\network\protocol\AddPlayerPacket;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\RemoveEntityPacket;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\math\Vector3;
use pocketmine\plugin\PluginBase;
use pocketmine\utils\TextFormat;
use pocketmine\event\block\SignChangeEvent;
use pocketmine\scheduler\CallbackTask;
use pocketmine\level\Level;
class Main extends PluginBase implements Listener {
public $playersInArena = array();
public $gameState = array();
public $gamePlayers = array();
public $players = array();
public function onEnable() {
@mkdir($this->getDataFolder());
@mkdir($this->getDataFolder()."/data");
$this->saveDefaultConfig();
$this->reloadConfig();
$this->getServer()->getPluginManager()->registerEvents($this, $this);
$this->getLogger()->info( TextFormat::GREEN . "Skywars - Enabled!" );
$this->getLogger()->info( TextFormat::GREEN . "Skywars - Enabling games..." );
$this->getServer()->getScheduler()->scheduleRepeatingTask(new CallbackTask([$this, "update"]), 20);
$x = 1;
$arena = $this->getDataFolder() . "SW-" . $x . ".yml";
if(file_exists($arena)) {
do {
$id = "SW-$x";
$this->game = new Config($this->getDataFolder() . $id . ".yml", CONFIG::YAML);
$this->playersInArena[$id] = 0;
$this->gameState[$id] = 0;
$this->getLogger()->info( TextFormat::LIGHT_PURPLE . "$id Enabled!" );
++$x;
$arena = $this->getDataFolder() . "BH-" . $x . ".yml";
} while(file_exists($arena));
}
$this->getLogger()->info( TextFormat::GREEN . "Skywars - All games loaded/configured." );
}
/**
* @param CommandSender $sender
* @param Command $command
* @param string $label
* @param array $args
* @return bool
*/
public function onCommand(CommandSender $sender, Command $command, $label, array $args){
if($sender instanceof Player) {
$name = $sender->getPlayer()->getName();
if(strtolower($command->getName('sw'))) {
if(empty($args)) {
$sender->sendMessage("> Skywars Help Page:");
$sender->sendMessage("Default Commands:");
$sender->sendMessage("/sw join/j 'Joins a game'");
$sender->sendMessage("/sw leave/l 'Leaves the game'");
$sender->sendMessage("/sw join/j 'Joins an open game'");
$sender->sendMessage("/sw kit/k 'Select a kit pre/in game'");
$sender->sendMessage("/sw list/ls 'Shows a list of available games'");
$sender->sendMessage("Do '/sw admin' for admin commands!");
return true;
}
if(count($args == 1)) {
if($args[0] == "help" || $args[0] == "h") {
$sender->sendMessage("> Skywars Help Page:");
$sender->sendMessage("Default Commands:");
$sender->sendMessage("/sw join/j 'Joins a game'");
$sender->sendMessage("/sw leave/l 'Leaves the game'");
$sender->sendMessage("/sw join/j 'Joins an open game'");
$sender->sendMessage("/sw kit/k 'Select a kit pre/in game'");
$sender->sendMessage("/sw list/ls 'Shows a list of available games'");
$sender->sendMessage("Do '/sw admin' for admin commands!");
return true;
}
if($args[0] == "pos" && $sender->isOp()|| $args[0] == "p" && $sender->isOp()) {
$sender->sendMessage($sender->getPosition());
return true;
}
if($args[0] == "admin" && $sender->isOp()|| $args[0] == "a" && $sender->isOp()) {
$sender->sendMessage("> Skywars Help Page:");
$sender->sendMessage("===============");
$sender->sendMessage("Admin Commands:");
$sender->sendMessage("/sw <start/s> <arenaid> 'Forces a game to start'");
$sender->sendMessage("/sw <create/cre> <id> 'Creates a arena(Must configure in .yml)'");
$sender->sendMessage("/sw <setwarp/sw> <lobby/pod/spawn> <arenaid> 'Sets warps for your arena'");
$sender->sendMessage("/sw <remove/r> <id> 'Deletes an arena'");
$sender->sendMessage("===============");
return true;
}
if($args[0] == "leave" || $args[0] == "l") {
if(isset($this->gamePlayers[$name])) {
$id = $this->gamePlayers[$name];
--$this->playersInArena[$id];
unset($this->gamePlayers[$name]);
foreach($this->players as $p) {
if(strpos($p, $name) == 5) {
unset($this->players[$id . "-" . $name]);
}
$x = 0;
do {
$p = $this->players[$id . "-" . $x];
$p->sendMessage("/> $name left");
++$x;
} while ($p !== null);
$spawn = $this->getConfig()->get("SpawnWorld");
$world = $this->getServer()->getLevelByName($spawn);
$x = $this->getConfig()->get("SpawnX");
$y = $this->getConfig()->get("SpawnY");
$z = $this->getConfig()->get("SpawnZ");
$world->getChunk($x, $z);
$world->getChunk($world->getSpawnLocation()->getX(), $world->getSpawnLocation()->getZ());
$sender->teleport($world->getSpawnLocation(), 0, 0);
$pos = new Vector3($x, $y, $z);
$sender->teleport($pos);
return true;
}
}else{
$sender->sendMessage("> You are not in a game!");
return true;
}
}
if($args[0] == "list" || $args[0] == "ls") {
$max = $this->getConfig()->get("MaxPlayers");
foreach($this->games as $arena) {
$x = 1;
if($this->playersInArena["SW-" . $x] < $max) {
$players = $this->playersInArena["SW-" . $x];
$sender->sendMessage("> SW-" . $x . " - $players/$max Players (Type /sw join sw-" . $x . " to join)");
++$x;
} else {
return true;
}
}
return true;
}
}
if(count($args >= 2)) {
if($args[0] == "join" || $args[0] == "j") {
if(isset($args[1])) {
if($this->gameState[strtoupper($args[1])] == 0) {
if(!isset($this->gamePlayers[$name])) {
$arena = $this->getDataFolder() . strtoupper($args[1]) . ".yml";
if(file_exists($arena)) {
if(isset($this->playersInArena[strtoupper($args[1])])) {
$max = $this->getConfig()->get("MaxPlayers");
if($this->playersInArena[strtoupper($args[1])] < $max) {
$this->gamePlayers[$name] = strtoupper($args[1]);
$conf = new Config($this->getDataFolder() . strtoupper($args[1]) . ".yml", CONFIG::YAML);
$world = $this->getServer()->getLevelByName($conf->get("LobbyWorld"));
$x = $conf->get("LobbyX");
$y = $conf->get("LobbyY");
$z = $conf->get("LobbyZ");
$world->getChunk($x, $z);
$world->getChunk($world->getSpawnLocation()->getX(), $world->getSpawnLocation()->getZ());
$sender->teleport($world->getSpawnLocation(), 0, 0);
$pos = new Vector3($x, $y, $z);
$sender->teleport($pos);
$sender->sendMessage("> Welcome to arena '" . $conf->get("FullName") . "'!\nThe game will start soon!");
++$this->playersInArena[strtoupper($args[1])];
return true;
}else{
$conf = new Config($this->getDataFolder() . $args[1] . ".yml", CONFIG::YAML);
$world = $this->getServer()->getLevelByName($conf->get("LobbyWorld"));
$x = $conf->get("LobbyX");
$y = $conf->get("LobbyY");
$z = $conf->get("LobbyZ");
$world->getChunk($x, $z);
$world->getChunk($world->getSpawnLocation()->getX(), $world->getSpawnLocation()->getZ());
$sender->teleport($world->getSpawnLocation(), 0, 0);
$pos = new Vector3($x, $y, $z);
$sender->teleport($pos);
$this->seekers[$player] = strtoupper($args[1]);
$sender->sendMessage("> Welcome to arena '" . $conf->get("FullName") . "'!\nGame will start soon!");
$this->arenas[strtoupper($args[1])]++;
$this->tm2[strtoupper($args[1])]++;
$this->tag[$player] = 3;
return true;
}
}else{
$sender->sendMessage("> Game is full!");
return true;
}
}else{
$sender->sendMessage("> Game is stopped!");
return true;
}
}else{
$sender->sendMessage("> Internal ERROR!");
return true;
}
}else{
$sender->sendMessage("> You are already in-game!");
return true;
}
}else{
$sender->sendMessage("> Game is in progress!");
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment