Skip to content

Commit

Permalink
supplierify registered objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Jul 26, 2024
1 parent 02c094a commit 924e7a3
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 33 deletions.
39 changes: 20 additions & 19 deletions src/main/java/dev/hephaestus/glowcase/Glowcase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.hephaestus.glowcase;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
Expand Down Expand Up @@ -38,34 +40,33 @@ public class Glowcase implements ModInitializer {

public static final TagKey<Item> ITEM_TAG = TagKey.of(RegistryKeys.ITEM, id("items"));

public static final Supplier<HyperlinkBlock> HYPERLINK_BLOCK = Suppliers.ofInstance(Registry.register(Registries.BLOCK, id("hyperlink_block"), new HyperlinkBlock()));
public static final Supplier<Item> HYPERLINK_BLOCK_ITEM = Suppliers.ofInstance(Registry.register(Registries.ITEM, id("hyperlink_block"), new BlockItem(HYPERLINK_BLOCK.get(), new Item.Settings())));
public static final Supplier<BlockEntityType<HyperlinkBlockEntity>> HYPERLINK_BLOCK_ENTITY = Suppliers.ofInstance(Registry.register(Registries.BLOCK_ENTITY_TYPE, id("hyperlink_block"), BlockEntityType.Builder.create(HyperlinkBlockEntity::new, HYPERLINK_BLOCK.get()).build(null)));

public static final HyperlinkBlock HYPERLINK_BLOCK = Registry.register(Registries.BLOCK, id("hyperlink_block"), new HyperlinkBlock());
public static final Item HYPERLINK_BLOCK_ITEM = Registry.register(Registries.ITEM, id("hyperlink_block"), new BlockItem(HYPERLINK_BLOCK, new Item.Settings()));
public static final BlockEntityType<HyperlinkBlockEntity> HYPERLINK_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, id("hyperlink_block"), BlockEntityType.Builder.create(HyperlinkBlockEntity::new, HYPERLINK_BLOCK).build());
public static final Supplier<ItemDisplayBlock> ITEM_DISPLAY_BLOCK = Suppliers.ofInstance(Registry.register(Registries.BLOCK, id("item_display_block"), new ItemDisplayBlock()));
public static final Supplier<Item> ITEM_DISPLAY_BLOCK_ITEM = Suppliers.ofInstance(Registry.register(Registries.ITEM, id("item_display_block"), new BlockItem(ITEM_DISPLAY_BLOCK.get(), new Item.Settings())));
public static final Supplier<BlockEntityType<ItemDisplayBlockEntity>> ITEM_DISPLAY_BLOCK_ENTITY = Suppliers.ofInstance(Registry.register(Registries.BLOCK_ENTITY_TYPE, id("item_display_block"), BlockEntityType.Builder.create(ItemDisplayBlockEntity::new, ITEM_DISPLAY_BLOCK.get()).build(null)));

public static final ItemDisplayBlock ITEM_DISPLAY_BLOCK = Registry.register(Registries.BLOCK, id("item_display_block"), new ItemDisplayBlock());
public static final Item ITEM_DISPLAY_BLOCK_ITEM = Registry.register(Registries.ITEM, id("item_display_block"), new BlockItem(ITEM_DISPLAY_BLOCK, new Item.Settings()));
public static final BlockEntityType<ItemDisplayBlockEntity> ITEM_DISPLAY_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, id("item_display_block"), BlockEntityType.Builder.create(ItemDisplayBlockEntity::new, ITEM_DISPLAY_BLOCK).build());
public static final Supplier<MailboxBlock> MAILBOX_BLOCK = Suppliers.ofInstance(Registry.register(Registries.BLOCK, id("mailbox"), new MailboxBlock()));
public static final Supplier<Item> MAILBOX_ITEM = Suppliers.ofInstance(Registry.register(Registries.ITEM, id("mailbox"), new BlockItem(MAILBOX_BLOCK.get(), new Item.Settings())));
public static final Supplier<BlockEntityType<MailboxBlockEntity>> MAILBOX_BLOCK_ENTITY = Suppliers.ofInstance(Registry.register(Registries.BLOCK_ENTITY_TYPE, id("mailbox"), BlockEntityType.Builder.create(MailboxBlockEntity::new, MAILBOX_BLOCK.get()).build(null)));

