Skip to content

Commit

Permalink
Pass default properties to block and item configs
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Dec 26, 2024
1 parent 7e8d285 commit 2d2e0d7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public class BlockAction<M extends IModBase> extends ConfigurableTypeActionRegis
* @param callback A callback that will be called when the entry is registered.
*/
public static <M extends IModBase> void register(@Nullable BiFunction<BlockConfigCommon<M>, Block, ? extends Item> itemBlockConstructor, BlockConfigCommon<M> config, @Nullable Callable<?> callback) {
register(config, itemBlockConstructor == null ? callback : null); // Delay onForgeRegistered callback until item has been registered if one is applicable
register(config, itemBlockConstructor == null ? callback : null); // Delay onRegistryRegistered callback until item has been registered if one is applicable
if(itemBlockConstructor != null) {
ItemConfigCommon<M> itemConfig = new ItemConfigCommon<>(config.getMod(), config.getNamedId(), (iConfig) -> {
ItemConfigCommon<M> itemConfig = new ItemConfigCommon<>(config.getMod(), config.getNamedId(), (iConfig, properties) -> {
Item itemBlock = itemBlockConstructor.apply(config, config.getInstance());
Objects.requireNonNull(itemBlock, "Received a null item for the item block constructor of " + config.getNamedId());
return itemBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -30,9 +33,9 @@ public abstract class BlockConfigCommon<M extends IModBase> extends ExtendedConf
private Item itemInstance;
private BlockClientConfig<M> clientConfig;

public BlockConfigCommon(M mod, String namedId, Function<BlockConfigCommon<M>, ? extends Block> blockConstructor,
public BlockConfigCommon(M mod, String namedId, BiFunction<BlockConfigCommon<M>, Block.Properties, ? extends Block> blockConstructor,
@Nullable BiFunction<BlockConfigCommon<M>, Block, ? extends Item> itemConstructor) {
super(mod, namedId, blockConstructor);
super(mod, namedId, eConfig -> blockConstructor.apply(eConfig, Block.Properties.of().setId((ResourceKey<Block>) eConfig.getResourceKey())));
this.itemConstructor = itemConstructor;
}

Expand All @@ -42,7 +45,10 @@ public BlockConfigCommon(M mod, String namedId, Function<BlockConfigCommon<M>, ?

protected static <M extends IModBase> BiFunction<BlockConfigCommon<M>, Block, ? extends BlockItem> getDefaultItemConstructor(M mod, @Nullable Function<Item.Properties, Item.Properties> itemPropertiesModifier) {
return (eConfig, block) -> {
Item.Properties itemProperties = new Item.Properties();
Item.Properties itemProperties = new Item.Properties()
.setId(ResourceKey.create(Registries.ITEM,
ResourceLocation.fromNamespaceAndPath(eConfig.getMod().getModId(), eConfig.getNamedId())))
.useBlockDescriptionPrefix();
if (itemPropertiesModifier != null) {
itemProperties = itemPropertiesModifier.apply(itemProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ public static <M extends IModBase, T extends Mob> BiFunction<EntityConfigCommon<

public static <M extends IModBase, T extends Mob> BiFunction<EntityConfigCommon<M, T>, Supplier<EntityType<T>>, ItemConfigCommon<M>> getDefaultSpawnEggItemConfigConstructor(M mod, String itemName, int primaryColorIn, int secondaryColorIn, @Nullable Function<Item.Properties, Item.Properties> itemPropertiesModifier) {
return (entityConfig, entityType) -> {
Item.Properties itemProperties = new Item.Properties();
if (itemPropertiesModifier != null) {
itemProperties = itemPropertiesModifier.apply(itemProperties);
}
Item.Properties finalItemProperties = itemProperties;
ItemConfigCommon<M> itemConfig = new ItemConfigCommon<>(mod, itemName, (itemConfigSub) ->new SpawnEggItem(entityType.get(), finalItemProperties));
ItemConfigCommon<M> itemConfig = new ItemConfigCommon<>(mod, itemName, (itemConfigSub, properties) -> {
if (itemPropertiesModifier != null) {
properties = itemPropertiesModifier.apply(properties);
}
return new SpawnEggItem(entityType.get(), properties);
});
entityConfig.setSpawnEggItemConfig(itemConfig);
return itemConfig;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.cyclops.cyclopscore.config.ConfigurableTypeCommon;
Expand All @@ -10,7 +11,7 @@
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Function;
import java.util.function.BiFunction;

/**
* Config for items.
Expand All @@ -26,8 +27,8 @@ public class ItemConfigCommon<M extends IModBase> extends ExtendedConfigRegistry
* @param namedId The unique name ID for the configurable.
* @param elementConstructor The element constructor.
*/
public ItemConfigCommon(M mod, String namedId, Function<ItemConfigCommon<M>, ? extends Item> elementConstructor) {
super(mod, namedId, elementConstructor);
public ItemConfigCommon(M mod, String namedId, BiFunction<ItemConfigCommon<M>, Item.Properties, ? extends Item> elementConstructor) {
super(mod, namedId, eConfig -> elementConstructor.apply(eConfig, new Item.Properties().setId((ResourceKey<Item>) eConfig.getResourceKey())));
}

@Override
Expand Down

0 comments on commit 2d2e0d7

Please sign in to comment.