Skip to content

Commit

Permalink
Update plugin to api 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectInfinity committed Jul 14, 2018
1 parent db1ced7 commit 0b08e57
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 38 deletions.
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: PocketVote
main: ProjectInfinity\PocketVote\PocketVote
version: 2.1.2
api: [3.0.0-ALPHA6, 3.0.0-ALPHA7, 3.0.0-ALPHA8, 3.0.0-ALPHA9, 3.0.0-ALPHA10, 3.0.0-ALPHA11, 3.0.0-ALPHA12]
version: 2.1.3
api: [3.0.0, 3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.0.6]
author: ProjectInfinity
permissions:
pocketvote.admin:
Expand Down
10 changes: 5 additions & 5 deletions src/ProjectInfinity/PocketVote/PocketVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ public function onEnable() {
}
}

$this->schedulerTask = $this->getServer()->getScheduler()->scheduleRepeatingTask(new SchedulerTask($this), 1200); # 1200 ticks = 60 seconds.
$this->schedulerTask = $this->getScheduler()->scheduleRepeatingTask(new SchedulerTask($this), 1200); # 1200 ticks = 60 seconds.
# Get voting link.
$this->getServer()->getScheduler()->scheduleAsyncTask(new VoteLinkTask($this->identity));
$this->getServer()->getAsyncPool()->submitTask(new VoteLinkTask($this->identity));
# Report usage.
$this->getServer()->getScheduler()->scheduleAsyncTask(new HeartbeatTask($this->identity));
$this->getServer()->getAsyncPool()->submitTask(new HeartbeatTask($this->identity));
}

public function onDisable() {
$this->getServer()->getScheduler()->cancelTasks($this);
$this->getScheduler()->cancelAllTasks();
self::$plugin = null;
self::$cert = null;
self::$dev = null;
Expand All @@ -183,7 +183,7 @@ public function startScheduler(int $seconds) {
if(!$this->schedulerTask->isCancelled()) $this->schedulerTask->cancel();

$this->schedulerTs = $time;
$this->schedulerTask = $this->getServer()->getScheduler()->scheduleRepeatingTask(new SchedulerTask($this), $seconds > 0 ? ($seconds * 20) : 1200);
$this->schedulerTask = $this->getScheduler()->scheduleRepeatingTask(new SchedulerTask($this), $seconds > 0 ? ($seconds * 20) : 1200);
$this->schedulerFreq = $seconds;

$this->getLogger()->debug("Scheduler interval changed to $seconds seconds.");
Expand Down
2 changes: 2 additions & 0 deletions src/ProjectInfinity/PocketVote/VoteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(PocketVote $plugin) {

/**
* @priority LOWEST
* @param VoteEvent $event
*/
public function onVoteEvent(VoteEvent $event) {
if($event->isCancelled()) return;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function onVoteEvent(VoteEvent $event) {

/**
* @priority LOWEST
* @param PlayerJoinEvent $event
*/
public function onPlayerJoin(PlayerJoinEvent $event) {
if(PocketVote::$hasVRC) $this->vm->scheduleVRCTask($event->getPlayer()->getName()); # TODO: This should be possible to disable and only allow through commands.
Expand Down
4 changes: 2 additions & 2 deletions src/ProjectInfinity/PocketVote/cmd/PocketVoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args

case 'DIAGNOSE':
$sender->sendMessage(TextFormat::GREEN.'Scheduling a diagnosis...');
$this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new DiagnoseTask($this->plugin->getDescription()->getVersion(), $sender->getName()));
$this->plugin->getServer()->getAsyncPool()->submitTask(new DiagnoseTask($this->plugin->getDescription()->getVersion(), $sender->getName()));
break;

case 'CMD':
Expand Down Expand Up @@ -179,7 +179,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
$sender->sendMessage(TextFormat::RED.'You need to specify a name. /pv link [name]');
return true;
}
$this->getPlugin()->getServer()->getScheduler()->scheduleAsyncTask(new SetLinkNameTask($sender->getName(), $args[1]));
$this->getPlugin()->getServer()->getAsyncPool()->submitTask(new SetLinkNameTask($sender->getName(), $args[1]));
break;

default:
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/cmd/VoteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function execute(CommandSender $sender, String $commandLabel, array $args
return true;
}
if(isset($args[0]) && strtoupper($args[0]) === 'TOP') {
$this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new TopVoterTask($this->plugin->identity, $sender->getName()));
$this->plugin->getServer()->getAsyncPool()->submitTask(new TopVoterTask($this->plugin->identity, $sender->getName()));
return true;
}
$link = $this->plugin->getVoteManager()->getVoteLink();
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/cmd/guru/GuAddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function execute(CommandSender $sender, String $commandLabel, array $args
break;
}

$this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new AddLinkTask($sender->getName(), $title, $link));
$this->plugin->getServer()->getAsyncPool()->submitTask(new AddLinkTask($sender->getName(), $title, $link));
return true;
}
}
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/cmd/guru/GuDelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function execute(CommandSender $sender, String $commandLabel, array $args
return true;
}

