Skip to content
This repository has been archived by the owner on Apr 22, 2024. It is now read-only.

Commit

Permalink
Block entities
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRealEmissions committed Mar 27, 2024
1 parent 96f87ee commit c678655
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.dumbcode.projectnublar.core.data.DataGenerator;

Check warning on line 6 in src/main/java/net/dumbcode/projectnublar/ProjectNublar.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.dumbcode.projectnublar.core.data.DataGenerator;`
import net.dumbcode.projectnublar.core.registry.Registrar;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntity;

Check warning on line 9 in src/main/java/net/dumbcode/projectnublar/ProjectNublar.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.world.level.block.entity.BlockEntity;`
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModLoadingContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.dumbcode.projectnublar.core.blocks.elements.SoundBlock;
import net.dumbcode.projectnublar.core.blocks.elements.TestBlock;
import net.dumbcode.projectnublar.core.blocks.elements.TestOreBlock;
import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntities;
import net.dumbcode.projectnublar.core.data.ModRecipeProvider;
import net.dumbcode.projectnublar.core.data.loot.ModBlockLootTables;
import net.dumbcode.projectnublar.core.exceptions.UtilityClassException;
Expand All @@ -15,6 +16,7 @@
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;

Check warning on line 19 in src/main/java/net/dumbcode/projectnublar/core/blocks/DumbBlocks.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.world.level.block.entity.BlockEntity;`
import net.minecraft.world.level.storage.loot.LootTable;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -45,6 +47,7 @@ public enum Blocks {
DumbItems.Items.TEST_ITEM.getRegistry().item().get(),
MOD_ID
))
.associatedEntity(DumbBlockEntities.Entities.TEST_BLOCK)
),
TEST_ORE_BLOCK(
TestOreBlock::new,
Expand Down Expand Up @@ -100,11 +103,12 @@ public Supplier<IDumbBlock> getBlockConstructor() {
}
}

public record Metadata(Tags tags, Function<ModBlockLootTables.Builder, LootTable.@Nullable Builder> lootTableBuilder, List<UnaryOperator<ModRecipeProvider.Builder>> recipeBuilders) {
public record Metadata(Tags tags, Function<ModBlockLootTables.Builder, LootTable.@Nullable Builder> lootTableBuilder, List<UnaryOperator<ModRecipeProvider.Builder>> recipeBuilders, @Nullable DumbBlockEntities.Entities associatedEntity) {
public static class Builder {
private Tags tags = Tags.of();
private Function<ModBlockLootTables.Builder, LootTable.@Nullable Builder> lootTableBuilder;
private List<UnaryOperator<ModRecipeProvider.Builder>> recipeBuilders;
private @Nullable DumbBlockEntities.Entities associatedEntity;

Builder() {
}
Expand Down Expand Up @@ -136,11 +140,16 @@ public Builder recipe(UnaryOperator<ModRecipeProvider.Builder>... recipes) {
return this;
}

public Builder associatedEntity(DumbBlockEntities.Entities associatedEntity) {
this.associatedEntity = associatedEntity;
return this;
}

public Metadata build() {
if (lootTableBuilder == null) {
throw new IllegalStateException("Loot table builder is not set");
}
return new Metadata(tags, lootTableBuilder, recipeBuilders == null ? List.of() : recipeBuilders);
return new Metadata(tags, lootTableBuilder, recipeBuilders == null ? List.of() : recipeBuilders, associatedEntity);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
package net.dumbcode.projectnublar.core.blocks.elements;

import net.dumbcode.projectnublar.core.blocks.DumbBlock;

Check warning on line 3 in src/main/java/net/dumbcode/projectnublar/core/blocks/elements/TestBlock.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.dumbcode.projectnublar.core.blocks.DumbBlock;`
import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntities;

