Skip to content

Commit

Permalink
Fix #48 and remove 'default-price' from config and use itemid+itemdat…
Browse files Browse the repository at this point in the history
…a hash for checking banned items list.

The 'default-price' condition is unneeded as it can never occur.
  • Loading branch information
Muqsit committed Mar 30, 2018
1 parent 6d99be8 commit de73600
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 37 deletions.
5 changes: 4 additions & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
---
default-price: 15000
# These items cannot be sold in-game using "/cs add".
banned-items:
- 35:1
- 7

# Whether to enable the "/cs synceconomy" command.
'enable-sync': false
...
92 changes: 58 additions & 34 deletions src/ChestShop/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,6 @@ class Main extends PluginBase{

const PREFIX = TF::BOLD.TF::YELLOW.'CS '.TF::RESET;

const CONFIG = [
'config.yml' => [
'default-price' => 15000,
'banned-items' => [
'35:1',
7,
],
'enable-sync' => false
]
];

const HELP_CMD = [
TF::YELLOW.TF::BOLD.'Chest Shop'.TF::RESET,
TF::YELLOW.'/{cmd} add [price]'.TF::GRAY.' - Add the item in your hand to the chest shop.',
Expand All @@ -65,9 +54,6 @@ class Main extends PluginBase{
const NBT_CHESTSHOP_PRICE = 0;
const NBT_CHESTSHOP_ID = 1;

/** @var int */
public $defaultprice;

/** @var Item[] */
protected $shops = [];

Expand Down Expand Up @@ -104,20 +90,19 @@ public function onEnable() : void{
mkdir($this->getDataFolder());
}

foreach(array_keys(self::CONFIG) as $file){
$this->updateConfig($file);
}
$this->updateConfig("config.yml");
$this->updateConfig("shops.yml");

$shops = yaml_parse_file($this->getDataFolder().'shops.yml');
if(!empty($shops)){
$this->shops = $shops;
}

$config = yaml_parse_file($this->getDataFolder().'config.yml');
$this->defaultprice = $config["default-price"] ?? 15000;
$this->notallowed = array_flip($config["banned-items"] ?? []);

if($config['enable-sync'] == true){
$this->loadBannedItemsList($config["banned-items"]);

if($config['enable-sync']){
$this->economyshop = $this->getServer()->getPluginManager()->getPlugin('EconomyShop');
}else{
$this->economyshop = false;
Expand Down Expand Up @@ -145,7 +130,7 @@ public function initializeMenu() : void{
}

$this->menu = InvMenu::create(InvMenu::TYPE_CHEST)
->readOnly()
->readonly()
->sessionize()
->setName("Chest Shop")
->setListener([new ShopListener($this), "onTransaction"]);
Expand All @@ -159,15 +144,34 @@ public function onDisable() : void{
* Updates config with newer data.
*/
private function updateConfig(string $config) : void{
if(isset(self::CONFIG[$config])){
$path = $this->getDataFolder().$config;
$data = is_file($path) ? yaml_parse_file($path) : self::CONFIG[$config];
foreach(self::CONFIG[$config] as $key => $value){
$path = $this->getDataFolder().$config;

if(!is_file($path)){
if(!$this->saveResource($config)){
throw new \Error("Tried updating a non-existing config file ({$config}).");
}
return;
}

$data = yaml_parse_file($path);

$resource = $this->getResource($config);
$default_config = yaml_parse(stream_get_contents($resource));
fclose($resource);

if(!empty($default_config)){
$changed = false;

foreach($default_config as $key => $value){
if(!isset($data[$key])){
$data[$key] = $value;
$changed = true;
}
}
yaml_emit_file($path, $data);

if($changed){
yaml_emit_file($path, $data);
}
}
}

Expand All @@ -190,8 +194,31 @@ public function reload() : void{
$this->shops = $shops;
}
$config = yaml_parse_file($this->getDataFolder().'config.yml');
$this->defaultprice = $config["default-price"] ?? 15000;
$this->notallowed = array_flip($config["banned-items"] ?? []);
$this->loadBannedItemsList($config["banned-items"]);
}

public function loadBannedItemsList(array $bannedItems) : void{
$corruptedItems = [];

foreach($bannedItems as $itemdata){
$itemdata = explode(":", $itemdata, 2);

if(!isset($itemdata[1])){
$itemdata[1] = 0;
}

if(is_numeric($itemdata[0]) && is_numeric($itemdata[1])){
$itemId = (int) $itemdata[0];
$itemDamage = (int) $itemdata[1];
$this->notallowed[($itemId << 16) | $itemDamage] = 1;
}else{
$corruptedItems[] = implode(":", $itemdata);
}
}

if(!empty($corruptedItems)){
$this->getLogger()->warning("Some items could not be added to the ban list due to incorrect formatting ({implode(", ", $corruptedItems)})");
}
}

/**
Expand All @@ -218,7 +245,7 @@ public function getItemFromShop(int $id) : Item{
public function fillInventoryWithShop(Inventory $inventory, int $page = 0) : void{
$contents = [];

if(count($this->shops) !== 0) {
if(!empty($this->shops)) {
$chunked = array_chunk($this->shops, 24, true);
if($page < 0){
$page = count($chunked) - 1;
Expand Down Expand Up @@ -269,7 +296,7 @@ public function addToChestShop(Item $item, int $price) : void{
* @param int $slot
*/
public function removeItemOffShop(int $page, int $slot) : void{
if(count($this->shops) === 0){
if(empty($this->shops)){
return;
}
$keys = array_keys($this->shops);//$this->shops is an associative array.
Expand All @@ -288,10 +315,7 @@ public function removeItemOffShop(int $page, int $slot) : void{
* @return bool
*/
private function isNotAllowed(int $itemId, int $itemDamage = 0) : bool{
if($itemDamage === 0){
return isset($this->notallowed[$itemId]) || isset($this->notallowed[$itemId.':'.$itemDamage]);
}
return isset($this->notallowed[$itemId.':'.$itemDamage]);
return isset($this->notallowed[($itemId << 16) | $itemDamage]);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/ChestShop/ShopListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ public function onTransaction(Player $player, Item $itemPuttingIn, Item $itemTak
$itemTakingOut = $inventoryAction->getSourceItem();//item in the chest inventory when clicked.

$nbt = $itemTakingOut->getNamedTag();

if($nbt->hasTag("turner")){
$pagedata = $nbt->getIntArray("turner");
$page = $pagedata[Main::NBT_TURNER_DIRECTION] === Main::LEFT_TURNER ? --$pagedata[Main::NBT_TURNER_CURRENTPAGE] : ++$pagedata[Main::NBT_TURNER_CURRENTPAGE];
$this->plugin->fillInventoryWithShop($inventoryAction->getInventory(), $page);
}elseif($nbt->hasTag("ChestShop")){
return true;
}

if($nbt->hasTag("ChestShop")){
$cs = $nbt->getIntArray("ChestShop");

$price = $cs[Main::NBT_CHESTSHOP_PRICE] ?? $this->plugin->defaultprice;
$price = $cs[Main::NBT_CHESTSHOP_PRICE];
if($this->economy->myMoney($player) >= $price){
$item = $this->plugin->getItemFromShop($cs[Main::NBT_CHESTSHOP_ID]);
$player->sendMessage(Main::PREFIX.TF::GREEN.'Purchased '.TF::BOLD.$item->getName().TF::RESET.TF::GREEN.TF::GRAY.' (x'.$item->getCount().')'.TF::GREEN.' for $'.$price.'.');
Expand All @@ -76,6 +80,7 @@ public function onTransaction(Player $player, Item $itemPuttingIn, Item $itemTak
$inventoryAction->getInventory()->onClose($player);
}
}

return true;
}
}

0 comments on commit de73600

Please sign in to comment.