Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I have a tool that harvests multiple crops at once, that either replants or harvests the crops depending on an enchant on the tool. The harvesting and loot dropping works fine, but when it comes to updating what block is rendering, only the clicked block is updated properly, while the others drop the loot but appear to say as the fully grown crop, until right clicked again. The normal path for the tool is to just break the crop, as if the player harvested the crop. The other path is to reset the growth of the crop and drop the crop's loot, minus one seed. I have already tried all combinations of method parameters for setBlock() and destroyBlock(), and the updateing of the crop blocks still does not happen. 

The code thus far:

public class ScytheItem extends HoeItem
{
    public ScytheItem(Tier tier, int atkBase, float atkSpeed)
    {
        super(tier, atkBase, atkSpeed, new Item.Properties().stacksTo(1));
    }

    @Override
    public @NotNull InteractionResult useOn(@NotNull UseOnContext context)
    {
        Level level = context.getLevel();
        Player player = context.getPlayer();
        InteractionHand hand = context.getHand();
        ItemStack stack = context.getItemInHand();

        if (!level.isClientSide() && player != null)
        {
            BlockPos pos = context.getClickedPos();
            Block block = level.getBlockState(pos).getBlock();

            if(block instanceof CropBlock)
            {
                level.playSound(null, pos, block.getSoundType(level.getBlockState(pos), level, pos, player).getBreakSound(), SoundSource.BLOCKS, 1.0F, AOTMain.RANDOM.nextFloat());
                Map<Enchantment, Integer> enchantments = stack.getAllEnchantments();
                int harvestSize = 1 + enchantments.getOrDefault(EnchantmentInit.HARVESTING_SIZE.get(), 0);

                for (int xOff = -harvestSize; xOff <= harvestSize; xOff++) {
                    for (int zOff = -harvestSize; zOff <= harvestSize; zOff++) {
                        BlockPos newPos = pos.offset(xOff, 0, zOff);
                        Block blockAt = level.getBlockState(newPos).getBlock();

                        if (blockAt instanceof CropBlock crop)
                        {
                            if (crop.isMaxAge(level.getBlockState(newPos)))
                            {
                                if (enchantments.containsKey(EnchantmentInit.AUTO_PLANTER.get()))
                                {
                                    dropLoot(level, newPos, level.getBlockState(newPos), block);

                                    level.setBlock(newPos, crop.getStateForAge(0), Block.UPDATE_CLIENTS);   // the broken bit
                                } else
                                    level.destroyBlock(newPos, true);                                       // the broken bit
                            }
                        }
                    }
                }
                stack.hurtAndBreak(1, player, (p) -> p.broadcastBreakEvent(hand));
            }
        }

        return super.useOn(context);
    }

    private void dropLoot(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state, @NotNull Block block)
    {
        List<ItemStack> drops = getDrops(level, pos, state);
        findSeeds(block, drops).ifPresent(seed -> seed.shrink(1));
        drops.forEach((drop) -> Block.popResource(level, pos, drop));
    }

    private List<ItemStack> getDrops(@NotNull Level level, @NotNull BlockPos pos, @NotNull BlockState state) { return Block.getDrops(state, (ServerLevel) level, pos, null); }

    private Optional<ItemStack> findSeeds(@NotNull Block block, final Collection<ItemStack> stacks)
    {
        Item seedsItem = block.asItem();
        return stacks.stream().filter((stack) -> stack.getItem() == seedsItem).findAny();
    }
}

 

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.