public function onPlayerTrade(Player $giver, Player $taker)
 {
     $good = clone $giver->getInventory()->getItemInHand();
     $gift = clone $good;
     $gift->setCount(1);
     $taker->getInventory()->addItem($gift);
     $good->setCount($n = $good->getCount() - 1);
     $slot = $giver->getInventory()->getHeldItemSlot();
     if ($n <= 0) {
         $giver->getInventory()->clear($slot);
     } else {
         $giver->getInventory()->setItem($slot, $good);
     }
     $item = MPMU::itemName($good);
     if (MPMU::apiVersion("1.12.0")) {
         $giver->sendTip(mc::_("Gave one %1%", $item));
         $taker->sendTip(mc::_("Received %1%", $item));
     } else {
         $giver->sendMessage(mc::_("Gave one %1%", $item));
         $taker->sendMessage(mc::_("Received %1%", $item));
     }
 }
Esempio n. 2
0
 /**
  * @priority LOWEST
  */
 public function onEntityInteract(EntityDamageEvent $ev)
 {
     if ($ev->isCancelled()) {
         return;
     }
     if (!$ev instanceof EntityDamageByEntityEvent) {
         return;
     }
     $giver = $ev->getDamager();
     if (!$giver instanceof Player) {
         return;
     }
     $taker = $ev->getEntity();
     if (!$taker instanceof TraderNpc) {
         return;
     }
     $ev->setCancelled();
     // OK, now what...
     if ($giver->isCreative() || $giver->isSpectator()) {
         $giver->sendMessage(mc::_("No purchases while in %1% mode.", MPMU::gamemodeStr($giver->getGamemode())));
         return;
     }
     $shop = $taker->namedtag->shop->getValue();
     if (!isset($this->keepers[$shop])) {
         $this->owner->getLogger()->error(mc::_("Invalid shop %5% for NPC at %1%,%2%,%3% (%4%)", $taker->floorX(), $taker->floorY(), $taker->floorZ(), $taker->getLevel()->getName(), $shop));
         $giver->sendMessage(mc::_("Sorry, shop is closed!"));
         return;
     }
     $hand = $giver->getInventory()->getItemInHand();
     if ($this->owner->getCurrency() !== false ? $hand->getId() == $this->owner->getCurrency() : $hand->getId() == Item::GOLD_INGOT) {
         // OK, we want to buy stuff...
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$this, "startTrade"], [$giver, $taker, $shop]), 10);
     } else {
         if ($this->owner->isWeapon($hand)) {
             $this->shopMsg($giver, $shop, "under-attack");
             $giver->attack($this->keepers[$shop]["attack"], new EntityDamageByEntityEvent($taker, $giver, EntityDamageEvent::CAUSE_ENTITY_ATTACK, $this->keepers[$shop]["attack"], 1.0));
         } else {
             $this->shopMsg($giver, $shop, "help-info");
         }
     }
 }
Esempio n. 3
0
 public function playerTouchSign(PlayerInteractEvent $ev)
 {
     if ($ev->getBlock()->getId() != Block::SIGN_POST && $ev->getBlock()->getId() != Block::WALL_SIGN) {
         return;
     }
     //echo "TOUCHED\n";
     $sign = $ev->getPlayer()->getLevel()->getTile($ev->getBlock());
     if (!$sign instanceof Sign) {
         return;
     }
     //echo __METHOD__.",".__LINE__."\n";
     $lines = $sign->getText();
     //print_r($lines);
     //print_r($this->texts);
     if (!isset($this->texts[$lines[0]])) {
         return;
     }
     //echo __METHOD__.",".__LINE__."\n";
     if ($ev->getPlayer()->isCreative() || $ev->getPlayer()->isSpectator()) {
         $ev->getPlayer()->sendMessage(mc::_("No trading possible, while in %1% mode", MPMU::gamemodeStr($ev->getPlayer()->getGamemode())));
         return;
     }
     $this->activateSign($ev->getPlayer(), $sign);
 }
