package su.a71.new_soviet.items; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.mojang.datafixers.util.Pair; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.*; import net.minecraft.sound.SoundCategory; import net.minecraft.util.ActionResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.World; import net.minecraft.world.event.GameEvent; import su.a71.new_soviet.registration.NSE_Sounds; import su.a71.new_soviet.registration.NSE_Tags; import java.util.Map; import java.util.function.Consumer; import java.util.function.Predicate; public class RakeItem extends MiningToolItem { protected static final Map, Consumer>> TILLING_ACTIONS = Maps.newHashMap(ImmutableMap.of(Blocks.GRASS_BLOCK, Pair.of(HoeItem::canTillFarmland, RakeItem.createTillAction(Blocks.FARMLAND.getDefaultState())), Blocks.DIRT_PATH, Pair.of(HoeItem::canTillFarmland, RakeItem.createTillAction(Blocks.FARMLAND.getDefaultState())), Blocks.DIRT, Pair.of(HoeItem::canTillFarmland, RakeItem.createTillAction(Blocks.FARMLAND.getDefaultState())), Blocks.COARSE_DIRT, Pair.of(RakeItem::canTillFarmland, RakeItem.createTillAction(Blocks.DIRT.getDefaultState())), Blocks.ROOTED_DIRT, Pair.of(itemUsageContext -> true, RakeItem.createTillAndDropAction(Blocks.DIRT.getDefaultState(), Items.HANGING_ROOTS)))); public RakeItem(ToolMaterial material, int attackDamage, float attackSpeed, Settings settings) { super(attackDamage, attackSpeed, material, NSE_Tags.Blocks.RAKE_MINEABLE, settings); } public ActionResult useOnBlock(ItemUsageContext context) { BlockPos blockPos; World world = context.getWorld(); Pair, Consumer> pair = TILLING_ACTIONS.get(world.getBlockState(blockPos = context.getBlockPos()).getBlock()); if (pair == null) { return ActionResult.PASS; } Predicate predicate = pair.getFirst(); Consumer consumer = pair.getSecond(); if (predicate.test(context)) { PlayerEntity playerEntity = context.getPlayer(); world.playSound(playerEntity, blockPos, NSE_Sounds.ITEM_RAKE_TILL, SoundCategory.BLOCKS, 1.0f, 1.0f); if (!world.isClient) { consumer.accept(context); if (playerEntity != null) { context.getStack().damage(1, playerEntity, p -> p.sendToolBreakStatus(context.getHand())); } } return ActionResult.success(world.isClient); } return ActionResult.PASS; } // YES public static Consumer createTillAction(BlockState result) { return context -> { context.getWorld().setBlockState(context.getBlockPos(), result, Block.NOTIFY_ALL | Block.REDRAW_ON_MAIN_THREAD); context.getWorld().emitGameEvent(GameEvent.BLOCK_CHANGE, context.getBlockPos(), GameEvent.Emitter.of(context.getPlayer(), result)); }; } public static Consumer createTillAndDropAction(BlockState result, ItemConvertible droppedItem) { return context -> { context.getWorld().setBlockState(context.getBlockPos(), result, Block.NOTIFY_ALL | Block.REDRAW_ON_MAIN_THREAD); context.getWorld().emitGameEvent(GameEvent.BLOCK_CHANGE, context.getBlockPos(), GameEvent.Emitter.of(context.getPlayer(), result)); Block.dropStack(context.getWorld(), context.getBlockPos(), context.getSide(), new ItemStack(droppedItem)); }; } public static boolean canTillFarmland(ItemUsageContext context) { return context.getSide() != Direction.DOWN && context.getWorld().getBlockState(context.getBlockPos().up()).isAir(); } }