public static final MailboxBlock MAILBOX_BLOCK = Registry.register(Registries.BLOCK, id("mailbox"), new MailboxBlock());
public static final Item MAILBOX_ITEM = Registry.register(Registries.ITEM, id("mailbox"), new BlockItem(MAILBOX_BLOCK, new Item.Settings()));
public static final BlockEntityType<MailboxBlockEntity> MAILBOX_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, id("mailbox"), BlockEntityType.Builder.create(MailboxBlockEntity::new, MAILBOX_BLOCK).build());
public static final Supplier<TextBlock> TEXT_BLOCK = Suppliers.ofInstance(Registry.register(Registries.BLOCK, id("text_block"), new TextBlock()));
public static final Supplier<Item> TEXT_BLOCK_ITEM = Suppliers.ofInstance(Registry.register(Registries.ITEM, id("text_block"), new BlockItem(TEXT_BLOCK.get(), new Item.Settings())));
public static final Supplier<BlockEntityType<TextBlockEntity>> TEXT_BLOCK_ENTITY = Suppliers.ofInstance(Registry.register(Registries.BLOCK_ENTITY_TYPE, id("text_block"), BlockEntityType.Builder.create(TextBlockEntity::new, TEXT_BLOCK.get()).build(null)));

public static final TextBlock TEXT_BLOCK = Registry.register(Registries.BLOCK, id("text_block"), new TextBlock());
public static final Item TEXT_BLOCK_ITEM = Registry.register(Registries.ITEM, id("text_block"), new BlockItem(TEXT_BLOCK, new Item.Settings()));
public static final BlockEntityType<TextBlockEntity> TEXT_BLOCK_ENTITY = Registry.register(Registries.BLOCK_ENTITY_TYPE, id("text_block"), BlockEntityType.Builder.create(TextBlockEntity::new, TEXT_BLOCK).build());

public static final ItemGroup ITEM_GROUP = Registry.register(Registries.ITEM_GROUP, id("items"), FabricItemGroup.builder()
public static final Supplier<ItemGroup> ITEM_GROUP = Suppliers.ofInstance(Registry.register(Registries.ITEM_GROUP, id("items"), FabricItemGroup.builder()
.displayName(Text.translatable("itemGroup.glowcase.items"))
.icon(() -> new ItemStack(Items.GLOWSTONE))
.entries((displayContext, entries) -> {
entries.add(HYPERLINK_BLOCK_ITEM);
entries.add(ITEM_DISPLAY_BLOCK_ITEM);
entries.add(MAILBOX_ITEM);
entries.add(TEXT_BLOCK_ITEM);
entries.add(HYPERLINK_BLOCK_ITEM.get());
entries.add(ITEM_DISPLAY_BLOCK_ITEM.get());
entries.add(MAILBOX_ITEM.get());
entries.add(TEXT_BLOCK_ITEM.get());
})
.build()
);
));

