Added /give, /gamemode

This commit is contained in:
Shoghi Cervantes 2014-04-02 05:59:26 +02:00
parent 7cd29f0fcf
commit df3e456162
4 changed files with 183 additions and 76 deletions

View File

@ -78,80 +78,4 @@ class BlockAPI{
return $output; return $output;
} }
public function blockUpdateAround(Position $pos, $type = Level::BLOCK_UPDATE_NORMAL, $delay = false){
if($delay !== false){
$this->scheduleBlockUpdate($pos->getSide(0), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(1), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(2), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(3), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(4), $delay, $type);
$this->scheduleBlockUpdate($pos->getSide(5), $delay, $type);
}else{
$this->blockUpdate($pos->getSide(0), $type);
$this->blockUpdate($pos->getSide(1), $type);
$this->blockUpdate($pos->getSide(2), $type);
$this->blockUpdate($pos->getSide(3), $type);
$this->blockUpdate($pos->getSide(4), $type);
$this->blockUpdate($pos->getSide(5), $type);
}
}
public function blockUpdate(Position $pos, $type = Level::BLOCK_UPDATE_NORMAL){
if(!($pos instanceof block\Block)){
$block = $pos->level->getBlock($pos);
}else{
$pos = new Position($pos->x, $pos->y, $pos->z, $pos->level);
$block = $pos->level->getBlock($pos);
}
if($block === false){
return false;
}
$level = $block->onUpdate($type);
if($level === Level::BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level);
}
return $level;
}
public function scheduleBlockUpdate(Position $pos, $delay, $type = Level::BLOCK_UPDATE_SCHEDULED){
$type = (int) $type;
if($delay < 0){
return false;
}
$index = $pos->x . "." . $pos->y . "." . $pos->z . "." . $pos->level->getName() . "." . $type;
$delay = microtime(true) + $delay * 0.05;
if(!isset($this->scheduledUpdates[$index])){
$this->scheduledUpdates[$index] = $pos;
$this->server->query("INSERT INTO blockUpdates (x, y, z, level, type, delay) VALUES (" . $pos->x . ", " . $pos->y . ", " . $pos->z . ", '" . $pos->level->getName() . "', " . $type . ", " . $delay . ");");
return true;
}
return false;
}
public function blockUpdateTick(){
$time = microtime(true);
if(count($this->scheduledUpdates) > 0){
$update = $this->server->query("SELECT x,y,z,level,type FROM blockUpdates WHERE delay <= " . $time . ";");
if($update instanceof \SQLite3Result){
$upp = array();
while(($up = $update->fetchArray(SQLITE3_ASSOC)) !== false){
$index = $up["x"] . "." . $up["y"] . "." . $up["z"] . "." . $up["level"] . "." . $up["type"];
if(isset($this->scheduledUpdates[$index])){
$upp[] = array((int) $up["type"], $this->scheduledUpdates[$index]);
unset($this->scheduledUpdates[$index]);
}
}
$this->server->query("DELETE FROM blockUpdates WHERE delay <= " . $time . ";");
foreach($upp as $b){
$this->blockUpdate($b[1], $b[0]);
}
}
}
}
} }

View File

@ -27,6 +27,8 @@ use pocketmine\command\defaults\BanListCommand;
use pocketmine\command\defaults\DefaultGamemodeCommand; use pocketmine\command\defaults\DefaultGamemodeCommand;
use pocketmine\command\defaults\DeopCommand; use pocketmine\command\defaults\DeopCommand;
use pocketmine\command\defaults\DifficultyCommand; use pocketmine\command\defaults\DifficultyCommand;
use pocketmine\command\defaults\GamemodeCommand;
use pocketmine\command\defaults\GiveCommand;
use pocketmine\command\defaults\HelpCommand; use pocketmine\command\defaults\HelpCommand;
use pocketmine\command\defaults\KickCommand; use pocketmine\command\defaults\KickCommand;
use pocketmine\command\defaults\ListCommand; use pocketmine\command\defaults\ListCommand;
@ -86,6 +88,8 @@ class SimpleCommandMap implements CommandMap{
$this->register("pocketmine", new SaveOnCommand("save-on")); $this->register("pocketmine", new SaveOnCommand("save-on"));
$this->register("pocketmine", new SaveOffCommand("save-off")); $this->register("pocketmine", new SaveOffCommand("save-off"));
$this->register("pocketmine", new SaveCommand("save-all")); $this->register("pocketmine", new SaveCommand("save-all"));
$this->register("pocketmine", new GiveCommand("give"));
$this->register("pocketmine", new GamemodeCommand("gamemode"));
} }

View File

@ -0,0 +1,95 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
class GamemodeCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Changes the player to a specific game mode",
"/gamemode <mode> [player]"
);
$this->setPermission("pocketmine.command.gamemode");
}
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) === 0){
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
return false;
}
$gameMode = Server::getGamemodeFromString($args[0]);
if($gameMode === -1){
$sender->sendMessage("Unknown game mode");
return true;
Server::getInstance()->setConfigInt("gamemode", $gameMode);
$sender->sendMessage("Default game mode set to " . strtolower(Server::getGamemodeString($gameMode)));
}
$target = $sender;
if(isset($args[1])){
$target = Server::getInstance()->getPlayer($args[1]);
if($target === null){
$sender->sendMessage("Can't find player " . $args[1]);
return true;
}
}elseif(!($sender instanceof Player)){
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
return true;
}
if($gameMode !== $target->getGamemode()){
$target->setGamemode($gameMode);
if($gameMode !== $target->getGamemode()){
$sender->sendMessage("Game mode change for " . $target->getName() . " failed!");
}else{
if($target === $sender){
Command::broadcastCommandMessage($sender, "Set own gamemode to " . strtolower(Server::getGamemodeString($gameMode)) . " mode");
}else{
Command::broadcastCommandMessage($sender, "Set " . $target->getName() . "'s gamemode to " . strtolower(Server::getGamemodeString($gameMode)) . " mode");
}
}
}else{
$sender->sendMessage($target->getName() . " already has game mode " . strtolower(Server::getGamemodeString($gameMode)));
return true;
}
return true;
}
}

View File

@ -0,0 +1,84 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace pocketmine\command\defaults;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\item\Item;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\utils\TextFormat;
class GiveCommand extends VanillaCommand{
public function __construct($name){
parent::__construct(
$name,
"Gives the specified player a certain amount of items",
"/give <player> <item[:damage]> [amount]"
);
$this->setPermission("pocketmine.command.give");
}
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 2){
$sender->sendMessage(TextFormat::RED . "Usage: " . $this->usageMessage);
return false;
}
$player = Server::getInstance()->getPlayer($args[0]);
$item = Item::fromString($args[1]);
if(!isset($args[2])){
$item->setCount($item->getMaxStackSize());
}else{
$item->setCount((int) $args[2]);
}
if($player instanceof Player){
if(($player->getGamemode() & 0x01) === 0x01){
$sender->sendMessage(TextFormat::RED . "Player is in creative mode");
return true;
}
if($item->getID() == 0){
$sender->sendMessage(TextFormat::RED . "There is no item called " . $args[1] . ".");
return true;
}
$player->addItem(clone $item);
}else{
$sender->sendMessage(TextFormat::RED . "Can't find player " . $args[0]);
return true;
}
Command::broadcastCommandMessage($sender, "Gave " . $player->getName() . " " . $item->getCount() . " of " . $item->getName() . " (" . $item->getID() . ":" . $item->getMetadata() . ")");
return true;
}
}