Skip to content

Commit 653f0db

Browse files
author
Falkirks
committed
Version 1.0
1 parent 2f99494 commit 653f0db

File tree

9 files changed

+113
-23
lines changed

9 files changed

+113
-23
lines changed

plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: BuyCraft
22
main: buycraft\BuyCraft
3-
version: 0.7
3+
version: 1.0
44
author: Falk
55
api: [1.0.0]
66
load: POSTWORLD

resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ debug: false
2525
commandThrottleCount: 150
2626
# Prevent setting of secret
2727
disable-secret-command: false
28+
# Point players to directly to payment screen
29+
directPay: false
30+
# Amount of packages to show per page
31+
packagePageSize: 5
2832

2933
# _
3034
# | |

src/buycraft/commands/BuyCommand.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
namespace buycraft\commands;
33

44
use buycraft\BuyCraft;
5+
use buycraft\task\VisitLinkTask;
56
use pocketmine\command\Command;
67
use pocketmine\command\CommandSender;
78
use pocketmine\command\PluginIdentifiableCommand;
9+
use pocketmine\Player;
810

911
class BuyCommand extends Command implements PluginIdentifiableCommand{
1012
private $main;
@@ -15,7 +17,7 @@ public function __construct(BuyCraft $main){
1517
public function execute(CommandSender $sender, $label, array $args){
1618
if(!$this->getPlugin()->getConfig()->get('disableBuyCommand')){
1719
$pageToView = 0;
18-
$categoryToView = 0;
20+
$categoryToView = false;
1921
if(count($args) > 0){
2022
if($args[0] == "page" && count($args) == 2 || count($args) == 3){
2123
if(count($args) == 2){
@@ -28,7 +30,20 @@ public function execute(CommandSender $sender, $label, array $args){
2830
}
2931
else{
3032
if(count($args) == 1 && is_numeric($args[0])){
31-
//TODO show package
33+
$package = $this->getPlugin()->getPackageManager()->getPackage($args[0]);
34+
if($package !== false){
35+
if($this->getPlugin()->getConfig()->get('directPay')){
36+
$link = $this->getPlugin()->getAuthPayloadSetting('serverStore') . "/checkout/packages?popup=true&action=add&direct=true&package=" . $package->getId() . "&ign=" . $sender->getName();
37+
}
38+
else{
39+
$link = $this->getPlugin()->getAuthPayloadSetting('serverStore') . "/checkout/packages?action=add&package=" . $package->getId() . "&ign=" . $sender->getName();
40+
}
41+
$linkTask = new VisitLinkTask($this->getPlugin(), ['url' => $link], ($sender instanceof Player ? $sender->getName() : false));
42+
$linkTask->call();
43+
}
44+
else{
45+
$sender->sendMessage($this->getPlugin()->getConfig()->get('packageNotFound'));
46+
}
3247
return true;
3348
}
3449
else{
@@ -37,12 +52,27 @@ public function execute(CommandSender $sender, $label, array $args){
3752
}
3853
}
3954
}
40-
if(is_numeric($pageToView) && is_numeric($categoryToView)){
41-
//TODO show category
55+
if(is_numeric($pageToView) && is_numeric($categoryToView) || $categoryToView === false){
56+
$packages = $this->getPlugin()->getPackageManager()->getPage($pageToView, $categoryToView);
57+
if($packages !== false){
58+
if(count($packages) > 0){
59+
foreach($packages as $package){
60+
$sender->sendMessage($this->getPlugin()->getConfig()->get('packageId') . ": " . $package->getNiceId());
61+
$sender->sendMessage($this->getPlugin()->getConfig()->get('packageName') . ": " . $package->getName());
62+
$sender->sendMessage($this->getPlugin()->getConfig()->get('packagePrice') . ": " . $package->getPrice() . ' ' . $this->getPlugin()->getAuthPayloadSetting('serverCurrency'));
63+
$sender->sendMessage("--------");
64+
}
65+
}
66+
else{
67+
$sender->sendMessage($this->getPlugin()->getConfig()->get('pageNotFound'));
68+
}
69+
}
70+
else{
71+
$sender->sendMessage($this->getPlugin()->getConfig()->get('noPackagesForSale'));
72+
}
4273
}
4374
else{
4475
$sender->sendMessage($this->getPlugin()->getConfig()->get('invalidBuyCommand'));
45-
return true;
4676
}
4777
}
4878
else{

src/buycraft/commands/BuyCraftCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public function execute(CommandSender $sender, $label, array $args){
2626
$sender->sendMessage("Scheduled authentication and package reload. If you don't get a success message try again.");
2727
$auth = new AuthenticateTask($this->getPlugin(), [], ($sender instanceof Player ? $sender : false));
2828
$auth->call();
29-
$fetch = new ReloadCategoriesTask($this->getPlugin(), [], ($sender instanceof Player ? $sender : false));
30-
$fetch->call();
3129
}
3230
else{
3331
$sender->sendMessage("Not authenticated with BuyCraft.net.");

src/buycraft/packages/Category.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@ class Category{
88
private $name;
99
private $desc;
1010
private $item;
11+
private $niceId;
1112

12-
public function __construct($id, $name, $desc, $item){
13+
public function __construct($id, $name, $desc, $item, $niceId){
1314
$this->desc = $desc;
1415
$this->id = $id;
1516
$this->item = $item;
1617
$this->name = $name;
18+
$this->niceId = $niceId;
19+
}
20+
21+
/**
22+
* @param mixed $niceId
23+
*/
24+
public function setNiceId($niceId){
25+
$this->niceId = $niceId;
26+
}
27+
28+
/**
29+
* @return mixed
30+
*/
31+
public function getNiceId(){
32+
return $this->niceId;
1733
}
1834

1935
/**

src/buycraft/packages/Package.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Package{
88
private $name;
99
private $desc;
1010
private $price;
11+
private $niceId;
1112

1213
public function __construct($id, $name, $desc, $price, $item, Category $c = null){
1314
$this->category = $c;
@@ -21,6 +22,20 @@ public function __construct($id, $name, $desc, $price, $item, Category $c = null
2122
}
2223
}
2324

25+
/**
26+
* @param mixed $niceId
27+
*/
28+
public function setNiceId($niceId){
29+
$this->niceId = $niceId;
30+
}
31+
32+
/**
33+
* @return mixed
34+
*/
35+
public function getNiceId(){
36+
return $this->niceId;
37+
}
38+
2439
/**
2540
* @return Category
2641
*/

src/buycraft/packages/PackageManager.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ public function __construct(BuyCraft $main){
1212
$this->main = $main;
1313
$this->categories = [];
1414
$this->packages = [];
15+
$this->pageSize = $this->main->getConfig()->get('packagePageSize');
1516
}
1617
public function addCategory($id, $name, $desc, $item){
17-
$this->categories[$id] = new Category($id, $name, $desc, $item);
18+
$this->categories[] = new Category($id, $name, $desc, $item, count($this->categories));
1819
}
1920
public function addPackage($categoryId, $id, $item, $name, $desc, $price){
20-
if(isset($this->categories[$categoryId])){
21-
$this->packages[$id] = new Package($id, $name, $desc, $price, $item, $this->categories[$categoryId]);
21+
$category = $this->getCategoryById($categoryId);
22+
if($category instanceof Category){
23+
$this->packages[] = new Package($id, $name, $desc, $price, $item, $category);
2224
}
2325
else{
24-
$this->packages[$id] = new Package($id, $name, $desc, $price, $item);
26+
$this->packages[] = new Package($id, $name, $desc, $price, $item);
2527
}
2628
}
2729
public function cleanCategories(){
@@ -30,18 +32,42 @@ public function cleanCategories(){
3032
unset($this->categories[$i]);
3133
}
3234
}
35+
foreach($this->packages as $i => $p){
36+
$p->setNiceId($i);
37+
}
3338
}
3439
public function getCategories(){
3540
return $this->categories;
3641
}
37-
public function getCategory($id){
38-
return $this->categories[$id];
42+
public function getCategory($niceId){
43+
return (isset($this->categories[$niceId]) ? $this->categories[$niceId] : false);
44+
}
45+
public function getCategoryById($id){
46+
foreach($this->getCategories() as $category){
47+
if($category->getId() === $id){
48+
return $category;
49+
}
50+
}
51+
return false;
3952
}
4053
public function getPackages(){
4154
return $this->packages;
4255
}
43-
public function getPackage($id){
44-
return $this->packages[$id];
56+
public function getPackage($niceId){
57+
return (isset($this->packages[$niceId]) ? $this->packages[$niceId] : false);
58+
}
59+
public function getPage($page = 0, $category = 0){
60+
$start = $page * $this->pageSize;
61+
if($category === false){
62+
$outArray = array_slice($this->getPackages(), $start, $this->pageSize);
63+
}
64+
elseif($this->getCategory($category) instanceof Category){
65+
$outArray = array_slice($this->getCategory($category)->getPackages(), $start, $this->pageSize);
66+
}
67+
else{
68+
$outArray = false;
69+
}
70+
return $outArray;
4571
}
4672
public function reset(){
4773
$this->categories = [];

src/buycraft/task/ReloadCategoriesTask.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use buycraft\api\ApiAsyncTask;
77
use buycraft\BuyCraft;
88
use pocketmine\command\CommandSender;
9+
use pocketmine\Player;
910

1011
class ReloadCategoriesTask extends ApiAsyncTask{
1112
public function onConfig(BuyCraft $plugin){
@@ -18,13 +19,13 @@ public function onRun(){
1819
}
1920
public function onOutput(BuyCraft $main, CommandSender $sender){
2021
$out = $this->getOutput();
21-
if($out["code"] === 0){
22+
if($out['code'] === 0){
2223
$main->getPackageManager()->reset();
23-
foreach($out["payload"] as $category){
24-
$main->getPackageManager()->addCategory((isset($category["id"]) ? $category["id"] : 0), $category["name"], $category["shortDescription"], $category["guiItemId"]);
24+
foreach($out['payload'] as $category){
25+
$main->getPackageManager()->addCategory((isset($category['id']) ? $category['id'] : 0), $category['name'], $category['shortDescription'], $category['guiItemId']);
2526
}
26-
$sender->sendMessage("Loaded categories.");
27-
$fetch = new ReloadPackagesTask($main, [], ($player !== null ? $player : false));
27+
$sender->sendMessage("Loaded " . count($out['payload']) . " categories.");
28+
$fetch = new ReloadPackagesTask($main, [], ($sender instanceof Player ? $sender : false));
2829
$fetch->call();
2930
}
3031
else{

src/buycraft/task/VisitLinkTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function onRun(){
1717
$this->send();
1818
}
1919
public function onOutput(BuyCraft $main, CommandSender $sender){
20-
$out = $this->getData();
20+
$out = $this->getOutput()['payload'];
2121
if($out !== null && $out !== false){
2222
if(isset($out['url']) && $out['url'] !== null){
2323
$sender->sendMessage($main->getConfig()->get('pleaseVisit') . ": " . $out['url']);

0 commit comments

Comments
 (0)