Check warning on line 4 in src/main/java/net/dumbcode/projectnublar/core/blocks/elements/TestBlock.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntities;`
import net.dumbcode.projectnublar.core.blocks.entity.DumbEntityBlock;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;

Check warning on line 8 in src/main/java/net/dumbcode/projectnublar/core/blocks/elements/TestBlock.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.world.level.block.RenderShape;`
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import software.bernie.geckolib.renderer.GeoBlockRenderer;

Check warning on line 13 in src/main/java/net/dumbcode/projectnublar/core/blocks/elements/TestBlock.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import software.bernie.geckolib.renderer.GeoBlockRenderer;`

public class TestBlock extends DumbBlock {
public class TestBlock extends DumbEntityBlock {

public TestBlock() {
super(Properties.ofFullCopy(Blocks.AMETHYST_BLOCK));
}

@Nullable
@Override
public BlockEntity newBlockEntity(@NotNull BlockPos pPos, @NotNull BlockState pState) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.dumbcode.projectnublar.core.blocks.entity;

import net.dumbcode.projectnublar.core.blocks.entity.elements.TestBlockEntity;
import net.dumbcode.projectnublar.core.registry.Registrar;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.entity.EntityRendererProvider;

Check warning on line 6 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbBlockEntities.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.client.renderer.entity.EntityRendererProvider;`
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import software.bernie.geckolib.model.DefaultedBlockGeoModel;
import software.bernie.geckolib.renderer.GeoBlockRenderer;

Check warning on line 13 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbBlockEntities.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import software.bernie.geckolib.renderer.GeoBlockRenderer;`

import java.util.function.Function;

Check warning on line 15 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbBlockEntities.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import java.util.function.Function;`

import static net.dumbcode.projectnublar.ProjectNublar.MOD_ID;

