Skip to content

Commit

Permalink
fix for #16 and #17
Browse files Browse the repository at this point in the history
  • Loading branch information
xBeastMode committed Feb 15, 2019
1 parent 15c3154 commit 5910e33
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 175 deletions.
19 changes: 3 additions & 16 deletions src/InfinityGamers/Gears/EventListener.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace InfinityGamers\Gears;

use InfinityGamers\Gears\Kit\Acrobat;
use InfinityGamers\Gears\Kit\Berserker;
use InfinityGamers\Gears\Kit\Kit;
Expand All @@ -16,9 +14,7 @@
use pocketmine\inventory\PlayerInventory;
use pocketmine\level\sound\DoorSound;
use pocketmine\Player;

class EventListener implements Listener{

/** @var Gears */
protected $core;

Expand All @@ -30,7 +26,7 @@ class EventListener implements Listener{
*
*/
public function __construct(Gears $core){;
$this->core = $core;;
$this->core = $core;
}

/**
Expand All @@ -47,14 +43,11 @@ public function pickup(InventoryPickupItemEvent $event){
if($data->hasTag("scorpio")){
$launcher = $data->getString("launcher");
$thrower = $this->core->getServer()->getPlayerExact($launcher);

if($thrower instanceof Player){

if($player === $thrower){
$event->setCancelled();
return;
}

$player->teleport($thrower);
$player->level->addSound(new DoorSound($player));
}
Expand All @@ -81,9 +74,7 @@ public function move(PlayerMoveEvent $e){
*
*/
public function interact(PlayerInteractEvent $e){

$player = $e->getPlayer();

if($this->core->getStorage()->isKitEnabled($player)){
$this->core->getKitManager()->callEvent([
'Player' => $e->getPlayer(),
Expand Down Expand Up @@ -115,16 +106,13 @@ public function interact(PlayerInteractEvent $e){
*
*/
public function damage(EntityDamageEvent $e){

$target = $e->getEntity();

if($target instanceof Player){
$kit = $this->core->getStorage()->getPlayerKit($target);
if($kit instanceof Acrobat && $kit->isAbilityActive() && $e->getCause() === EntityDamageEvent::CAUSE_FALL){
$e->setCancelled();
}
}

if($e instanceof EntityDamageByEntityEvent){
$cause = $e->getDamager();
if($cause instanceof Player and $target instanceof Player){
Expand All @@ -135,9 +123,7 @@ public function damage(EntityDamageEvent $e){
'Item' => $cause->getInventory()->getItemInHand()
],
Kit::HIT_PLAYER_MODE);

$ability = $this->core->getStorage()->getPlayerKit($cause);

if($ability instanceof Berserker && $ability->isAbilityActive()){
$e->setBaseDamage($e->getBaseDamage() * 2);
}
Expand All @@ -154,8 +140,9 @@ public function damage(EntityDamageEvent $e){
public function death(PlayerDeathEvent $e){
$player = $e->getPlayer();

if($this->core->getKitManager()->unloadKit($player)){
if($this->core->getStorage()->isKitEnabled($player)){
$this->core->getStorage()->getPlayerKit($player)->onDeath($player);
$this->core->getKitManager()->unloadKit($player);
}
}

Expand Down
97 changes: 13 additions & 84 deletions src/InfinityGamers/Gears/Gears.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,21 @@ public function setUpDirectories(){
}

public function registerKits(){
$kits = [];

$kit = $this->parseKitData('Acrobat');
if($kit !== null){
$kits[] = new Acrobat($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Bartender');
if($kit !== null){
$kits[] = new Bartender($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Berserker');
if($kit !== null){
$kits[] = new Berserker($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Iceman');
if($kit !== null){
$kits[] = new Iceman($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Magneto');

if($kit !== null){
$kits[] = new Magneto($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Scorpio');
if($kit !== null){
$kits[] = new Scorpio($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Spider');
if($kit !== null){
$kits[] = new Spider($kit[0], $kit[1], $kit[2], $kit[3]);
}

$kit = $this->parseKitData('Swapper');
if($kit !== null){
$kits[] = new Swapper($kit[0], $kit[1], $kit[2], $kit[3]);
}
$kitClasses = [
'Acrobat' => Acrobat::class,
'Bartender' => Bartender::class,
'Berserker' => Berserker::class,
'Iceman' => Iceman::class,
'Magneto' => Magneto::class,
'Scorpio' => Scorpio::class,
'Spider' => Spider::class,
'Swapper' => Swapper::class,
'Thor' => Thor::class
];

$kit = $this->parseKitData('Thor');
if($kit !== null){
$kits[] = new Thor($kit[0], $kit[1], $kit[2], $kit[3]);
foreach($kitClasses as $kitName => $kitClass){
$this->getKitManager()->registerKit(new $kitClass(...$this->parseKitData($kitName)));
}

$this->getKitManager()->registerKits($kits);
}

/**
Expand Down Expand Up @@ -185,19 +151,14 @@ public function setRawKitConfigData(string $kit, array $config){
*/
public function runKitCommands(Player $player, string $kitName){
$kit = $this->kits->get($kitName);

if($kit !== false){

foreach($kit['commands'] as &$command){
$command = str_replace(["@player", "@kit"],
[$player->getName(), $kitName], $command);
$command = RandomUtils::colorMessage($command);
}

$this->sendCommands($kit['commands']);

}

return null;

}
Expand All @@ -212,17 +173,12 @@ public function runKitCommands(Player $player, string $kitName){
*/
public function setKitEffects(Player $player, string $kitName){
$kit = $this->kits->get($kitName);

if($kit !== false){

$effects = $this->parseEffects($kit['effects']);

foreach($effects as $effect){
$player->addEffect($effect);
}

}

return null;

}
Expand All @@ -236,7 +192,6 @@ public function setKitEffects(Player $player, string $kitName){
*/
public function parseKitData(string $kitName){
$kit = $this->kits->get($kitName);

if($kit !== false){

$items = array_merge($this->parseItemsWithEnchants([$kit['helmet'], $kit['chest'], $kit['legs'], $kit['boots']]), $this->parseItemsWithEnchants($kit['items']));
Expand All @@ -253,7 +208,6 @@ public function parseKitData(string $kitName){
return [$special, $items, $coolDown, $deactivate];

}

return null;

}
Expand All @@ -266,7 +220,6 @@ public function parseKitData(string $kitName){
*
*/
public function parseKitDataRaw(array $kit){

$items = array_merge($this->parseItemsWithEnchants([$kit['helmet'], $kit['chest'], $kit['legs'], $kit['boots']]), $this->parseItemsWithEnchants($kit['items']));
/** @var \DateTime $coolDown */
$coolDown = exc::stringToTimestamp($kit['cooldown'])[0];
Expand Down Expand Up @@ -302,7 +255,6 @@ public function sendCommands(array $commands){
*/
public function parseEffects(array $effects){
$out = [];

foreach($effects as $effect){
if($effect instanceof EffectInstance){
$out[] = $effect;
Expand All @@ -314,7 +266,6 @@ public function parseEffects(array $effects){
}
}
}

return $out;
}

Expand All @@ -327,41 +278,30 @@ public function parseEffects(array $effects){
*/
public function parseItemsWithEnchants(array $items){
$out = [];

foreach($items as $key => $item){
if($item instanceof Item){
$out[] = $item;
}else{

$parts = explode(':', $item);

$id = array_shift($parts);
$meta = array_shift($parts);
$amount = array_shift($parts);
$name = array_shift($parts);

$item = Item::fromString("$id:$meta");

if(!($item->getId() === Item::AIR)){

$item->setCount($amount);

$parts = implode(":", $parts);

foreach($this->parseEnchants([$parts]) as $enchant){
$item->addEnchantment($enchant);
}

if(strtolower($name) !== "default"){
$item->setCustomName(RandomUtils::colorMessage($name));
}

$out[] = $item;
}

}
}

return $out;
}

Expand All @@ -376,18 +316,14 @@ public function parseItemsWithEnchants(array $items){
public function parseEnchants(array $enchants){
/** @var EnchantmentInstance[] $out */
$out = [];

$i = 1;

/** @var Enchantment $lastEnchantment */
$lastEnchantment = null;

foreach($enchants as $enchant){
if($enchant instanceof EnchantmentInstance){
$out[] = $enchant;
}else{
$parts = explode(':', $enchant);

foreach($parts as $part){
if(($i % 2) === 0){
if($lastEnchantment !== null){
Expand All @@ -400,7 +336,6 @@ public function parseEnchants(array $enchants){
}
}
}

return $out;
}

Expand All @@ -419,33 +354,28 @@ public function getArmorContents(array &$contents): array{
Item::IRON_HELMET,
Item::LEATHER_HELMET
];

$chestplateIds = [
Item::CHAIN_CHESTPLATE,
Item::DIAMOND_CHESTPLATE,
Item::GOLD_CHESTPLATE,
Item::IRON_CHESTPLATE,
Item::LEATHER_CHESTPLATE
];

$leggingIds = [
Item::CHAIN_LEGGINGS,
Item::DIAMOND_LEGGINGS,
Item::GOLD_LEGGINGS,
Item::IRON_LEGGINGS,
Item::LEATHER_LEGGINGS
];

$bootIds = [
Item::CHAIN_BOOTS,
Item::DIAMOND_BOOTS,
Item::GOLD_BOOTS,
Item::IRON_BOOTS,
Item::LEATHER_BOOTS
];

$armor = [];

foreach($contents as $index => $content){
if(in_array($content->getId(), $helmetIds)){
$armor[0] = $content;
Expand All @@ -461,7 +391,6 @@ public function getArmorContents(array &$contents): array{
unset($contents[$index]);
}
}

return $armor;
}
}
6 changes: 0 additions & 6 deletions src/InfinityGamers/Gears/Kit/Acrobat.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,15 @@ public function __construct(Item $specialItem, array $items = [], int $coolDown,
public function onUseSpecialItem($data){
$player = $data['Player'];
$item = $data['Item'];

if(($player instanceof Player) and ($item instanceof Item)){
if(!$item->hasCustomBlockData()) return false;

/** @var CompoundTag $data */
$data = $item->getCustomBlockData();

if(!$data->hasTag("kit_name")) return false;

if(strtolower($data->getString("kit_name")) === "acrobat"){
if($this->checkCoolDown($player)){

$this->getGearsInstance()->getScheduler()->scheduleRepeatingTask(
new AcrobatTask($this->getGearsInstance(), $player, $player->asPosition(), $this->coolDown), 20);

$motion = $player->getDirectionVector();
$motion->x = $motion->x * 2.5;
$motion->y = 1.5;
Expand Down
Loading

0 comments on commit 5910e33

Please sign in to comment.