Skip to content

Commit

Permalink
only generate dynamic models when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtThiemann committed Oct 30, 2024
1 parent fe8ef39 commit 82e95cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/Resource/DynamicResources/DecoratedPotModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use Aternos\Renderchest\Exception\ModelResolutionException;
use Aternos\Renderchest\Model\ModelInterface;
use Aternos\Renderchest\Resource\ResourceLocator;
use Aternos\Renderchest\Resource\ResourceManagerInterface;
use Exception;

class DecoratedPotModelGenerator extends DynamicResourceGenerator
{
protected array $models = [];
protected ?array $models = null;

/**
* @inheritDoc
Expand All @@ -22,12 +20,15 @@ public static function getNamespace(): string
}

/**
* @param ResourceManagerInterface $resourceManager
* @throws Exception
* @return void
* @throws ModelResolutionException
*/
public function __construct(ResourceManagerInterface $resourceManager)
protected function initializeModels(): void
{
parent::__construct($resourceManager);
if ($this->models !== null) {
return;
}
$this->models = [];
foreach (Constants::POTTERY_SHERDS as $sherd) {
$this->createBasePot($sherd);
$this->createOverlayModel($sherd);
Expand Down Expand Up @@ -72,6 +73,7 @@ protected function createOverlayModel(string $sherd): ModelInterface
*/
public function getModel(ResourceLocator $locator): ModelInterface
{
$this->initializeModels();
if (!isset($this->models[strval($locator)])) {
throw new ModelResolutionException("Cannot resolve model locator " . $locator);
}
Expand All @@ -80,9 +82,11 @@ public function getModel(ResourceLocator $locator): ModelInterface

/**
* @inheritDoc
* @throws ModelResolutionException
*/
public function getAllItems(string $namespace): array
{
$this->initializeModels();
return array_keys($this->models);
}
}
16 changes: 11 additions & 5 deletions src/Resource/DynamicResources/LeatherArmorTrimModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Aternos\Renderchest\Model\GeneratedItem;
use Aternos\Renderchest\Model\ModelInterface;
use Aternos\Renderchest\Resource\ResourceLocator;
use Aternos\Renderchest\Resource\ResourceManagerInterface;
use Exception;

class LeatherArmorTrimModelGenerator extends DynamicResourceGenerator
{
protected array $models = [];
protected ?array $models = null;

/**
* @inheritDoc
Expand All @@ -23,12 +22,15 @@ public static function getNamespace(): string
}

/**
* @param ResourceManagerInterface $resourceManager
* @return void
* @throws Exception
*/
public function __construct(ResourceManagerInterface $resourceManager)
protected function initializeModels(): void
{
parent::__construct($resourceManager);
if ($this->models !== null) {
return;
}
$this->models = [];
foreach (Constants::ARMOR_ITEM_TYPES as $armorItem) {
$this->createModelFromLayers("leather_" . $armorItem . "_base", [
"minecraft:item/leather_" . $armorItem
Expand Down Expand Up @@ -68,9 +70,11 @@ protected function createModelFromLayers(string $locatorPath, array $layers): Ge

/**
* @inheritDoc
* @throws Exception
*/
public function getModel(ResourceLocator $locator): ModelInterface
{
$this->initializeModels();
if (!isset($this->models[strval($locator)])) {
throw new ModelResolutionException("Cannot resolve model locator " . $locator);
}
Expand All @@ -79,9 +83,11 @@ public function getModel(ResourceLocator $locator): ModelInterface

/**
* @inheritDoc
* @throws Exception
*/
public function getAllItems(string $namespace): array
{
$this->initializeModels();
return array_keys($this->models);
}
}

0 comments on commit 82e95cb

Please sign in to comment.