Skip to content

Commit c1377c8

Browse files
committed
takecoinとseecoinを完成
1 parent fa1ca66 commit c1377c8

5 files changed

Lines changed: 104 additions & 6 deletions

File tree

src/space/yurisi/SecureCoinAPI/SecureCoinAPI.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use pocketmine\plugin\PluginBase;
77
use space\yurisi\SecureCoinAPI\command\addcoinCommand;
88
use space\yurisi\SecureCoinAPI\command\mymoneyCommand;
9+
use space\yurisi\SecureCoinAPI\command\seecoinCommand;
10+
use space\yurisi\SecureCoinAPI\command\takecoinCommand;
911
use space\yurisi\SecureCoinAPI\database\coinJson;
1012
use space\yurisi\SecureCoinAPI\database\historySQLite;
1113
use space\yurisi\SecureCoinAPI\event\player\LoginEvent;
@@ -24,6 +26,8 @@ protected function onEnable(): void {
2426
$this->getServer()->getPluginManager()->registerEvents(new LoginEvent($this), $this);
2527
$this->getServer()->getCommandMap()->register('SecureCoinAPI', new addcoinCommand($this));
2628
$this->getServer()->getCommandMap()->register('SecureCoinAPI', new mymoneyCommand($this));
29+
$this->getServer()->getCommandMap()->register('SecureCoinAPI', new seecoinCommand($this));
30+
$this->getServer()->getCommandMap()->register('SecureCoinAPI', new takecoinCommand($this));
2731
self::$main = $this;
2832
}
2933

@@ -54,8 +58,8 @@ public function addCoin(History $history) {
5458
* @return void
5559
*/
5660
public function takeCoin(History $history) {
57-
$this->coinJson->takeCoin($history->getReceivedPlayer(), $history->getAmount());
58-
$history->setAmount(-$history->getAmount());
61+
$take = $this->coinJson->takeCoin($history->getReceivedPlayer(), $history->getAmount());
62+
$history->setAmount(-$take);
5963
$this->history->registerHistory($history);
6064
}
6165

src/space/yurisi/SecureCoinAPI/command/addcoinCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
4646
null,
4747
$amount,
4848
$this->main->getName(),
49-
'指定したプレイヤーにお金を追加するコマンド'
49+
$this->getDescription()
5050
));
5151

5252
$sender->sendMessage("§c{$receive_player}{$amount}円を与えました");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace space\yurisi\SecureCoinAPI\command;
4+
5+
use pocketmine\command\CommandSender;
6+
use pocketmine\command\defaults\VanillaCommand;
7+
use pocketmine\player\Player;
8+
use space\yurisi\SecureCoinAPI\SecureCoinAPI;
9+
10+
class seecoinCommand extends VanillaCommand {
11+
12+
public function __construct(private SecureCoinAPI $main) {
13+
parent::__construct("seecoin", "プレイヤーのお金を確認します", "/seecoin [playerName]");
14+
}
15+
16+
public function execute(CommandSender $sender, string $commandLabel, array $args) {
17+
if (!isset($args[0])) {
18+
$sender->sendMessage($this->getUsage());
19+
return;
20+
}
21+
22+
$receive_player = $this->main->getServer()->getPlayerByPrefix($args[0]);
23+
24+
if ($receive_player instanceof Player) {
25+
$receive_player = $receive_player->getName();
26+
} else {
27+
$receive_player = $args[0];
28+
}
29+
30+
if (!$this->main->isRegister($receive_player)) {
31+
$sender->sendMessage("対象が存在しません。");
32+
return;
33+
}
34+
35+
$amount = $this->main->getCoin($receive_player);
36+
37+
$sender->sendMessage("§c{$receive_player}の所持金: {$amount}");
38+
}
39+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace space\yurisi\SecureCoinAPI\command;
4+
5+
use pocketmine\command\CommandSender;
6+
use pocketmine\command\defaults\VanillaCommand;
7+
use pocketmine\player\Player;
8+
use space\yurisi\SecureCoinAPI\History;
9+
use space\yurisi\SecureCoinAPI\SecureCoinAPI;
10+
11+
class takecoinCommand extends VanillaCommand {
12+
13+
public function __construct(private SecureCoinAPI $main) {
14+
parent::__construct("takecoin", "プレイヤーからお金を奪います", "/takecoin [playerName] [amount]");
15+
}
16+
17+
public function execute(CommandSender $sender, string $commandLabel, array $args) {
18+
if (!isset($args[0]) || !isset($args[1])) {
19+
$sender->sendMessage($this->getUsage());
20+
return;
21+
}
22+
23+
if (!is_numeric($args[1])) {
24+
$sender->sendMessage($this->getUsage());
25+
return;
26+
}
27+
28+
$amount = (int)floor((int)$args[1]);
29+
30+
$receive_player = $this->main->getServer()->getPlayerByPrefix($args[0]);
31+
32+
if ($receive_player instanceof Player) {
33+
$receive_player = $receive_player->getName();
34+
} else {
35+
$receive_player = $args[0];
36+
}
37+
38+
if (!$this->main->isRegister($receive_player)) {
39+
$sender->sendMessage("対象が存在しません。");
40+
return;
41+
}
42+
43+
$this->main->takeCoin(new History(
44+
$receive_player,
45+
null,
46+
$amount,
47+
$this->main->getName(),
48+
$this->getDescription()
49+
));
50+
51+
$sender->sendMessage("§c{$receive_player}から{$amount}円を奪いました");
52+
}
53+
}

src/space/yurisi/SecureCoinAPI/database/coinJson.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ public function addCoin(string $name, int $amount) {
3838
$this->data[$name] += $amount;
3939
}
4040

41-
public function takeCoin(string $name, int $amount): bool {
41+
public function takeCoin(string $name, int $amount):int {
4242
$name = strtolower($name);
4343
if ($this->data[$name] < $amount) {
44-
return false;
44+
$amount = $this->data[$name];
45+
$this->data[$name] = 0;
46+
return $amount;
4547
}
4648
$this->data[$name] -= $amount;
47-
return true;
49+
return $amount;
4850
}
4951

5052
public function save(): void {

0 commit comments

Comments
 (0)