-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Registration groups + decorative blocks
Registration groups are a mini version of the registration class, designed to let you group similar blocks/items into smaller groups to keep code cleaner Decorative blocks started, with a system to pass in a model with "texture sets" - a key/value pair to match textures to the definition in the model file without child mods depending on the datagen system to find them. Rotation dec stuff isn't finished yet!
- Loading branch information
Showing
7 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/com/fureniku/metropolis/RegistrationGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.fureniku.metropolis; | ||
|
||
import com.fureniku.metropolis.utils.CreativeTabSet; | ||
import com.fureniku.metropolis.utils.Debug; | ||
import net.minecraft.core.registries.Registries; | ||
import net.minecraft.world.item.BlockItem; | ||
import net.minecraft.world.item.CreativeModeTab; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.level.block.Block; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.neoforge.event.BuildCreativeModeTabContentsEvent; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
import net.neoforged.neoforge.registries.ForgeRegistries; | ||
import net.neoforged.neoforge.registries.RegistryObject; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Class to keep RegistrationBase derivitive classes a bit cleaner. Group your blocks into these classes, if you want to. | ||
*/ | ||
public abstract class RegistrationGroup { | ||
|
||
protected final RegistrationBase registration; | ||
|
||
/** | ||
* Constructor. | ||
* @param registrationBase the core registration class of your mod | ||
*/ | ||
public RegistrationGroup(RegistrationBase registrationBase) { | ||
registration = registrationBase; | ||
} | ||
|
||
/** | ||
* Initialize the registration, and register all your stuff. Cannot be called from the constructor! | ||
* Handle the creation of blocks in here - either creating blocksets, or call registerBlockSet(name, BlockClass::new) | ||
* Handle the creation of items | ||
* Handle the creation of creative tabs (new creativetabset, pass creativeTabs as first arg) | ||
* Do anything else you want at this stage. | ||
* @param modEventBus | ||
*/ | ||
public abstract void init(IEventBus modEventBus); | ||
|
||
/** | ||
* Add all your blocks to the created creative tabs in here! | ||
* <p>It may be neater to create a function for each tab, and call those functions from here.</p> | ||
* <p>Tab registry is done using <code>yourTab.addItem(ItemStack)</code></p> | ||
* <p>A good example of achieving that here could be <code>yourTab.addItem(getItem("item_name").get().getDefaultInstance())</code></p> | ||
* <p>If you're using <code>BlockSet</code>s, you can instead call <code>blockSet.registerToCreativeTab(yourTab)</code> to add all blocks in the set to the tab.</p> | ||
*/ | ||
public abstract void generateCreativeTabs(); | ||
|
||
/** | ||
* Create an ArrayList of all your creative tabs here. | ||
* @return an <code>ArrayList{@literal <CreativeTabSet>}</code> with all your creative tabs in | ||
*/ | ||
public abstract ArrayList<CreativeTabSet> getCreativeTabs(); | ||
|
||
/** | ||
* Passthrough to register a blockset | ||
* @param name blockset name | ||
* @param blockClass block's class | ||
*/ | ||
protected void registerBlockSet(String name, Supplier<Block> blockClass) { | ||
registration.registerBlockSet(name, blockClass); | ||
} | ||
|
||
/** | ||
* Passthrough to get a block | ||
* @param key block name | ||
* @return registry object of the block | ||
*/ | ||
protected RegistryObject<Block> getBlock(String key) { | ||
return registration.getBlock(key); | ||
} | ||
|
||
/** | ||
* Passthrough to get a item | ||
* @param key item name | ||
* @return registry object of the item | ||
*/ | ||
protected RegistryObject<Item> getItem(String key) { | ||
return registration.getItem(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/fureniku/metropolis/blocks/MetroBlockDecorative.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.fureniku.metropolis.blocks; | ||
|
||
import com.fureniku.metropolis.datagen.MetroBlockStateProvider; | ||
import com.fureniku.metropolis.datagen.TextureSet; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder; | ||
import net.neoforged.neoforge.registries.RegistryObject; | ||
|
||
public class MetroBlockDecorative extends MetroBlockBase { | ||
|
||
private final VoxelShape BLOCK_SHAPE; | ||
private TextureSet[] _resources; | ||
private String _modelName; | ||
|
||
public MetroBlockDecorative(Properties props, String modelName, ResourceLocation resource) { | ||
this(props, 16, 16, modelName, new TextureSet("texture", resource)); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, float height, String modelName, ResourceLocation resource) { | ||
this(props, height, 16, modelName, new TextureSet("texture", resource)); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, float height, float width, String modelName, ResourceLocation resource) { | ||
this(props, height, width, modelName, new TextureSet("texture", resource)); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, VoxelShape shape, String modelName, ResourceLocation resource) { | ||
this(props, shape, modelName, new TextureSet("texture", resource)); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, String modelName, TextureSet... textures) { | ||
this(props, 16, 16, modelName, textures); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, float height, String modelName, TextureSet... textures) { | ||
this(props, height, 16, modelName, textures); | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, float height, float width, String modelName, TextureSet... textures) { | ||
super(props); | ||
float inset = (16-width)/2; | ||
BLOCK_SHAPE = Block.box(inset, 0, inset, 16-inset, height, 16-inset); | ||
_resources = textures; | ||
_modelName = modelName; | ||
} | ||
|
||
public MetroBlockDecorative(Properties props, VoxelShape shape, String modelName, TextureSet... textures) { | ||
super(props); | ||
BLOCK_SHAPE = shape; | ||
_resources = textures; | ||
_modelName = modelName; | ||
} | ||
|
||
@Override | ||
protected VoxelShape getShapeFromBlockState(BlockState pState) { | ||
return BLOCK_SHAPE; | ||
} | ||
|
||
@Override | ||
public void generateBlockState(RegistryObject<Block> blockRegistryObject, MetroBlockStateProvider blockStateProvider) { | ||
Block block = blockRegistryObject.get(); | ||
BlockModelBuilder bmb = blockStateProvider.getModelFilesWithTexture(block, "", "blocks/decorative/" + _modelName, _resources[0].getTexture()); | ||
if (_resources.length > 1) { | ||
for (int i = 1; i < _resources.length; i++) { | ||
bmb = bmb.texture(_resources[i].getKey(), _resources[i].getTexture()); | ||
} | ||
} | ||
|
||
blockStateProvider.simpleBlockWithItem(block, bmb); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/fureniku/metropolis/blocks/MetroBlockDecorativeRotatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.fureniku.metropolis.blocks; | ||
|
||
import com.fureniku.metropolis.datagen.MetroBlockStateProvider; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.item.context.BlockPlaceContext; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.block.Blocks; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.level.block.state.StateDefinition; | ||
import net.minecraft.world.level.block.state.properties.BooleanProperty; | ||
import net.minecraft.world.level.block.state.properties.DirectionProperty; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import net.neoforged.neoforge.registries.RegistryObject; | ||
|
||
public class MetroBlockDecorativeRotatable extends MetroBlockBase { | ||
|
||
protected static final DirectionProperty DIRECTION = DirectionProperty.create("rotation", Direction.Plane.HORIZONTAL); | ||
private final VoxelShape BLOCK_SHAPE; | ||
|
||
public MetroBlockDecorativeRotatable(Properties props) { | ||
this(props, 16, 16); | ||
} | ||
|
||
public MetroBlockDecorativeRotatable(Properties props, float height) { | ||
this(props, height, 16); | ||
} | ||
|
||
public MetroBlockDecorativeRotatable(Properties props, float height, float width) { | ||
this(props, width, height, width); | ||
} | ||
|
||
public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape) { | ||
super(props); | ||
BLOCK_SHAPE = shape; | ||
this.registerDefaultState(this.defaultBlockState().setValue(DIRECTION, Direction.NORTH)); | ||
} | ||
|
||
public MetroBlockDecorativeRotatable(Properties props, float sizeX, float sizeY, float sizeZ) { | ||
super(props); | ||
float insetX = (16-sizeX)/2; | ||
float insetZ = (16-sizeZ)/2; | ||
BLOCK_SHAPE = Block.box(insetX, 0, insetZ, 16-insetX, 16-sizeY, 16-insetZ); | ||
this.registerDefaultState(this.defaultBlockState().setValue(DIRECTION, Direction.NORTH)); | ||
} | ||
|
||
@Override | ||
public void generateBlockState(RegistryObject<Block> blockRegistryObject, MetroBlockStateProvider blockStateProvider) { | ||
blockStateProvider.simpleBlockWithItem(blockRegistryObject.get()); | ||
} | ||
|
||
@Override | ||
protected BlockState getPlacementState(BlockPlaceContext context) { | ||
BlockState blockstate = this.defaultBlockState().setValue(DIRECTION, context.getHorizontalDirection()); | ||
return blockstate; | ||
} | ||
|
||
@Override | ||
protected void createBlockState(StateDefinition.Builder builder) { | ||
builder.add(DIRECTION); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/com/fureniku/metropolis/datagen/TextureSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.fureniku.metropolis.datagen; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public class TextureSet { | ||
|
||
private String _name; | ||
private ResourceLocation _location; | ||
|
||
public TextureSet(String key, ResourceLocation value) { | ||
_name = key; | ||
_location = value; | ||
} | ||
|
||
public String getKey() { | ||
return _name; | ||
} | ||
|
||
public ResourceLocation getTexture() { | ||
return _location; | ||
} | ||
} |