$this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new DeleteLinkTask($sender->getName(), (int)$args[0]));
$this->plugin->getServer()->getAsyncPool()->submitTask(new DeleteLinkTask($sender->getName(), (int)$args[0]));
return true;
}
}
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/cmd/guru/GuListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function execute(CommandSender $sender, String $commandLabel, array $args
$sender->sendMessage(TextFormat::RED.'You do not have permission to do that.');
return true;
}
PocketVote::getPlugin()->getServer()->getScheduler()->scheduleAsyncTask(new GetLinksTask($sender->getName()));
PocketVote::getPlugin()->getServer()->getAsyncPool()->submitTask(new GetLinksTask($sender->getName()));
return true;
}
}
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/cmd/guru/GuruCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class GuruCommand extends Command {

public function __construct() {
parent::__construct('guru', 'MCPE Guru help command', '/guru [subject]', ['gu']);
parent::__construct('guru', 'MCPE Guru help command', '/guru', ['gu']);
}

public function execute(CommandSender $sender, String $commandLabel, array $args) {
Expand Down
30 changes: 22 additions & 8 deletions src/ProjectInfinity/PocketVote/event/VoteDispatchEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,49 @@

namespace ProjectInfinity\PocketVote\event;

use pocketmine\event\Cancellable;
use pocketmine\event\plugin\PluginEvent;
use pocketmine\Server;
use ProjectInfinity\PocketVote\PocketVote;

class VoteDispatchEvent extends VoteEvent {
class VoteDispatchEvent extends PluginEvent implements Cancellable {

public static $handlerList = null;

private $player, $ip, $site;

public function __construct(PocketVote $plugin, $player, $ip, $site) {
parent::__construct($plugin, $player, $ip, $site);
parent::__construct($plugin);
$this->player = $player;
$this->ip = $ip;
$this->site = $site;
}

/**
* @return string
* Returns the player that voted.
*
* @return mixed
*/
public function getPlayer() {
return parent::getPlayer();
return $this->player;
}

/**
* @return string
* Get the IP of the player that voted.
*
* @return mixed
*/
public function getIp() {
return parent::getIp();
return $this->ip;
}

/**
* @return string
* Get the site the player voted on.
*
* @return mixed
*/
public function getSite() {
return parent::getSite();
return $this->site;
}

}
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/event/VoteEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function getSite() {
*
* @return bool
*/
public function isOnline() {
public function isOnline(): bool {
return Server::getInstance()->getPlayer($this->player) !== null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ProjectInfinity/PocketVote/task/HeartbeatTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function onRun() {

if($this->secret === null || $this->identity === null) return;

$curl = curl_init($this->isDev ? 'http://127.0.0.1/v2/heartbeat' : 'https://api.pocketvote.io/v2/heartbeat');
$curl = curl_init($this->isDev ? 'http://127.0.0.1:9000/v2/heartbeat' : 'https://api.pocketvote.io/v2/heartbeat');

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
Expand Down
4 changes: 2 additions & 2 deletions src/ProjectInfinity/PocketVote/task/SchedulerTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(PocketVote $plugin) {

public function onRun(int $currentTick) {
$this->plugin->getLogger()->debug('Checking for outstanding votes.');
if(!$this->plugin->multiserver || ($this->plugin->multiserver && strtolower($this->plugin->multiserver_role) === 'master')) $this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new VoteCheckTask($this->plugin->identity, $this->plugin->secret, $this->version));
if($this->plugin->multiserver && strtolower($this->plugin->multiserver_role) === 'slave') $this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new SlaveCheckTask());
if(!$this->plugin->multiserver || ($this->plugin->multiserver && strtolower($this->plugin->multiserver_role) === 'master')) $this->plugin->getServer()->getAsyncPool()->submitTask(new VoteCheckTask($this->plugin->identity, $this->plugin->secret, $this->version));
if($this->plugin->multiserver && strtolower($this->plugin->multiserver_role) === 'slave') $this->plugin->getServer()->getAsyncPool()->submitTask(new SlaveCheckTask());
}
}
24 changes: 12 additions & 12 deletions src/ProjectInfinity/PocketVote/util/VoteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,47 @@ public function __construct(PocketVote $plugin) {
$this->votes = $plugin->getConfig()->get('votes', []);
$this->loadedVRC = [];
$this->currentVRCTasks = [];
if($this->plugin->expiration > 0) $plugin->getServer()->getScheduler()->scheduleDelayedRepeatingTask(new ExpireVotesTask(), 300, 6000);
if($this->plugin->expiration > 0) $plugin->getScheduler()->scheduleDelayedRepeatingTask(new ExpireVotesTask(), 300, 6000);
}

public function addVRC($record) {
public function addVRC($record): void {
$this->loadedVRC[] = $record;
}

public function getVRC(): array {
return $this->loadedVRC;
}

public function removeVRCTask($player) {
public function removeVRCTask($player): void {
if(!isset($this->currentVRCTasks[$player])) return;
unset($this->currentVRCTasks[$player]);
}

public function scheduleVRCTask($player) {
public function scheduleVRCTask($player) : void {
if((PocketVote::$hasVRC && !$this->plugin->multiserver) || (PocketVote::$hasVRC && $this->plugin->multiserver && $this->plugin->multiserver_role === 'master')) {
if(isset($this->currentVRCTasks[$player])) return;
# Only run when VRC is enabled and multiserver is off or VRC is enabled and multiserver and server role is set to master.
$this->plugin->getServer()->getScheduler()->scheduleAsyncTask(new VRCCheckTask($player));
$this->plugin->getScheduler()->scheduleAsyncTask(new VRCCheckTask($player));
}
}

public function getVoteLink() {
return $this->voteLink;
}

public function setVoteLink($link) {
public function setVoteLink($link): void {
$this->voteLink = $link;
}

public function hasVotes($player) {
public function hasVotes($player): bool {
$player = strtolower($player);
foreach($this->votes as $key => $vote) {
if(strtolower($vote['player']) === $player) return true;
}
return false;
}

public function getVotes($player) {
public function getVotes($player): array {
$votes = [];
$player = strtolower($player);
foreach($this->votes as $key => $vote) {
Expand All @@ -75,20 +75,20 @@ public function getVotes($player) {
return $votes;
}

public function addVote($player, $site, $ip) {
public function addVote($player, $site, $ip): void {
$this->votes[] = ['player' => $player, 'site' => $site, 'ip' => $ip, 'expires' => time() + $this->plugin->expiration];
}

public function removeVote($key) {
public function removeVote($key): void {
unset($this->votes[$key]);
}

public function commit() {
public function commit(): void {
$this->plugin->getConfig()->set('votes', array_values($this->votes));
$this->plugin->saveConfig();
}

public function expireVotes() {
public function expireVotes(): void {
$expired = 0;
$ts = time();
foreach($this->votes as $key => $vote) {
Expand Down

0 comments on commit 0b08e57

Please sign in to comment.