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 24cf9a3 commit 2ba3650
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
package dev.anvilcraft.lib.data;

import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.data.recipes.RecipeProvider;
import net.minecraft.data.tags.TagsProvider;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Consumer;

public interface DataProviderType<P extends DataProvider> {
DataProviderType<AnvilLibItemModelProvider> ITEM_MODEL = new DataProviderType<>() {
};
DataProviderType<AnvilLibBlockModelProvider> BLOCK_MODEL = new DataProviderType<>() {
};
DataProviderType<RegistratorRecipeProvider> RECIPE = new DataProviderType<>() {
@Override
public void create(@NotNull DataGenerator.PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<RegistratorRecipeProvider>> consumer) {
generator.addProvider(output -> {
RegistratorRecipeProvider provider = new RegistratorRecipeProvider(output);
consumer.forEach(c -> c.accept(provider));
return provider;
});
}
};
DataProviderType<TagsProvider<Item>> ITEM_TAG = new DataProviderType<>() {
};
DataProviderType<TagsProvider<Block>> BLOCK_TAG = new DataProviderType<>() {
};
DataProviderType<LanguageProvider> LANG = new DataProviderType<>() {
@Override
public void create(@NotNull DataGenerator.PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<LanguageProvider>> consumer) {
generator.addProvider(output -> {
LanguageProvider provider = new LanguageProvider(output, namespace);
consumer.forEach(c -> c.accept(provider));
return provider;
});
generator.addProvider(output -> {
LanguageProvider provider = new UpsideDownLanguageProvider(output, namespace);
consumer.forEach(c -> c.accept(provider));
return provider;
});
}
};

default void create(@NotNull DataGenerator.PackGenerator generator, @NotNull String namespace, @NotNull List<Consumer<P>> consumer) {
}

@SuppressWarnings("unchecked")
default void create(@NotNull String namespace, @NotNull DataGenerator.PackGenerator generator, @NotNull List<Consumer<? extends DataProvider>> consumer) {
this.create(generator, namespace, consumer.stream().map(c -> (Consumer<P>) c).toList());
}
}
25 changes: 21 additions & 4 deletions common/src/main/java/dev/anvilcraft/lib/data/LanguageProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@
import org.jetbrains.annotations.NotNull;

import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

