Jump to content

[1.18.1] Usage of EnumProperty causing exceptionally long loading times


Recommended Posts

Posted (edited)

I'm working on a mod that adds fluid pipes to the game, which make use of boolean properties in order to generate the correct model based on the surrounding configuration of pipes and other fluid-capable blocks.

They work perfectly, but recently I decided I wanted them to have built-in push and pull functions such that I can extract liquid out of any given block. To do this I replaced the boolean properties with a set of enum properties corresponding to each face which determined their connection type.

These also work perfectly, but upon loading up the test client, I noticed that the loading times required to get the the menu screen, and particularly to load up an existing save, were far longer than before. To isolate this I reverted the block properties back to the original set of boolean properties I was using and it loaded up normally.

This is my block class:

public class TestBlockStateBlock extends GenericXPBlock {

    public TestBlockStateBlock() {
        super(Properties.of(Material.METAL)
                .strength(9f)
                .destroyTime(1.2f)
                .requiresCorrectToolForDrops()
                .explosionResistance(8f)
                .noOcclusion()
                .lightLevel(new ToIntFunction<BlockState>() {
                    @Override
                    public int applyAsInt(BlockState value) {
                        return 12;
                    }
                })
                .noOcclusion()
                .emissiveRendering(new StatePredicate() {
                    @Override
                    public boolean test(BlockState state, BlockGetter getter, BlockPos pos) {
                        return true;
                    }
                })
                .isViewBlocking(new StatePredicate() {
                    @Override
                    public boolean test(BlockState p_61036_, BlockGetter p_61037_, BlockPos p_61038_) {
                        return false;
                    }
                }));



        BlockState state = this.getStateDefinition().any();

        for(Property<ConnectionType> c : connectionTypes().values()){
            state = state.setValue(c, ConnectionType.none);
        }


        this.registerDefaultState(state);


    }

    //-----SETTING UP ENUM PROPERTIES-----//

    enum ConnectionType implements StringRepresentable {
        none,
        connected,
        interfaced_idle,
        interfaced_pull,
        interfaced_push;

        @Override
        public String getSerializedName() {
            return this.name();
        }
    }

    public HashMap<String, Property<ConnectionType>> connectionTypes(){

        HashMap<String, Property<ConnectionType>> map = new HashMap<>(6);
        map.put("up", EnumProperty.create("connection_type_up", ConnectionType.class));
        map.put("down", EnumProperty.create("connection_type_down", ConnectionType.class));
        map.put("north", EnumProperty.create("connection_type_north", ConnectionType.class));
        map.put("south", EnumProperty.create("connection_type_south", ConnectionType.class));
        map.put("east", EnumProperty.create("connection_type_east", ConnectionType.class));
        map.put("west", EnumProperty.create("connection_type_west", ConnectionType.class));

        return map;

    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {

        for(Property<ConnectionType> c : connectionTypes().values()){
            pBuilder.add(c);
        }
    }

    //-----PIPE BEHAVIOR-----//

    @Override
    public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        return this.getCollisionShape(pState, pLevel, pPos, pContext);
    }

