Esempio n. 1
1
 public function onTransaction(InventoryTransactionEvent $ev)
 {
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     $tg = $ev->getTransaction();
     $pl = null;
     $ti = null;
     foreach ($tg->getInventories() as $i) {
         echo "INV " . get_class($i) . "\n";
         //##DEBUG
         if ($i instanceof PlayerInventory) {
             $pl = $i->getHolder();
         }
         if ($i instanceof TraderInventory) {
             $ti = $i;
         }
     }
     if ($ti == null) {
         return;
     }
     // This does not involve us!
     if ($pl == null) {
         $this->owner->getLogger()->error(mc::_("Unable to identify player in inventory transaction"));
         return;
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     echo "PLAYER " . $pl->getName() . " is buying\n";
     //##DEBUG
     // Calculate total $$
     $xx = $this->getState("trade-inv", $pl, null);
     if ($xx == null) {
         return;
     }
     // This is a normal Chest transaction...
     echo "FROM SHOP " . $xx["shop"] . "\n";
     //##DEBUG
     $added = [];
     foreach ($tg->getTransactions() as $t) {
         if ($t->getInventory() instanceof PlayerInventory) {
             // Handling PlayerInventory changes...
             foreach ($this->playerInvTransaction($t) as $nt) {
                 $added[] = $nt;
             }
             continue;
         }
         foreach ($this->traderInvTransaction($t) as $nt) {
             $added[] = $nt;
         }
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     // Test if the transaction is valid...
     // Make a copy of current inventory
     $tsinv = [];
     foreach ($pl->getInventory()->getContents() as $slot => $item) {
         if ($item->getId() == Item::AIR) {
             continue;
         }
         $tsinv[$slot] = [implode(":", [$item->getId(), $item->getDamage()]), $item->getCount()];
     }
     //var_dump($tsinv);//##DEBUG
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     //print_r($tsinv);//##DEBUG
     // Apply transactions to copy
     foreach ([$tg->getTransactions(), $added] as &$tset) {
         foreach ($tset as &$nt) {
             if ($nt->getInventory() instanceof PlayerInventory) {
                 $item = clone $nt->getTargetItem();
                 $slot = $nt->getSlot();
                 if ($item->getId() == Item::AIR) {
                     if (isset($tsinv[$slot])) {
                         unset($tsinv[$slot]);
                     }
                 } else {
                     $tsinv[$slot] = [implode(":", [$item->getId(), $item->getDamage()]), $item->getCount()];
                 }
             }
         }
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     //		print_r($tsinv);//##DEBUG
     echo $pl->getName() . " has " . $xx["money"] . "G\n";
     //##DEBUG
     $total = 0;
     var_dump($xx["money"]);
     //##DEBUG
     var_dump($tsinv);
     //##DEBUG
     foreach ($tsinv as $slot => $item) {
         list($idmeta, $cnt) = $item;
         echo "slot={$slot} idmeta={$idmeta} cnt={$cnt}\n";
         //##DEBUG
         if (!isset($this->keepers[$xx["shop"]]["items"][$idmeta])) {
             $this->shopMsg($pl, $xx["shop"], "inventory-error");
             $ev->setCancelled();
             return;
         }
         list($i, $price) = $this->keepers[$xx["shop"]]["items"][$idmeta];
         echo "-{$idmeta} - {$cnt}/" . $i->getCount() . " *  {$price} ..." . round($cnt / $i->getCount()) * $price . "\n";
         //##DEBUG
         $total += round($cnt / $i->getCount()) * $price;
     }
     echo "TOTAL={$total}\n";
     //##DEBUG
     if ($total > $xx["money"]) {
         $this->shopMsg($pl, $xx["shop"], "not-enough-g", $total, $xx["money"]);
         $ev->setCancelled();
         return;
     }
     echo __METHOD__ . "," . __LINE__ . "\n";
     //##DEBUG
     foreach ($added as $nt) {
         $tg->addTransaction($nt);
     }
     // Make sure inventory is properly synced
     foreach ($tg->getInventories() as $i) {
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 5);
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 10);
         $this->owner->getServer()->getScheduler()->scheduleDelayedTask(new PluginCallbackTask($this->owner, [$i, "sendContents"], [$pl]), 15);
     }
 }
 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. 3
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. 4
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. 5
0
 /**
  * @priority LOW
  */
 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");
         }
     }
 }