Jump to content

[1.16.2] Custom Piston Block


samjviana
 Share

Recommended Posts

I'm trying to create an custom piston with another slime color. And i actully did make it work but having some problems.
The piston itself cannot be pushed and i couldn't find out how to change that.
When the piston is retracting the piston head texture changes to the default and and the retraction ends it changes back to the actual texture.

When i break the piston while extended the head remains.

Please correct me if I did something wrong or if something could be done better.

The Piston Head code:

public class BlackStickyPistonHeadBlock extends PistonHeadBlock {
    public BlackStickyPistonHeadBlock(Properties properties) {
        super(properties);
    }
    
    @Override
    public boolean isValidPosition(BlockState state, IWorldReader worldIn, BlockPos pos) {
        return super.isValidPosition(state, worldIn, pos);
    }  

    @Override
    public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
        if (facing.getOpposite() == stateIn.get(FACING)) {
            if (!stateIn.isValidPosition(worldIn, currentPos))
            {
                return stateIn;
            }
        }
        if (!stateIn.isValidPosition(worldIn, currentPos)) {
            if (facing.getOpposite() == stateIn.get(FACING))
            {
                return Blocks.AIR.getDefaultState();
            }
        }
        return stateIn;
    }
}

The Piston Block code (Most part of it is just "copied" from the vanilla PistonBlock code):

public class BlackStickyPistonBlock extends PistonBlock {
    private final boolean isSticky;

    public BlackStickyPistonBlock(boolean sticky, Properties properties) {
        super(sticky, properties);
        this.isSticky = sticky;
    }

    private boolean doMove(World worldIn, BlockPos pos, Direction directionIn, boolean extending) {
        BlockPos blockpos = pos.offset(directionIn);
        if (!extending && worldIn.getBlockState(blockpos).isIn(ModBlocks.BLACK_STICKY_PISTON_HEAD.get())) {
            worldIn.setBlockState(blockpos, Blocks.AIR.getDefaultState(), 20);
        }

        PistonBlockStructureHelper pistonblockstructurehelper = new PistonBlockStructureHelper(worldIn, pos, directionIn, extending);
        if (!pistonblockstructurehelper.canMove()) {
            return false;
        }
        else {
            Map<BlockPos, BlockState> map = Maps.newHashMap();
            List<BlockPos> list = pistonblockstructurehelper.getBlocksToMove();
            List<BlockState> list1 = Lists.newArrayList();

            for (int i = 0; i < list.size(); ++i) {
                BlockPos blockpos1 = list.get(i);
                BlockState blockstate = worldIn.getBlockState(blockpos1);
                list1.add(blockstate);
                map.put(blockpos1, blockstate);
            }

            List<BlockPos> list2 = pistonblockstructurehelper.getBlocksToDestroy();
            BlockState[] ablockstate = new BlockState[list.size() + list2.size()];
            Direction direction = extending ? directionIn : directionIn.getOpposite();
            int j = 0;

            for (int k = list2.size() - 1; k >= 0; --k) {
                BlockPos blockpos2 = list2.get(k);
                BlockState blockstate1 = worldIn.getBlockState(blockpos2);
                TileEntity tileentity = blockstate1.hasTileEntity() ? worldIn.getTileEntity(blockpos2) : null;
                spawnDrops(blockstate1, worldIn, blockpos2, tileentity);
                worldIn.setBlockState(blockpos2, Blocks.AIR.getDefaultState(), 18);
                ablockstate[j++] = blockstate1;
            }

            for (int l = list.size() - 1; l >= 0; --l) {
                BlockPos blockpos3 = list.get(l);
                BlockState blockstate5 = worldIn.getBlockState(blockpos3);
                blockpos3 = blockpos3.offset(direction);
                map.remove(blockpos3);
                worldIn.setBlockState(blockpos3, Blocks.MOVING_PISTON.getDefaultState().with(FACING, directionIn), 68);
                worldIn.setTileEntity(blockpos3, MovingPistonBlock.createTilePiston(list1.get(l), directionIn, extending, false));
                ablockstate[j++] = blockstate5;
            }

            if (extending) {
                PistonType pistontype = this.isSticky ? PistonType.STICKY : PistonType.DEFAULT;
                BlockState blockstate4 = ModBlocks.BLACK_STICKY_PISTON_HEAD.get().getDefaultState().with(PistonHeadBlock.FACING, directionIn).with(PistonHeadBlock.TYPE, pistontype);
                BlockState blockstate6 = Blocks.MOVING_PISTON.getDefaultState().with(MovingPistonBlock.FACING, directionIn).with(MovingPistonBlock.TYPE, this.isSticky ? PistonType.STICKY : PistonType.DEFAULT);
                map.remove(blockpos);
                worldIn.setBlockState(blockpos, blockstate6, 68);
                worldIn.setTileEntity(blockpos, MovingPistonBlock.createTilePiston(blockstate4, directionIn, true, true));
            }

            BlockState blockstate3 = Blocks.AIR.getDefaultState();

            for (BlockPos blockpos4 : map.keySet()) {
                worldIn.setBlockState(blockpos4, blockstate3, 82);
            }

            for (Entry<BlockPos, BlockState> entry : map.entrySet()) {
                BlockPos blockpos5 = entry.getKey();
                BlockState blockstate2 = entry.getValue();
                blockstate2.updateDiagonalNeighbors(worldIn, blockpos5, 2);
                blockstate3.func_235734_a_(worldIn, blockpos5, 2);
                blockstate3.updateDiagonalNeighbors(worldIn, blockpos5, 2);
            }

            j = 0;

            for (int i1 = list2.size() - 1; i1 >= 0; --i1) {
                BlockState blockstate7 = ablockstate[j++];
                BlockPos blockpos6 = list2.get(i1);
                blockstate7.updateDiagonalNeighbors(worldIn, blockpos6, 2);
                worldIn.notifyNeighborsOfStateChange(blockpos6, blockstate7.getBlock());
            }

            for (int j1 = list.size() - 1; j1 >= 0; --j1) {
                worldIn.notifyNeighborsOfStateChange(list.get(j1), ablockstate[j++].getBlock());
            }

            if (extending) {
                worldIn.notifyNeighborsOfStateChange(blockpos, ModBlocks.BLACK_STICKY_PISTON_HEAD.get());
            }

            return true;
        }
    }

