Skip to content

Commit

Permalink
Every day I hate Java a little more
Browse files Browse the repository at this point in the history
  • Loading branch information
Fureniku committed Jan 20, 2024
1 parent ca09a89 commit a691351
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
package com.fureniku.metropolis.blockentity;

import com.fureniku.metropolis.Metropolis;
import com.fureniku.metropolis.test.RegistrationTest;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

public class MetroBlockEntity extends BlockEntity {

private BlockEntityType _type;
public abstract class MetroBlockEntity extends BlockEntity {

public MetroBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state);
}

public MetroBlockEntity(BlockPos pos, BlockState state) {
super(Metropolis.registrationTest.TEST_BLOCK_ENTITY_DECORATIVE_ENTITY.get(), pos, state);
}

public BlockEntityType getBlockEntityType() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public interface MetroBlockStateFactory<T> {
}

public static MetroBlockStateFactory getBlockFactory(HelperBase... helpersIn) {
return (props, shape, modelDir, modelName, tag, dynamicShape, textures) -> new MetroEntityBlockDecorative(props, shape, modelDir, modelName, tag, SimpleUtils.containsType(OffsetHelper.class, helpersIn), textures) {
return (props, shape, modelDir, modelName, tag, dynamicShape, textures) -> new MetroBlockDecorativeBase(props, shape, modelDir, modelName, tag, SimpleUtils.containsType(OffsetHelper.class, helpersIn), textures) {
@Override
public ArrayList<HelperBase> getHelpers() {
return new ArrayList<>(Arrays.asList(helpersIn));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.fureniku.metropolis.blockentity.MetroBlockEntity;
import com.fureniku.metropolis.blocks.IToggleable;
import com.fureniku.metropolis.blocks.decorative.builders.MetroBlockDecorativeBuilder;
import com.fureniku.metropolis.blocks.decorative.helpers.HelperBase;
import com.fureniku.metropolis.blocks.decorative.helpers.OffsetHelper;
import com.fureniku.metropolis.datagen.TextureSet;
import com.fureniku.metropolis.utils.Debug;
import com.fureniku.metropolis.utils.SimpleUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.EntityBlock;
Expand All @@ -24,12 +26,23 @@ public MetroEntityBlockDecorative(Properties props, VoxelShape shape, String mod

@Nullable
public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {
return new MetroBlockEntity(pos, state);
Debug.LogError("createBlockEntity must be overriden in EntityBlock class");
return null;
}

@Nullable
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return createBlockEntity(pos, state);
}

//Boilerplate function which every overriding class will need to use for construction because java's generics suck
public static MetroBlockStateFactory getBlockFactory(HelperBase... helpersIn) {
return (props, shape, modelDir, modelName, tag, dynamicShape, textures) -> new MetroEntityBlockDecorative(props, shape, modelDir, modelName, tag, SimpleUtils.containsType(OffsetHelper.class, helpersIn), textures) {
@Override
public ArrayList<HelperBase> getHelpers() {
return new ArrayList<>(Arrays.asList(helpersIn));
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public MetroBlockDecorativeBuilder(BlockBehaviour.Properties props) {
_props = props;
}

public MetroBlockDecorativeBuilder(MetroBlockDecorativeBuilder partial) {
_props = partial._props;
_blockShape = partial._blockShape;
_modelDir = partial._modelDir;
_modelName = partial._modelName;
_tag = partial._tag;
_textures = partial._textures;
_helpers = partial._helpers;
}

//region Normal shapes
public MetroBlockDecorativeBuilder setHeight(float height) { return setShape(16, height, 16); }
public MetroBlockDecorativeBuilder setWidth(float width) { return setShape(width, 16, width); }
Expand Down Expand Up @@ -112,12 +102,21 @@ public MetroBlockDecorativeBuilder setConnectFullHelper() {
public ArrayList<HelperBase> getHelpers() { return _helpers; }

/**
* Generic build function. Creates a block instance of whatever object was specified - or MetroBlockDecorativeBase otherwise.
* Generic build function. Due to how generics work, this will always create an instance of MetroBlockDecorativeBase
* @return The created block instance
*/
public T build() {
HelperBase[] helpersArray = _helpers.toArray(new HelperBase[0]);
MetroBlockDecorativeBase.MetroBlockStateFactory<T> factory = MetroBlockDecorativeBase.getBlockFactory(helpersArray);
T.MetroBlockStateFactory<T> factory = T.getBlockFactory(helpersArray);
return factory.makeBlock(_props, _blockShape, _modelDir, _modelName, _tag, SimpleUtils.containsType(OffsetHelper.class, helpersArray), _textures);
}

/**
* Typed build function. Will return an instance of whatever class is provided in the given factory.
* @return the created block instance
* @param <T>
*/
public <T extends MetroBlockDecorativeBase> T buildAs(T.MetroBlockStateFactory factory) {
return (T) factory.makeBlock(_props, _blockShape, _modelDir, _modelName, _tag, SimpleUtils.containsType(OffsetHelper.class, _helpers), _textures);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public BlockModelBuilder prepareModels(Block block, String modelDir, String mode
public BlockModelBuilder prepareModels(Block block, String nameSuffix, String modelDir, String modelName, TextureSet[] resources) {
BlockModelBuilder bmb;
if (modelName == null || resources == null) {
bmb = getModelFilesWithTexture(block, nameSuffix, modelDir + block.getName(), modLoc(modelDir + block.getName()));
bmb = getModelFilesWithTexture(block, nameSuffix, modelDir + name(block), modLoc(modelDir + name(block)));
} else {
bmb = applyTexturesToModel(resources, getModelFilesWithTexture(block, nameSuffix, modelDir + modelName, resources[0].getTexture()));
if (resources.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fureniku.metropolis.test;

import com.fureniku.metropolis.Metropolis;
import com.fureniku.metropolis.blockentity.MetroBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -8,6 +9,6 @@
public class BlockEntityTest extends MetroBlockEntity {

public BlockEntityTest(BlockPos pos, BlockState state) {
super(pos, state);
super(Metropolis.registrationTest.TEST_BLOCK_ENTITY_DECORATIVE_ENTITY.get(), pos, state);
}
}
13 changes: 3 additions & 10 deletions src/main/java/com/fureniku/metropolis/test/RegistrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,15 @@ public void init(IEventBus modEventBus) {
.setConnectHorizontalHelper(BlockConnectionType.ALL, shapes)
.build()));


/* TODO update to new registration for block entities with buildAs()
blockNames.add("test_block_entity_decorative");
/*RegistryObject<Block> blockEntity = retrieveRegisterBlockSet("test_block_entity_decorative", () ->
new MetroBlockDecorativeBuilder(_props, DecorativeBuilderType.DECORATIVE)
.setModelDirectory("blocks/decorative/")
.setModelName("barrier_concrete_middle")
.setTextures(TEST_TEXTURE_1)
.buildEntity());*/

TEST_BLOCK_ENTITY_DECORATIVE_ENTITY = registerBlockEntityWithBlock("test_block_entity_decorative", () ->
new MetroBlockDecorativeBuilder(_props)
.setModelDirectory("blocks/decorative/")
.setModelName("barrier_concrete_middle")
.setTextures(TEST_TEXTURE_1)
.setConnectHorizontalHelper(BlockConnectionType.ALL, shapes)
.build(), MetroBlockEntity::new);//Metropolis.registrationTest.getBlock("test_block_entity_decorative").get())
//.setConnectHorizontalHelper(BlockConnectionType.ALL, shapes)
.buildAs(factory with helper as parameter), BlockEntityTest::new);*/

_testTab = new CreativeTabSet(getCreativeTabDeferredRegister(), "tab_test", getItem(blockNames.get(0)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

public class ShapeUtils {

public static final VoxelShape FULL_BOX = Shapes.box(0, 0, 0, 1, 1, 1);
private static final Object2IntMap<BlockState> stateToIndex = new Object2IntOpenHashMap<>();

public static VoxelShape makeShape(float size, float height) {
Expand Down

0 comments on commit a691351

Please sign in to comment.