Skip to content
This repository has been archived by the owner on Mar 12, 2021. It is now read-only.

Commit

Permalink
Fix weather cmd and ticking
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenophilicy committed Jul 5, 2020
1 parent 80b5788 commit ea6855d
Show file tree
Hide file tree
Showing 31 changed files with 113 additions and 136 deletions.
5 changes: 3 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: TableSpoon
main: Xenophilicy\TableSpoon\TableSpoon
version: 0.0.3-ALPHA
version: 0.0.4-ALPHA
api: 3.0.0
authors: [CortexPE, Xenophilicy, HyperFlareMC]
description: Adds cool features to PocketMine
description: Adds cool features to PocketMine
load: STARTUP
6 changes: 2 additions & 4 deletions src/Xenophilicy/TableSpoon/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
use pocketmine\network\mcpe\protocol\types\SpawnSettings;
use pocketmine\Player;
use pocketmine\plugin\Plugin;
use Xenophilicy\TableSpoon\level\LevelManager;
use Xenophilicy\TableSpoon\level\weather\Weather;
use Xenophilicy\TableSpoon\player\PlayerSession;
use Xenophilicy\TableSpoon\player\PlayerSessionManager;
Expand Down Expand Up @@ -77,9 +76,8 @@ public function onPlayerQuit(PlayerQuitEvent $event): void{
* @priority LOWEST
*/
public function onLevelLoad(LevelLoadEvent $ev){
LevelManager::init();
$level = $ev->getLevel();
$TEMPORARY_ENTITIES = [Entity::XP_ORB, Entity::LIGHTNING_BOLT,];
$TEMPORARY_ENTITIES = [Entity::XP_ORB, Entity::LIGHTNING_BOLT];
$lvlWeather = TableSpoon::$weatherData[$level->getId()] = new Weather($level, 0);
if(TableSpoon::$settings["weather"]["enabled"]){
$lvlWeather->setCanCalculate(($level->getName() != TableSpoon::$settings["dimensions"]["nether"]["name"] && $level->getName() != TableSpoon::$settings["dimensions"]["end"]["name"]));
Expand Down Expand Up @@ -188,7 +186,7 @@ public function onInteract(PlayerInteractEvent $event){
$item = $event->getItem();
$player = $event->getPlayer();
$check = ($event->getAction() == PlayerInteractEvent::RIGHT_CLICK_BLOCK || $event->getAction() == PlayerInteractEvent::RIGHT_CLICK_AIR);
$isBlocked = (in_array($event->getBlock()->getId(), [Block::ITEM_FRAME_BLOCK,]));
$isBlocked = (in_array($event->getBlock()->getId(), [Block::ITEM_FRAME_BLOCK]));
if(!$check || $isBlocked) return;
if(!$event->getItem() instanceof Armor) return;
$inventory = $player->getArmorInventory();
Expand Down
2 changes: 1 addition & 1 deletion src/Xenophilicy/TableSpoon/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function genNetherSpawn(Position $pos, Level $level): Position{
$level->setBlock(new Vector3($pos->x + $x, $pos->y - 1, $pos->z + $z), new Obsidian());
}
}
return new Position($pos->x-1, $pos->y, $pos->z-1, $level);
return new Position($pos->x - 1, $pos->y, $pos->z - 1, $level);
}

private static function checkBlock(Block $block): bool{
Expand Down
4 changes: 2 additions & 2 deletions src/Xenophilicy/TableSpoon/block/Beacon.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getHardness(): float{
*/
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null): bool{
$this->getLevel()->setBlock($this, $this, true, true);
$nbt = new CompoundTag("", [new StringTag("id", Tile::BEACON), new ByteTag("isMovable", 0), new IntTag("primary", 0), new IntTag("secondary", 0), new IntTag("x", $blockReplace->x), new IntTag("y", $blockReplace->y), new IntTag("z", $blockReplace->z),]);
$nbt = new CompoundTag("", [new StringTag("id", Tile::BEACON), new ByteTag("isMovable", 0), new IntTag("primary", 0), new IntTag("secondary", 0), new IntTag("x", $blockReplace->x), new IntTag("y", $blockReplace->y), new IntTag("z", $blockReplace->z)]);
Tile::createTile(Tile::BEACON, $this->getLevel(), $nbt);
return true;
}
Expand All @@ -107,7 +107,7 @@ public function onActivate(Item $item, Player $player = null): bool{
/** @var TileBeacon $beacon */
$beacon = $t;
}else{
$nbt = new CompoundTag("", [new StringTag("id", Tile::BEACON), new ByteTag("isMovable", 0), new IntTag("primary", 0), new IntTag("secondary", 0), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z),]);
$nbt = new CompoundTag("", [new StringTag("id", Tile::BEACON), new ByteTag("isMovable", 0), new IntTag("primary", 0), new IntTag("secondary", 0), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z)]);
$beacon = Tile::createTile(Tile::BEACON, $this->getLevel(), $nbt);
}
if($player->isCreative() && TableSpoon::$settings["player"]["limited-creative"]){
Expand Down
4 changes: 2 additions & 2 deletions src/Xenophilicy/TableSpoon/block/BrewingStand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BrewingStand extends PMBrewingStand{
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null): bool{
$parent = parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
if(!$blockReplace->getSide(Vector3::SIDE_DOWN)->isTransparent()){
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::BREWING_STAND), new IntTag(Tile::TAG_X, (int)$this->x), new IntTag(Tile::TAG_Y, (int)$this->y), new IntTag(Tile::TAG_Z, (int)$this->z),]);
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::BREWING_STAND), new IntTag(Tile::TAG_X, (int)$this->x), new IntTag(Tile::TAG_Y, (int)$this->y), new IntTag(Tile::TAG_Z, (int)$this->z)]);
$nbt->setInt(BrewingStandTile::TAG_BREW_TIME, BrewingStandTile::MAX_BREW_TIME);
if($item->hasCustomName()){
$nbt->setString("CustomName", $item->getCustomName());
Expand All @@ -50,7 +50,7 @@ public function onActivate(Item $item, Player $player = null): bool{
if($tile instanceof BrewingStandTile){
$player->addWindow($tile->getInventory());
}else{
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::BREWING_STAND), new IntTag(Tile::TAG_X, (int)$this->x), new IntTag(Tile::TAG_Y, (int)$this->y), new IntTag(Tile::TAG_Z, (int)$this->z),]);
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::BREWING_STAND), new IntTag(Tile::TAG_X, (int)$this->x), new IntTag(Tile::TAG_Y, (int)$this->y), new IntTag(Tile::TAG_Z, (int)$this->z)]);
$nbt->setInt(BrewingStandTile::TAG_BREW_TIME, BrewingStandTile::MAX_BREW_TIME);
if($item->hasCustomName()){
$nbt->setString("CustomName", $item->getCustomName());
Expand Down
2 changes: 1 addition & 1 deletion src/Xenophilicy/TableSpoon/block/EndPortalFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct($meta = 0){
* @return bool
*/
public function place(Item $item, Block $block, Block $target, int $face, Vector3 $facePos, Player $player = null): bool{
$faces = [0 => 3, 1 => 0, 2 => 1, 3 => 2,];
$faces = [0 => 3, 1 => 0, 2 => 1, 3 => 2];
$this->meta = $faces[$player instanceof Player ? $player->getDirection() : 0];
$this->getLevel()->setBlock($block, $this, true, true);
return true;
Expand Down
1 change: 0 additions & 1 deletion src/Xenophilicy/TableSpoon/block/Fire.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function onRandomTick(): void{
if(!$forever){
if($weather->canCalculate()){
$rainy = ($weather->isRainy() || $weather->isRainyThunder());

if($rainy && (Utils::canSeeSky($this->getLevel(), $this->asVector3()) || Utils::canSeeSky($this->getLevel(), $this->getSide(Vector3::SIDE_NORTH)) || Utils::canSeeSky($this->getLevel(), $this->getSide(Vector3::SIDE_SOUTH)) || Utils::canSeeSky($this->getLevel(), $this->getSide(Vector3::SIDE_EAST)) || Utils::canSeeSky($this->getLevel(), $this->getSide(Vector3::SIDE_WEST)))){
$this->level->setBlock($this, BlockFactory::get(Block::AIR));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Xenophilicy/TableSpoon/block/Hopper.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function onActivate(Item $item, Player $player = null): bool{
}
$player->addWindow($t->getInventory());
}else{
$nbt = new CompoundTag("", [new ListTag("Items", []), new StringTag("id", Tile::HOPPER), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z),]);
$nbt = new CompoundTag("", [new ListTag("Items", []), new StringTag("id", Tile::HOPPER), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z)]);
/** @var HopperTile $t */
$t = Tile::createTile(Tile::HOPPER, $this->getLevel(), $nbt);
if($player->isCreative() and TableSpoon::$settings["player"]["limited-creative"]){
Expand All @@ -76,11 +76,11 @@ public function onActivate(Item $item, Player $player = null): bool{
}

public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null): bool{
$faces = [0 => 0, 1 => 0, 2 => 3, 3 => 2, 4 => 5, 5 => 4,];
$faces = [0 => 0, 1 => 0, 2 => 3, 3 => 2, 4 => 5, 5 => 4];
$this->meta = $faces[$face];
$this->getLevel()->setBlock($blockReplace, $this, true, true);

$nbt = new CompoundTag("", [new ListTag("Items", []), new StringTag("id", Tile::HOPPER), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z),]);
$nbt = new CompoundTag("", [new ListTag("Items", []), new StringTag("id", Tile::HOPPER), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z)]);

if($item->hasCustomName()){
$nbt->setString("CustomName", $item->getCustomName());
Expand Down
4 changes: 2 additions & 2 deletions src/Xenophilicy/TableSpoon/block/Jukebox.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function place(Item $item, Block $blockReplace, Block $blockClicked, int
$this->getLevel()->setBlock($blockReplace, $this, true, true);
$tile = $this->getLevel()->getTile($this);
if(!($tile instanceof JukeboxTile)){
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::JUKEBOX), new IntTag(Tile::TAG_X, (int)$this->getX()), new IntTag(Tile::TAG_Y, (int)$this->getY()), new IntTag(Tile::TAG_Z, (int)$this->getZ()),]);
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::JUKEBOX), new IntTag(Tile::TAG_X, (int)$this->getX()), new IntTag(Tile::TAG_Y, (int)$this->getY()), new IntTag(Tile::TAG_Z, (int)$this->getZ())]);
Tile::createTile(Tile::JUKEBOX, $this->getLevel(), $nbt);
}
return true;
Expand All @@ -83,7 +83,7 @@ public function onActivate(Item $item, Player $player = null): bool{
}
}
}else{
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::JUKEBOX), new IntTag(Tile::TAG_X, (int)$this->getX()), new IntTag(Tile::TAG_Y, (int)$this->getY()), new IntTag(Tile::TAG_Z, (int)$this->getZ()),]);
$nbt = new CompoundTag("", [new StringTag(Tile::TAG_ID, Tile::JUKEBOX), new IntTag(Tile::TAG_X, (int)$this->getX()), new IntTag(Tile::TAG_Y, (int)$this->getY()), new IntTag(Tile::TAG_Z, (int)$this->getZ())]);
/** @var JukeboxTile $tile */
$tile = Tile::createTile(Tile::JUKEBOX, $this->getLevel(), $nbt);