    private boolean shouldBeExtended(World worldIn, BlockPos pos, Direction facing) {
        for(Direction direction : Direction.values()) {
            if (direction != facing && worldIn.isSidePowered(pos.offset(direction), direction)) {
                return true;
            }
        }

        if (worldIn.isSidePowered(pos, Direction.DOWN)) {
            return true;
        }
        else {
            BlockPos blockpos = pos.up();

            for(Direction direction1 : Direction.values()) {
                if (direction1 != Direction.DOWN && worldIn.isSidePowered(blockpos.offset(direction1), direction1)) {
                    return true;
                }
            }

            return false;
        }
    }
      
    @Override
    public boolean eventReceived(BlockState state, World worldIn, BlockPos pos, int id, int param) {
        Direction direction = state.get(FACING);
        if (!worldIn.isRemote) {
            boolean flag = this.shouldBeExtended(worldIn, pos, direction);
            if (flag && (id == 1 || id == 2)) {
                worldIn.setBlockState(pos, state.with(EXTENDED, Boolean.valueOf(true)), 2);
                return false;
            }

            if (!flag && id == 0) {
                return false;
            }
        }

        if (id == 0) {
            if (net.minecraftforge.event.ForgeEventFactory.onPistonMovePre(worldIn, pos, direction, true))
                return false;
            if (!this.doMove(worldIn, pos, direction, true)) {
                return false;
            }

            worldIn.setBlockState(pos, state.with(EXTENDED, Boolean.valueOf(true)), 67);
            worldIn.playSound((PlayerEntity) null, pos, SoundEvents.BLOCK_PISTON_EXTEND, SoundCategory.BLOCKS, 0.5F,
                    worldIn.rand.nextFloat() * 0.25F + 0.6F);
        } else if (id == 1 || id == 2) {
            if (net.minecraftforge.event.ForgeEventFactory.onPistonMovePre(worldIn, pos, direction, false))
                return false;
            TileEntity tileentity1 = worldIn.getTileEntity(pos.offset(direction));
            if (tileentity1 instanceof PistonTileEntity) {
                ((PistonTileEntity) tileentity1).clearPistonTileEntity();
            }

            BlockState blockstate = Blocks.MOVING_PISTON.getDefaultState().with(MovingPistonBlock.FACING, direction)
                    .with(MovingPistonBlock.TYPE, this.isSticky ? PistonType.STICKY : PistonType.DEFAULT);
            worldIn.setBlockState(pos, blockstate, 20);
            worldIn.setTileEntity(pos, MovingPistonBlock.createTilePiston(
                    this.getDefaultState().with(FACING, Direction.byIndex(param & 7)), direction, false, true));
            worldIn.func_230547_a_(pos, blockstate.getBlock());
            blockstate.func_235734_a_(worldIn, pos, 2);
            if (this.isSticky) {
                BlockPos blockpos = pos.add(direction.getXOffset() * 2, direction.getYOffset() * 2,
                        direction.getZOffset() * 2);
                BlockState blockstate1 = worldIn.getBlockState(blockpos);
                boolean flag1 = false;
                if (blockstate1.isIn(Blocks.MOVING_PISTON)) {
                    TileEntity tileentity = worldIn.getTileEntity(blockpos);
                    if (tileentity instanceof PistonTileEntity) {
                        PistonTileEntity pistontileentity = (PistonTileEntity) tileentity;
                        if (pistontileentity.getFacing() == direction && pistontileentity.isExtending()) {
                            pistontileentity.clearPistonTileEntity();
                            flag1 = true;
                        }
                    }
                }

                if (!flag1) {
                    if (id != 1 || blockstate1.isAir()
                            || !canPush(blockstate1, worldIn, blockpos, direction.getOpposite(), false, direction)
                            || blockstate1.getPushReaction() != PushReaction.NORMAL && !blockstate1.isIn(Blocks.PISTON)
                                    && !blockstate1.isIn(Blocks.STICKY_PISTON)) {
                        worldIn.removeBlock(pos.offset(direction), false);
                    } else {
                        this.doMove(worldIn, pos, direction, false);
                    }
                }
            } else {
                worldIn.removeBlock(pos.offset(direction), false);
            }

            worldIn.playSound((PlayerEntity) null, pos, SoundEvents.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.5F,
                    worldIn.rand.nextFloat() * 0.15F + 0.6F);
        }

        net.minecraftforge.event.ForgeEventFactory.onPistonMovePost(worldIn, pos, direction, (id == 0));
        return true;
    }
}

 

