Skip to content

Commit

Permalink
Merge branch 'remote-work'
Browse files Browse the repository at this point in the history
  • Loading branch information
Fureniku committed Dec 18, 2023
2 parents 91f412d + e49d6a3 commit 56f18d5
Show file tree
Hide file tree
Showing 20 changed files with 702 additions and 84 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/fureniku/metropolis/RegistrationBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public abstract class RegistrationBase {
protected final DeferredRegister<Item> itemRegistry;
protected final DeferredRegister<CreativeModeTab> creativeTabs;

/**
* Used by registration groups
*/
public final String modid;

private HashMap<String, RegistryObject<Block>> block_map = new HashMap<>();
private HashMap<String, RegistryObject<Item>> item_map = new HashMap<>();

Expand All @@ -44,6 +49,7 @@ public abstract class RegistrationBase {
* @param modEventBus the event bus
*/
public RegistrationBase(String modid, IEventBus modEventBus) {
this.modid = modid;
blockRegistry = DeferredRegister.create(ForgeRegistries.BLOCKS, modid);
itemRegistry = DeferredRegister.create(ForgeRegistries.ITEMS, modid);
creativeTabs = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, modid);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/fureniku/metropolis/RegistrationGroup.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.fureniku.metropolis;

import com.fureniku.metropolis.datagen.TextureSet;
import com.fureniku.metropolis.utils.CreativeTabSet;
import com.fureniku.metropolis.utils.Debug;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -87,4 +89,44 @@ protected RegistryObject<Block> getBlock(String key) {
protected RegistryObject<Item> getItem(String key) {
return registration.getItem(key);
}

/**
* Get a resource location for the named resource. Override to provide subfolders, else it defaults to getLocFull.
* @param name the name of the resource you're getting in the location.
* @return resource location
*/
protected ResourceLocation getLoc(String name) {
return getLocFull(name);
}

/**
* Get a resource location for the named resource. Always returns the contextual root (blocks/, models/ etc).
* Override getLoc if you consistently want a subfolder.
* @param name the name of the resource you're getting in the location.
* @return resource location
*/
protected final ResourceLocation getLocFull(String name) {
return new ResourceLocation(registration.modid, name);
}

/**
* Get a TextureSet for the texture name at the named resource locaation.
* Calls getLoc(), so will use defined subfolders.
* @param name The name of the texture for a face, passed to the model json
* @param loc The name of the texture file (without png)
* @return TextureSet
*/
protected TextureSet texture(String name, String loc) {
return texture(name, getLoc(loc));
}

/**
* Get a TextureSet for the texture name at the named resource locaation.
* @param name The name of the texture for a face, passed to the model json
* @param loc ResourceLocation for the texture file
* @return TextureSet
*/
protected TextureSet texture(String name, ResourceLocation loc) {
return new TextureSet(name, loc);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/fureniku/metropolis/blocks/IToggleable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.fureniku.metropolis.blocks;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;

public interface IToggleable {

void toggleBlock(Level level, BlockPos pos, BlockState state);

void setToggledState(Level level, BlockPos pos, BlockState state, boolean toggled);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.fureniku.metropolis.blocks.decorative;

import com.fureniku.metropolis.blocks.decorative.builders.MetroBlockDecorativeConnectingBuilder;
import com.fureniku.metropolis.blocks.decorative.helpers.HelperBase;
import com.fureniku.metropolis.datagen.MetroBlockStateProvider;
import com.fureniku.metropolis.datagen.TextureSet;
import com.fureniku.metropolis.enums.BlockConnectionType;
Expand All @@ -20,6 +21,8 @@
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
import net.neoforged.neoforge.registries.RegistryObject;

import java.util.ArrayList;

public class MetroBlockConnectingHorizontal extends MetroBlockDecorative {

protected static final BooleanProperty NORTH = BlockStateProperties.NORTH;
Expand All @@ -45,6 +48,8 @@ public MetroBlockConnectingHorizontal(Properties props, String modelName, String
_connectedModelName = connectedModelName;
_itemModelName = itemModelName;

public MetroBlockConnectingHorizontal(Properties props, VoxelShape shape, String modelDir, String modelName, BlockOffsetDirection offsetDirection, boolean checkUp, boolean checkDown, TextureSet... textures) {
super(props, shape, modelDir, modelName, new ArrayList<HelperBase>(), textures);
_checkUp = checkUp;
_checkDown = checkDown;
_centerFourSided = centerFourSided;
Expand All @@ -57,7 +62,7 @@ public MetroBlockConnectingHorizontal(Properties props, String modelName, String
}

public MetroBlockConnectingHorizontal(MetroBlockDecorativeConnectingBuilder builder) {
this(builder.getProps(), builder.getModelName(), builder.getConnectedModelName(), builder.getItemModelName(), builder.getOffsetDirection(), builder.getCheckUp(), builder.getCheckDown(), builder.getCenterFourSided(), builder.getIndependentModelsPerSide(), builder.getShapes(), builder.getTag(), builder.getConnectionType(), builder.getTextures());
this(builder.getProps(), builder.getShape(), builder.getModelDirectory(), builder.getModelName(), builder.getOffsetDirection(), builder.getCheckUp(), builder.getCheckDown(), builder.getTextures());
}

public String getTag() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
package com.fureniku.metropolis.blocks.decorative;

import com.fureniku.metropolis.blocks.IToggleable;
import com.fureniku.metropolis.blocks.MetroBlockBase;
import com.fureniku.metropolis.blocks.decorative.builders.MetroBlockDecorativeBuilder;
import com.fureniku.metropolis.blocks.decorative.helpers.*;
import com.fureniku.metropolis.datagen.MetroBlockStateProvider;
import com.fureniku.metropolis.datagen.TextureSet;
import com.fureniku.metropolis.enums.BlockOffsetDirection;
import com.fureniku.metropolis.enums.ToggleType;
import com.fureniku.metropolis.utils.SimpleUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
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.StateHolder;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.neoforged.neoforge.client.model.generators.BlockModelBuilder;
import net.neoforged.neoforge.registries.RegistryObject;

import java.util.ArrayList;
import java.util.Optional;

public class MetroBlockDecorative extends MetroBlockBase {
public class MetroBlockDecorative extends MetroBlockBase implements IToggleable {

private final VoxelShape BLOCK_SHAPE;
protected TextureSet[] _resources;
protected String _modelName;
private BlockOffsetDirection _offsetDirection = BlockOffsetDirection.NONE;
protected String _modelDir;

public MetroBlockDecorative(Properties props, VoxelShape shape, String modelName, BlockOffsetDirection offsetDirection, TextureSet... textures) {
super(offsetDirection == BlockOffsetDirection.NONE ? props : props.dynamicShape());
private final ArrayList<HelperBase> _helpers;

private RotationHelper _rotationHelper;
private ToggleHelper _toggleHelper;
private OffsetHelper _offsetHelper;

public MetroBlockDecorative(Properties props, VoxelShape shape, String modelDir, String modelName, ArrayList<HelperBase> helpers, TextureSet... textures) {
super(SimpleUtils.containsType(helpers, OffsetHelper.class) ? props.dynamicShape() : props);
BLOCK_SHAPE = shape;
_resources = textures;
_modelDir = modelDir;
_modelName = modelName;
if (offsetDirection != BlockOffsetDirection.NONE) {
_offsetDirection = offsetDirection;
_helpers = helpers;
BlockState stateHolder = this.getStateDefinition().any();

for (int i = 0; i < _helpers.size(); i++) {
stateHolder = _helpers.get(i).setDefaultState(stateHolder);
assignHelper(helpers.get(i));
}
}

public MetroBlockDecorative(MetroBlockDecorativeBuilder builder) {
this(builder.getProps(), builder.getShape(), builder.getModelName(), builder.getOffsetDirection(), builder.getTextures());
this.registerDefaultState(stateHolder);
}

@Override
protected Vec3 getOffset(BlockState blockState, BlockGetter level, BlockPos pos) {
double x = 0;
double y = 0;
double z = 0;
switch (_offsetDirection) {
case NONE -> {
return Vec3.ZERO;
}
case DOWN -> {
y = getOffsetBlockPosValue(level, pos.below(), Direction.Axis.Y, true);
}
case BACK -> {
}
case LEFT -> {
}
case FORWARD -> {
}
case RIGHT -> {
}
case UP -> {
y = getOffsetBlockPosValue(level, pos.above(), Direction.Axis.Y, false);
}
private void assignHelper(HelperBase helper) {
switch (helper.getType()) {
case OFFSET -> _offsetHelper = (OffsetHelper) helper;
case ROTATION -> _rotationHelper = (RotationHelper) helper;
case TOGGLE -> _toggleHelper = (ToggleHelper) helper;
}
double finalX = x;
double finalY = y;
double finalZ = z;
blockState.offsetFunction = Optional.of((state, lvl, blockPos) -> new Vec3(finalX, finalY, finalZ));
return new Vec3(x, y, z);
}

private double getOffsetBlockPosValue(BlockGetter level, BlockPos pos, Direction.Axis axis, boolean positive) {
if (level.getBlockState(pos).getBlock() == Blocks.AIR) {
return 0;
}
VoxelShape shape = level.getBlockState(pos).getShape(level, pos);
return positive ? (1 - shape.max(axis)) * -1 : shape.min(axis);
@Override
protected void createBlockState(StateDefinition.Builder<Block, BlockState> builder) {
if (_toggleHelper != null) builder.add(_toggleHelper.TOGGLED);
if (_rotationHelper != null) builder.add(_rotationHelper.DIRECTION);
}

@Override
protected VoxelShape getShapeFromBlockState(BlockState pState) {
//TODO how do we handle this now
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());
BlockModelBuilder bmb = prepareModels(block, blockStateProvider);
boolean generatedBlock = false;
for (int i = 0; i < _helpers.size(); i++) {
if (_helpers.get(i) instanceof HelperBlockstate) {
HelperBlockstate helper = (HelperBlockstate) _helpers.get(i);
//helper.generateBlockstate();
generatedBlock = true;
}
}

blockStateProvider.simpleBlockWithItem(block, applyTexturesToModels(bmb)[0]);
}

//TODO resolve
protected BlockModelBuilder applyTexturesToModel(BlockModelBuilder bmb) {
if (_resources.length > 1) {
for (int i = 1; i < _resources.length; i++) {
Expand All @@ -105,6 +105,7 @@ protected BlockModelBuilder applyTexturesToModel(BlockModelBuilder bmb) {
return bmb;
}

//TODO resolve
protected BlockModelBuilder[] applyTexturesToModels(BlockModelBuilder... bmb) {
if (_resources.length > 1) {
for (int i = 0; i < bmb.length; i++) {
Expand All @@ -114,5 +115,67 @@ protected BlockModelBuilder[] applyTexturesToModels(BlockModelBuilder... bmb) {
}
}
return bmb;
if (!generatedBlock) {
blockStateProvider.simpleBlock(block, bmb);
}
blockStateProvider.simpleBlockItem(block, bmb);
}

//TODO resolve
public BlockModelBuilder prepareModels(Block block, MetroBlockStateProvider blockStateProvider) {
BlockModelBuilder bmb;
if (_modelName == null || _resources == null) {
bmb = blockStateProvider.getModelFilesWithTexture(block, "", _modelDir + block.getName(), blockStateProvider.modLoc("blocks/decorative/" + block.getName()));
} else {
bmb = blockStateProvider.getModelFilesWithTexture(block, "", _modelDir + _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());
}
}
}
return bmb;
}

//region Offset
@Override
protected Vec3 getOffset(BlockState blockState, BlockGetter level, BlockPos pos) {
if (_offsetHelper != null) {
Vec3 offset = _offsetHelper.getOffset(level, pos);
blockState.offsetFunction = Optional.of((state, lvl, blockPos) -> offset);
return offset;
}
return Vec3.ZERO;
}
//endregion

//region Togglable
@Override
public void onNeighbourChanged(BlockState state, Level level, BlockPos pos, Block block, BlockPos neighborPos) {
if (_toggleHelper != null) {
_toggleHelper.neighbourChanged(state, level, pos, this);
}
}

@Override
protected void onRightClickRemote(BlockState state, BlockPos pos, Player player) {
if (_toggleHelper != null) {
_toggleHelper.rightClick(state, pos, player, this);
}
}

@Override
public void toggleBlock(Level level, BlockPos pos, BlockState state) {
if (_toggleHelper != null) {
_toggleHelper.toggleBlock(state);
}
}

@Override
public void setToggledState(Level level, BlockPos pos, BlockState state, boolean toggled) {
if (_toggleHelper != null) {
setBlock(level, pos, _toggleHelper.setToggledState(state, toggled));
}
}
//endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public class MetroBlockDecorativeRotatable extends MetroBlockDecorative {
* @param props
* @param shape
*/
public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape, String modelName, BlockOffsetDirection offsetDirection, TextureSet... textures) {
super(props, shape, modelName, offsetDirection, textures);
public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape, String modelDir, String modelName, BlockOffsetDirection offsetDirection, TextureSet... textures) {
super(props, shape, modelDir, modelName, offsetDirection, textures);
BLOCK_SHAPE_NORTH = shape;
BLOCK_SHAPE_EAST = ShapeUtils.rotateVoxelShape(shape, Direction.EAST);
BLOCK_SHAPE_SOUTH = ShapeUtils.rotateVoxelShape(shape, Direction.SOUTH);
Expand All @@ -42,23 +42,12 @@ public MetroBlockDecorativeRotatable(Properties props, VoxelShape shape, String
}

public MetroBlockDecorativeRotatable(MetroBlockDecorativeBuilder builder) {
this(builder.getProps(), builder.getShape(), builder.getModelName(), builder.getOffsetDirection(), builder.getTextures());
this(builder.getProps(), builder.getShape(), builder.getModelDirectory(), builder.getModelName(), builder.getOffsetDirection(), builder.getTextures());
}

@Override
public void generateBlockState(RegistryObject<Block> blockRegistryObject, MetroBlockStateProvider blockStateProvider) {
Block block = blockRegistryObject.get();
BlockModelBuilder bmb;
if (_modelName == null || _resources == null) {
bmb = blockStateProvider.getModelFilesWithTexture(block, "", "blocks/decorative/" + block.getName(), blockStateProvider.modLoc("blocks/decorative/" + block.getName()));
} else {
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());
}
}
}
prepareModels(blockRegistryObject.get(), blockStateProvider);
blockStateProvider.horizontalBlock(block, bmb);
}

Expand Down
Loading

0 comments on commit 56f18d5

Please sign in to comment.