    @Override
    public VoxelShape getVisualShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        return this.getCollisionShape(pState, pLevel, pPos, pContext);
    }

    @Override
    public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) {
        return this.getCollisionShape(pState, pReader, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getInteractionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
        return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getOcclusionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
        return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getCollisionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {

        HashMap<String, VoxelShape> shapes = new HashMap<>(6);

        VoxelShape core = Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D));

        shapes.put("up", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,16 / 16D,10 / 16D)));
        shapes.put("down", Shapes.create(new AABB(6 / 16D,0 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D)));

        shapes.put("north", Shapes.create(new AABB(6 / 16D,6 / 16D,0 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-z
        shapes.put("south", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,16 / 16D))); //+z

        shapes.put("east", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,16 / 16D,10 / 16D,10 / 16D))); //+x
        shapes.put("west", Shapes.create(new AABB(0 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-x


        VoxelShape finishedshape = core;

        for(String direction : connectionTypes().keySet()){

            if(!pState.getValue(connectionTypes().get(direction)).equals(ConnectionType.none)){
                finishedshape = Shapes.join(finishedshape, shapes.get(direction), BooleanOp.OR);
            }

        }

        return finishedshape;

    }


    public static HashMap<String, BlockPos> getNeighbors(BlockPos pos){

        HashMap<String, BlockPos> neighbors = new HashMap<>(6);
        neighbors.put("up", pos.above());
        neighbors.put("down", pos.below());
        neighbors.put("north", pos.north());
        neighbors.put("south", pos.south());
        neighbors.put("east", pos.east());
        neighbors.put("west", pos.west());

        return neighbors;
    }

    public BlockState generateCorrectState(Level level, BlockPos pos){

        BlockState state = this.defaultBlockState();
        HashMap<String, BlockPos> neighbors = getNeighbors(pos);

        for(String direction : neighbors.keySet()){

            Block block = level.getBlockState(neighbors.get(direction)).getBlock();

            if(block instanceof GenericXPBlock){
                state = state.setValue(connectionTypes().get(direction), ConnectionType.connected);
            }
            else if(block instanceof ExperienceObeliskBlock){
                state = state.setValue(connectionTypes().get(direction), ConnectionType.interfaced_idle);
            }


        }

        return state;
    }


    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext pContext) {

        return this.generateCorrectState(pContext.getLevel(), pContext.getClickedPos());
    }


    @Override
    public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) {
        BlockState newState = generateCorrectState(pLevel, pPos);
        if(newState != pState){
            pLevel.setBlockAndUpdate(pPos, newState);
            pLevel.onBlockStateChange(pPos, pState, newState);
        }
    }

    //-----BLOCK ENTITY-----//



}

And this is the original block class:

public class ExperienceDuctBlock extends GenericXPBlock implements EntityBlock {

    public ExperienceDuctBlock() {

        super(Properties.of(Material.METAL)
                .strength(9f)
                .destroyTime(1.2f)
                .requiresCorrectToolForDrops()
                .explosionResistance(8f)
                .noOcclusion()
                .lightLevel(new ToIntFunction<BlockState>() {
                    @Override
                    public int applyAsInt(BlockState value) {
                        return 12;
                    }
                })
                .noOcclusion()
                .emissiveRendering(new StatePredicate() {
                    @Override
                    public boolean test(BlockState state, BlockGetter getter, BlockPos pos) {
                        return true;
                    }
                })
                .isViewBlocking(new StatePredicate() {
                    @Override
                    public boolean test(BlockState p_61036_, BlockGetter p_61037_, BlockPos p_61038_) {
                        return false;
                    }
                }));


        BlockState state = this.getStateDefinition().any();

        for(Property<Boolean> c : connections().values()){
            state = state.setValue(c, false);
        }

        for(Property<Boolean> i : interfaces().values()){
            state = state.setValue(i, false);
        }



        this.registerDefaultState(state);

    }

    @Override
    public InteractionResult use(BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHit) {

        boolean isInterfaced = false;

        for(Property<Boolean> interfaced : interfaces().values()){
            if(pState.getValue(interfaced)){
                isInterfaced = true;
            }
        }


        if(pLevel.isClientSide() && isInterfaced){
            GuiWrapper.openDuctGUI(pState, pLevel, pPos, pPlayer);
            return InteractionResult.CONSUME;
        }
        else{
            return InteractionResult.PASS;
        }

    }


    //-----PIPE BEHAVIOR-----//


    public HashMap<String, Property<Boolean>> connections(){

        HashMap<String, Property<Boolean>> map = new HashMap<>(6);
        map.put("up", BooleanProperty.create("up"));
        map.put("down", BooleanProperty.create("down"));
        map.put("north", BooleanProperty.create("north"));
        map.put("south", BooleanProperty.create("south"));
        map.put("east", BooleanProperty.create("east"));
        map.put("west", BooleanProperty.create("west"));

        return map;

    }

    public HashMap<String, Property<Boolean>> interfaces(){

        HashMap<String, Property<Boolean>> map = new HashMap<>(6);
        map.put("up", BooleanProperty.create("interfaced_up"));
        map.put("down", BooleanProperty.create("interfaced_down"));
        map.put("north", BooleanProperty.create("interfaced_north"));
        map.put("south", BooleanProperty.create("interfaced_south"));
        map.put("east", BooleanProperty.create("interfaced_east"));
        map.put("west", BooleanProperty.create("interfaced_west"));

        return map;

    }

