Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Sandertv/BlockSniper
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed Feb 25, 2017
2 parents a0a2d08 + cefbbd3 commit 19ff9ab
Show file tree
Hide file tree
Showing 22 changed files with 284 additions and 183 deletions.
5 changes: 4 additions & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: "BlockSniper"
version: 1.0.1
version: 1.2.0
api: [2.0.0, 2.1.0, 3.0.0-ALPHA1, 3.0.0-ALPHA2, 3.0.0-ALPHA3]
author: "Sandertv"
main: Sandertv\BlockSniper\Loader
Expand Down Expand Up @@ -69,6 +69,9 @@ permissions:
blocksniper.type.biome:
default: op
description: "Allows access to the biome type"
blocksniper.type.raise:
default: op
description: "Allows access to the raise type"
blocksniper.shape:
default: false
description: "Allows access to all BlockSniper shapes."
Expand Down
2 changes: 1 addition & 1 deletion resources/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Brush-Item: 396
Maximum-Radius: 15

# Maximum undo stores to save, old ones will get destroyed automatically. Setting this number too high could result in lag or data loss.
Maximum-Undo-Stores: 7
Maximum-Undo-Stores: 15

# Whether to reset the size, or make it remain the current size when smallest size with decrement brush is reached.
Reset-Decrement-Brush: true
Expand Down
21 changes: 12 additions & 9 deletions src/Sandertv/BlockSniper/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class Loader extends PluginBase {

const VERSION = "1.1.0";
const VERSION = "1.2.0";
const API_TARGET = "2.0.0 - 3.0.0-ALPHA3";

public $undoStore;
Expand All @@ -37,7 +37,13 @@ class Loader extends PluginBase {
public $language;

public function onEnable() {
$this->getLogger()->info(TF::GREEN . "BlockSniper has been enabled.");
$this->reloadAll();

$this->registerCommands();
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
}

public function reloadAll() {
$this->brush = new Brush($this);
$this->undoStore = new UndoStorer($this);
$this->cloneStore = new CloneStorer($this);
Expand All @@ -47,21 +53,18 @@ public function onEnable() {
if(!is_dir($this->getDataFolder() . "templates/")) {
mkdir($this->getDataFolder() . "templates/");
}
$this->saveResource("settings.yml");
$this->settings = new Config($this->getDataFolder() . "settings.yml", Config::YAML);

// Language file setup
if(!is_dir($this->getDataFolder() . "languages/")) {
mkdir($this->getDataFolder() . "languages/");
}

$this->saveResource("settings.yml");
$this->settings = new Config($this->getDataFolder() . "settings.yml", Config::YAML);

if(!$this->setupLanguageFile()) {
$this->getLogger()->info(TF::AQUA . "[BlockSniper] No valid language selected, English has been auto-selected.\n" . TF::AQUA . "Please setup a language by using /blocksniper language <lang>.");
} else {
$this->getLogger()->info(TF::AQUA . "[BlockSniper] Language selected: " . TF::GREEN . $this->getSettings()->get("Message-Language"));
}

$this->registerCommands();
$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
}

/**
Expand Down
43 changes: 26 additions & 17 deletions src/Sandertv/BlockSniper/UndoStorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class UndoStorer {

public $totalStores = 0;
public $totalStores;
public $undoStore = [];
public $lastUndo;

Expand All @@ -33,8 +33,8 @@ public function saveUndo(array $blocks) {
}
unset($i);

if(count($this->undoStore) === $this->getOwner()->settings->get("Maximum-Undo-Stores")) {
$this->unsetFirstUndo(); // Unset the first undo to make sure the array won't get too big.
if($this->getTotalUndoStores() === $this->getOwner()->settings->get("Maximum-Undo-Stores")) {
$this->unsetFirstUndo();
}
$this->getOwner()->getServer()->getScheduler()->scheduleDelayedTask(new UndoDiminishTask($this->getOwner()), 2400);

Expand All @@ -52,20 +52,22 @@ public function unsetFirstUndo() {
unset($this->undoStore[min(array_keys($this->undoStore))]);
}

public function restoreLastUndo() {
foreach($this->undoStore[max(array_keys($this->undoStore))] as $key => $block) {
$Id = explode("(", $key);
$blockId = $Id[0];
$meta = explode(":", $blockId);
$meta = $meta[1];
$x = $block["x"];
$y = $block["y"];
$z = $block["z"];
$finalBlock = Item::get($blockId, $meta)->getBlock();
$finalBlock->setDamage((int)$meta !== null ? $meta : 0);
$this->getOwner()->getServer()->getLevelByName($block["level"])->setBlock(new Vector3($x, $y, $z), $finalBlock, false, false);
public function restoreLastUndo(int $amount = 1) {
for($currentAmount = 0; $currentAmount < $amount; $currentAmount++) {
foreach($this->undoStore[max(array_keys($this->undoStore))] as $key => $block) {
$Id = explode("(", $key);
$blockId = $Id[0];
$meta = explode(":", $blockId);
$meta = $meta[1];
$x = $block["x"];
$y = $block["y"];
$z = $block["z"];
$finalBlock = Item::get($blockId, $meta)->getBlock();
$finalBlock->setDamage((int)$meta !== null ? $meta : 0);
$this->getOwner()->getServer()->getLevelByName($block["level"])->setBlock(new Vector3($x, $y, $z), $finalBlock, false, false);
}
$this->unsetLastUndo();
}
$this->unsetLastUndo();
}

public function unsetLastUndo() {
Expand All @@ -81,7 +83,7 @@ public function resetUndoStorage() {
* @return bool
*/
public function undoStorageExists() {
if($this->totalStores === 0 || !is_array($this->undoStore) || empty($this->undoStore)) {
if(!is_array($this->undoStore) || empty($this->undoStore)) {
return false;
}
return true;
Expand All @@ -100,4 +102,11 @@ public function getLastUndoBlockAmount() {
public function getLastUndoActivity(): int {
return (time() - $this->lastUndo);
}

/**
* @return int
*/
public function getTotalUndoStores(): int {
return count($this->undoStore);
}
}
38 changes: 36 additions & 2 deletions src/Sandertv/BlockSniper/brush/BaseShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,49 @@ abstract class BaseShape {
const MAX_WORLD_HEIGHT = 256;
const MIN_WORLD_HEIGHT = 0;

const SHAPE_CUBE = 0;
const SHAPE_SPHERE = 1;
const SHAPE_SPHERE = 0;
const SHAPE_CUBE = 1;
const SHAPE_CYLINDER = 2;
const SHAPE_CUBOID = 3;

public function __construct(Loader $main) {
$this->main = $main;
}

/**
* @param string $shape
*
* @return bool
*/
public static function isShape(string $shape): bool {
$shapeConst = strtoupper("shape_" . $shape);
if(defined("self::$shapeConst")) {
return true;
}
return false;
}

/**
* Registers a new Shape. Example:
* Triangle, 4
*
* Defines the shape as a constant making it able to be used.
*
*
* @param string $shape
* @param int $number
*
* @return bool
*/
public static function registerShape(string $shape, int $number): bool {
$shapeConst = strtoupper("shape_" . str_replace("_", "", $shape));
if(defined("self::$shapeConst")) {
return false;
}
define(('Sandertv\BlockSniper\brush\BaseShape\\' . $shapeConst), $number);
return true;
}

public abstract function getName(): string;

public abstract function getPermission(): string;
Expand Down
39 changes: 37 additions & 2 deletions src/Sandertv/BlockSniper/brush/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,54 @@ abstract class BaseType {
const TYPE_REPLACE = 3;
const TYPE_FLATTEN = 4;
const TYPE_DRAIN = 5;
const TYPE_LEAF_BLOWER = 6;
const TYPE_LEAFBLOWER = 6;
const TYPE_CLEAN = 7;
const TYPE_BIOME = 8;
const TYPE_CLEAN_ENTITIES = 9;
const TYPE_CLEANENTITIES = 9;
const TYPE_MELT = 10;
const TYPE_EXPAND = 11;
const TYPE_RAISE = 12;

public $main;

public function __construct(Loader $main) {
$this->main = $main;
}

/**
* @param string $type
*
* @return bool
*/
public static function isType(string $type): bool {
$typeConst = strtoupper("type_" . $type);
if(defined("self::$typeConst")) {
return true;
}
return false;
}

/**
* Registers a new Type. Example:
* Raise, 12
*
* Defines the type as a constant making it able to be used.
*
*
* @param string $type
* @param int $number
*
* @return bool
*/
public static function registerType(string $type, int $number): bool {
$typeConst = strtoupper("type_" . str_replace("_", "", $type));
if(defined("self::$typeConst")) {
return false;
}
define(('Sandertv\BlockSniper\brush\BaseType\\' . $typeConst), $number);
return true;
}

public abstract function getName(): string;

public abstract function getPermission(): string;
Expand Down
86 changes: 7 additions & 79 deletions src/Sandertv/BlockSniper/brush/Brush.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@
use pocketmine\item\Item;
use pocketmine\Player;
use ReflectionClass;
use Sandertv\BlockSniper\brush\shapes\CubeShape;
use Sandertv\BlockSniper\brush\shapes\CuboidShape;
use Sandertv\BlockSniper\brush\shapes\CylinderStandingShape;
use Sandertv\BlockSniper\brush\shapes\SphereShape;
use Sandertv\BlockSniper\brush\types\BiomeType;
use Sandertv\BlockSniper\brush\types\CleanEntitiesType;
use Sandertv\BlockSniper\brush\types\CleanType;
use Sandertv\BlockSniper\brush\types\DrainType;
use Sandertv\BlockSniper\brush\types\ExpandType;
use Sandertv\BlockSniper\brush\types\FillType;
use Sandertv\BlockSniper\brush\types\FlattenType;
use Sandertv\BlockSniper\brush\types\LayerType;
use Sandertv\BlockSniper\brush\types\LeafBlowerType;
use Sandertv\BlockSniper\brush\types\MeltType;
use Sandertv\BlockSniper\brush\types\OverlayType;
use Sandertv\BlockSniper\brush\types\ReplaceType;
use Sandertv\BlockSniper\Loader;

class Brush {
Expand Down Expand Up @@ -188,25 +172,9 @@ public static function setShape(Player $player, string $shape) {
* @return BaseShape
*/
public static function getShape(Player $player): BaseShape {
$shapeName = self::$brush[$player->getId()]["shape"];
switch($shapeName) {
case "cube":
$shape = new CubeShape(self::$owner, $player, $player->getLevel(), self::getSize($player), $player->getTargetBlock(100));
break;
case "sphere":
$shape = new SphereShape(self::$owner, $player, $player->getLevel(), self::getSize($player), $player->getTargetBlock(100));
break;
case "cuboid":
$shape = new CuboidShape(self::$owner, $player, $player->getLevel(), self::getSize($player), self::getHeight($player), $player->getTargetBlock(100));
break;
case "cylinder":
$shape = new CylinderStandingShape(self::$owner, $player, $player->getLevel(), self::getSize($player), self::getHeight($player), $player->getTargetBlock(100));
break;

default:
$shape = new SphereShape(self::$owner, $player, $player->getLevel(), self::getSize($player), $player->getTargetBlock(100));
break;
}
$shapeName = 'Sandertv\BlockSniper\brush\shapes\\' . (ucfirst(self::$brush[$player->getId()]["shape"]) . "Shape");
$shape = new $shapeName(self::$owner, $player, $player->getLevel(), self::getSize($player), $player->getTargetBlock(100));

return $shape;
}

Expand Down Expand Up @@ -235,49 +203,9 @@ public static function getHeight(Player $player): int {
* @return BaseType
*/
public static function getType(Player $player, array $blocks = []): BaseType {
$typeName = self::$brush[$player->getId()]["type"];
switch($typeName) {
case "fill":
$type = new FillType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "clean":
$type = new CleanType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "drain":
$type = new DrainType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "flatten":
$type = new FlattenType(self::$owner, $player, $player->getLevel(), $blocks, $player->getTargetBlock(100));
break;
case "layer":
$type = new LayerType(self::$owner, $player, $player->getLevel(), $blocks, $player->getTargetBlock(100));
break;
case "leafblower":
$type = new LeafBlowerType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "overlay":
$type = new OverlayType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "replace":
$type = new ReplaceType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "expand":
$type = new ExpandType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "melt":
$type = new MeltType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "cleanentities":
$type = new CleanEntitiesType(self::$owner, $player, $player->getLevel(), $blocks);
break;
case "biome":
$type = new BiomeType(self::$owner, $player, $player->getLevel(), $blocks);
break;

default:
$type = new FillType(self::$owner, $player, $player->getLevel(), $blocks);
break;
}
$typeName = 'Sandertv\BlockSniper\brush\types\\' . (ucfirst(self::$brush[$player->getId()]["type"]) . "Type");
$type = new $typeName(self::$owner, $player, $player->getLevel(), $blocks);

return $type;
}

Expand All @@ -289,7 +217,7 @@ public static function setBiome(Player $player, string $biome) {
self::$brush[$player->getId()]["biome"] = $biome;
}

public static function getBiomeIdFromString(Player $player): int {
public static function getBiomeId(Player $player): int {
$biomes = new ReflectionClass('pocketmine\level\generator\biome\Biome');
$const = strtoupper(str_replace(" ", "_", self::$brush[$player->getId()]["biome"]));
if($biomes->hasConstant($const)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Sandertv/BlockSniper/brush/shapes/CuboidShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ class CuboidShape extends BaseShape {
public $center;
public $player;

public function __construct(Loader $main, Player $player, Level $level, float $width = null, float $height = null, Position $center = null) {
public function __construct(Loader $main, Player $player, Level $level, float $width = null, Position $center = null) {
parent::__construct($main);
$this->level = $level;
$this->width = $width;
$this->height = $height;
$this->height = Brush::getHeight($player);
$this->center = $center;
$this->player = $player;
}
Expand Down
Loading

0 comments on commit 19ff9ab

Please sign in to comment.