Skip to content

Commit

Permalink
partly works
Browse files Browse the repository at this point in the history
  • Loading branch information
ferriarnus committed Dec 27, 2023
1 parent 5ccfd9a commit 121e8b2
Show file tree
Hide file tree
Showing 37 changed files with 1,419 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// 1.20.2 2023-12-27T14:38:20.1780825 Ender IO Data (examplemod com.example.examplemod.registry.EnderBlockRegistry@430b5c0e)
c1179f903b7e75f188bc87b191d646264a7c1c10 assets/examplemod/blockstates/example_block.json
d32c5ad4076b1e22be3420304265a566242e4fb7 assets/examplemod/lang/en_us.json
7c88b8231b94199736a42deb3c7c4afaf43ce8c6 assets/examplemod/models/block/example_block.json
dea7c460ab71faf20207d3584b00732bdd44ae43 data/examplemod/loot_tables/blocks/example_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.2 2023-12-27T14:38:20.1780825 Ender IO Data (examplemod com.example.examplemod.registry.EnderItemRegistry@155f437a)
608e98bf5926abfd9d441b916410bb804f31f633 assets/examplemod/models/item/example_block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// 1.20.2 2023-12-27T14:38:20.1660732 Ender IO Data (examplemod com.example.examplemod.registry.EnderItemRegistry@7b3ebc30)
af0d8df01e38c368aa227d28d9f4050fedcf032a assets/examplemod/models/item/example_item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "examplemod:block/example_block"
}
}
}
3 changes: 3 additions & 0 deletions src/generated/resources/assets/examplemod/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"block.examplemod.example_block": "Example block"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:block/cube_all",
"textures": {
"all": "examplemod:block/example_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "examplemod:item/example_block"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "minecraft:item/generated",
"textures": {
"layer0": "examplemod:item/example_item"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "minecraft:block",
"pools": [
{
"bonus_rolls": 0.0,
"conditions": [
{
"condition": "minecraft:survives_explosion"
}
],
"entries": [
{
"type": "minecraft:item",
"name": "examplemod:example_block"
}
],
"rolls": 1.0
}
],
"random_sequence": "examplemod:blocks/example_block"
}
29 changes: 22 additions & 7 deletions src/main/java/com/example/examplemod/ExampleMod.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.example.examplemod;

import com.example.examplemod.data.EnderBlockLootProvider;
import com.example.examplemod.data.EnderItemModelProvider;
import com.example.examplemod.registry.EnderBlockRegistry;
import com.example.examplemod.registry.EnderDeferredBlock;
import com.example.examplemod.registry.EnderItemRegistry;
import com.mojang.logging.LogUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
Expand All @@ -21,6 +28,7 @@
import net.neoforged.fml.config.ModConfig;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
Expand All @@ -39,16 +47,23 @@ public class ExampleMod
// Directly reference a slf4j logger
private static final Logger LOGGER = LogUtils.getLogger();
// Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace
public static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(MODID);
public static final EnderBlockRegistry BLOCKS = EnderBlockRegistry.createRegistry(MODID);
// Create a Deferred Register to hold Items which will all be registered under the "examplemod" namespace
public static final DeferredRegister.Items ITEMS = DeferredRegister.createItems(MODID);
public static final EnderItemRegistry ITEMS = EnderItemRegistry.createRegistry(MODID);
// Create a Deferred Register to hold CreativeModeTabs which will all be registered under the "examplemod" namespace
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MODID);

// Creates a new Block with the id "examplemod:example_block", combining the namespace and path
public static final DeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS.registerSimpleBlock("example_block", BlockBehaviour.Properties.of().mapColor(MapColor.STONE));
// Creates a new BlockItem with the id "examplemod:example_block", combining the namespace and path
public static final DeferredItem<BlockItem> EXAMPLE_BLOCK_ITEM = ITEMS.registerSimpleBlockItem("example_block", EXAMPLE_BLOCK);
public static final EnderDeferredBlock<Block> EXAMPLE_BLOCK = BLOCKS
.registerBlock("example_block", BlockBehaviour.Properties.of().mapColor(MapColor.STONE))
.addBlockTags(BlockTags.MUSHROOM_GROW_BLOCK)
.setBlockStateProvider(BlockStateProvider::simpleBlock)
.setLootTable(EnderBlockLootProvider::dropSelf)
.createBlockItem()
.addBlockItemTags(ItemTags.PLANKS)
.setModelProvider(EnderItemModelProvider::basicItem)
.setTab(CreativeModeTabs.BUILDING_BLOCKS)
.finishBlockItem();

