Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelwhiz committed Aug 4, 2024
1 parent c373a6c commit 162d944
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 189 deletions.
6 changes: 6 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ version: 1.0.0
main: pixelwhiz\minecart\Main
api: 5.0.0
author: pixelwhiz
depend:
- EconomyAPI
src-namespace-prefix: pixelwhiz\minecart
permissions:
gokart.cmd:
default: true
description: Allows players to use the /gokart command.
...
4 changes: 2 additions & 2 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace pixelwhiz\minecart;

use pixelwhiz\minecart\commands\GokartCommands;
use pixelwhiz\minecart\entity\Minecart;
use pixelwhiz\minecart\listeners\GasStationListener;
use pixelwhiz\minecart\listeners\MinecartListener;
Expand Down Expand Up @@ -32,9 +33,8 @@ public function onEnable(): void
parent::onEnable();
self::$instance = $this;
Minecarts::init();
$this->config = new Config($this->getDataFolder() . "GasStation.json", Config::JSON);
Server::getInstance()->getCommandMap()->register("gokart", new GokartCommands());
Server::getInstance()->getPluginManager()->registerEvents(new MinecartListener(), $this);
Server::getInstance()->getPluginManager()->registerEvents(new GasStationListener(), $this);
EntityFactory::getInstance()->register(Minecart::class, function (World $world, CompoundTag $nbt): Entity {
return new Minecart(EntityDataHelper::parseLocation($nbt, $world), $nbt);
}, ["Minecart"]);
Expand Down
22 changes: 22 additions & 0 deletions src/Minecarts.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Minecarts {

public static self $instance;

public static array $isMoving = [];
public static array $isRecharging = [];

public static function init() : void {
self::$instance = new self();
}
Expand All @@ -45,6 +48,8 @@ public function ride(Player $player, PMMinecart $item) {
$energy = $namedTag->getTag("Energy") ? $namedTag->getFloat("Energy") : 100;
$entity::$energy = $energy;

self::$isMoving[$entity->getId()] = false;

$player->getInventory()->setItemInHand(VanillaItems::AIR());

$link = new SetActorLinkPacket();
Expand All @@ -62,6 +67,9 @@ public function unride(Player $player = null, Minecart $entity, bool $isClose =
} elseif ($isClose === true) {
$entity->close();
}

unset(self::$isMoving[$entity->getId()]);

$item = VanillaItems::MINECART();
$entity->setTargetEntity(null);
$player->setTargetEntity(null);
Expand Down Expand Up @@ -94,4 +102,18 @@ public function getRider(Minecart $entity) : ?Player {
return $player;
}

public function isMoving(Minecart $entity) : bool {
if (!isset(self::$isMoving[$entity->getId()]) || self::$isMoving[$entity->getId()] === false) {
return false;
}
return true;
}

public function isRecharging(Minecart $entity) : bool {
if (!isset(self::$isRecharging[$entity->getId()]) || self::$isRecharging[$entity->getId()] === false) {
return false;
}
return true;
}

}
23 changes: 22 additions & 1 deletion src/commands/GokartCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,41 @@

namespace pixelwhiz\minecart\commands;

use pixelwhiz\minecart\Minecarts;
use pixelwhiz\minecart\utils\GasStation;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\lang\Translatable;
use pocketmine\player\Player;
use pocketmine\utils\TextFormat;

class GokartCommands extends Command {

public function __construct()
{
parent::__construct("gokart", "Gokart commands", "/gokart spawn shopkeeper", [""]);
parent::__construct("gokart", "Recharge your minecart energy", TextFormat::GRAY."Usage: ".TextFormat::RED."/gokart recharge", [""]);
$this->setPermission("gokart.cmd");
}

public function execute(CommandSender $sender, string $commandLabel, array $args)
{
if (!$sender instanceof Player) return false;

if (!isset($args[0])) {
$sender->sendMessage($this->getUsage());
return false;
}

if ($args[0] === "recharge") {
$entity = Minecarts::getInstance()->getMinecart($sender);
if ($entity === null) {
$sender->sendMessage(TextFormat::RED."You must be in a minecart to use this command!");
return false;
}

GasStation::open($sender, $entity);
}
return true;
}

}
5 changes: 3 additions & 2 deletions src/entity/Minecart.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function walk(): bool {

Controller::shouldDrop($this);
Controller::shouldDespawn($this);
Controller::updateFuel($this, self::$startPos);
Controller::updateEnergy($this, self::$startPos);

$motion = Controller::getMotion($this);
$motionX = $motion["x"];
Expand All @@ -64,7 +64,7 @@ public function walkBackward(): bool {

Controller::shouldDrop($this);
Controller::shouldDespawn($this);
Controller::updateFuel($this, self::$startPos);
Controller::updateEnergy($this, self::$startPos);

$motion = Controller::getMotion($this);
$motionX = -$motion["x"] / 2;
Expand All @@ -81,6 +81,7 @@ protected function entityBaseTick(int $tickDiff = 1): bool
{
$player = Minecarts::getInstance()->getRider($this);
if ($player instanceof Player) {
if (Minecarts::getInstance()->isRecharging($this) === true) return false;
$player->sendTip("Minecart Energy: ". (int)$this->getEnergy() . "%%");
return true;
}
Expand Down
46 changes: 0 additions & 46 deletions src/entity/RefuelingOfficer.php

This file was deleted.

80 changes: 0 additions & 80 deletions src/forms/EnergyShop.php

This file was deleted.

44 changes: 0 additions & 44 deletions src/listeners/GasStationListener.php

This file was deleted.

3 changes: 3 additions & 0 deletions src/listeners/MinecartListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function onReceive(DataPacketReceiveEvent $event) {
];
}

Minecarts::$isMoving[$entity->getId()] = true;
$entity->walk();
} else if ($isSPressed) {
if (count(array_filter($entity::$startPos, fn($value) => $value === null)) === 3) {
Expand All @@ -72,8 +73,10 @@ public function onReceive(DataPacketReceiveEvent $event) {
];
}

Minecarts::$isMoving[$entity->getId()] = true;
$entity->walkBackward();
} else if (!$isWPressed && !$isSPressed) {
Minecarts::$isMoving[$entity->getId()] = false;
$entity::$startPos = [
"x" => null,
"y" => null,
Expand Down
Loading

0 comments on commit 162d944

Please sign in to comment.