Skip to content

Instantly share code, notes, and snippets.

@msjyoo
Created March 2, 2014 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msjyoo/9301231 to your computer and use it in GitHub Desktop.
Save msjyoo/9301231 to your computer and use it in GitHub Desktop.
<?php
/*
name=BattleField
description=A heavily customized PVP Ranking plugin
author=sekjun9878
version=1.0.0
class=BattleField
apiversion=9
*/
class BattleField implements Plugin
{
private $api, $server, $database;
private $config = array();
private $stmt = array();
public function __construct(ServerAPI $api, $server = false)
{
$this->api = $api;
$this->server = ServerAPI::request();
}
public function init()
{
try
{
//create or open the database
$this->database = new SQLite3($this->api->plugin->configPath($this).'Database.sqlite');
}
catch(Exception $e)
{
die($e->getMessage());
}
$this->database->query("CREATE TABLE IF NOT EXISTS PlayerData (id INTEGER PRIMARY KEY AUTOINCREMENT, Username TEXT UNIQUE, LastIP TEXT, CurrentXP INTEGER DEFAULT 0, LastKiller TEXT DEFAULT \"false\", KillCount INTEGER DEFAULT 0, DeathCount INTEGER DEFAULT 0, KillStreak INTEGER DEFAULT 0)");
$this->database->query("CREATE TABLE IF NOT EXISTS KillLog (id INTEGER PRIMARY KEY AUTOINCREMENT, Victim TEXT, Killer TEXT, Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)");
$this->api->addHandler("player.join", array($this, "eventHandler"), 100);
$this->api->addHandler("player.death", array($this, "eventHandler"), 100);
$this->api->addHandler("player.spawn", array($this, "eventHandler"), 100);
$this->api->addHandler("player.respawn", array($this, "eventHandler"), 100);
$this->config['config'] = new Config($this->api->plugin->configPath($this)."config.yml", CONFIG_YAML, array(
"killstreak" => 5,
));
$this->stmt['insertnewplayer'] = $this->database->prepare("INSERT INTO PlayerData (Username, LastIP) VALUES (:username, :IP)");
$this->stmt['checknewplayer'] = $this->database->prepare("SELECT * FROM PlayerData WHERE Username=:username");
$this->stmt['updatenewplayer'] = $this->database->prepare("UPDATE PlayerData SET Username=:username, LastIP=:IP WHERE Username=:username");
$this->stmt['insertkilllog'] = $this->database->prepare("INSERT INTO KillLog (Victim, Killer) VALUES (:victim, :killer)");
$this->stmt['increasekillcount'] = $this->database->prepare("UPDATE PlayerData SET KillCount=KillCount+1 WHERE Username=:killer");
$this->stmt['increasekillstreak'] = $this->database->prepare("UPDATE PlayerData SET KillStreak=KillStreak+1 WHERE Username=:killer");
$this->stmt['increasedeathcount'] = $this->database->prepare("UPDATE PlayerData SET DeathCount=DeathCount+1 WHERE Username=:victim");
$this->stmt['resetkillstreak'] = $this->database->prepare("UPDATE PlayerData SET KillStreak=0 WHERE Username=:victim");
$this->stmt['getkillstreak'] = $this->database->prepare("SELECT KillStreak FROM PlayerData WHERE Username=:killer");
$this->stmt['getkillcount'] = $this->database->prepare("SELECT KillCount FROM PlayerData WHERE Username=:username");
$this->stmt['getdeathcount'] = $this->database->prepare("SELECT DeathCount FROM PlayerData WHERE Username=:username");
$this->api->schedule(1200 , array($this, 'Announce'), array(), true);
}
public function Announce()
{
foreach($this->api->player->getAll() as $p)
{
$this->stmt['getkillcount']->reset();
$this->stmt['getkillcount']->bindValue(':username', $p->username, SQLITE3_TEXT);
$KillCount = $this->stmt['getkillcount']->execute()->fetchArray();
$KillCount = $KillCount['KillCount'];
$this->stmt['getdeathcount']->reset();
$this->stmt['getdeathcount']->bindValue(':username', $p->username, SQLITE3_TEXT);
$DeathCount = $this->stmt['getdeathcount']->execute()->fetchArray();
$DeathCount = $DeathCount['DeathCount'];
$p->sendChat("Your current stats: ".$KillCount." Kills and ".$DeathCount." Deaths.");
}
}
public function eventHandler($data, $event)
{
switch($event)
{
case "player.join":
$this->stmt['checknewplayer']->reset();
$this->stmt['checknewplayer']->bindValue(':username', $data->username, SQLITE3_TEXT);
if($this->stmt['checknewplayer']->execute()->fetchArray(SQLITE3_ASSOC) == false)
{
$this->stmt['insertnewplayer']->reset();
$this->stmt['insertnewplayer']->bindValue(':username', $data->username, SQLITE3_TEXT);
$this->stmt['insertnewplayer']->bindValue(':IP', $data->ip, SQLITE3_TEXT);
$this->stmt['insertnewplayer']->execute();
}
else
{
$this->stmt['updatenewplayer']->reset();
$this->stmt['updatenewplayer']->bindValue(':username', $data->username, SQLITE3_TEXT);
$this->stmt['updatenewplayer']->bindValue(':IP', $data->ip, SQLITE3_TEXT);
$this->stmt['updatenewplayer']->execute();
}
break;
case "player.death":
if(is_numeric($data["cause"]))
{
$e = $this->api->entity->get($data["cause"]);
if($e instanceof Entity and $e->class == ENTITY_PLAYER)
{
$killer = $e->name;
$victim = $data['player'];
$this->stmt['insertkilllog']->reset();
$this->stmt['insertkilllog']->bindValue(':victim', $victim, SQLITE3_TEXT);
$this->stmt['insertkilllog']->bindValue(':killer', $killer, SQLITE3_TEXT);
$this->stmt['insertkilllog']->execute();
$this->stmt['increasekillcount']->reset();
$this->stmt['increasekillcount']->bindValue(':killer', $killer, SQLITE3_TEXT);
$this->stmt['increasekillcount']->execute();
$this->stmt['increasekillstreak']->reset();
$this->stmt['increasekillstreak']->bindValue(':killer', $killer, SQLITE3_TEXT);
$this->stmt['increasekillstreak']->execute();
//Killer / Victim actions
$this->stmt['increasedeathcount']->reset();
$this->stmt['increasedeathcount']->bindValue(':victim', $victim, SQLITE3_TEXT);
$this->stmt['increasedeathcount']->execute();
$this->stmt['resetkillstreak']->reset();
$this->stmt['resetkillstreak']->bindValue(':victim', $victim, SQLITE3_TEXT);
$this->stmt['resetkillstreak']->execute();
$this->stmt['getkillstreak']->reset();
$this->stmt['getkillstreak']->bindValue(':victim', $victim, SQLITE3_TEXT);
$killstreak = $this->stmt['getkillstreak']->execute()->fetchArray();
$killstreak = $killstreak['KillStreak'];
if($killstreak == $this->config['config']->get("killstreak"))
{
$this->api->chat->broadcast("$killer is on a killing spree!");
}
}
}
break;
case "player.spawn":
case "player.respawn":
if($data->getGamemode() === "creative") break;
$data->setSlot(0, BlockAPI::getItem(IRON_SWORD));
$data->setArmor(0, BlockAPI::getItem(CHAIN_HELMET));
$data->setArmor(1, BlockAPI::getItem(CHAIN_CHESTPLATE));
$data->setArmor(2, BlockAPI::getItem(CHAIN_LEGGINGS));
$data->setArmor(3, BlockAPI::getItem(CHAIN_BOOTS));
$randx = mt_rand(0, 256);
$randz = mt_rand(0, 256);
$y = $this->getHighestPoint($randx, $randz, $data->level);
$data->teleport(new Vector3($randx, $y, $randz));
$data->sendChat("----------------------------------------------------");
$data->sendChat("** Welcome to BattleField v1");
$data->sendChat("** This Server is a Free For All Deathmatch Server.");
$data->sendChat("** At the start of the game, you get 1 Iron Sword and Full Chain Armor.");
$data->sendChat("** Current Players: ".count($this->api->player->getAll())."/".$this->server->maxClients);
$data->sendChat("** Kill them All!!!!!!");
$data->sendChat("----------------------------------------------------");
break;
}
}
private function getHighestPoint($x, $z, $level)
{
for($re = 0; $re < 128; $re++)
{
if($level->getBlock(new Vector3($x, $re, $z)) instanceof AirBlock)
{
return $re+1;
}
}
return 128;
}
public function __destruct()
{
$this->database->close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment