Skip to content

Commit b94f67a

Browse files
committed
fix possibility loop on register/override block and item on asyncTask
1 parent 9897884 commit b94f67a

File tree

9 files changed

+126
-66
lines changed

9 files changed

+126
-66
lines changed

src/SenseiTarzan/SymplyPlugin/Behavior/SymplyBlockPalette.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private function selectModeSort(bool $blockNetworkIdsAreHashes, array $states, ?
156156
}else{
157157
$stateDataToStateIdLookup[$name][$state->getRawStateProperties()] = $stateId;
158158
}
159-
$stateId++;
159+
++$stateId;
160160
}
161161
}
162162
}

src/SenseiTarzan/SymplyPlugin/Behavior/SymplyItemFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ public function overwrite(Closure $itemClosure, null|Closure|false $serializer =
175175
$creativeIventoryEntry = VanillaGroupMinecraft::getCreativeInventoryEntry($item);
176176
$this->overwrite[$namespaceId] = $item;
177177
if ($creativeIventoryEntry) {
178-
var_dump($item->getVanillaName());
179178
CreativeInventory::getInstance()->remove($item);
180179
CreativeInventory::getInstance()->add($item, $creativeIventoryEntry->getCategory(), $creativeIventoryEntry->getGroup());
181180
}

src/SenseiTarzan/SymplyPlugin/Main.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ protected function onEnable() : void
8888
$server = Server::getInstance();
8989
$asyncPool = $server->getAsyncPool();
9090
$asyncPool->addWorkerStartHook(static function(int $workerId) use($asyncPool) : void{
91-
$asyncPool->submitTaskToWorker(new AsyncRegisterVanillaTask(), $workerId);
92-
$asyncPool->submitTaskToWorker(new AsyncRegisterBehaviorsTask(), $workerId);
93-
$asyncPool->submitTaskToWorker(new AsyncOverwriteTask(), $workerId);
94-
$asyncPool->submitTaskToWorker(new AsyncSortBlockStateTask(), $workerId);
95-
$asyncPool->submitTaskToWorker(new AsyncRegisterSchemaTask(), $workerId);
91+
$asyncPool->submitTaskToWorker(new AsyncRegisterVanillaTask($workerId), $workerId);
92+
$asyncPool->submitTaskToWorker(new AsyncRegisterBehaviorsTask($workerId), $workerId);
93+
$asyncPool->submitTaskToWorker(new AsyncOverwriteTask($workerId), $workerId);
94+
$asyncPool->submitTaskToWorker(new AsyncSortBlockStateTask($workerId), $workerId);
95+
$asyncPool->submitTaskToWorker(new AsyncRegisterSchemaTask($workerId), $workerId);
9696
});
9797
Main::getInstance()->getSymplyCraftManager()->onLoad();
9898
}),0);