Esempio n. 4
0
 public function startTrade($buyer, $seller, $shop)
 {
     if (!MPMU::access($buyer, "goldstd.shopkeep.shop")) {
         return;
     }
     if ($this->getState("trade-inv", $buyer, null) !== null) {
         echo "Already trading\n";
         //##DEBUG
         return;
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     $l = $seller->getLevel();
     $tile = null;
     for ($i = -2; $i <= 0 && $tile == null; $i--) {
         $pos = $seller->add(0, $i, 0);
         $tile = $l->getTile($pos);
         if ($tile instanceof Chest) {
             break;
         } else {
             $tile = null;
         }
     }
     if ($tile == null) {
         $this->owner->getLogger()->error(mc::_("Error trading with NPC at %1%,%2%,%3% (%4%)", $seller->floorX(), $seller->floorY(), $seller->floorZ(), $seller->getLevel()->getName()));
         $buyer->sendMessage(mc::_("Sorry, nothing happens..."));
         return;
     }
     $inv = ["player" => [], "chest" => null];
     $inv["money"] = $this->owner->getMoney($buyer);
     var_dump($inv["money"]);
     //##DEBUG
     $inv["shop"] = $shop;
     foreach ($buyer->getInventory()->getContents() as $slot => &$item) {
         $inv["player"][$slot] = implode(":", [$item->getId(), $item->getDamage(), $item->getCount()]);
     }
     $inv["chest"] = new TraderInventory($tile, $buyer);
     $contents = [];
     foreach ($this->keepers[$shop]["items"] as $idmeta => $it) {
         $item = clone $it[0];
         $item->setCount(1);
         $contents[] = $item;
     }
     $inv["chest"]->setContents($contents);
     $this->setState("trade-inv", $buyer, $inv);
     $buyer->getInventory()->clearAll();
     $buyer->addWindow($inv["chest"]);
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
 }
Esempio n. 5
0
 public function onCommand(CommandSender $sender, Command $cmd, $label, array $args)
 {
     switch ($cmd->getName()) {
         case "pay":
             if (!$this->trading) {
                 $sender->sendMessage(mc::_("PAY command has been disabled"));
                 return true;
             }
             if (!MPMU::inGame($sender)) {
                 return false;
             }
             if ($sender->isCreative() || $sender->isSpectator()) {
                 $sender->sendMessage(mc::_("You cannot use this in creative or specator mode"));
                 return true;
             }
             if (count($args) == 1) {
                 if (is_numeric($args[0])) {
                     $money = intval($args[0]);
                     if ($this->getMoney($sender->getName()) < $money) {
                         $sender->sendMessage(mc::_("You do not have enough money"));
                         return true;
                     }
                     $this->trading->setAttr($sender, "payment", $money);
                     $sender->sendMessage(mc::_("Next payout will be for %1%G", $money));
                     return true;
                 }
                 return false;
             } elseif (count($args) == 0) {
                 $sender->sendMessage(mc::_("Next payout will be for %1%G", $this->trading->getAttr($sender, "payment")));
                 return true;
             }
             return false;
         case "balance":
             if (!MPMU::inGame($sender)) {
                 return false;
             }
             if ($sender->isCreative() || $sender->isSpectator()) {
                 $sender->sendMessage(mc::_("You cannot use this in creative or specator mode"));
                 return true;
             }
             $sender->sendMessage(mc::_("You have %1%G", $this->getMoney($sender->getName())));
             return true;
         case "shopkeep":
             if ($this->keepers) {
                 return $this->keepers->subCmd($sender, $args);
             }
             $sender->sendMessage(mc::_("shopkeep command disabled"));
             return true;
     }
     return false;
 }
Esempio n. 6
0
 public function startTrade($buyer, $seller, $shop)
 {
     if (!MPMU::access($buyer, "goldstd.shopkeep.shop")) {
         return;
     }
     $this->cart->start($buyer, $this->keepers[$shop]["items"]);
 }