public class DumbBlockEntities {
public enum Entities {
TEST_BLOCK(
TestBlockEntity::new,
TestBlockEntity.Renderer::new
);

private final BlockEntityType.BlockEntitySupplier<DumbBlockEntity> blockEntityConstructor;
private final BlockEntityRendererProvider<DumbBlockEntity> renderer;
private final Registry registry = Registry.of(
RegistryObject.create(new ResourceLocation(MOD_ID, getRegisterName()), Registrar.BLOCK_ENTITY_TYPES.getRegistryKey(), MOD_ID)
);
private final DefaultedBlockGeoModel<DumbBlockEntity> model = new DefaultedBlockGeoModel<>(new ResourceLocation(MOD_ID, getRegisterName()));

Entities(BlockEntityType.BlockEntitySupplier<DumbBlockEntity> blockEntityConstructor, BlockEntityRendererProvider<DumbBlockEntity> renderer) {
this.blockEntityConstructor = blockEntityConstructor;
this.renderer = renderer;
}

public Registry getRegistry() {
return this.registry;
}

public DefaultedBlockGeoModel<DumbBlockEntity> getModel() {
return this.model;
}

public BlockEntityRendererProvider<DumbBlockEntity> getRenderer() {
return this.renderer;
}

public BlockEntityType.BlockEntitySupplier<DumbBlockEntity> getBlockEntityConstructor() {
return this.blockEntityConstructor;
}

public @NotNull String getRegisterName() {
return this.name().toLowerCase();
}
}

public record Registry(RegistryObject<BlockEntityType<DumbBlockEntity>> blockEntityType) {
@Contract("_ -> new")
public static @NotNull Registry of(RegistryObject<BlockEntityType<DumbBlockEntity>> blockEntityType) {
return new Registry(blockEntityType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package net.dumbcode.projectnublar.core.blocks.entity;

import com.google.common.base.Function;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;

Check warning on line 7 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbBlockEntity.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.world.level.block.entity.BlockEntityType;`
import net.minecraft.world.level.block.state.BlockState;
import software.bernie.geckolib.animatable.GeoBlockEntity;
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
import software.bernie.geckolib.renderer.GeoBlockRenderer;
import software.bernie.geckolib.util.GeckoLibUtil;

public abstract class DumbBlockEntity extends BlockEntity implements GeoBlockEntity {

private final DumbBlockEntities.Entities entity;

Check warning on line 16 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbBlockEntity.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Field can be local

Field can be converted to a local variable
protected final AnimatableInstanceCache instanceCache = GeckoLibUtil.createInstanceCache(this);

protected DumbBlockEntity(DumbBlockEntities.Entities entity, BlockPos pPos, BlockState pBlockState) {
super(entity.getRegistry().blockEntityType().get(), pPos, pBlockState);
this.entity = entity;
}

public Function<EntityRendererProvider.Context, ? extends GeoBlockRenderer<DumbBlockEntity>> getRenderer() {
return Renderer::new;
}

@Override
public AnimatableInstanceCache getAnimatableInstanceCache() {
return instanceCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.dumbcode.projectnublar.core.blocks.entity;

import net.dumbcode.projectnublar.core.blocks.DumbBlock;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.NotNull;

public abstract class DumbEntityBlock extends DumbBlock implements EntityBlock {
protected DumbEntityBlock(Properties properties) {
super(properties);
}

@Override
public @NotNull RenderShape getRenderShape(@NotNull BlockState pState) {

Check warning on line 15 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/DumbEntityBlock.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Deprecated API usage

Overrides deprecated method in 'net.minecraft.world.level.block.state.BlockBehaviour'
return RenderShape.ENTITYBLOCK_ANIMATED;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package net.dumbcode.projectnublar.core.blocks.entity.elements;

import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntities;
import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntity;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.entity.EntityRendererProvider;

Check warning on line 6 in src/main/java/net/dumbcode/projectnublar/core/blocks/entity/elements/TestBlockEntity.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import net.minecraft.client.renderer.entity.EntityRendererProvider;`
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.state.BlockState;
import software.bernie.geckolib.core.animation.AnimatableManager;
import software.bernie.geckolib.renderer.GeoBlockRenderer;

public class TestBlockEntity extends DumbBlockEntity {
private static final DumbBlockEntities.Entities ENTITY = DumbBlockEntities.Entities.TEST_BLOCK;

public static class Renderer extends GeoBlockRenderer<DumbBlockEntity> {
public Renderer(BlockEntityRendererProvider.Context context) {
super(ENTITY.getModel());
}
}

public TestBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(ENTITY, pPos, pBlockState);
}

@Override
public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package net.dumbcode.projectnublar.core.entities;
package net.dumbcode.projectnublar.core.mobs;

import net.dumbcode.projectnublar.core.entities.elements.DinosaurEntity;
import net.dumbcode.projectnublar.core.exceptions.UtilityClassException;
import net.dumbcode.projectnublar.core.mobs.elements.DinosaurMob;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobCategory;
import org.jetbrains.annotations.NotNull;

public final class DumbEntities {
public enum Entities {
DINOSAUR(EntityType.Builder.of(DinosaurEntity::new, MobCategory.CREATURE));
public final class DumbMobs {
public enum Mobs {
DINOSAUR(EntityType.Builder.of(DinosaurMob::new, MobCategory.CREATURE));

private final EntityType<? extends Entity> nativeType;
Entities(EntityType.@NotNull Builder<? extends Entity> of) {
Mobs(EntityType.@NotNull Builder<? extends Entity> of) {
this.nativeType = of.build(this.name().toLowerCase());
}

Expand All @@ -22,7 +22,7 @@ public <T extends Entity> EntityType<T> getNativeType() {
}
}

private DumbEntities() {
private DumbMobs() {
throw new UtilityClassException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import net.dumbcode.projectnublar.ProjectNublar;
import net.dumbcode.projectnublar.core.blocks.IDumbBlock;
import net.dumbcode.projectnublar.core.blocks.DumbBlocks;
import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntities;
import net.dumbcode.projectnublar.core.blocks.entity.DumbBlockEntity;
import net.dumbcode.projectnublar.core.creativetab.DumbCreativeTab;
import net.dumbcode.projectnublar.core.creativetab.DumbCreativeTabs;
import net.dumbcode.projectnublar.core.entities.DumbEntities;
import net.dumbcode.projectnublar.core.mobs.DumbMobs;
import net.dumbcode.projectnublar.core.exceptions.UtilityClassException;
import net.dumbcode.projectnublar.core.items.DumbItem;
import net.dumbcode.projectnublar.core.items.DumbItems;
Expand All @@ -15,7 +17,9 @@
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.material.Fluid;
import net.minecraftforge.client.event.EntityRenderersEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fluids.FluidType;
Expand All @@ -25,7 +29,8 @@
import net.minecraftforge.registries.RegisterEvent;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import static net.dumbcode.projectnublar.ProjectNublar.MOD_ID;
Expand All @@ -43,13 +48,15 @@ public class Registrar {
// viscosity, can fluid drown, can it be passed on a boat, extinguish fire etc.
public static final DeferredRegister<FluidType> FLUID_TYPES = DeferredRegister.create(ForgeRegistries.FLUID_TYPES, MOD_ID);
public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, MOD_ID);
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, MOD_ID);
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, MOD_ID);

public static void register(@NotNull IEventBus bus) {
BLOCKS.register(bus);
ITEMS.register(bus);
CREATIVE_MODE_TABS.register(bus);
ENTITY_TYPES.register(bus);
BLOCK_ENTITY_TYPES.register(bus);
FLUID_TYPES.register(bus);
FLUIDS.register(bus);
}
Expand Down Expand Up @@ -77,12 +84,24 @@ public static void register(@NotNull RegisterEvent event) {
}
});
event.register(Registrar.ENTITY_TYPES.getRegistryKey(), helper -> {
for(DumbEntities.Entities entity : DumbEntities.Entities.values()) {
for(DumbMobs.Mobs entity : DumbMobs.Mobs.values()) {
helper.register(ProjectNublar.resourceLocation(entity.name().toLowerCase()), entity.getNativeType());
}
});
event.register(
Registrar.CREATIVE_MODE_TABS.getRegistryKey(), helper -> {
event.register(Registrar.BLOCK_ENTITY_TYPES.getRegistryKey(), helper -> {
for (DumbBlockEntities.Entities entity : DumbBlockEntities.Entities.values()) {
List<Block> validBlocks = new ArrayList<>();
for (DumbBlocks.Blocks block : DumbBlocks.Blocks.values()) {
DumbBlockEntities.Entities associatedEntity = block.getMetadata().associatedEntity();
if (associatedEntity == null) continue;
if (!associatedEntity.equals(entity)) continue;
validBlocks.add(block.getRegistry().block().get());
}
BlockEntityType.Builder<DumbBlockEntity> builder = BlockEntityType.Builder.of(entity.getBlockEntityConstructor(), validBlocks.toArray(new Block[0]));
helper.register(ProjectNublar.resourceLocation(entity.getRegisterName()), builder.build(null));

Check warning on line 101 in src/main/java/net/dumbcode/projectnublar/core/registry/Registrar.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Nullability and data flow problems

Passing `null` argument to parameter annotated as @NotNull
}
});
event.register(Registrar.CREATIVE_MODE_TABS.getRegistryKey(), helper -> {
for (DumbCreativeTabs.CreativeTabs creativeTab : DumbCreativeTabs.CreativeTabs.values()) {
Supplier<DumbCreativeTab> creativeTabConstructor = creativeTab.getCreativeTabConstructor();
DumbCreativeTab instance = creativeTabConstructor.get();
Expand All @@ -92,6 +111,13 @@ public static void register(@NotNull RegisterEvent event) {
});
}

@SubscribeEvent
public static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) {
for (DumbBlockEntities.Entities blockEntity : DumbBlockEntities.Entities.values()) {
event.registerBlockEntityRenderer(blockEntity.getRegistry().blockEntityType().get(), blockEntity.getRenderer());
}
}

private Registrar() {
throw new UtilityClassException();
}
Expand Down

0 comments on commit c678655

Please sign in to comment.