src/SenseiTarzan/SymplyPlugin/Task/AsyncOverwriteTask.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
use pmmp\thread\ThreadSafeArray;
2727
use pocketmine\scheduler\AsyncTask;
28+
use pocketmine\Server;
29+
use pocketmine\thread\log\AttachableThreadSafeLogger;
2830
use ReflectionException;
2931
use SenseiTarzan\SymplyPlugin\Behavior\SymplyBlockFactory;
3032
use SenseiTarzan\SymplyPlugin\Behavior\SymplyItemFactory;
@@ -35,31 +37,45 @@
3537
class AsyncOverwriteTask extends AsyncTask
3638
{
3739

38-
private ThreadSafeArray $blockFuncs;
39-
private ThreadSafeArray $itemFuncs;
40+
private ThreadSafeArray $blockFuncs;
41+
private ThreadSafeArray $itemFuncs;
4042

41-
public function __construct()
42-
{
43-
$this->blockFuncs = SymplyCache::getInstance()->getTransmitterBlockOverwrite();
44-
$this->itemFuncs = SymplyCache::getInstance()->getTransmitterItemOverwrite();
45-
}
43+
private AttachableThreadSafeLogger $logger;
4644

47-
/**
48-
* @inheritDoc
49-
* @throws ReflectionException
50-
*/
51-
public function onRun() : void
52-
{
53-
try {
54-
foreach ($this->blockFuncs as [$blockClosure, $serialize, $deserialize]) {
55-
SymplyBlockFactory::getInstance(true)->overwrite($blockClosure, $serialize, $deserialize);
56-
}
45+
public function __construct(private int $workerId)
46+
{
47+
$this->logger = Server::getInstance()->getLogger();
48+
$this->blockFuncs = new ThreadSafeArray();
49+
foreach (SymplyCache::getInstance()->getTransmitterBlockOverwrite() as $array) {
50+
$this->blockFuncs[] = $array;
51+
}
52+
$this->itemFuncs = new ThreadSafeArray();
53+
foreach (SymplyCache::getInstance()->getTransmitterItemOverwrite() as $array) {
54+
$this->itemFuncs[] = $array;
55+
}
56+
}
5757

58-
foreach ($this->itemFuncs as [$itemClosure, $serialize, $deserialize, $argv]) {
59-
SymplyItemFactory::getInstance(true)->overwrite($itemClosure, $serialize, $deserialize, unserialize($argv));
60-
}
61-
}catch (Throwable){
58+
/**
59+
* @inheritDoc
60+
* @throws ReflectionException
61+
*/
62+
public function onRun(): void
63+
{
64+
foreach ($this->blockFuncs as [$blockClosure, $serialize, $deserialize]) {
65+
try {
66+
SymplyBlockFactory::getInstance(true)->overwrite($blockClosure, $serialize, $deserialize);
67+
} catch (Throwable $throwable) {
68+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
69+
}
70+
}
6271

63-
}
64-
}
72+
foreach ($this->itemFuncs as [$itemClosure, $serialize, $deserialize, $argv]) {
73+
try {
74+
SymplyItemFactory::getInstance(true)->overwrite($itemClosure, $serialize, $deserialize, unserialize($argv));
75+
} catch (Throwable $throwable) {
76+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
77+
}
78+
}
79+
$this->logger->debug("[SymplyPlugin] WorkerId " . $this->workerId . ": finish overwrite items and blocks");
80+
}
6581
}

src/SenseiTarzan/SymplyPlugin/Task/AsyncRegisterBehaviorsTask.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
use pmmp\thread\ThreadSafeArray;
2727
use pocketmine\scheduler\AsyncTask;
28+
use pocketmine\Server;
29+
use pocketmine\thread\log\AttachableThreadSafeLogger;
2830
use SenseiTarzan\SymplyPlugin\Behavior\SymplyBlockFactory;
2931
use SenseiTarzan\SymplyPlugin\Behavior\SymplyItemFactory;
3032
use SenseiTarzan\SymplyPlugin\Utils\SymplyCache;
@@ -37,27 +39,41 @@ class AsyncRegisterBehaviorsTask extends AsyncTask
3739
private ThreadSafeArray $blockFuncs;
3840
private ThreadSafeArray $itemFuncs;
3941

40-
public function __construct()
42+
private AttachableThreadSafeLogger $logger;
43+
44+
public function __construct(private int $workerId)
4145
{
42-
$this->blockFuncs = SymplyCache::getInstance()->getTransmitterBlockCustom();
43-
$this->itemFuncs = SymplyCache::getInstance()->getTransmitterItemCustom();
46+
$this->logger = Server::getInstance()->getLogger();
47+
$this->blockFuncs = new ThreadSafeArray();
48+
foreach (SymplyCache::getInstance()->getTransmitterBlockCustom() as $array) {
49+
$this->blockFuncs[] = $array;
50+
}
51+
$this->itemFuncs = new ThreadSafeArray();
52+
foreach (SymplyCache::getInstance()->getTransmitterItemCustom() as $array) {
53+
$this->itemFuncs[] = $array;
54+
}
4455
}
4556

4657
/**
4758
* @inheritDoc
4859
*/
4960
public function onRun() : void
5061
{
51-
try {
52-
foreach ($this->blockFuncs as [$blockClosure, $serialize, $deserialize, $argv]) {
53-
SymplyBlockFactory::getInstance(true)->register($blockClosure, $serialize, $deserialize, unserialize($argv, ['allowed_classes' => true]));
54-
}
55-
SymplyBlockFactory::getInstance()->initBlockBuilders();
56-
foreach ($this->itemFuncs as [$itemClosure, $serialize, $deserialize, $argv]) {
57-
SymplyItemFactory::getInstance(true)->register($itemClosure, $serialize, $deserialize, unserialize($argv, ['allowed_classes' => true]));
58-
}
59-
}catch (Throwable){
60-
61-
}
62-
}
62+
foreach ($this->blockFuncs as [$blockClosure, $serialize, $deserialize, $argv]) {
63+
try {
64+
SymplyBlockFactory::getInstance(true)->register($blockClosure, $serialize, $deserialize, unserialize($argv, ['allowed_classes' => true]));
65+
}catch (Throwable $throwable){
66+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
67+
}
68+
}
69+
SymplyBlockFactory::getInstance()->initBlockBuilders();
70+
foreach ($this->itemFuncs as [$itemClosure, $serialize, $deserialize, $argv]) {
71+
try {
72+
SymplyItemFactory::getInstance(true)->register($itemClosure, $serialize, $deserialize, unserialize($argv, ['allowed_classes' => true]));
73+
}catch (Throwable $throwable){
74+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
75+
}
76+
}
77+
$this->logger->debug("[SymplyPlugin] WorkerId " . $this->workerId . ": finish registering custom items and blocks");
78+
}
6379
}

