Added ChestLoot for games. Setup chest filling via ChestLoot for SurvivalGames.

This commit is contained in:
William Burns 2015-07-14 11:56:13 +01:00
parent 5f73e50b2a
commit d36dd26f42
6 changed files with 216 additions and 4 deletions

View File

@ -37,6 +37,7 @@
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Mineplex" />
</component>
</project>

View File

@ -0,0 +1,61 @@
<?php
/**
* Created by PhpStorm.
* User: william
* Date: 14/07/2015
* Time: 09:57
*/
namespace mineplex\plugin\gameengine\game\components\loot;
use mineplex\plugin\gameengine\game\loot\RandomItem;
use mineplex\plugin\util\UtilMath;
use pocketmine\item\Item;
class ChestLoot {
private $randomItems = array();
private $totalLoot;
function __construct()
{
$this->totalLoot = 0;
}
function addItem(int $id, int $chance, int $size, String $name)
{
$this->addLoot(new RandomItem(new Item($id, 0, 1, $name), $chance, $size, $size));
}
function addItemFull(int $id, int $chance, int $min, int $max, String $name)
{
$this->addLoot(new RandomItem(new Item($id, 0, 1, $name), $chance, $min, $max));
}
function addLoot(RandomItem $ri)
{
array_push($this->randomItems, $ri);
$this->totalLoot += $ri->getChance();
}
function getLoot()
{
$num = UtilMath::random($this->totalLoot);
foreach ($this->randomItems as $ri)
{
if ($ri instanceof RandomItem)
{
$num -= $ri->getChance();
if ($num < 0)
{
return $ri->getItem();
}
}
}
return null;
}
}

View File

@ -0,0 +1,39 @@
<?php
/**
* Created by PhpStorm.
* User: william
* Date: 14/07/2015
* Time: 10:01
*/
namespace mineplex\plugin\gameengine\game\loot;
use mineplex\plugin\util\UtilMath;
use pocketmine\item\Item;
class RandomItem {
private $chance;
private $item;
private $min;
private $max;
function __construct(Item $item, int $chance, int $min, int $max)
{
$this->chance = $chance;
$this->item = $item;
$this->min = $min;
$this->max = $max;
}
function getChance()
{
return $this->chance;
}
function getItem()
{
return new Item($this->item->getId(), 0, UtilMath::randBetween($this->min, $this-max), $this->item->getName());
}
}

View File

@ -295,7 +295,7 @@ class WorldComponent implements Listener, BenchTask
//This will return a UID for the game
public function getNewGameId()
{
return rand(0, 999999); //Make this acutally unique
return rand(0, 999999); //Make this actually unique
}
public function isWorldReady()

View File

