Skip to content

Commit

Permalink
🌟 更新注册 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Aug 31, 2024
1 parent eac1b86 commit 7a0bd9b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.entry.BlockEntry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;

Expand All @@ -16,17 +17,21 @@ public class BlockBuilder<T extends Block> extends EntryBuilder<T> {
private final Function<BlockBehaviour.Properties, T> factory;
private final BlockBehaviour.Properties properties = BlockBehaviour.Properties.of();

public BlockBuilder(AbstractRegistrator registrar, String id, Function<BlockBehaviour.Properties, T> factory) {
super(registrar, id);
public BlockBuilder(AbstractRegistrator registrator, String id, Function<BlockBehaviour.Properties, T> factory) {
super(registrator, id);
this.factory = factory;
this.entry = new BlockEntry<>(this);
}

public BlockBuilder<T> model(BiConsumer<BlockEntry<T>, AnvilLibBlockModelProvider> consumer) {
this.registrar.data(DataProviderType.BLOCK_MODEL, p -> consumer.accept(this.entry, p));
this.registrator.data(DataProviderType.BLOCK_MODEL, p -> consumer.accept(this.entry, p));
return this;
}

public <I extends BlockItem> BlockItemBuilder<I, T> item() {
return (BlockItemBuilder<I, T>) new BlockItemBuilder<>(this.registrator, this, this.id, BlockItem::new);
}

public T build() {
T block = this.factory.apply(this.properties);
this.entry.set(block);
Expand All @@ -35,7 +40,7 @@ public T build() {

@Override
public BlockEntry<T> register() {
this.registrar.addBuilder(BuiltInRegistries.BLOCK, this);
this.registrator.addBuilder(BuiltInRegistries.BLOCK, this);
return this.entry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

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

public class BlockItemBuilder<T extends BlockItem, B extends Block> extends ItemBuilder<T> {
protected final BlockBuilder<B> blockBuilder;

public BlockItemBuilder(AbstractRegistrator registrar, BlockBuilder<B> builder, String id, Function<Item.Properties, T> factory) {
super(registrar, id, factory);
public BlockItemBuilder(AbstractRegistrator registrator, BlockBuilder<B> builder, String id, BiFunction<Block, Item.Properties, T> factory) {
super(registrator, id, (properties) -> factory.apply(null, properties));
this.blockBuilder = builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import net.minecraft.resources.ResourceLocation;

public abstract class EntryBuilder<T> {
protected final AbstractRegistrator registrar;
protected final AbstractRegistrator registrator;
protected final String id;

protected EntryBuilder(AbstractRegistrator registrar, String id) {
this.registrar = registrar;
protected EntryBuilder(AbstractRegistrator registrator, String id) {
this.registrator = registrator;
this.id = id;
}

Expand All @@ -19,6 +19,6 @@ protected EntryBuilder(AbstractRegistrator registrar, String id) {
public abstract RegistryEntry<T> register();

public ResourceLocation getId() {
return this.registrar.of(this.id);
return this.registrator.of(this.id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.entry.ItemEntry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -23,14 +22,14 @@ public class ItemBuilder<T extends Item> extends EntryBuilder<T> {
private final Item.Properties properties = new Item.Properties();
private final Function<Item.Properties, T> factory;

public ItemBuilder(AbstractRegistrator registrar, String id, Function<Item.Properties, T> factory) {
super(registrar, id);
public ItemBuilder(AbstractRegistrator registrator, String id, Function<Item.Properties, T> factory) {
super(registrator, id);
this.factory = factory;
this.entry = new ItemEntry<>(this);
}

public ItemBuilder<T> model(BiConsumer<ItemEntry<T>, AnvilLibItemModelProvider> consumer) {
this.registrar.data(DataProviderType.ITEM_MODEL, p -> consumer.accept(this.entry, p));
this.registrator.data(DataProviderType.ITEM_MODEL, p -> consumer.accept(this.entry, p));
return this;
}

Expand All @@ -45,7 +44,7 @@ public final ItemBuilder<T> tag(TagKey<Item>... tags) {
}

public ItemBuilder<T> recipe(BiConsumer<ItemEntry<T>, RegistratorRecipeProvider> consumer) {
this.registrar.data(DataProviderType.RECIPE, p -> consumer.accept(this.entry, p));
this.registrator.data(DataProviderType.RECIPE, p -> consumer.accept(this.entry, p));
return this;
}

Expand Down Expand Up @@ -138,7 +137,7 @@ public T build() {
}

public ItemEntry<T> register() {
this.registrar.addBuilder(BuiltInRegistries.ITEM, this);
this.registrator.addBuilder(BuiltInRegistries.ITEM, this);
return this.entry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

public final class TagKeyEntry<T> extends RegistryEntry<TagKey<T>> {
private TagKey<T> self = null;
private final AbstractRegistrator registrar;
private final AbstractRegistrator registrator;
private final ResourceKey<? extends Registry<T>> registry;
@NotNull
private final String defaultPath;
private final String forgePath;

private TagKeyEntry(AbstractRegistrator registrar, ResourceKey<? extends Registry<T>> registry, @NotNull String defaultPath, String forgePath) {
this.registrar = registrar;
private TagKeyEntry(AbstractRegistrator registrator, ResourceKey<? extends Registry<T>> registry, @NotNull String defaultPath, String forgePath) {
this.registrator = registrator;
this.registry = registry;
this.defaultPath = defaultPath;
this.forgePath = forgePath;
}

@NotNull
public static <T> TagKeyEntry<T> create(AbstractRegistrator registrar, ResourceKey<? extends Registry<T>> registry, @NotNull String fabricPath, String forgePath) {
return new TagKeyEntry<>(registrar, registry, fabricPath, forgePath);
public static <T> TagKeyEntry<T> create(AbstractRegistrator registrator, ResourceKey<? extends Registry<T>> registry, @NotNull String fabricPath, String forgePath) {
return new TagKeyEntry<>(registrator, registry, fabricPath, forgePath);
}

@Override
Expand All @@ -36,8 +36,8 @@ public static <T> TagKeyEntry<T> create(AbstractRegistrator registrar, ResourceK
case FORGE -> this.forgePath != null ? this.forgePath : this.defaultPath;
default -> this.defaultPath;
};
ResourceLocation location = this.registrar != null ?
this.registrar.of(path) :
ResourceLocation location = this.registrator != null ?
this.registrator.of(path) :
switch (AnvilLib.getPlatform()) {
case FORGE -> new ResourceLocation("forge", path);
default -> new ResourceLocation("c", path);
Expand Down

0 comments on commit 7a0bd9b

Please sign in to comment.