Skip to content

Commit

Permalink
🌟 更新注册 API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Sep 4, 2024
1 parent d9ac129 commit 2ec021a
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package dev.anvilcraft.lib.registrator;

import dev.anvilcraft.lib.data.DataProviderType;
import dev.anvilcraft.lib.registrator.builder.CreativeModeTabBuilder;
import dev.anvilcraft.lib.registrator.builder.EntryBuilder;
import dev.anvilcraft.lib.registrator.builder.ItemBuilder;
import dev.anvilcraft.lib.registrator.builder.BlockBuilder;
import dev.anvilcraft.lib.registrator.entry.RegistryEntry;
import dev.anvilcraft.lib.registrator.entry.TagKeyEntry;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -78,17 +77,7 @@ public <T extends BlockEntity> EntryBuilder<T> blockEntity(String id) {
}

public EntryBuilder<CreativeModeTab> tab(String id, Consumer<CreativeModeTab.Builder> consumer) {
return new EntryBuilder<>(AbstractRegistrator.this, id) {
public CreativeModeTab build() {
return null;
}

@Override
public RegistryEntry<CreativeModeTab> register() {
this.registrator.addBuilder(BuiltInRegistries.CREATIVE_MODE_TAB, this);
return null;
}
};
return CreativeModeTabBuilder.create(this, id, consumer);
}

public <T extends AbstractContainerMenu> EntryBuilder<T> menu(String id) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package dev.anvilcraft.lib.registrator.builder;

import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.entry.RegistryEntry;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.ItemStack;

import java.util.function.Consumer;
import java.util.function.Supplier;

public abstract class CreativeModeTabBuilder extends EntryBuilder<CreativeModeTab> {
protected final RegistryEntry<CreativeModeTab> tab = new RegistryEntry<>() {
};
protected final CreativeModeTab.Builder builder;

protected CreativeModeTabBuilder(AbstractRegistrator registrator, String id, CreativeModeTab.Builder builder) {
super(registrator, id);
this.builder = builder;
}

@ExpectPlatform
public static CreativeModeTabBuilder create(AbstractRegistrator registrator, String id, Consumer<CreativeModeTab.Builder> consumer) {
throw new AssertionError();
}

public CreativeModeTabBuilder title(Component title) {
this.builder.title(title);
return this;
}

public CreativeModeTabBuilder icon(Supplier<ItemStack> icon) {
this.builder.icon(icon);
return this;
}

public CreativeModeTabBuilder displayItems(CreativeModeTab.DisplayItemsGenerator displayItemsGenerator) {
this.builder.displayItems(displayItemsGenerator);
return this;
}

public CreativeModeTabBuilder alignedRight() {
this.builder.alignedRight();
return this;
}

public CreativeModeTabBuilder hideTitle() {
this.builder.hideTitle();
return this;
}

public CreativeModeTabBuilder noScrollBar() {
this.builder.noScrollBar();
return this;
}

public CreativeModeTabBuilder backgroundSuffix(String backgroundSuffix) {
this.builder.backgroundSuffix(backgroundSuffix);
return this;
}

@Override
public CreativeModeTab build() {
this.tab.set(this.builder.build());
return this.tab.get();
}

@Override
public RegistryEntry<CreativeModeTab> register() {
this.registrator.addBuilder(BuiltInRegistries.CREATIVE_MODE_TAB, this);
return this.tab;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.anvilcraft.lib.registrator.builder.fabric;

import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.builder.CreativeModeTabBuilder;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
import net.minecraft.world.item.CreativeModeTab;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public class CreativeModeTabBuilderImpl extends CreativeModeTabBuilder {
protected CreativeModeTabBuilderImpl(AbstractRegistrator registrator, String id) {
super(registrator, id, FabricItemGroup.builder());
}

public static @NotNull CreativeModeTabBuilder create(AbstractRegistrator registrator, String id, @NotNull Consumer<CreativeModeTab.Builder> consumer) {
CreativeModeTabBuilderImpl builder = new CreativeModeTabBuilderImpl(registrator, id);
consumer.accept(builder.builder);
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.anvilcraft.lib.registrator.builder.forge;

import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.builder.CreativeModeTabBuilder;
import net.minecraft.world.item.CreativeModeTab;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public class CreativeModeTabBuilderImpl extends CreativeModeTabBuilder {
protected CreativeModeTabBuilderImpl(AbstractRegistrator registrator, String id) {
super(registrator, id, CreativeModeTab.builder());
}

public static @NotNull CreativeModeTabBuilder create(AbstractRegistrator registrator, String id, @NotNull Consumer<CreativeModeTab.Builder> consumer) {
CreativeModeTabBuilderImpl builder = new CreativeModeTabBuilderImpl(registrator, id);
consumer.accept(builder.builder);
return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.anvilcraft.lib.registrator.builder.forge;

import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.builder.CreativeModeTabBuilder;
import net.minecraft.world.item.CreativeModeTab;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;

public class CreativeModeTabBuilderImpl extends CreativeModeTabBuilder {
protected CreativeModeTabBuilderImpl(AbstractRegistrator registrator, String id) {
super(registrator, id, CreativeModeTab.builder());
}

public static @NotNull CreativeModeTabBuilder create(AbstractRegistrator registrator, String id, @NotNull Consumer<CreativeModeTab.Builder> consumer) {
CreativeModeTabBuilderImpl builder = new CreativeModeTabBuilderImpl(registrator, id);
consumer.accept(builder.builder);
return builder;
}
}

0 comments on commit 2ec021a

Please sign in to comment.