Expand Down
2 changes: 1 addition & 1 deletion src/Xenophilicy/TableSpoon/block/SlimeBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public function hasEntityCollision(): bool{
* @return array
*/
public function getDrops(Item $item): array{
return [Item::get(Item::SLIME_BLOCK, 0, 1),];
return [Item::get(Item::SLIME_BLOCK, 0, 1)];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@
*/
abstract class PortalMultiBlock implements MultiBlock{

/** @var int */
private $teleportDuration;

/**
* PortalMultiBlock constructor.
*/
public function __construct(){
$this->teleportDuration = 1;
}

final public function getTeleportationDuration(): int{
return $this->teleportDuration;
final public function getTeleportationDuration(Player $player): int{
return $player->isAdventure() || $player->isSurvival() ? 80 : 1;
}

abstract public function getTargetWorldInstance(): Level;
Expand Down
126 changes: 59 additions & 67 deletions src/Xenophilicy/TableSpoon/commands/WeatherCommand.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<?php

declare(strict_types=1);

namespace Xenophilicy\TableSpoon\commands;

use pocketmine\command\CommandSender;
use pocketmine\command\defaults\VanillaCommand;
use pocketmine\lang\TranslationContainer;
use pocketmine\level\Level;
use pocketmine\Player;
use pocketmine\utils\TextFormat;
use pocketmine\utils\TextFormat as TF;
use Xenophilicy\TableSpoon\level\weather\Weather;
use Xenophilicy\TableSpoon\TableSpoon;

Expand All @@ -19,12 +18,11 @@
class WeatherCommand extends VanillaCommand{

/**
* WeatherCommand constructor.
*
* WeatherCommand constructor
* @param string $name
*/
public function __construct($name){
parent::__construct($name, "Changes the Weather", "/weather [level] < get | clear | sunny | rain | rainy_thunder | thunder >");
parent::__construct($name, "Set the weather on a level");
$this->setPermission("pocketmine.command.weather");
}

Expand All @@ -36,75 +34,69 @@ public function __construct($name){
* @return bool
*/
public function execute(CommandSender $sender, $currentAlias, array $args){
if(!$this->testPermission($sender)){
return true;
}
if(count($args) < 1){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
if($sender instanceof Player){
if($args[0] == "get"){
switch(TableSpoon::$weatherData[$sender->getLevel()->getId()]->getWeather()){
case 0:
$sender->sendMessage("Weather: Clear");
return true;
case 1:
$sender->sendMessage("Weather: Rainy");
return true;
case 2:
$sender->sendMessage("Weather: Rainy Thunder");
return true;
case 3:
$sender->sendMessage("Weather: Thunder");
return true;
}
if(!$this->testPermission($sender)) return true;
if(count($args) === 1){
if(!$sender instanceof Player){
$sender->sendMessage(TF::RED . "You must specify a level");
return false;
}
$wea = Weather::getWeatherFromString($args[0]);
if(!isset($args[1])) $duration = mt_rand(min(TableSpoon::$settings["weather"]["min"], TableSpoon::$settings["weather"]["max"]), max(TableSpoon::$settings["weather"]["min"], TableSpoon::$settings["weather"]["max"]));else $duration = (int)$args[1];
if($wea >= 0 and $wea <= 3){
TableSpoon::$weatherData[$sender->getLevel()->getId()]->setWeather($wea, $duration);
$sender->sendMessage("Weather Successfully changed on " . $sender->getLevel()->getName());
if($args[0] === "get"){
$sender->sendMessage($this->getWeather(TableSpoon::$weatherData[$sender->getLevel()->getId()]->getWeather()));
return true;
}else{
$sender->sendMessage(TextFormat::RED . "Invalid Weather");
}
$this->applyWeather($sender->getLevel(), Weather::getWeatherFromString($args[0]), $sender);
}elseif(count($args) === 2){
$level = $sender->getServer()->getLevelByName($args[0]);
if(!$level instanceof Level){
$sender->sendMessage(TF::RED . "Couldn't find level " . $args[0]);
return false;
}
}
if(count($args) < 2){
$sender->sendMessage(new TranslationContainer("commands.generic.usage", [$this->usageMessage]));
return false;
}
$level = $sender->getServer()->getLevelByName($args[0]);
if(!$level instanceof Level){
$sender->sendMessage(TextFormat::RED . "Couldn't find level: " . $args[0]);
if($args[1] === "get"){
$sender->sendMessage($this->getWeather(TableSpoon::$weatherData[$level->getId()]->getWeather()));
return true;
}
$this->applyWeather($level, Weather::getWeatherFromString($args[1]), $sender);
}else{
$sender->sendMessage(TF::RED . "Usage: /weather [level] <get|clear|sunny|rain|storm|thunder>");
return false;
}
if($args[1] == "get"){
switch(TableSpoon::$weatherData[$level->getId()]->getWeather()){
case 0:
$sender->sendMessage("Weather: Clear");
return true;
case 1:
$sender->sendMessage("Weather: Rainy");
return true;
case 2:
$sender->sendMessage("Weather: Rainy Thunder");
return true;
case 3:
$sender->sendMessage("Weather: Thunder");
return true;
}
return true;
}

/**
* @param int $weather
* @return string
*/
private function getWeather(int $weather): string{
switch($weather){
case 0:
return TF::GREEN . "Weather: Clear";
case 1:
return TF::GREEN . "Weather: Rainy";
case 2:
return TF::GREEN . "Weather: Rainy Thunder";
case 3:
return TF::GREEN . "Weather: Thunder";
default:
return TF::YELLOW . "Weather: Unknown";
}
$wea = Weather::getWeatherFromString($args[1]);
if(!isset($args[1])) $duration = mt_rand(min(TableSpoon::$settings["weather"]["min"], TableSpoon::$settings["weather"]["max"]), max(TableSpoon::$settings["weather"]["min"], TableSpoon::$settings["weather"]["max"]));else $duration = (int)$args[1];
if($wea >= 0 and $wea <= 3){
TableSpoon::$weatherData[$level->getId()]->setWeather($wea, $duration);
$sender->sendMessage("Weather Successfully changed on " . $level->getName());
return true;
}

/**
* @param Level $level
* @param int $weather
* @param CommandSender $sender
* @return void
*/
private function applyWeather(Level $level, int $weather, CommandSender $sender){
$min = TableSpoon::$settings["weather"]["duration"]["minimum"];
$max = TableSpoon::$settings["weather"]["duration"]["maximum"];
if(!isset($args[1])) $duration = mt_rand($min, $max);else $duration = (int)$args[1];
if($weather >= 0 and $weather <= 3){
TableSpoon::$weatherData[$level->getId()]->setWeather($weather, $duration);
$sender->sendMessage(TF::GREEN . "Weather Successfully changed on " . $level->getName());
}else{
$sender->sendMessage(TextFormat::RED . "Invalid Weather");
return false;
$sender->sendMessage(TF::RED . "Invalid Weather");
}
}
}
Loading

0 comments on commit ea6855d

Please sign in to comment.