    public static HashMap<String, BlockPos> getNeighbors(BlockPos pos){

        HashMap<String, BlockPos> neighbors = new HashMap<>(6);
        neighbors.put("up", pos.above());
        neighbors.put("down", pos.below());
        neighbors.put("north", pos.north());
        neighbors.put("south", pos.south());
        neighbors.put("east", pos.east());
        neighbors.put("west", pos.west());

        return neighbors;
    }

    @Override
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {

        for(Property<Boolean> c : connections().values()){
            pBuilder.add(c);
        }

        for(Property<Boolean> i : interfaces().values()){
            pBuilder.add(i);
        }

    }


    @Override
    public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        return this.getCollisionShape(pState, pLevel, pPos, pContext);
    }

    @Override
    public VoxelShape getVisualShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
        return this.getCollisionShape(pState, pLevel, pPos, pContext);
    }

    @Override
    public VoxelShape getBlockSupportShape(BlockState pState, BlockGetter pReader, BlockPos pPos) {
        return this.getCollisionShape(pState, pReader, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getInteractionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
        return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getOcclusionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
        return this.getCollisionShape(pState, pLevel, pPos, CollisionContext.empty());
    }

    @Override
    public VoxelShape getCollisionShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {

        HashMap<String, VoxelShape> shapes = new HashMap<>(6);

        VoxelShape core = Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D));

        shapes.put("up", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,16 / 16D,10 / 16D)));
        shapes.put("down", Shapes.create(new AABB(6 / 16D,0 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D)));

        shapes.put("north", Shapes.create(new AABB(6 / 16D,6 / 16D,0 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-z
        shapes.put("south", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,16 / 16D))); //+z

        shapes.put("east", Shapes.create(new AABB(6 / 16D,6 / 16D,6 / 16D,16 / 16D,10 / 16D,10 / 16D))); //+x
        shapes.put("west", Shapes.create(new AABB(0 / 16D,6 / 16D,6 / 16D,10 / 16D,10 / 16D,10 / 16D))); //-x


        VoxelShape finishedshape = core;

        for(String key : connections().keySet()){

            if(pState.getValue(connections().get(key))){
                finishedshape = Shapes.join(finishedshape, shapes.get(key), BooleanOp.OR);
            }

            if(pState.getValue(interfaces().get(key))){
                finishedshape = Shapes.join(finishedshape, shapes.get(key), BooleanOp.OR);
            }

        }

        return finishedshape;
    }


    public BlockState generateCorrectState(Level level, BlockPos pos){

        BlockState state = this.defaultBlockState();
        HashMap<String, BlockPos> neighbors = getNeighbors(pos);

        for(String key : neighbors.keySet()){

            Block block = level.getBlockState(neighbors.get(key)).getBlock();
            state = state.setValue(connections().get(key), block instanceof GenericXPBlock);
            state = state.setValue(interfaces().get(key), block instanceof ExperienceObeliskBlock);

        }

        return state;
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockPlaceContext pContext) {

        return this.generateCorrectState(pContext.getLevel(), pContext.getClickedPos());
    }


    @Override
    public void neighborChanged(BlockState pState, Level pLevel, BlockPos pPos, Block pBlock, BlockPos pFromPos, boolean pIsMoving) {
        BlockState newState = generateCorrectState(pLevel, pPos);
        if(newState != pState){
            pLevel.setBlockAndUpdate(pPos, newState);
            pLevel.onBlockStateChange(pPos, pState, newState);
        }
    }

    //-----BLOCK ENTITY-----//


    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {

        return ModTileEntitiesInit.XPDUCT_BE.get().create(pPos, pState);
    }



    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pBlockEntityType) {

        return pBlockEntityType == ModTileEntitiesInit.XPDUCT_BE.get() ? ExperienceDuctEntity::tick : null;
    }

}

Is there some part of my implementation that is causing increased memory usage or some other related problem?

Edited by cyanog3n

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.