package dev.lumareth.improvedAerodynamics.flight; import dev.lumareth.improvedAerodynamics.ImprovedAerodynamics; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.TagKey; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; /** * Shift-right-click on a block in the {@code improved_aerodynamics:flappable} tag (Warium's horizontal control * surfaces) with empty hands makes it a flap, or a normal control surface again. A flap follows the flap lever of the * control node it is linked to, on top of the pilot's input. */ public final class FlapInteraction { public static final TagKey FLAPPABLE = TagKey.create(Registries.BLOCK, new ResourceLocation(ImprovedAerodynamics.MOD_ID, "flappable")); private FlapInteraction() { } @SubscribeEvent public static void onRightClickBlock(PlayerInteractEvent.RightClickBlock event) { Player player = event.getEntity(); if (event.getHand() != InteractionHand.MAIN_HAND || !player.isShiftKeyDown() || player.isPassenger() || !player.getMainHandItem().isEmpty() || !player.getOffhandItem().isEmpty()) { return; } Level level = event.getLevel(); BlockPos pos = event.getPos(); BlockState state = level.getBlockState(pos); if (!state.is(FLAPPABLE)) { return; } // Handled on both sides so the block's own right-click action does not run on either event.setCanceled(true); event.setCancellationResult(InteractionResult.sidedSuccess(level.isClientSide)); if (level.isClientSide) { return; } BlockEntity blockEntity = level.getBlockEntity(pos); if (blockEntity == null) { return; } CompoundTag tag = blockEntity.getPersistentData(); boolean flap = !tag.getBoolean(WariumCapture.FLAP_TAG); tag.putBoolean(WariumCapture.FLAP_TAG, flap); blockEntity.setChanged(); if (flap && !tag.contains("ControlX")) { player.displayClientMessage(Component.translatable("message.improved_aerodynamics.flap.unlinked"), true); } else { player.displayClientMessage(Component.translatable(flap ? "message.improved_aerodynamics.flap.on" : "message.improved_aerodynamics.flap.off"), true); } } }