// Creates a new food item with the id "examplemod:example_id", nutrition 1 and saturation 2
public static final DeferredItem<Item> EXAMPLE_ITEM = ITEMS.registerSimpleItem("example_item", new Item.Properties().food(new FoodProperties.Builder()
Expand Down Expand Up @@ -102,8 +117,8 @@ private void commonSetup(final FMLCommonSetupEvent event)
// Add the example block item to the building blocks tab
private void addCreative(BuildCreativeModeTabContentsEvent event)
{
if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS)
event.accept(EXAMPLE_BLOCK_ITEM);
//if (event.getTabKey() == CreativeModeTabs.BUILDING_BLOCKS)
//event.accept(EXAMPLE_BLOCK_ITEM);
}

// You can use SubscribeEvent and let the Event Bus discover methods to call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.example.examplemod.data;

import com.example.examplemod.registry.EnderBlockRegistry;
import com.example.examplemod.registry.EnderDeferredBlock;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.loot.BlockLootSubProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.flag.FeatureFlags;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootTable;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.Collections;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.function.BiConsumer;

public class EnderBlockLootProvider extends BlockLootSubProvider {

private final EnderBlockRegistry registry;

public EnderBlockLootProvider(Set<Item> explosionResistant, EnderBlockRegistry registry) {
super(explosionResistant, FeatureFlags.REGISTRY.allFlags());
this.registry = registry;
}

@Override
protected void generate() {
for (DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
BiConsumer<EnderBlockLootProvider, Block> lootTable = ((EnderDeferredBlock<Block>) block).getLootTable();
if (lootTable != null) {
lootTable.accept(this, block.get());
}
}
}

//TODO why these 2 methods, can we join them?
@Override
public void generate(BiConsumer<ResourceLocation, LootTable.Builder> p_249322_) {
this.generate();
Set<ResourceLocation> set = new HashSet<>();

for(DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
if (block.get().isEnabled(this.enabledFeatures)) {
ResourceLocation resourcelocation = block.get().getLootTable();
if (resourcelocation != BuiltInLootTables.EMPTY && set.add(resourcelocation)) {
LootTable.Builder loottable$builder = this.map.remove(resourcelocation);
if (loottable$builder == null) {
throw new IllegalStateException(
String.format(Locale.ROOT, "Missing loottable '%s' for '%s'", resourcelocation, BuiltInRegistries.BLOCK.getKey(block.get()))
);
}

p_249322_.accept(resourcelocation, loottable$builder);
}
}
}

if (!this.map.isEmpty()) {
throw new IllegalStateException("Created block loot tables for non-blocks: " + this.map.keySet());
}
}

@Override
public void dropSelf(Block block) {
super.dropSelf(block);
}

public void createDoor(Block block) {
this.add(block, super::createDoorTable);
}

@Override
public void add(Block p_250610_, LootTable.Builder p_249817_) {
super.add(p_250610_, p_249817_);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.example.examplemod.data;

import com.example.examplemod.registry.EnderBlockRegistry;
import com.example.examplemod.registry.EnderDeferredBlock;
import net.minecraft.data.PackOutput;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.client.model.generators.BlockStateProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.function.BiConsumer;

public class EnderBlockStateProvider extends BlockStateProvider {
private final EnderBlockRegistry registry;

public EnderBlockStateProvider(PackOutput output, String modid, ExistingFileHelper exFileHelper, EnderBlockRegistry registry) {
super(output, modid, exFileHelper);
this.registry = registry;
}

@Override
protected void registerStatesAndModels() {
for (DeferredHolder<Block, ? extends Block> block : registry.getEntries()) {
BiConsumer<BlockStateProvider, Block> blockstate = ((EnderDeferredBlock<Block>) block).getBlockStateProvider();
if (blockstate != null) {
blockstate.accept(this, block.get());
}
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/example/examplemod/data/EnderDataProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.example.examplemod.data;

import com.example.examplemod.ExampleMod;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

public class EnderDataProvider implements DataProvider {
private final String modid;
private final List<DataProvider> subProviders = new ArrayList<>();
private final static EnderDataProvider INSTANCE = new EnderDataProvider(ExampleMod.MODID);

public EnderDataProvider(String modid) {
this.modid = modid;
}

public static EnderDataProvider getInstance() {
return INSTANCE;
}

public void addSubProvider(boolean include, DataProvider provider) {
if (include) {
subProviders.add(provider);
}
}

@Override
public CompletableFuture<?> run(CachedOutput pOutput) {
List<CompletableFuture<?>> list = new ArrayList<>();
for (DataProvider provider : subProviders) {
list.add(provider.run(pOutput));
}
return CompletableFuture.allOf(list.toArray(CompletableFuture[]::new));
}

@Override
public String getName() {
return "Ender IO Data (" + modid + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.example.examplemod.data;

import com.example.examplemod.registry.EnderDeferredItem;
import com.example.examplemod.registry.EnderItemRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ItemLike;
import net.neoforged.neoforge.client.model.generators.ItemModelBuilder;
import net.neoforged.neoforge.client.model.generators.ItemModelProvider;
import net.neoforged.neoforge.client.model.generators.ModelFile;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import net.neoforged.neoforge.registries.DeferredHolder;

import java.util.Objects;
import java.util.function.BiConsumer;

public class EnderItemModelProvider extends ItemModelProvider {
private final EnderItemRegistry registry;

public EnderItemModelProvider(PackOutput output, String modid, ExistingFileHelper existingFileHelper, EnderItemRegistry itemRegistry) {
super(output, modid, existingFileHelper);
this.registry = itemRegistry;
}

@Override
protected void registerModels() {
for (DeferredHolder<Item, ? extends Item> item : registry.getEntries()) {
BiConsumer<EnderItemModelProvider, Item> modelProvider = ((EnderDeferredItem<? extends Item>) item).getModelProvider();
if (modelProvider != null) {
modelProvider.accept(this, item.get());
}
}
}

public ItemModelBuilder basicBlock(Item item) {
return basicBlock(Objects.requireNonNull(BuiltInRegistries.ITEM.getKey(item)));
}

public ItemModelBuilder basicBlock(ResourceLocation item) {
return getBuilder(item.toString())
.parent(new ModelFile.UncheckedModelFile("item/generated"))
.texture("layer0", new ResourceLocation(item.getNamespace(), "block/" + item.getPath()));
}

public ItemModelBuilder basicItem(Item item, ResourceLocation texture) {
return getBuilder(BuiltInRegistries.ITEM.getKey(item).toString())
.parent(new ModelFile.UncheckedModelFile("item/generated"))
.texture("layer0", new ResourceLocation(texture.getNamespace(), "item/" + texture.getPath()));
}

public ResourceLocation itemTexture(ItemLike item) {
return Objects.requireNonNull(BuiltInRegistries.ITEM.getKey(item.asItem()));
}


}

44 changes: 44 additions & 0 deletions src/main/java/com/example/examplemod/data/EnderLangProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.examplemod.data;

import com.example.examplemod.registry.EnderBlockRegistry;
import com.example.examplemod.registry.EnderDeferredBlock;
import com.example.examplemod.registry.EnderDeferredItem;
import com.example.examplemod.registry.EnderItemRegistry;
import net.minecraft.data.PackOutput;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.common.data.LanguageProvider;
import net.neoforged.neoforge.registries.DeferredHolder;

import javax.annotation.Nullable;

public class EnderLangProvider extends LanguageProvider {
@Nullable
private EnderBlockRegistry blocks;
@Nullable
private EnderItemRegistry items;

public EnderLangProvider(PackOutput output, String modid, String locale, EnderBlockRegistry registry) {
super(output, modid, locale);
this.blocks = registry;
}

public EnderLangProvider(PackOutput output, String modid, String locale, EnderItemRegistry registry) {
super(output, modid, locale);
this.items = registry;
}

@Override
protected void addTranslations() {
if (blocks != null) {
for (DeferredHolder<Block, ? extends Block> block : blocks.getEntries()) {
this.add(block.get(), ((EnderDeferredBlock<Block>) block).getTranslation());
}
}
if (items != null) {
for (DeferredHolder<Item, ? extends Item> item : items.getEntries()) {
this.add(item.get(), ((EnderDeferredItem<Item>) item).getTranslation());
}
}
}
}
Loading

0 comments on commit 121e8b2

Please sign in to comment.