Files
src
build.gradle
gradle.properties
settings.gradle
Code
66 235
src/main/java/com/ascendcraft/Ascendcraft.java
package com.ascendcraft;
import com.mojang.serialization.MapCodec;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.biome.v1.BiomeModifications;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectors;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents;
import net.fabricmc.fabric.api.loot.v3.LootTableEvents;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.CropBlock;
import net.minecraft.block.PlantBlock;
import net.minecraft.component.type.ConsumableComponents;
import net.minecraft.component.type.FoodComponent;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Items;
import net.minecraft.loot.LootPool;
import net.minecraft.loot.LootTables;
import net.minecraft.loot.condition.RandomChanceLootCondition;
import net.minecraft.loot.entry.ItemEntry;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.util.Identifier;
import net.minecraft.item.ItemConvertible;
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.gen.GenerationStep;
public class Ascendcraft implements ModInitializer {
private static final Block BARLEY_CROP = crop("barley_crop", true);
private static final Block CABBAGE_CROP = crop("cabbage_crop", false);
private static final Block WILD_BARLEY = wildBarley();
private static final Item BARLEY_SEEDS = seed("barley_seeds", BARLEY_CROP);
private static final Item CABBAGE_SEEDS = seed("cabbage_seeds", CABBAGE_CROP);
private static final Item BARLEY = item("barley", 0, 0, null, false);
private static final Item CABBAGE = item("cabbage", 2, 0.3f, null, false);
private static final Item BARLEY_FLOUR = item("barley_flour", 0, 0, null, false);
private static final Item BARLEY_BREAD = item("barley_bread", 5, 0.6f, null, false);
private static final Item CABBAGE_STEW = item("cabbage_stew", 7, 0.6f, Items.BOWL, false);
private static final Item ROASTED_CABBAGE = item("roasted_cabbage", 5, 0.5f, null, false);
private static final Item MEAT_PIE = item("meat_pie", 8, 0.7f, null, false);
private static final Item BERRY_TART = item("berry_tart", 6, 0.4f, null, false);
private static final Item APPLE_CIDER = item("apple_cider", 4, 0.3f, Items.GLASS_BOTTLE, true);
private static final Item HONEY_TEA = item("honey_tea", 3, 0.4f, Items.GLASS_BOTTLE, true);
private static Identifier id(String name) { return Identifier.of("ascendcraft", name); }
private static Block wildBarley() {
RegistryKey<Block> key = RegistryKey.of(RegistryKeys.BLOCK, id("wild_barley"));
return Registry.register(Registries.BLOCK, key, new WildBarley(AbstractBlock.Settings.create()
.registryKey(key).noCollision().breakInstantly().sounds(BlockSoundGroup.CROP)));
}
private static Block crop(String name, boolean barley) {
RegistryKey<Block> key = RegistryKey.of(RegistryKeys.BLOCK, id(name));
return Registry.register(Registries.BLOCK, key, new RusticCrop(AbstractBlock.Settings.create()
.registryKey(key).noCollision().ticksRandomly().breakInstantly().sounds(BlockSoundGroup.CROP), barley));
}
private static Item seed(String name, Block block) {
RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id(name));
return Registry.register(Registries.ITEM, key, new BlockItem(block, new Item.Settings().registryKey(key)));
}
private static Item item(String name, int nutrition, float saturation, Item remainder, boolean drink) {
RegistryKey<Item> key = RegistryKey.of(RegistryKeys.ITEM, id(name));
Item.Settings settings = new Item.Settings().registryKey(key);
if (nutrition > 0) {
FoodComponent food = new FoodComponent.Builder().nutrition(nutrition).saturationModifier(saturation).build();
if (drink) settings.food(food, ConsumableComponents.DRINK);
else settings.food(food);
}
if (remainder != null) settings.useRemainder(remainder).maxCount(16);
return Registry.register(Registries.ITEM, key, new Item(settings));
}
@Override
public void onInitialize() {
BiomeModifications.addFeature(BiomeSelectors.includeByKey(BiomeKeys.PLAINS, BiomeKeys.MEADOW),
GenerationStep.Feature.VEGETAL_DECORATION,
RegistryKey.of(RegistryKeys.PLACED_FEATURE, id("wild_barley")));
LootTableEvents.MODIFY.register((key, table, source, registries) -> {
if (source.isBuiltin() && (key.equals(LootTables.VILLAGE_PLAINS_CHEST)
|| key.equals(LootTables.VILLAGE_DESERT_HOUSE_CHEST)
|| key.equals(LootTables.VILLAGE_TAIGA_HOUSE_CHEST)
|| key.equals(LootTables.VILLAGE_SNOWY_HOUSE_CHEST)
|| key.equals(LootTables.VILLAGE_SAVANNA_HOUSE_CHEST))) {
table.pool(LootPool.builder().with(ItemEntry.builder(BARLEY_SEEDS))
.conditionally(RandomChanceLootCondition.builder(0.35f)));
table.pool(LootPool.builder().with(ItemEntry.builder(CABBAGE_SEEDS))
.conditionally(RandomChanceLootCondition.builder(0.35f)));
}
});
RegistryKey<ItemGroup> foodGroup = RegistryKey.of(RegistryKeys.ITEM_GROUP, Identifier.of("minecraft", "food_and_drinks"));
ItemGroupEvents.modifyEntriesEvent(foodGroup).register(entries -> {
entries.add(CABBAGE); entries.add(BARLEY_BREAD); entries.add(CABBAGE_STEW);
entries.add(ROASTED_CABBAGE); entries.add(MEAT_PIE); entries.add(BERRY_TART);
entries.add(APPLE_CIDER); entries.add(HONEY_TEA);
});
RegistryKey<ItemGroup> ingredients = RegistryKey.of(RegistryKeys.ITEM_GROUP, Identifier.of("minecraft", "ingredients"));
ItemGroupEvents.modifyEntriesEvent(ingredients).register(entries -> {
entries.add(BARLEY_SEEDS); entries.add(CABBAGE_SEEDS); entries.add(BARLEY); entries.add(BARLEY_FLOUR);
});
}
private static final class RusticCrop extends CropBlock {
private final boolean barley;
private RusticCrop(AbstractBlock.Settings settings, boolean barley) { super(settings); this.barley = barley; }
@Override protected ItemConvertible getSeedsItem() { return barley ? BARLEY_SEEDS : CABBAGE_SEEDS; }
}
private static final class WildBarley extends PlantBlock {
private static final MapCodec<WildBarley> CODEC = createCodec(WildBarley::new);
private WildBarley(AbstractBlock.Settings settings) { super(settings); }
@Override protected MapCodec<? extends PlantBlock> getCodec() { return CODEC; }
}
}