Edited by samjviana
Link to comment
Share on other sites

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I'm researching the how to create biomes in json. There is a great tool for creating the jsons online. Using the vanilla `river` biome, I want to essentially create a custom disk feature that mixes 3 blocks instead of a single-block provider. Any ideas on where to start with that?
    • I figured out what was wrong ... you can spot it pretty quick most likely: { "type": "minecraft:smelting", "ingredient": { "item": "minecraft:gold_nugget_ore" }, "result": "minecraft:gold_nugget", "experience": 0.25, "cookingtime": 150 }   ... so, after changing `minecraft` to `foundations` for all the custom items and all the recipes work. Well that's what copy/paste will get you. One last question about recipes, though. What does the following json key/value do in the game? "group": "copper_ingot"  
    • # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # entry should be kept on an individual line. The IP address should # be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # space. # # Additionally, comments (such as these) may be inserted on individual # lines or following the machine name denoted by a '#' symbol. # # For example: # #      102.54.94.97     rhino.acme.com          # source server #       38.25.63.10     x.acme.com              # x client host # localhost name resolution is handled within DNS itself. #    127.0.0.1       localhost #    ::1             localhost ###0.0.0.0 apps.corel.com ###0.0.0.0 mc.corel.com 0.0.0.0 origin-mc.corel.com ###0.0.0.0 iws.corel.com
    • Is there a way to create a custom map marker for a custom structure/biome (same as a vanilla mansion)?
    • I’ve scrounged the Internet for solutions and found absolutely nothing that has worked for me I have posted my code in a GitHub Repository. I bet the culprit is somewhere in the Container or TileEntity classes (Furnace packages are in blocks/coke_furnace or blocks/foundry) PS I know I’m on 1.12.2 and that it’s not supported I don’t want to update my game or my code. Lol It’s my favorite version and the most widely modded. I don’t plan to release the mod, I’m just customizing my game to my will. I’ll send it to friends but that’s it.
  • Topics

×
×
  • Create New...

Important Information

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