#1.21.1 Pistons Bug

9 messages · Page 1 of 1 (latest)

weak cove
#

In my mod i added a feature that u can click with a fat item and piston becomes silent but when I extend - piston head breaks

#
package net.p3x.svyat_mod.block;
import net.minecraft.block.Block;
import net.minecraft.block.PistonBlock;
import net.minecraft.util.math.Direction;

public class SilentPistonBlock extends PistonBlock {

    public SilentPistonBlock(boolean sticky, Settings settings) {
        super(sticky, settings);
        this.setDefaultState(this.stateManager.getDefaultState()
                .with(FACING, Direction.NORTH)
                .with(EXTENDED, false));
    }

    public Block getHeadBlock() {
        return ModBlocks.SILENT_PISTON_HEAD;
    }
}
#
package net.p3x.svyat_mod.block;
import net.minecraft.block.PistonHeadBlock;
import net.minecraft.block.enums.PistonType;
import net.minecraft.util.math.Direction;

public class SilentPistonHeadBlock extends PistonHeadBlock {

    public SilentPistonHeadBlock(Settings settings) {
        super(settings);
        this.setDefaultState(this.stateManager.getDefaultState()
                .with(FACING, Direction.NORTH)
                .with(TYPE, PistonType.DEFAULT)
                .with(SHORT, false)
        );
    }
}
#

also I copied all the json files but it didn`t look useful

#

and here`s registering of blocks

public static final Block SILENT_STICKY_PISTON = registerBlock("silent_sticky_piston",
            new SilentPistonBlock(true, AbstractBlock.Settings.copy(Blocks.PISTON)));

    public static final Block SILENT_PISTON_HEAD = registerBlock("silent_piston_head",
            new SilentPistonHeadBlock(AbstractBlock.Settings.copy(Blocks.PISTON_HEAD)));
#

and fat item

package net.p3x.svyat_mod.custom;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.ActionResult;
import net.minecraft.world.World;
import net.p3x.svyat_mod.block.ModBlocks;
import java.util.Map;

public class FatItem extends Item {

    private static final Map<Block, Block> FAT_MAP =
            Map.of(
                    Blocks.PISTON, ModBlocks.SILENT_PISTON
            );
    
    public FatItem(Settings settings) {
        super(settings);
    }

    @Override
    public ActionResult useOnBlock(ItemUsageContext context) {
        World world = context.getWorld();
        Block clickedBlock = world.getBlockState(context.getBlockPos()).getBlock();

        if(FAT_MAP.containsKey(clickedBlock)) {
            if (!world.isClient()) {
                world.setBlockState(context.getBlockPos(), FAT_MAP.get(clickedBlock).getDefaultState());

                context.getStack().decrement(1);

                world.playSound(null, context.getBlockPos(), SoundEvents.ITEM_HONEYCOMB_WAX_ON, SoundCategory.BLOCKS);
                return ActionResult.SUCCESS;
            }
        }
        return ActionResult.FAIL;
    }
}
raw hill
weak cove
raw hill
# weak cove only delete the sounds of other loud blocks like pistons, doors, trapdoors and m...

If all you need is to do that with zero other changes, then subclassing is an overcomplication.
An alternative method is attaching some sort of data to the target's class that keeps track of whether it is under the effects of your FatItem, and have your FatItem modify that rather than replace the target with a subclass. A generally very trusted API for attaching custom data to Vanilla classes in Fabric is the Cardinal Components API (see https://github.com/Ladysnake/Cardinal-Components-API for the repo, which includes a wiki)

Then, you could use a Mixin to place the code playing the sound of the target behind a condition that requires the block or target in question to not be affected by your FatItem in order to play the sound.

This could be a simpler approach to just making a given block make no sound if you used an item on it, albeit depending on the exact behavior you want you'll likely have to tweak it somewhat or perhaps you'll prefer a different approach.

GitHub

Component API to add external data to objects. Contribute to Ladysnake/Cardinal-Components-API development by creating an account on GitHub.