public static Identifier id(String... path) {
return Identifier.of(MODID, String.join(".", path));
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/dev/hephaestus/glowcase/GlowcaseClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public class GlowcaseClient implements ClientModInitializer {
public void onInitializeClient() {
Glowcase.proxy = new GlowcaseClientProxy();

BlockEntityRendererFactories.register(Glowcase.TEXT_BLOCK_ENTITY, TextBlockEntityRenderer::new);
BlockEntityRendererFactories.register(Glowcase.HYPERLINK_BLOCK_ENTITY, HyperlinkBlockEntityRenderer::new);
BlockEntityRendererFactories.register(Glowcase.ITEM_DISPLAY_BLOCK_ENTITY, ItemDisplayBlockEntityRenderer::new);
BlockEntityRendererFactories.register(Glowcase.TEXT_BLOCK_ENTITY.get(), TextBlockEntityRenderer::new);
BlockEntityRendererFactories.register(Glowcase.HYPERLINK_BLOCK_ENTITY.get(), HyperlinkBlockEntityRenderer::new);
BlockEntityRendererFactories.register(Glowcase.ITEM_DISPLAY_BLOCK_ENTITY.get(), ItemDisplayBlockEntityRenderer::new);

WorldRenderEvents.AFTER_TRANSLUCENT.register(BakedBlockEntityRenderer.Manager::render);
InvalidateRenderStateCallback.EVENT.register(BakedBlockEntityRenderer.Manager::reset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(World world, BlockState state, BlockEntityType<T> type) {
return checkType(type, Glowcase.ITEM_DISPLAY_BLOCK_ENTITY, ItemDisplayBlockEntity::tick);
return checkType(type, Glowcase.ITEM_DISPLAY_BLOCK_ENTITY.get(), ItemDisplayBlockEntity::tick);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class HyperlinkBlockEntity extends BlockEntity {
private String url = "";

public HyperlinkBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.HYPERLINK_BLOCK_ENTITY, pos, state);
super(Glowcase.HYPERLINK_BLOCK_ENTITY.get(), pos, state);
}

public String getUrl() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ItemDisplayBlockEntity extends BlockEntity {
public Set<UUID> givenTo = new HashSet<>();

public ItemDisplayBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.ITEM_DISPLAY_BLOCK_ENTITY, pos, state);
super(Glowcase.ITEM_DISPLAY_BLOCK_ENTITY.get(), pos, state);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MailboxBlockEntity extends BlockEntity {
private UUID owner;

public MailboxBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.MAILBOX_BLOCK_ENTITY, pos, state);
super(Glowcase.MAILBOX_BLOCK_ENTITY.get(), pos, state);
}

public void setOwner(ServerPlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class TextBlockEntity extends BlockEntity {
public boolean renderDirty = true;

public TextBlockEntity(BlockPos pos, BlockState state) {
super(Glowcase.TEXT_BLOCK_ENTITY, pos, state);
super(Glowcase.TEXT_BLOCK_ENTITY.get(), pos, state);
lines.add(Text.empty());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public record HyperlinkBlockEntityRenderer(BlockEntityRendererFactory.Context context) implements BlockEntityRenderer<HyperlinkBlockEntity> {
private static final MinecraftClient mc = MinecraftClient.getInstance();

public static final ItemStack STACK = new ItemStack(Glowcase.HYPERLINK_BLOCK);
public static final ItemStack STACK = new ItemStack(Glowcase.HYPERLINK_BLOCK.get());

public void render(HyperlinkBlockEntity entity, float f, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
Camera camera = context.getRenderDispatcher().camera;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
public class PolydexCompatibility {
public static void onInitialize() {
HoverDisplayBuilder.register(Glowcase.ITEM_DISPLAY_BLOCK, PolydexCompatibility::setupItemDisplayBlock);
HoverDisplayBuilder.register(Glowcase.HYPERLINK_BLOCK, PolydexCompatibility::setupHyperlinkBlock);
HoverDisplayBuilder.register(Glowcase.ITEM_DISPLAY_BLOCK.get(), PolydexCompatibility::setupItemDisplayBlock);
HoverDisplayBuilder.register(Glowcase.HYPERLINK_BLOCK.get(), PolydexCompatibility::setupHyperlinkBlock);
}

private static void setupHyperlinkBlock(HoverDisplayBuilder hoverDisplayBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public record EditHyperlinkBlock(BlockPos pos, String url) implements CustomPayl

public static void receive(EditHyperlinkBlock payload, ServerPlayNetworking.Context context) {
context.player().server.submit(() -> {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.HYPERLINK_BLOCK) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof HyperlinkBlockEntity link && payload.url().length() <= URL_MAX_LENGTH) {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.HYPERLINK_BLOCK.get()) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof HyperlinkBlockEntity link && payload.url().length() <= URL_MAX_LENGTH) {
link.setUrl(payload.url());
}
});
Expand Down Expand Up @@ -70,7 +70,7 @@ public static void receive(EditItemDisplayBlockSettings payload, ServerPlayNetwo
if(payload.values().rotation() < 0 || payload.values().rotation() >= RotationPropertyHelper.getMax()) return;

context.player().server.execute(() -> {
if (canEditGlowcase(context.player(), payload.pos(), Glowcase.ITEM_DISPLAY_BLOCK) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof ItemDisplayBlockEntity be) {
if (canEditGlowcase(context.player(), payload.pos(), Glowcase.ITEM_DISPLAY_BLOCK.get()) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof ItemDisplayBlockEntity be) {
be.givesItem = payload.givesItem();
be.rotationType = payload.rotationType();
be.offset = payload.offset();
Expand Down Expand Up @@ -116,7 +116,7 @@ public record EditTextBlock(BlockPos pos, TextBlockEntity.TextAlignment alignmen

public static void receive(EditTextBlock payload, ServerPlayNetworking.Context context) {
context.player().server.execute(() -> {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.TEXT_BLOCK) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof TextBlockEntity be) {
if(canEditGlowcase(context.player(), payload.pos(), Glowcase.TEXT_BLOCK.get()) && context.player().getServerWorld().getBlockEntity(payload.pos()) instanceof TextBlockEntity be) {
be.scale = payload.values().scale();
be.lines = payload.values().lines();
be.textAlignment = payload.alignment();
Expand Down

0 comments on commit 924e7a3

Please sign in to comment.