Skip to content

Conversation

@mineLdiver
Copy link
Member

@mineLdiver mineLdiver commented Jan 7, 2025

Added helper methods for registering new entries to registries.

Examples:

  1. Singular registration
@EventListener
void registerBlocks(BlockRegistryEvent event) {
    event.register(Namespace.MINECRAFT.id("stone"), Block.STONE);
}
  1. Singular registration with explicit raw ID
@EventListener
void registerBlocks(BlockRegistryEvent event) {
    event.register(Block.STONE.id, Namespace.MINECRAFT.id("stone"), Block.STONE);
}
  1. Bulk registration
@EventListener
void registerBlocks(BlockRegistryEvent event) {
    event.register()
        .accept(Namespace.MINECRAFT.id("stone"), Block.STONE)
        .accept(Namespace.MINECRAFT.id("dirt"), Block.DIRT)
        .accept(Namespace.MINECRAFT.id("obsidian"), Block.OBSIDIAN);
}
  1. Bulk registration with common namespace
@EventListener
void registerBlocks(BlockRegistryEvent event) {
    event.register(Namespace.MINECRAFT)
        .accept("stone", Block.STONE)
        .accept("dirt", Block.DIRT)
        .accept("obsidian", Block.OBSIDIAN);
}
  1. Bulk registration with common namespace and explicit raw ID
@EventListener
void registerBlocks(BlockRegistryEvent event) {
    event.register(block -> block.id, Namespace.MINECRAFT)
        .accept("stone", Block.STONE)
        .accept("dirt", Block.DIRT)
        .accept("obsidian", Block.OBSIDIAN);
}
  1. Bulk registration outside of an event
Registry.register(BlockRegistry.INSTANCE)
    .accept(Namespace.MINECRAFT.id("stone"), Block.STONE)
    .accept(Namespace.MINECRAFT.id("dirt"), Block.DIRT)
    .accept(Namespace.MINECRAFT.id("obsidian"), Block.OBSIDIAN);
  1. Bulk registration outside of an event with common namespace
Registry.register(BlockRegistry.INSTANCE, Namespace.MINECRAFT)
    .accept("stone", Block.STONE)
    .accept("dirt", Block.DIRT)
    .accept("obsidian", Block.OBSIDIAN);
  1. Bulk registration outside of an event with common namespace and explicit raw ID
Registry.register(BlockRegistry.INSTANCE, block -> block.id, Namespace.MINECRAFT)
    .accept("stone", Block.STONE)
    .accept("dirt", Block.DIRT)
    .accept("obsidian", Block.OBSIDIAN);

The event methods are only available in a subclass of RegistryEvent, specifically RegistryEvent.EntryTypeBound, because the RegistryEvent doesn't define the registry's entry generic type (so Block for BlockRegistry), making it impossible to implement type-safe helper methods. Defining the generic type in a subclass allows to introduce these helper methods in a backwards-compatible way. All StationAPI registry events were updated to extend RegistryEvent.EntryTypeBound, and all usages of Registry#register were changed to the helper methods where applicable.

@mineLdiver mineLdiver added the enhancement New feature or request label Jan 7, 2025
Copy link
Member

@calmilamsy calmilamsy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, this is a HUGE QoL improvement.

Registry.register(PacketTypeRegistry.INSTANCE, NAMESPACE.id("items/entity_spawn"), StationItemEntitySpawnS2CPacket.TYPE);
Registry.register(PacketTypeRegistry.INSTANCE, NAMESPACE.id("items/interact"), StationPlayerInteractBlockC2SPacket.TYPE);
Registry.register(PacketTypeRegistry.INSTANCE, NAMESPACE.id("items/slot_update"), StationScreenHandlerSlotUpdateS2CPacket.TYPE);
BulkBiConsumer.of((String id, PacketType<?> type) -> Registry.register(PacketTypeRegistry.INSTANCE, NAMESPACE.id(id), type))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be an idea to add a helper method inside Registry that just requires the registry instance and returns this for you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, didn't think of that.

