Skip to content

Commit

Permalink
refactor(1.21.2): Change item and block registration to receive ident…
Browse files Browse the repository at this point in the history
…ifier in suppliers
  • Loading branch information
BlayTheNinth committed Oct 10, 2024
1 parent a098ce3 commit d2051b0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,55 @@
import net.minecraft.world.level.block.state.BlockBehaviour;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

public interface BalmBlocks {
BlockBehaviour.Properties blockProperties();

DeferredObject<Block> registerBlock(Supplier<Block> supplier, ResourceLocation identifier);
DeferredObject<Block> registerBlock(Function<ResourceLocation, Block> supplier, ResourceLocation identifier);

DeferredObject<Item> registerBlockItem(Function<ResourceLocation, BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);

void register(Function<ResourceLocation, Block> blockSupplier, BiFunction<Block, ResourceLocation, BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);

default DeferredObject<Item> registerBlockItem(Function<ResourceLocation, BlockItem> supplier, ResourceLocation identifier) {
return registerBlockItem(supplier, identifier, identifier.withPath(identifier.getNamespace()));
}

default void register(Function<ResourceLocation, Block> blockSupplier, BiFunction<Block, ResourceLocation, BlockItem> blockItemSupplier, ResourceLocation identifier) {
register(blockSupplier, blockItemSupplier, identifier, identifier.withPath(identifier.getNamespace()));
}

@Deprecated
default BlockBehaviour.Properties blockProperties() {
return BlockBehaviour.Properties.of();
}

@Deprecated
default DeferredObject<Block> registerBlock(Supplier<Block> supplier, ResourceLocation identifier) {
return registerBlock((id) -> supplier.get(), identifier);
}

@Deprecated
default DeferredObject<Item> registerBlockItem(Supplier<BlockItem> supplier, ResourceLocation identifier) {
return registerBlockItem(supplier, identifier, identifier.withPath(identifier.getNamespace()));
}

DeferredObject<Item> registerBlockItem(Supplier<BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);
@Deprecated
default DeferredObject<Item> registerBlockItem(Supplier<BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return registerBlockItem((id) -> supplier.get(), identifier, creativeTab);
}

@Deprecated
default void register(Supplier<Block> blockSupplier, Supplier<BlockItem> blockItemSupplier, ResourceLocation identifier) {
register(blockSupplier, blockItemSupplier, identifier, identifier.withPath(identifier.getNamespace()));
}

void register(Supplier<Block> blockSupplier, Supplier<BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);
@Deprecated
default void register(Supplier<Block> blockSupplier, Supplier<BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
registerBlock(blockSupplier, identifier);
registerBlockItem((id) -> blockItemSupplier.get(), identifier, creativeTab);
}

}
21 changes: 18 additions & 3 deletions common/src/main/java/net/blay09/mods/balm/api/item/BalmItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,35 @@
import org.jetbrains.annotations.Nullable;

import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Supplier;

public interface BalmItems {
Item.Properties itemProperties();

default DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier) {
default DeferredObject<Item> registerItem(Function<ResourceLocation, Item> supplier, ResourceLocation identifier) {
return registerItem(supplier, identifier, identifier.withPath(identifier.getNamespace()));
}

DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);
DeferredObject<Item> registerItem(Function<ResourceLocation, Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab);

DeferredObject<CreativeModeTab> registerCreativeModeTab(Supplier<ItemStack> iconSupplier, ResourceLocation identifier);

void addToCreativeModeTab(ResourceLocation tabIdentifier, Supplier<ItemLike[]> itemsSupplier);

void setCreativeModeTabSorting(ResourceLocation tabIdentifier, Comparator<ItemLike> comparator);

@Deprecated
default Item.Properties itemProperties() {
return new Item.Properties();
}

@Deprecated
default DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier) {
return registerItem(supplier, identifier, identifier.withPath(identifier.getNamespace()));
}

@Deprecated
default DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return registerItem((id) -> supplier.get(), identifier, creativeTab);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,29 @@
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;
import java.util.function.BiFunction;
import java.util.function.Function;

public class FabricBalmBlocks implements BalmBlocks {
@Override
public BlockBehaviour.Properties blockProperties() {
return BlockBehaviour.Properties.of();
}

@Override
public DeferredObject<Block> registerBlock(Supplier<Block> supplier, ResourceLocation identifier) {
public DeferredObject<Block> registerBlock(Function<ResourceLocation, Block> supplier, ResourceLocation identifier) {
return new DeferredObject<>(identifier, () -> {
Block block = supplier.get();
final var block = supplier.apply(identifier);
return Registry.register(BuiltInRegistries.BLOCK, identifier, block);
}).resolveImmediately();
}

@Override
public DeferredObject<Item> registerBlockItem(Supplier<BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return Balm.getItems().registerItem(supplier::get, identifier, creativeTab);
public DeferredObject<Item> registerBlockItem(Function<ResourceLocation, BlockItem> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return Balm.getItems().registerItem(() -> supplier.apply(identifier), identifier, creativeTab);
}

@Override
public void register(Supplier<Block> blockSupplier, Supplier<BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
registerBlock(blockSupplier, identifier);
registerBlockItem(blockItemSupplier, identifier, creativeTab);
public void register(Function<ResourceLocation, Block> blockSupplier, BiFunction<Block, ResourceLocation, BlockItem> blockItemSupplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
final var deferredBlock = registerBlock(blockSupplier, identifier);
registerBlockItem((id) -> blockItemSupplier.apply(deferredBlock.get(), id), identifier, creativeTab);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;

public class FabricBalmItems implements BalmItems {
Expand All @@ -29,14 +30,9 @@ public class FabricBalmItems implements BalmItems {
private final Map<ResourceLocation, Comparator<ItemLike>> creativeTabSorting = new HashMap<>();

@Override
public Item.Properties itemProperties() {
return new Item.Properties();
}

@Override
public DeferredObject<Item> registerItem(Supplier<Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
public DeferredObject<Item> registerItem(Function<ResourceLocation, Item> supplier, ResourceLocation identifier, @Nullable ResourceLocation creativeTab) {
return new DeferredObject<>(identifier, () -> {
Item item = supplier.get();
Item item = supplier.apply(identifier);
item = Registry.register(BuiltInRegistries.ITEM, identifier, item);
if (creativeTab != null) {
manageCreativeModeTab(creativeTab);
Expand Down

0 comments on commit d2051b0

Please sign in to comment.