diff --git a/EconomyPShop/plugin.yml b/EconomyPShop/plugin.yml new file mode 100644 index 00000000..d8120b2c --- /dev/null +++ b/EconomyPShop/plugin.yml @@ -0,0 +1,24 @@ +name: EconomyPShop +version: "2.0.0" +author: onebone +main: onebone\economypshop\EconomyPShop + +api: +- 1.6.0 +- 1.7.0 +depend: +- EconomyAPI +- ItemCloud + +permissions: + economypshop.*: + description: Allows all permissions to the player + children: + economypshop.shop.*: + description: Allows player all permissions related to shop + children: + economypshop.shop.create: + description: Allows player to create shop + default: true + economypshop.shop.destroy.others: + description: Allows player to destroy others' shop diff --git a/EconomyPShop/resources/ShopText.yml b/EconomyPShop/resources/ShopText.yml new file mode 100644 index 00000000..3a9654d9 --- /dev/null +++ b/EconomyPShop/resources/ShopText.yml @@ -0,0 +1,5 @@ +pshop: +- "[PSHOP]" +- "%MONETARY_UNIT%%1" +- "%2" +- "Amount: %3" \ No newline at end of file diff --git a/EconomyPShop/resources/config.yml b/EconomyPShop/resources/config.yml new file mode 100644 index 00000000..b9427351 --- /dev/null +++ b/EconomyPShop/resources/config.yml @@ -0,0 +1 @@ +shop-tax: 100 \ No newline at end of file diff --git a/EconomyPShop/resources/language.properties b/EconomyPShop/resources/language.properties new file mode 100644 index 00000000..97ff3204 --- /dev/null +++ b/EconomyPShop/resources/language.properties @@ -0,0 +1,15 @@ +# EconomyPShop language properties file +no-permission-create-shop=You don't have permission to create shop. +insert-right-format=Please write the sign in right format. +shop-created=Your shop was opened with item %3 of "%1" for %MONETARY_UNIT%%2. +shop-removed=Your shop has been removed. +shop-others-removed=%1's shop has been removed. +no-permission-remove-shop=You don't have permission to remove shop. +no-shop-tax=You don't have enough money to pay tax. +same-player=You can't buy items from your shop. +tap-again=Please tap again if you are sure to buy %3 of "%1" for %MONETARY_UNIT%%2 +no-stock=Sorry, there is no enough stock in the shop. +no-space=You have no space in your inventory to buy item. +bought-item=You have bought %3 of "%1" by %MONETARY_UNIT%%2. +no-money=You don't have enough money to buy item. +shop-owner-no-account=Shop owner does not have an ItemCloud account. \ No newline at end of file diff --git a/EconomyPShop/src/onebone/economypshop/EconomyPShop.php b/EconomyPShop/src/onebone/economypshop/EconomyPShop.php new file mode 100644 index 00000000..2ef1c4ba --- /dev/null +++ b/EconomyPShop/src/onebone/economypshop/EconomyPShop.php @@ -0,0 +1,229 @@ +getDataFolder())){ + mkdir($this->getDataFolder()); + } + $this->saveResource("ShopText.yml"); + $this->saveResource("language.properties"); + $this->saveDefaultConfig(); + + $this->shop = (new Config($this->getDataFolder()."Shops.yml", Config::YAML))->getAll(); + $this->shopText = (new Config($this->getDataFolder()."ShopText.yml", Config::YAML)); + $this->lang = (new Config($this->getDataFolder()."language.properties", Config::PROPERTIES)); + + $this->getServer()->getPluginManager()->registerEvents($this, $this); + $this->itemcloud = MainClass::getInstance(); + + $this->tap = []; + $this->placeQueue = []; + } + + public function onDisable(){ + $file = new Config($this->getDataFolder()."Shops.yml", Config::YAML); + $file->setAll($this->shop); + $file->save(); + } + + public function onSignChange(SignChangeEvent $event){ + $line = $event->getLines(); + if(($val = $this->getTag($line[0])) !== false){ + $player = $event->getPlayer(); + if(!$player->hasPermission("economypshop.shop.create")){ + $player->sendMessage($this->getMessage("no-permission-create-shop")); + return; + } + + $money = EconomyAPI::getInstance()->myMoney($player->getName()); + if($money < $this->getConfig()->get("shop-tax")){ + $player->sendMessage($this->getMessage("no-shop-tax")); + return; + } + EconomyAPI::getInstance()->reduceMoney($player->getName(), $this->getConfig()->get("shop-tax"), "EconomyPShop"); + + $cost = $line[1]; + $item = $line[2]; + $amount = $line[3]; + + if(!is_numeric($cost) or !is_numeric($amount)){ + $player->sendMessage($this->getMessage("insert-right-format")); + return; + } + + // Item identify + $item = $this->getItem($line[2]); + if($item === false){ + $player->sendMessage($this->getMessage("item-not-support", array($line[2], "", ""))); + return; + } + if($item[1] === false){ // Item name found + $id = explode(":", strtolower($line[2])); + $line[2] = $item[0]; + }else{ + $tmp = $this->getItem(strtolower($line[2])); + $id = explode(":", $tmp[0]); + } + $id[0] = (int)$id[0]; + if(!isset($id[1])){ + $id[1] = 0; + } + // Item identify end + + $block = $event->getBlock(); + $this->shop[$block->getX().":".$block->getY().":".$block->getZ().":".$block->getLevel()->getFolderName()] = [ + "x" => $block->getX(), + "y" => $block->getY(), + "z" => $block->getZ(), + "level" => $block->getLevel()->getFolderName(), + "owner" => $player->getName(), + "price" => (int) $line[1], + "item" => (int) $id[0], + "itemName" => $line[2], + "meta" => (int) $id[1], + "amount" => (int) $line[3] + ]; + + $mu = EconomyAPI::getInstance()->getMonetaryUnit(); + $event->setLine(0, str_replace("%MONETARY_UNIT%", $mu, $val[0])); + $event->setLine(1, str_replace(["%MONETARY_UNIT%", "%1"], [$mu, $cost], $val[1])); + $event->setLine(2, str_replace(["%MONETARY_UNIT%", "%2"], [$mu, $line[2]], $val[2])); + $event->setLine(3, str_replace(["%MONETARY_UNIT%", "%3"], [$mu, $amount], $val[3])); + + $player->sendMessage($this->getMessage("shop-created", [$line[2], $cost, $amount])); + } + } + + public function onBlockBreak(BlockBreakEvent $event){ + $block = $event->getBlock(); + $loc = $block->getX().":".$block->getY().":".$block->getZ().":".$block->getLevel()->getFolderName(); + if(isset($this->shop[$loc])){ + $player = $event->getPlayer(); + $shop = $this->shop[$loc]; + + if($shop["owner"] == $player->getName()){ + unset($this->shop[$loc]); + $player->sendMessage($this->getMessage("shop-removed")); + }else{ + if($player->hasPermission("economypshop.shop.destroy.others")){ + $player->sendMessage($this->getMessage("shop-others-removed", [$shop["owner"], "%2", "%3"])); + unset($this->shop[$loc]); + }else{ + $player->sendMessage($this->getMessage("no-permission-remove-shop")); + $event->setCancelled(); + } + } + } + } + + public function onBlockTouch(PlayerInteractEvent $event){ + $block = $event->getBlock(); + $loc = $block->getX().":".$block->getY().":".$block->getZ().":".$block->getLevel()->getFolderName(); + if(isset($this->shop[$loc])){ + $player = $event->getPlayer(); + $shop = $this->shop[$loc]; + + if($shop["owner"] == $player->getName()){ + $player->sendMessage($this->getMessage("same-player")); + return; + } + + $now = microtime(true); + if(!isset($this->tap[$player->getName()]) or $now - $this->tap[$player->getName()][1] >= 1.5 or $this->tap[$player->getName()][0] !== $loc){ + $this->tap[$player->getName()] = [$loc, $now]; + $player->sendMessage($this->getMessage("tap-again", [$shop["itemName"], $shop["price"], $shop["amount"]])); + return; + }else{ + unset($this->tap[$player->getName()]); + } + + if(($cloud = $this->itemcloud->getCloudForPlayer($shop["owner"])) instanceof ItemCloud){ + if($shop["amount"] > $cloud->getCount($shop["item"], $shop["meta"])){ + $player->sendMessage($this->getMessage("no-stock")); + }else{ + if($player->getInventory()->canAddItem(($item = new Item($shop["item"], $shop["meta"], $shop["amount"]))) === false){ + $player->sendMessage($this->getMessage("no-space")); + }else{ + if(EconomyAPI::getInstance()->myMoney($player) > $shop["price"]){ + $player->getInventory()->addItem($item); + EconomyAPI::getInstance()->reduceMoney($player, $shop["price"]); + $player->sendMessage($this->getMessage("bought-item", [$shop["item"].":".$shop["meta"], $shop["price"], $shop["amount"]])); + }else{ + $player->sendMessage($this->getMessage("no-money")); + } + } + } + }else{ + $player->sendMessage($this->getMessage("shop-owner-no-account")); + } + if($event->getItem()->isPlaceable()){ + $this->placeQueue[$player->getName()] = true; + } + } + } + + public function getMessage($key, $val = ["%1", "%2", "%3"]){ + if($this->lang->exists($key)){ + return str_replace(["%1", "%2", "%3", "%MONETARY_UNIT%"], [$val[0], $val[1], $val[2], EconomyAPI::getInstance()->getMonetaryUnit()], $this->lang->get($key)); + } + return "There's no message named \"$key\""; + } + + public function onBlockPlace(BlockPlaceEvent $event){ + $user = $event->getPlayer()->getName(); + if(isset($this->placeQueue[$user])){ + $event->setCancelled(); + unset($this->placeQueue[$user]); + } + } + + public function getItem($item){ // gets ItemID and ItemName + $item = strtolower($item); + $e = explode(":", $item); + $e[1] = isset($e[1]) ? $e[1] : 0; + if(array_key_exists($item, ItemList::$items)){ + return array(ItemList::$items[$item], true); // Returns Item ID + }else{ + foreach(ItemList::$items as $name => $id){ + $explode = explode(":", $id); + $explode[1] = isset($explode[1]) ? $explode[1]:0; + if($explode[0] == $e[0] and $explode[1] == $e[1]){ + return array($name, false); + } + } + } + return false; + } + + public function getTag($firstLine){ + foreach($this->shopText->getAll() as $key => $val){ + if($key == $firstLine){ + return $val; + } + } + return false; + } +} \ No newline at end of file diff --git a/EconomyPShop/src/onebone/economypshop/ItemList.php b/EconomyPShop/src/onebone/economypshop/ItemList.php new file mode 100644 index 00000000..b19d838b --- /dev/null +++ b/EconomyPShop/src/onebone/economypshop/ItemList.php @@ -0,0 +1,302 @@ + 0, + "stone" => 1, + "grassblock" => 2, + "dirt" => 3, + "cobblestone" => 4, + "woodenplank" => 5, + "treesapling" => 6, + "firsapling" => "6:1", + "birchsapling" => "6:2", + "bedrock" => 7, + "water" => 8, + "stationarywater" => 9, + "lava" => 10, + "stationarylava" => 11, + "sand" => 12, + "gravel" => 13, + "goldore" => 14, + "ironore" => 15, + "coalore" => 16, + "oaklog" => 17, + "sideoaklog" => "17:1", + "birchlog" => "17:2", + "treeleaf" => "18", + "oaktreeleaf" => "18:1", + "birchtreeleaf" => "18:2", + "sponge" => 19, + "glass" => 20, + "lapisore" => 21, + "lapisblock" => 22, + "sandstone" => 24, + "sandstone2" => "24:1", + "sandstone3" => "24:2", + "bed" => 26, + "poweredrail" => 27, + "cobweb" => 30, + "bush" => 31, + "whitewool" => 35, + "orangewool" => "35:1", + "magentawool" => "35:2", + "skywool" => "35:3", + "yellowwool" => "35:4", + "greenwool" => "35:5", + "pinkwool" => "35:6", + "greywool" => "35:7", + "greywool2" => "35:8", + "bluishwool" => "35:9", + "purplewool" => "35:10", + "bluewool" => "35:11", + "brownwool" => "35:12", + "greenwool2" => "35:13", + "redwool" => "35:14", + "blackwool" => "35:15", + "yellowflower" => 37, + "poppy" => 38, + "brownmushroom" => 39, + "redmushroom" => 40, + "goldblock" => 41, + "ironblock" => 42, + "stonefoothold" => 43, + "sandfoothold" => "43:1", + "woodfoothold" => "43:2", + "cobblefoothold" => "43:3", + "brickfoothold" => "43:4", + "stonefoothold2" => "43:6", + "halfstone" => 44, + "halfsand" => "44:1", + "halfwood" => "44:2", + "halfcobble" => "44:3", + "halfbrick" => "44:4", + "halfstone2" => "44:6", + "brick" => 45, + "tnt" => 46, + "bookshelf" => 47, + "mossstone" => 48, + "obsidian" => 49, + "torch" => 50, + "fire" => 51, + "woodstair" => 53, + "chest" => 54, + "diamondore" => 56, + "diamondblock" => 57, + "craftingtable" => 58, + "crop" => 59, + "farmland" => 60, + "furnace" => 61, + "signblock" => 63, + "burningfurnace" => 62, + "woodendoor" => 64, + "ladder" => 65, + "cobblestair" => 67, + "wallsign" => 68, + "irondoor" => 71, + "redstoneore" => 73, + "glowredstone" => 74, + "snow" => 78, + "ice" => 79, + "snowblock" => 80, + "cactus" => 81, + "clayblock" => 82, + "sugarcane" => 83, + "fence" => 85, + "pumpkin" => 86, + "netherrack" => 87, + "glowingstone" => 89, + "jack-o-lanton" => 91, + "cake" => 92, + "invisiblebedrock" => 95, + "trapdoor" => 96, + "stonebrick" => 98, + "mossbrick" => "98:1", + "crackedbrick" => "98:2", + "brownshroomblock" => 99, + "redshroomblock" => 100, + "ironbars" => 101, + "flatglass" => 102, + "watermelon" => 103, + "vines" => 106, + "fencegate" => 107, + "brickstair" => 108, + "stonestair" => 109, + "mycelium" => 110, + "lilypad" => 111, + "netherbrick" => 112, + "netherbrickstair" => 114, + "endportalframe" => 120, + "endstone" => 121, + "cocoa" => 127, + "sandstair" => 128, + "emeraldore" => 129, + "emeraldblock" => 133, + "cobblestonewall" => 139, + "growingcarrot" => 141, + "growingpotato" => 142, + "quartzblock" => 155, + "softquartz" => "155:1", + "pilliarquartz" => "155:2", + "quartzstair" => 156, + "whiteclay" => 159, + "orangeclay" => "159:1", + "magentaclay" => "159:2", + "skyclay" => "159:3", + "yellowclay" => "159:4", + "greenclay" => "159:5", + "pinkclay" => "159:6", + "greyclay" => "159:7", + "greyclay2" => "159:8", + "bluishclay" => "159:9", + "purpleclay" => "159:10", + "blueclay" => "159:11", + "brownclay" => "159:12", + "greenclay2" => "159:13", + "redclay" => "159:14", + "blackclay" => "159:15", + "haybale" => 170, + "carpet" => 171, + "coalblock" => 173, + "podzol" => 243, + "beetroot" => 244, + "stonecutter" => 245, + "glowingobsidian" => 246, + "nethercore" => 247, + "updateblock1" => 248, + "updateblock2" => 249, + "errorgrass" => 253, + "errorleaves" => 254, + "errorstone" => 255, + "ironshovel" => 256, + "ironpickaxe" => 257, + "ironaxe" => 258, + "flintandsteel" => 259, + "apple" => 260, + "bow" => 261, + "arrow" => 262, + "coal" => 263, + "charcoal" => "263:1", + "diamond" => 264, + "ironingot" => 265, + "goldingot"=> 266, + "ironsword" => 267, + "woodsword" => 268, + "woodshovel" => 269, + "woodpickaxe" => 270, + "woodaxe" => 271, + "stonesword" => 272, + "stoneshovel" => 273, + "stonepickaxe" => 274, + "stoneaxe" => 275, + "diamondsword" => 276, + "diamondshovel" => 277, + "diamondpickaxe" => 278, + "diamondaxe" => 279, + "stick" => 280, + "bowl" => 281, + "mushroomstew" => 282, + "goldsword" => 283, + "goldshovel" => 284, + "goldpickaxe" => 285, + "goldaxe" => 286, + "web" => 287, + "feather" => 288, + "gunpowder" => 289, + "woodhoe" => 290, + "stonehoe" => 291, + "ironhoe" => 292, + "diamondhoe" => 293, + "goldhoe" => 294, + "seed" => 295, + "wheat" => 296, + "bread" => 297, + "leatherhat" => 298, + "leatherarmor" => 299, + "leatherpants" => 300, + "leatherboots" => 301, + "chairhat" => 302, + "chainchestplate" => 303, + "chainlegging" => 304, + "chainboots" => 305, + "ironhelmet" => 306, + "ironchestplate" => 307, + "ironlegging"=> 308, + "ironboots" => 309, + "diamondhelmet" => 310, + "diamondchestplate" => 311, + "diamondlegging" => 312, + "diamondboots" => 313, + "goldhelmet" => 314, + "goldchestplate" => 315, + "goldlegging" => 316, + "goldboots" => 317, + "flint" => 318, + "rawpork" => 319, + "pork" => 320, + "paint" => 321, + "sign" => 323, + "door" => 324, + "bucket" => 325, + "waterbucket" => 326, + "minecart" => 328, + "irondoor" => 330, + "redstone" => 331, + "snowball" => 332, + "boat" => 333, + "leather" => 334, + "claybrick" => 336, + "clay" => 337, + "sugarcane" => 338, + "paper" => 339, + "book" => 340, + "slimeball" => 341, + "egg" => 344, + "compass" => 345, + "clock" => 347, + "glowstone" => 348, + "ink" => 351, + "redrose" => "351:1", + "greencactus" => "351:2", + "cocoabean" => "351:3", + "lapislazuli" => "351:4", + "cotton" => "351:5", + "bluish" => "351:6", + "lightgrey" => "351:7", + "grey" => "351:8", + "pink" => "351:9", + "lightgreen" => "351:10", + "yellow" => "351:11", + "sky" => "351:12", + "magenta"=> "351:13", + "orange" => "351:14", + "bonemeal" => "351:15", + "bone" => 352, + "sugar" => 353, + "cake" => 354, + "bed" => 355, + "cookie" => 357, + "shears" => 359, + "melon" => 360, + "pumpkinseed" => 361, + "melonseed" => 362, + "rawbeef" => 363, + "steak" => 364, + "rawchicken" => 365, + "chicken" => 366, + "spawnegg" => 383, + "emerald" => 388, + "carrot" => 391, + "potato" => 392, + "bakedpotato" => 393, + "pumpkinpie" => 400, + "netherbrick" => 405, + "netherquartz" => 406, + "camera" => 456, + "beetroot" => 457, + "beetrootseed" => 458, + "beetrootsoup" => 459 +); +}