@ -1,7 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: Exerosis
* User: WilliamTiger
* Date: 7/14/2015
* Time: 3:26 AM
*/
@ -14,17 +14,22 @@ use mineplex\plugin\gameengine\arenas\events\ArenaEndEvent;
use mineplex\plugin\gameengine\arenas\events\ArenaStartEvent;
use mineplex\plugin\gameengine\game\components\gamestate\events\GameStateChangeEvent;
use mineplex\plugin\gameengine\game\components\gamestate\GameState;
use mineplex\plugin\gameengine\game\components\gamestate\GameStateComponent;
use mineplex\plugin\gameengine\game\components\loot\ChestLoot;
use mineplex\plugin\gameengine\game\components\world\event\WorldLoadSuccessEvent;
use mineplex\plugin\gameengine\game\components\world\WorldComponent;
use mineplex\plugin\util\UtilMath;
use pocketmine\event\HandlerList;
use pocketmine\event\Listener;
use pocketmine\item\Item;
use pocketmine\math\Math;
use pocketmine\Server;
use pocketmine\tile\Chest;
class ChestComponent implements Listener {
private $arena;
private $worldComponent;
private $loot;
//You can put what you want in the constructor, but for chests this is all you should need.
/**
@ -36,7 +41,55 @@ class ChestComponent implements Listener {
$this->arena = $arena;
$this->worldComponent = $worldComponent;
Server::getInstance()->getPluginManager()->registerEvents($this, $arena->getPlugin());
//Don't to game start stuff here cause there might be other components not enabled yet
$this->loot = new ChestLoot();
//Food
$this->loot->addItemFull(Item::BAKED_POTATO, 30, 1, 3, "Baked Potato");
$this->loot->addItemFull(Item::COOKED_BEEF, 30, 1, 2, "Steak");
$this->loot->addItemFull(Item::COOKED_CHICKEN, 30, 1, 2, "Cooked Chicken");
$this->loot->addItemFull(Item::CARROT, 30, 1, 3, "Carrot");
$this->loot->addItemFull(Item::MUSHROOM_STEW, 15, 1, 1, "Mushroom Stew");
$this->loot->addItemFull(Item::WHEAT, 30, 1, 6, "Wheat");
$this->loot->addItemFull(Item::APPLE, 30, 1, 4, "Apple");
$this->loot->addItemFull(Item::RAW_PORKCHOP, 30, 1, 4, "Pork");
//Weapons
$this->loot->addItem(Item::WOODEN_AXE, 80, 1, "Wooden Axe");
$this->loot->addItem(Item::WOODEN_SWORD, 70, 1, "Wooden Sword");
$this->loot->addItem(Item::STONE_AXE, 60, 1, "Stone Axe");
$this->loot->addItem(Item::STONE_SWORD, 30, 1, "Stone Sword");
//Leather Armour
$this->loot->addItem(Item::LEATHER_BOOTS, 30, 1, "Leather Boots");
$this->loot->addItem(Item::LEATHER_CAP, 30, 1, "Leather Cap");
$this->loot->addItem(Item::LEATHER_PANTS, 30, 1, "Leather Pants");
$this->loot->addItem(Item::LEATHER_TUNIC, 30, 1, "Leather Boots");
//Gold Armour
$this->loot->addItem(Item::GOLD_CHESTPLATE, 25, 1, "");
$this->loot->addItem(Item::GOLD_LEGGINGS, 25, 1, "");
$this->loot->addItem(Item::GOLD_BOOTS, 25, 1, "");
$this->loot->addItem(Item::GOLD_HELMET, 25, 1, "");
//Chain Armour
$this->loot->addItem(Item::CHAIN_BOOTS, 20, 1, "");
$this->loot->addItem(Item::CHAIN_CHESTPLATE, 20, 1, "");
$this->loot->addItem(Item::CHAIN_LEGGINGS, 20, 1, "");
$this->loot->addItem(Item::CHAIN_HELMET, 20, 1, "");
//Throwable
$this->loot->addItem(Item::BOW, 20, 1, "");
$this->loot->addItemFull(Item::ARROW, 20, 1, 3, "");
$this->loot->addItemFull(Item::SNOWBALL, 30, 1, 2, "");
$this->loot->addItemFull(Item::EGG, 30, 1, 2, "");
//Misc
$this->loot->addItem(Item::COMPASS, 20, 1, "");
$this->loot->addItemFull(Item::STICK, 30, 1, 2, "");
$this->loot->addItem(Item::FLINT, 30, 1, 2, "");
$this->loot->addItem(Item::FEATHER, 30, 1, 2, "");
$this->loot->addItem(Item::GOLD_INGOT, 20, 1, "");
}
function onStart(ArenaStartEvent $event)
@ -52,6 +105,31 @@ class ChestComponent implements Listener {
return;
//Called after the world is loaded
foreach ($this->worldComponent->getData("54") as $loc)
{
$block = $loc->getLevel()->getBlock($loc);
if ($block instanceof Chest)
{
$inv = $block->getInventory();
$items = 2;
if (UtilMath::random(100) > 50)
++$items;
if (UtilMath::random(100) > 65)
++$items;
if (UtilMath::random(100) > 80)
++$items;
if (UtilMath::random(100) > 95)
++$items;
for ($i = 0; $i < $items; $i++)
{
$chosenItem = $this->loot->getLoot();
$inv->setItem(UtilMath::random(27), $chosenItem);
}
}
}
}
function onStateChange(GameStateChangeEvent $event)

View File

@ -0,0 +1,33 @@
<?php
/**
* Created by PhpStorm.
* User: william
* Date: 14/07/2015
* Time: 10:57
*/
namespace mineplex\plugin\util;
class UtilMath {
static function random(int $num)
{
return rand(0, ($num - 1));
}
static function randInclusive(int $num)
{
return rand(0, $num);
}
static function randBetween(int $min, int $max)
{
return rand($min, ($max - 1));
}
static function randBetweenInclusive(int $min, int $max)
{
return rand($min, $max);
}
}