Customies icon indicating copy to clipboard operation
Customies copied to clipboard

Patching a little error that has been forgotten

Open KanekiOnMC opened this issue 1 month ago • 5 comments

After someone updated Customies, they forgot to create the id when the Closure is called in registerItem()

KanekiOnMC avatar Nov 16 '25 03:11 KanekiOnMC

Developers can just use the new id system in their code hardcoded in their own item class

https://github.com/Amblydia/Customies/wiki

CustomiesItemFactory::getInstance()->registerItem(static fn() => new CustomItem(ItemTypeIds::newId()), "customies:custom_item");

class CustomItem extends Item implements ItemComponents{
    use ItemComponentsTrait;

      public function __construct(ItemIdentifier $identifier){
          parent::__construct($identifier, "Custom item");
      }
 }
// or
CustomiesItemFactory::getInstance()->registerItem(static fn() => new CustomItem(), "customies:custom_item");

class CustomItem extends Item implements ItemComponents{
    use ItemComponentsTrait;

      public function __construct(){
          parent::__construct(new ItemIdentifier(ItemTypeIds::newId()), "Custom item");
      }
 }

HydroGames-dev avatar Nov 16 '25 03:11 HydroGames-dev

I disagree with both code approaches. ​The issue is that the resulting registered item ID could potentially be inconsistent across different threads calling ItemTypeIds::newId() if the registration sequence is not strictly synchronized (which should ideally be prevented).

It would be better to store the ID value outside of the item function, like this example:

$id = ItemTypeIds::newId();
CustomiesItemFactory::getInstance()->registerItem(static fn() => new CustomItem(new ItemIdentifier($id)), "customies:custom_item");

The purpose of this change is to allow plugin developers to easily keep track of their own registered type IDs. This change was first implemented for custom block registration and then subsequently applied to custom items.

DavyCraft648 avatar Nov 16 '25 10:11 DavyCraft648

You can still do that

$id = ItemTypeIds::newId();
CustomiesItemFactory::getInstance()->registerItem(static fn() => new CustomItem($id), "customies:custom_item");

// Item Class
public function __construct(ItemIdentifier $identifier){
    parent::__construct($identifier, "Custom item");
}

HydroGames-dev avatar Nov 16 '25 10:11 HydroGames-dev

No, I'm not criticizing this change, just commenting on the 2 solution codes you provided

DavyCraft648 avatar Nov 16 '25 10:11 DavyCraft648

No, I'm not criticizing this change, just commenting on the 2 solution codes you provided

Sorry mate, they just didn't updated the wiki, so mb, I didn't think through it

KanekiOnMC avatar Nov 19 '25 02:11 KanekiOnMC