public abstract class LanguageProvider implements DataProvider {
public class LanguageProvider implements DataProvider {
protected final String namespace;
protected final String languageCode;
protected final PackOutput dataOutput;
protected final Map<String, String> translations = Collections.synchronizedMap(new HashMap<>());

protected LanguageProvider(PackOutput dataOutput, String namespace, String languageCode) {
this.namespace = namespace;
this.languageCode = languageCode;
this.dataOutput = dataOutput;
}

public abstract void add(TranslationBuilder builder);
public static @NotNull LanguageProvider create(PackOutput dataOutput, String namespace, @NotNull String languageCode) {
if (languageCode.equals("en_ud")) {
return new UpsideDownLanguageProvider(dataOutput, namespace);
}
return new LanguageProvider(dataOutput, namespace, languageCode);
}

protected void add(@NotNull TranslationBuilder builder) {
this.translations.forEach(builder::add);
}

public void add(String translationKey, String value) {
this.translations.put(translationKey, value);
}

public LanguageProvider(PackOutput dataOutput, String namespace) {
this(dataOutput, namespace, "en_us");
Expand All @@ -36,8 +53,8 @@ public LanguageProvider(PackOutput dataOutput, String namespace) {

protected @NotNull Path getLangFilePath(String code) {
return this.dataOutput
.createPathProvider(PackOutput.Target.RESOURCE_PACK, "lang")
.json(new ResourceLocation(this.namespace, code));
.createPathProvider(PackOutput.Target.RESOURCE_PACK, "lang")
.json(new ResourceLocation(this.namespace, code));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import net.minecraft.data.recipes.RecipeProvider;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class RegistratorRecipeProvider extends RecipeProvider implements Consumer<FinishedRecipe> {
Consumer<FinishedRecipe> writer;
private final List<FinishedRecipe> recipes = Collections.synchronizedList(new ArrayList<>());
private Consumer<FinishedRecipe> writer;

public RegistratorRecipeProvider(PackOutput output) {
super(output);
Expand All @@ -17,7 +21,7 @@ public RegistratorRecipeProvider(PackOutput output) {
@Override
public void accept(FinishedRecipe finishedRecipe) {
if (this.writer == null) {
/*TODO log error*/
this.recipes.add(finishedRecipe);
return;
}
this.writer.accept(finishedRecipe);
Expand All @@ -26,5 +30,6 @@ public void accept(FinishedRecipe finishedRecipe) {
@Override
public void buildRecipes(@NotNull Consumer<FinishedRecipe> writer) {
this.writer = writer;
this.recipes.forEach(writer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class UpsideDownLanguageProvider extends LanguageProvider {
public class UpsideDownLanguageProvider extends LanguageProvider {
protected UpsideDownLanguageProvider(PackOutput dataOutput, String namespace) {
super(dataOutput, namespace, "en_ud");
}

@Override
public @NotNull String getName() {
return "UpsideDownLanguage";
}

@Override
public @NotNull CompletableFuture<?> run(@NotNull CachedOutput output) {
JsonObject object = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
@SuppressWarnings("unused")
public abstract class AbstractRegistrator {
protected final BuilderManager manager = new BuilderManager();
protected final Map<DataProviderType<?>, List<Consumer<? extends DataProvider>>> dataProviders = Collections.synchronizedMap(new HashMap<>());
protected final Map<DataProviderType<? extends DataProvider>, List<Consumer<? extends DataProvider>>> dataProviders = Collections.synchronizedMap(new HashMap<>());
private final String modid;

protected AbstractRegistrator(String modid) {
Expand Down Expand Up @@ -70,13 +70,12 @@ public <P extends DataProvider> void data(DataProviderType<P> type, Consumer<P>
public void init() {
}

@SuppressWarnings("unchecked")
protected <P extends DataProvider> List<Consumer<P>> getDataProviders(DataProviderType<P> type) {
return (List<Consumer<P>>) (List<?>) this.dataProviders.getOrDefault(type, Collections.emptyList());
}

@SuppressWarnings("UnusedReturnValue")
public <T extends DataProvider> AbstractRegistrator initDatagen(DataGenerator generator) {
public <T extends DataProvider> AbstractRegistrator initDatagen(DataGenerator.PackGenerator generator) {
for (Map.Entry<DataProviderType<? extends DataProvider>, List<Consumer<? extends DataProvider>>> entry : this.dataProviders.entrySet()) {
entry.getKey().create(this.modid, generator, entry.getValue());
}

return this;
}

Expand All @@ -89,4 +88,8 @@ public <T> AbstractRegistrator addBuilder(Registry<T> registry, EntryBuilder<? e
protected <V, T extends V> List<EntryBuilder<T>> getBuilders(Registry<V> registry) {
return this.manager.getBuilders(registry);
}

public void lang(String key, String name) {
this.data(DataProviderType.LANG, provider -> provider.add(key, name));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.entry.RegistryEntry;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

public abstract class EntryBuilder<T> {
protected final AbstractRegistrator registrator;
Expand All @@ -21,4 +22,22 @@ protected EntryBuilder(AbstractRegistrator registrator, String id) {
public ResourceLocation getId() {
return this.registrator.of(this.id);
}

protected static @NotNull String toTitleCase(@NotNull String input) {
// 使用下划线分割字符串
String[] parts = input.split("_");
StringBuilder result = new StringBuilder();

for (String part : parts) {
// 将每个单词的首字母转为大写,其余字母保持原样
if (!part.isEmpty()) {
result.append(Character.toUpperCase(part.charAt(0)));
result.append(part.substring(1));
}
result.append(" ");
}

// 删除最后一个多余的空格
return result.toString().trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.anvilcraft.lib.mixin.ItemPropertiesAccessor;
import dev.anvilcraft.lib.registrator.AbstractRegistrator;
import dev.anvilcraft.lib.registrator.entry.ItemEntry;
import net.minecraft.Util;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
Expand All @@ -26,6 +27,7 @@ public ItemBuilder(AbstractRegistrator registrator, String id, Function<Item.Pro
super(registrator, id);
this.factory = factory;
this.entry = new ItemEntry<>(this);
this.lang(toTitleCase(this.id));
}

public ItemBuilder<T> model(BiConsumer<ItemEntry<T>, AnvilLibItemModelProvider> consumer) {
Expand All @@ -48,7 +50,9 @@ public ItemBuilder<T> recipe(BiConsumer<ItemEntry<T>, RegistratorRecipeProvider>
return this;
}

@SuppressWarnings("UnusedReturnValue")
public ItemBuilder<T> lang(String name) {
this.registrator.lang(Util.makeDescriptionId("item", this.registrator.of(this.id)), name);
return this;
}

Expand All @@ -63,23 +67,23 @@ public ItemBuilder<T> initProperties(Supplier<Item> supplier) {
if (supplier instanceof ItemEntry<?> itemEntry) {
ItemPropertiesAccessor accessor = (ItemPropertiesAccessor) itemEntry.getItemBuilder().properties;
if (
accessor.getMaxStackSize() != defaultProperties.getMaxStackSize()
&& thisAccessor.getMaxStackSize() == defaultProperties.getMaxStackSize()
accessor.getMaxStackSize() != defaultProperties.getMaxStackSize()
&& thisAccessor.getMaxStackSize() == defaultProperties.getMaxStackSize()
) {
this.properties.stacksTo(accessor.getMaxStackSize());
}
if (
accessor.getMaxDamage() != defaultProperties.getMaxDamage()
&& thisAccessor.getMaxDamage() == defaultProperties.getMaxDamage()
accessor.getMaxDamage() != defaultProperties.getMaxDamage()
&& thisAccessor.getMaxDamage() == defaultProperties.getMaxDamage()
) {
this.properties.durability(accessor.getMaxDamage());
}
if (accessor.getCraftingRemainingItem() != null && thisAccessor.getCraftingRemainingItem() == null) {
this.properties.craftRemainder(accessor.getCraftingRemainingItem());
}
if (
accessor.getRarity() != defaultProperties.getRarity()
&& thisAccessor.getRarity() == defaultProperties.getRarity()
accessor.getRarity() != defaultProperties.getRarity()
&& thisAccessor.getRarity() == defaultProperties.getRarity()
) {
this.properties.rarity(accessor.getRarity());
}
Expand All @@ -93,34 +97,34 @@ public ItemBuilder<T> initProperties(Supplier<Item> supplier) {
Item item = supplier.get();
if (item != null) {
if (
item.getMaxDamage() != defaultProperties.getMaxDamage()
&& thisAccessor.getMaxDamage() == defaultProperties.getMaxDamage()
item.getMaxDamage() != defaultProperties.getMaxDamage()
&& thisAccessor.getMaxDamage() == defaultProperties.getMaxDamage()
) {
this.properties.durability(item.getMaxDamage());
}
if (item.getCraftingRemainingItem() != null && thisAccessor.getCraftingRemainingItem() == null) {
this.properties.craftRemainder(item.getCraftingRemainingItem());
}
if (
item.getRarity(new ItemStack(item)) != defaultProperties.getRarity()
&& thisAccessor.getRarity() == defaultProperties.getRarity()
item.getRarity(new ItemStack(item)) != defaultProperties.getRarity()
&& thisAccessor.getRarity() == defaultProperties.getRarity()
) {
this.properties.rarity(item.getRarity(new ItemStack(item)));
}
if (
item.getMaxStackSize() != defaultProperties.getMaxStackSize()
&& thisAccessor.getMaxStackSize() == defaultProperties.getMaxStackSize()
item.getMaxStackSize() != defaultProperties.getMaxStackSize()
&& thisAccessor.getMaxStackSize() == defaultProperties.getMaxStackSize()
) {
this.properties.stacksTo(item.getMaxStackSize());
}
if (
item.getFoodProperties() != null
&& thisAccessor.getFoodProperties() == null
item.getFoodProperties() != null
&& thisAccessor.getFoodProperties() == null
) {
this.properties.food(item.getFoodProperties());
}
if (
item.isFireResistant() && !thisAccessor.isFireResistant()
item.isFireResistant() && !thisAccessor.isFireResistant()
) {
this.properties.fireResistant();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// 1.20.1 2024-08-31T15:38:52.951909 AnvilLib Test/Recipes
4cd064cbd4fa1437c2a145d2176b2cbb5b7ccc06 data/anvil-lib-test/advancements/recipes/misc/test.json
5ff54d03f1e14bdfe21d703af7ab8622773d680c data/anvil-lib-test/recipes/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.1 2024-08-31T15:38:52.958558 AnvilLib Test/UpsideDownLanguage
4f2270cb5b55f19f5fd5a278fec75b064631cbfa assets/anvil-lib-test/lang/en_ud.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.1 2024-08-31T15:38:52.956432 AnvilLib Test/Language
5cca06a243afdcb758b4e9b0c89bb9a8e102719d assets/anvil-lib-test/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"item.anvil-lib-test.test": "ʇsǝ⟘"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"item.anvil-lib-test.test": "Test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"parent": "minecraft:recipes/root",
"criteria": {
"has_apple": {
"conditions": {
"items": [
{
"items": [
"minecraft:apple"
]
}
]
},
"trigger": "minecraft:inventory_changed"
},
"has_the_recipe": {
"conditions": {
"recipe": "anvil-lib-test:test"
},
"trigger": "minecraft:recipe_unlocked"
}
},
"requirements": [
[
"has_apple",
"has_the_recipe"
]
],
"rewards": {
"recipes": [
"anvil-lib-test:test"
]
},
"sends_telemetry_event": false
}
Loading

0 comments on commit 2ba3650

Please sign in to comment.