src/SenseiTarzan/SymplyPlugin/Task/AsyncRegisterSchemaTask.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
namespace SenseiTarzan\SymplyPlugin\Task;
2525

2626
use pocketmine\scheduler\AsyncTask;
27+
use pocketmine\Server;
28+
use pocketmine\thread\log\AttachableThreadSafeLogger;
2729
use SenseiTarzan\SymplyPlugin\Manager\SymplySchemaManager;
2830
use function serialize;
2931
use function unserialize;
@@ -32,9 +34,11 @@ class AsyncRegisterSchemaTask extends AsyncTask
3234
{
3335

3436
private string $listSchema;
37+
private AttachableThreadSafeLogger $logger;
3538

36-
public function __construct()
39+
public function __construct(private int $workerId)
3740
{
41+
$this->logger = Server::getInstance()->getLogger();
3842
$this->listSchema = serialize(SymplySchemaManager::getInstance()->getListSchema());
3943
}
4044

@@ -47,5 +51,7 @@ public function onRun() : void
4751
foreach ($schemas as $schema) {
4852
SymplySchemaManager::getInstance()->addSchema($schema);
4953
}
54+
55+
$this->logger->debug("[SymplyPlugin] WorkerId " . $this->workerId . ": finish registering schema");
5056
}
5157
}

src/SenseiTarzan/SymplyPlugin/Task/AsyncRegisterVanillaTask.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
use pmmp\thread\ThreadSafeArray;
2727
use pocketmine\scheduler\AsyncTask;
28+
use pocketmine\Server;
29+
use pocketmine\thread\log\AttachableThreadSafeLogger;
2830
use SenseiTarzan\SymplyPlugin\Behavior\SymplyBlockFactory;
2931
use SenseiTarzan\SymplyPlugin\Behavior\SymplyItemFactory;
3032
use SenseiTarzan\SymplyPlugin\Utils\SymplyCache;
@@ -37,26 +39,41 @@ class AsyncRegisterVanillaTask extends AsyncTask
3739
private ThreadSafeArray $blockFuncs;
3840
private ThreadSafeArray $itemFuncs;
3941

40-
public function __construct()
42+
private AttachableThreadSafeLogger $logger;
43+
44+
public function __construct(private int $workerId)
4145
{
42-
$this->blockFuncs = SymplyCache::getInstance()->getTransmitterBlockVanilla();
43-
$this->itemFuncs = SymplyCache::getInstance()->getTransmitterItemVanilla();
46+
$this->logger = Server::getInstance()->getLogger();
47+
48+
$this->blockFuncs = new ThreadSafeArray();
49+
foreach (SymplyCache::getInstance()->getTransmitterBlockVanilla() as $array) {
50+
$this->blockFuncs[] = $array;
51+
}
52+
$this->itemFuncs = new ThreadSafeArray();
53+
foreach (SymplyCache::getInstance()->getTransmitterItemVanilla() as $array) {
54+
$this->itemFuncs[] = $array;
55+
}
4456
}
4557

4658
/**
4759
* @inheritDoc
4860
*/
4961
public function onRun() : void
5062
{
51-
try {
52-
foreach ($this->blockFuncs as [$blockClosure, $identifier, $serialize, $deserialize]) {
53-
SymplyBlockFactory::getInstance(true)->registerVanilla($blockClosure, $identifier, $serialize, $deserialize);
54-
}
55-
foreach ($this->itemFuncs as [$itemClosure, $identifier, $serialize, $deserialize, $argv]) {
56-
SymplyItemFactory::getInstance(true)->registerVanilla($itemClosure, $identifier, $serialize, $deserialize, unserialize($argv));
57-
}
58-
}catch (Throwable) {
59-
60-
}
63+
foreach ($this->blockFuncs as [$blockClosure, $identifier, $serialize, $deserialize]) {
64+
try {
65+
SymplyBlockFactory::getInstance(true)->registerVanilla($blockClosure, $identifier, $serialize, $deserialize);
66+
}catch (Throwable $throwable) {
67+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
68+
}
69+
}
70+
foreach ($this->itemFuncs as [$itemClosure, $identifier, $serialize, $deserialize, $argv]) {
71+
try {
72+
SymplyItemFactory::getInstance(true)->registerVanilla($itemClosure, $identifier, $serialize, $deserialize, unserialize($argv));
73+
}catch (Throwable $throwable) {
74+
$this->logger->warning("[SymplyPlugin] WorkerId " . $this->workerId . ": " . $throwable->getMessage());
75+
}
76+
}
77+
$this->logger->debug("[SymplyPlugin] WorkerId " . $this->workerId . ": finish registering vanilla items and blocks");
6178
}
6279
}

src/SenseiTarzan/SymplyPlugin/Task/AsyncSortBlockStateTask.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
namespace SenseiTarzan\SymplyPlugin\Task;
2525

2626
use pocketmine\scheduler\AsyncTask;
27+
use pocketmine\Server;
28+
use pocketmine\thread\log\AttachableThreadSafeLogger;
2729
use SenseiTarzan\SymplyPlugin\Behavior\SymplyBlockPalette;
2830
use SenseiTarzan\SymplyPlugin\Utils\SymplyCache;
2931

@@ -32,8 +34,11 @@ class AsyncSortBlockStateTask extends AsyncTask
3234

3335
private bool $blockNetworkIdsAreHashes;
3436

35-
public function __construct()
37+
private AttachableThreadSafeLogger $logger;
38+
39+
public function __construct(private int $workerId)
3640
{
41+
$this->logger = Server::getInstance()->getLogger();
3742
$this->blockNetworkIdsAreHashes = SymplyCache::getInstance()->isBlockNetworkIdsAreHashes();
3843
}
3944

@@ -43,5 +48,6 @@ public function __construct()
4348
public function onRun() : void
4449
{
4550
SymplyBlockPalette::getInstance()->sort($this->blockNetworkIdsAreHashes);
51+
$this->logger->debug("[SymplyPlugin] WorkerId " . $this->workerId . ": finish sort block state task");
4652
}
4753
}

src/SenseiTarzan/SymplyPlugin/Utils/SymplyCache.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,47 +123,47 @@ public function addTransmitterItemVanilla(ThreadSafeArray $arrayClosure) : void
123123
}
124124

125125
/**
126-
* Donne les blocks custom a charger dans les threads
126+
* Gives custom blocks to load into threads
127127
*/
128128
public function getTransmitterBlockCustom() : ThreadSafeArray
129129
{
130130
return $this->transmitterBlockCustom;
131131
}
132132

133133
/**
134-
* Donne les items custom a charger dans les threads
134+
* Gives custom items to load into threads
135135
*/
136136
public function getTransmitterItemCustom() : ThreadSafeArray
137137
{
138138
return $this->transmitterItemCustom;
139139
}
140140

141141
/**
142-
* Donne les blocks vanilla a surcharge dans les threads
142+
* Overload vanilla blocks in threads
143143
*/
144144
public function getTransmitterBlockOverwrite() : ThreadSafeArray
145145
{
146146
return $this->transmitterBlockOverwrite;
147147
}
148148

149149
/**
150-
* Donne les items vanilla a surcharge dans les threads
150+
* Give vanilla items to overload in threads
151151
*/
152152
public function getTransmitterItemOverwrite() : ThreadSafeArray
153153
{
154154
return $this->transmitterItemOverwrite;
155155
}
156156

157157
/**
158-
* Donne les blocks vanilla a charger dans les threads
158+
* Gives vanilla blocks to load into threads
159159
*/
160160
public function getTransmitterBlockVanilla() : ThreadSafeArray
161161
{
162162
return $this->transmitterBlockVanilla;
163163
}
164164

165165
/**
166-
* Donne les item vanilla a charger dans les threads
166+
* Gives vanilla items to load into threads
167167
*/
168168
public function getTransmitterItemVanilla() : ThreadSafeArray
169169
{

0 commit comments

Comments
 (0)