@mineLdiver mineLdiver requested a review from calmilamsy January 9, 2025 19:54
@mineLdiver mineLdiver merged commit 212e775 into develop Feb 9, 2025
mineLdiver added a commit that referenced this pull request Feb 16, 2025
* Switched from HMI to RetroCommands

* Removed broken reimplementation of Divisor's old PlayerAPI. (#139)

* Removed Indigo renderer (#140)

* Updated UnsafeEvents

* ContextVariable

* ContextVariable#get

* Feature/register helper (#146)

* Added register helper methods to RegistryEvent

* Remove unused BulkConsumer.

* Updated all registries to be entry type bound.

* Updated all usages of Registry#register to new helper methods where applicable.

* Moved bulk registration helpers implementation to Registry interface and redirected event method helpers to that.

* Swapped namespace and rawIdGetter placements so they better reflect the original method arguments.

* cleanup/java-hacks (#145)

* Cleaned up Java hacks related to entrypoints

* Removed EnumFactory.

* Removed NativeImage Unsafe buffer cleaner invocation.

* Replaced fake BlockItem with null, since MixinExtras allows that, unlike vanilla Mixin's Redirect.

* Removed UnsafeProvider.

* Updated javadoc for EntrypointManager#registerLookup

* Fix a NPE in shears override

* Fix a NPE in FuelRegistry

* Added StationFlatteningBlock#onStateReplaced

* Fixed #159

* Change version
mineLdiver added a commit that referenced this pull request Jul 5, 2025
* Switched from HMI to RetroCommands

* Removed broken reimplementation of Divisor's old PlayerAPI. (#139)

* Removed Indigo renderer (#140)

* Updated UnsafeEvents

* ContextVariable

* ContextVariable#get

* Feature/register helper (#146)

* Added register helper methods to RegistryEvent

* Remove unused BulkConsumer.

* Updated all registries to be entry type bound.

* Updated all usages of Registry#register to new helper methods where applicable.

* Moved bulk registration helpers implementation to Registry interface and redirected event method helpers to that.

* Swapped namespace and rawIdGetter placements so they better reflect the original method arguments.

* cleanup/java-hacks (#145)

* Cleaned up Java hacks related to entrypoints

* Removed EnumFactory.

* Removed NativeImage Unsafe buffer cleaner invocation.

* Replaced fake BlockItem with null, since MixinExtras allows that, unlike vanilla Mixin's Redirect.

* Removed UnsafeProvider.

* Updated javadoc for EntrypointManager#registerLookup

* Fix a NPE in shears override

* Fix a NPE in FuelRegistry

* Added StationFlatteningBlock#onStateReplaced

* Fixed #159

* Change version

* Merge pull request #165 from ThexXTURBOXx/develop

Don't close filesystems when resolving mod namespaces

* Update modmenu

* Merge pull request #168

* Yeet

* Wait this is wrong

* Merge branch 'develop' into namespace-change

* Make stapi get angree (#169)

* Update BiomeMixin.java (#196)

* Add Block Support to CustomTooltipProvider (#187)

* Add Block support to CustomTooltipProvider

* Add some documentation to the interface

* Make Leaves' Log Check Work With Modded Logs and Leaves (#186)

* Fix

* And comment this out cause it's ugly

* Fix (#185)

* FUCK (#200)

* Why mine (#197)

* Potentially make mine scream (#188)

* Implement onBonemealUse on Vanilla Crops (#170)

* Yeet

* Also do sapling, also fix multiplayer

* Use the appropriate random

---------

Co-authored-by: mineLdiver <aabesedin@mail.ru>

* Fixed cal L moment (cascaded test worldgen)

* Change version

---------

Co-authored-by: Nico Mexis <nico.mexis@kabelmail.de>
Co-authored-by: calmilamsy <bumbill00@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants